-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoversleep.c
More file actions
89 lines (79 loc) · 2.03 KB
/
oversleep.c
File metadata and controls
89 lines (79 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Use in conjuction with repeated interupts, like so:
* "while true; do kill -INT <this pid>; done
* in order to demonstrate the increase in sleep time.
*
* This is because nanosleep's resolution is limited to that of the software
* clock, and it rounds UP to the nearest unit of the software clock. So on
* every interrupt, the time remaining gets rounded up.
*
* Linux provides a non-portable clock_nanosleep, which allows us to sleep
* based off of the 3 interval clocks, along with the TIME_ABSTIME flag,
* this lets us sleep based of of an absolute time in the future,
* not a relative amount.
* compile with -DABSTIME to test this.
*/
#define _POSIX_C_SOURCE 199309
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
void handler(int sig) {
// Do nothing...
}
#ifdef ABSTIME
void clock_nsleep() {
struct timespec now;
int err;
printf("Performing sleep with clock_nanosleep!\n");
clock_gettime(CLOCK_REALTIME, &now);
now.tv_sec += 10;
while (1) {
if ((err = clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME,
&now, NULL)) == 0) {
break;
}
if (err != EINTR) {
printf("clock_nanosleep\n");
exit(EXIT_FAILURE);
}
printf("Interrupted.\n");
}
}
#endif
void nsleep() {
struct timespec current_sleep, remaining;
printf("Performing sleep with plain nanosleep\n");
current_sleep.tv_sec = 10;
current_sleep.tv_nsec = 0;
while (1) {
if (nanosleep(¤t_sleep, &remaining) != -1) {
break;
}
if (errno != EINTR) {
printf("nanosleep\n");
exit(EXIT_FAILURE);
}
printf("Interrupted\n");
current_sleep = remaining;
}
}
int main(int argc, char **argv) {
struct sigaction sigact;
time_t start;
printf("pid: %d\n", getpid());
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigact.sa_handler = handler;
sigaction(SIGINT, &sigact, NULL);
start = time(NULL);
#ifdef ABSTIME
clock_nsleep();
#else
nsleep();
#endif
printf("Slept for %lld seconds!\n", time(NULL) - start);
}