In that year, I learned Linux C-alarm step by step

Source: Internet
Author: User



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);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.