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}