[Ns-developers] terminating an ns-3 run

Felipe Perrone perrone at bucknell.edu
Wed Sep 1 13:49:20 PDT 2010


Hi Adrian,

Thanks for the response. You're right, this looks simple enough, but  
there had to be a snag. First off, I was forgetting that one can't  
install a new handler on SIGKILL, or else people would be able to  
create programs that can't be killed. Second, this should be simple  
enough, but what works fine in a proof-of-concept program (not using  
ns-3) with SIGTERM or SIGUSR1, doesn't seem to work in ns-3.9 code. I  
modified examples/csma/csma-bridge.cc to look as shown below. I ran a  
recursive grep looking for calls to "signal" in the ns-3.9 sources,  
but didn't come up with anything. I'm now wondering if there's some  
goof with namespaces that I'm not aware of.

Feedback from you or anybody who can shed a light on this mystery is  
welcome.

Felipe

-------------------------

  32 #include <iostream>
  33 #include <fstream>
  34 #include <signal.h>
  35
  36 #include "ns3/simulator-module.h"
  37 #include "ns3/node-module.h"
  38 #include "ns3/core-module.h"
  39 #include "ns3/helper-module.h"
  40 #include "ns3/bridge-module.h"
  41 #include "ns3/data-collection-module.h"
  42
  43 using namespace ns3;
  44
  45 static void
  46 terminate(int)
  47 {
  48   std::cout << "signal caught" << std::endl;
  49   Simulator::Stop();
  50   Simulator::Destroy();
  51 }
  52
  53 NS_LOG_COMPONENT_DEFINE ("CsmaBridgeExample");
  54
  55 int
  56 main (int argc, char *argv[])
  57 {
  58   std::cout << "ns-3 run started" << std::endl;
  59
  60   // Install signal handler to deal with external termination
  61   if (signal (SIGUSR1, terminate) == SIG_ERR)
  62   {
  63     std::cout << "error installing signal handler" << std::endl;
  64   }
  65
  66   for (;;)
  67     pause();
...

------------------------------------------------------------
Luiz Felipe Perrone, Associate Professor
Dept. of Computer Science, Breakiron 68
Bucknell University, Lewisburg, PA 17837

Voice: +1-570-577-1687
Fax:   +1-570-577-1258
Skype: luiz.felipe.perrone
Web:   http://www.eg.bucknell.edu/~perrone/
------------------------------------------------------------

On Sep 1, 2010, at 12 :03 PM, Adrian Sai-Wah TAM wrote:

> Hi Felipe,
>
> Implementation-wise, I don't think that is hard because the simulator
> in NS-3 is a singleton object which can be easily accessed. What you
> need to do is simply catch the signal then do a ScheduleNow(). At this
> moment, I believe that is not difficult to do.
>
> - Adrian.
>
> On Tue, Aug 31, 2010 at 3:30 PM, Felipe Perrone  
> <perrone at bucknell.edu> wrote:
>> Hello ns-3 developers,
>>
>> I'm working on a external mechanism to determine the length of an  
>> ns-3 run
>> automatically. This mechanism will analyze a collection of samples  
>> stored in
>> a database to determine if the ns-3 run can end and then take  
>> action to
>> interrupt, that is, terminate, the run.
>>
>> One could install a signal handler for the ns-3 run (SIGKILL  
>> perhaps) and
>> have this handler go through the appropriate sequence of actions to  
>> shut
>> down the simulation properly (that is, make calls to Stop() and  
>> Destroy(),
>> flush output streams, etc.) Please let me know if you have
>> suggestions/advice on how this should be implemented.
>>
>> Cheers,
>> Felipe



More information about the Ns-developers mailing list