Elirovi Elirovi - 1 year ago 75
C Question

alarm(int) doesn't work in a while loop

I'm trying to execute this code:


function just prints "test".

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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download