This series of articles written by muge0913, reproduced please note the Source: http://blog.csdn.net/muge0913/article/details/7337363
Alarm () is used to set the signal sigalrm to be sent to the current process after the number of seconds specified by the seconds parameter. If the seconds parameter is 0, the previously set alarm is canceled and the remaining time is returned. The return value is 0 when the signal is sent normally. If the new alarm setting is sent when the previous setting is not completed, the time remaining in the previous setting is returned. Returns the remaining seconds of the previous alarm. If no alarm is set, returns 0.
After alarm () is executed, the process continues to be executed. In the later stage (after alarm), the process will receive the signal sigalrm and execute its processing function in seconds.
You can use the alarm function to set a timer that times out at a specified time in the future. When the timer times out, the sigalarm signal is generated. If this signal is not captured or ignored, the default action is to terminate the process that calls the alarm function.
Each process can only have one alarm clock. If the alarm clock has been set for the process when calling alarm, and it has not timed out, the residual value of the alarm clock is used as the return value of this alarm function call. The previously registered alarm clock will be replaced by a new value.
If there is an alarm clock that has not been previously registered for the process, and the seconds value of this call is 0, the previous alarm clock is canceled, and the remaining values are still returned as alarm values.
(These paragraphs are taken from advanced programming in UNIX environments.)
#include <stdio.h>#include <signal.h>#include <time.h>void func();void main(){ signal(SIGALRM,&func); alarm(4); while(1) pause();}void func(){ printf("getsigalrm!!\n"); alarm(1);}