Sleep and alarm

Source: Internet
Author: User
Tags printf signal handler sleep sleep function

Sleep function:

#include <unistd.h>

unsigned int sleep (unsigned int seconds);

This function causes the calling process to be suspended until one of the following conditions is met:

1 has passed the wall clock time specified by seconds

2 The calling process captures a signal and returns from the signal handler

Note: Due to other system activities, the actual return time is much later than required, like alarm.

Return value of sleep:

1 in the first of these cases, the return value is 0

2 The return value is not sufficient time (the required time minus the actual sleep time) when it is returned early due to capturing a signal sleep

Take a look at the example below and guess what the return value of sleep () is

1 #include <stdio.h>
2 #include <time.h>
3 #include <signal.h>
4
5 #define Diapause//sleep time 30s
6 #define TIMEOUT 5
7
8 void sigalrm (int signo);
9
Ten int main ()
11 {
struct Sigaction sa;
13
Sa.sa_handler = SIGALRM;
sa.sa_flags = 0;
Sigemptyset (&sa.sa_mask);
if (Sigaction (SIGALRM, &sa, NULL) < 0)
18 {
printf ("Sigaction error!\n");
return;
21}
22
(1)
24 {
printf ("Alarm (TIMEOUT) ... \ n");
Alarm (TIMEOUT);
printf ("... \ n");
printf ("sleep (diapause) = =%d\n", Sleep (diapause));
29}
30
0;
32}
33
void sigalrm (int signo)
35 {
printf ("timeout...\n");
The return of Panax Notoginseng;
38}

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.