The alarm function of UNIX programming

Source: Internet
Author: User

UNIX programming is very easy to encounter this function, we can first look at the introduction of this function, with the man tool, man alarm.

The introduction to the alarm function in online Help for UNIX systems is as follows:

#include <unistd.h>    
unsigned int alarm (unsigned int seconds);

Its main function is to set the signal transmission alarm clock. Its main function is to set the signal SIGALRM to the current process after the number of seconds specified by seconds, if the alarm function is called again within the time that is not completed, the latter timer setting overrides the previous setting, and when seconds is set to 0 o'clock, the timer will be canceled. It returns the time remaining for the last timer, and returns 0 if it is set for the first time.

For a simple example:

#include <unistd.h>    
#include <stdio.h>    
#include <stdlib.h>    
#include <signal.h>    
        
void Sig_alarm ()    
{    
    exit (0);    
}    
int main (int argc, char *argv[])    
{    
    signal (SIGALRM, sig_alarm);    
    Alarm (ten);    
    Sleep (in);    
    printf ("Hello world!\n");    
    return 0;    
}

The program ends at 10s and does not output Hello world!

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.