0

I would like to schedule an event in a specific time, more precisely, I want to generate a poisson traffic. So I need to send a packet at specific time intervals that are generated from an exponential distribution. I have done some researches on the Internet and found that the setitimer method can schedule an alarm signal after a certain time, but I don't know how to use it in my case.

Thanks for the advice. I followed what you have said. I still have a little query: in my code I need to schedule two independant events at the same time, typically, filling two queues simultaneously. This is what I did right now:

void Queue_class::SetPoissonTraficFirstQueue(void)

{  // add one packet to the queue 
   struct sigaction act ; 
   struct itimerval timer ; 
   act.sa_handler = this ; 
   sigempty_set(&act.sa_mask) ; // no signal is added to the sa_mask 
   act.sa_flags = 0 ; 
   sigaction(SIGALARM, &act,0) ; 
   timer.it_value.tv_usec = 0 ; 
   timer.it_value.tv_sec = exponential_variable() ; // generates a random exponential variable
   timer.it_interval.tv_sec = 0 ; 
   timer.it_interval.tv_usec = 0 ; 
   setitimer(ITIMER_REAL, *timer, NULL) ; 

 }

And the same function for the other queue but with a different exponential random variable

In the main, I do what follows:

int main()
{
   Queue_class queue ; 
   queue.SetPoissonTraficFirstQueue() ; 
   queue.SetPoissonTraficSecondQueue() ; 
   // do business
}

I have two question please:

1- Is it a good solution to call the function internally with the pointer "this" in sa_handler method?

2- In the main function, do the two processes occur simultaneously as I want: I mean are both queues filled at the same time?

Galileo
  • 321
  • 1
  • 4
  • 12
  • [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Some programmer dude Aug 03 '12 at 09:04
  • The second problem is really unrelated to your first question, and should have been put in a separate question here on SO instead of editing this question. You could always add a link back to this question to provide some background. – Some programmer dude Aug 04 '12 at 12:35

1 Answers1

0

Lets say you want to schedule a timer for 6PM today, you get a time_t of that time (using e.g. strptime or other functions), then you can use difftime to get the number of seconds between now and then. This value can be used for the "value" of the timer (i.e. itimerval.it_value.tv_sec). If the interval is cleared, then this will be a one-off timer.

On the other hand, if you want to create a recurring timer, that is called every X second, then set both the value and interval to X.

In both these vases, use ITIMER_REAL, and have a signal handler catch SIGALRM.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621