A detailed example of alarm function and pause function in Linux

Source: Internet
Author: User
Tags sleep function

Alarm (time), after execution, tells the kernel to let the kernel send a timed signal to the process after a timeframe, and then the process captures the signal and processes it;
The pause () function pauses the process to give up the CPU, but the pause of the function and the sleep of the previous sleep function are interrupted sleep, which means that the interrupt signal is received and then
The statement after the pause () and sleep () functions are executed directly when the process is re-executed;
Here is an example of a timed 2-second:

/*************************************************************************> File name:alarm.c> Author: > Mail: > Created time:2015 November 20 Friday 21:12 52 seconds ****************************************************************** /#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h>void sig_ handler (int num) {    printf ("Receive the Signal%d.\n", num);} int main () {    signal (SIGALRM, sig_handler);    Alarm (2);    while (1) {        pause ();        printf ("Pause is over.\n");    }    Exit (0);}

If we want the program to be timed every 2 seconds, this implementation is also very simple, we are in the function of processing the timing signal again 2 seconds, the example is as follows:

/*************************************************************************> File name:alarm.c> Author: > Mail: > Created time:2015 November 20 Friday 21:12 52 seconds ****************************************************************** /#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h>void sig_ handler (int num) {    printf ("Receive the Signal%d.\n", num);    Alarm (2);} int main () {    signal (SIGALRM, sig_handler);    Alarm (2);    while (1) {        pause ();        printf ("Pause is over.\n");    }    Exit (0);}

You can see that the program receives the signal 14 every 2 seconds, that is, the SIGALRM signal, and when the signal is processed, directly executes the statement below the pause () function, stating that pause () is a pause that can be interrupted;

A detailed example of alarm function and pause function in Linux

Related Article

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.