Lucas
Lucas

Reputation: 1617

How do I restart timer?

I use the following code to set an alarm.

struct itimerval timer;
struct sigaction sa;

sa.sa_handler = handler;
sa.sa_flags = SA_RESETHAND;
timer.it_value.tv_usec = 0;
timer.it_value.tv_sec = 1;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 0;
sigaction(SIGALRM, &sa, 0);
setitimer(ITIMER_REAL, &timer, 0); 

How do I restart the timer after it has exited the handler function and went into the while loop below. Do I need to reinitialize everything or just call setittimer?

while(pause() == -1)
{   
    // goes in here after handler function.. what needs to go here to restart timer?
}  

Upvotes: 0

Views: 1368

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409176

Singe you use SA_RESETHAND you need to call both sigaction and setitimer again. I suggest you put it in a function so you don't have to write the code to start (or restart) the timer twice or more.

Upvotes: 1

Related Questions