Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c Signal Handler

Tags:

c

signals

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

like image 464
Harry Avatar asked Feb 14 '26 07:02

Harry


1 Answers

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.

like image 58
Filipe Gonçalves Avatar answered Feb 16 '26 22:02

Filipe Gonçalves



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!