Reputation: 3052
I am writing an application wherein I want call a function every 1 second. This is what I've written so far
Timerspec.it_interval.tv_sec=1;
Timerspec.it_interval.tv_nsec=0;
Timerspec.it_value.tv_sec=1;
Timerspec.it_value.tv_nsec=0;
timer_t timerId;
struct sigaction sa
sa.sa_handler=&TimerFn;
sigaction(SIGALRM,&sa,NULL);
timer_create(CLOCK_REALTIME,NULL,&timerId);
timer_settime(timerId,0,(const itimerspec*)Timerspec,NULL);
If my timer function(TimerFn) takes more than 1 second to complete, how to ignore the SIGALRM signals while the handler is running. Please note I want the signal to be ignored only when the handler is running. If handler is not running TimerFn should get called.
Thanks
Upvotes: 3
Views: 717
Reputation: 21223
As mentioned in the comments, the signal is atomically blocked before entering the handler (unless SA_NODEFER
is used), and unblocked after returning from the handler, so there's nothing you need to do.
The way you declare and use sa
is buggy: you need to at least initialize the sa_flags
field to 0, and you need to initialize the sa_mask
field to the empty signal set:
struct sigaction sa;
sa.sa_handler = &TimerFn;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGALRM,&sa,NULL);
Note that the signal being caught is always blocked upon entering the handler and unblocked when leaving, even if you specified an empty signal mask in the sa_mask
field of struct sigaction
.
Upvotes: 1