Reputation: 374
I'm trying to execute this code:
signal(SIGALRM,handler);
alarm(1);
while(1){}
The handler
function just prints "test".
alarm(1)
will eventually be executed only one time in this case. I tried to put it in the loop and it seems that it doesn't get executed at all!
I am kinda new to signals. Can someone explains to me how this happens?
Upvotes: 1
Views: 1279
Reputation: 37928
If you just put alarm(1)
in the loop in your example, you'll call alarm(1)
infinitely many times within a few microseconds of each invocation. And then this happens:
If an alarm has already been set with alarm() but has not been delivered, another call to alarm() will supersede the prior call.
I.e., the alarm gets cleared in each iteration of the loop. And since your loop runs forever, the alarm is never permanently set.
Upvotes: 5