1CLOCK_NANOSLEEP(2) Linux Programmer's Manual CLOCK_NANOSLEEP(2)
2
3
4
6 clock_nanosleep - high-resolution sleep with specifiable clock
7
9 #include <time.h>
10
11 int clock_nanosleep(clockid_t clockid, int flags,
12 const struct timespec *request,
13 struct timespec *remain);
14
15 Link with -lrt (only for glibc versions before 2.17).
16
17 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19 clock_nanosleep():
20 _POSIX_C_SOURCE >= 200112L
21
23 Like nanosleep(2), clock_nanosleep() allows the calling thread to sleep
24 for an interval specified with nanosecond precision. It differs in al‐
25 lowing the caller to select the clock against which the sleep interval
26 is to be measured, and in allowing the sleep interval to be specified
27 as either an absolute or a relative value.
28
29 The time values passed to and returned by this call are specified using
30 timespec structures, defined as follows:
31
32 struct timespec {
33 time_t tv_sec; /* seconds */
34 long tv_nsec; /* nanoseconds [0 .. 999999999] */
35 };
36
37 The clockid argument specifies the clock against which the sleep inter‐
38 val is to be measured. This argument can have one of the following
39 values:
40
41 CLOCK_REALTIME
42 A settable system-wide real-time clock.
43
44 CLOCK_TAI (since Linux 3.10)
45 A system-wide clock derived from wall-clock time but ignoring
46 leap seconds.
47
48 CLOCK_MONOTONIC
49 A nonsettable, monotonically increasing clock that measures time
50 since some unspecified point in the past that does not change
51 after system startup.
52
53 CLOCK_BOOTIME (since Linux 2.6.39)
54 Identical to CLOCK_MONOTONIC, except that it also includes any
55 time that the system is suspended.
56
57 CLOCK_PROCESS_CPUTIME_ID
58 A settable per-process clock that measures CPU time consumed by
59 all threads in the process.
60
61 See clock_getres(2) for further details on these clocks. In addition,
62 the CPU clock IDs returned by clock_getcpuclockid(3) and
63 pthread_getcpuclockid(3) can also be passed in clockid.
64
65 If flags is 0, then the value specified in request is interpreted as an
66 interval relative to the current value of the clock specified by
67 clockid.
68
69 If flags is TIMER_ABSTIME, then request is interpreted as an absolute
70 time as measured by the clock, clockid. If request is less than or
71 equal to the current value of the clock, then clock_nanosleep() returns
72 immediately without suspending the calling thread.
73
74 clock_nanosleep() suspends the execution of the calling thread until
75 either at least the time specified by request has elapsed, or a signal
76 is delivered that causes a signal handler to be called or that termi‐
77 nates the process.
78
79 If the call is interrupted by a signal handler, clock_nanosleep() fails
80 with the error EINTR. In addition, if remain is not NULL, and flags
81 was not TIMER_ABSTIME, it returns the remaining unslept time in remain.
82 This value can then be used to call clock_nanosleep() again and com‐
83 plete a (relative) sleep.
84
86 On successfully sleeping for the requested interval, clock_nanosleep()
87 returns 0. If the call is interrupted by a signal handler or encoun‐
88 ters an error, then it returns one of the positive error number listed
89 in ERRORS.
90
92 EFAULT request or remain specified an invalid address.
93
94 EINTR The sleep was interrupted by a signal handler; see signal(7).
95
96 EINVAL The value in the tv_nsec field was not in the range 0 to
97 999999999 or tv_sec was negative.
98
99 EINVAL clockid was invalid. (CLOCK_THREAD_CPUTIME_ID is not a permit‐
100 ted value for clockid.)
101
102 ENOTSUP
103 The kernel does not support sleeping against this clockid.
104
106 The clock_nanosleep() system call first appeared in Linux 2.6. Support
107 is available in glibc since version 2.1.
108
110 POSIX.1-2001, POSIX.1-2008.
111
113 If the interval specified in request is not an exact multiple of the
114 granularity underlying clock (see time(7)), then the interval will be
115 rounded up to the next multiple. Furthermore, after the sleep com‐
116 pletes, there may still be a delay before the CPU becomes free to once
117 again execute the calling thread.
118
119 Using an absolute timer is useful for preventing timer drift problems
120 of the type described in nanosleep(2). (Such problems are exacerbated
121 in programs that try to restart a relative sleep that is repeatedly in‐
122 terrupted by signals.) To perform a relative sleep that avoids these
123 problems, call clock_gettime(2) for the desired clock, add the desired
124 interval to the returned time value, and then call clock_nanosleep()
125 with the TIMER_ABSTIME flag.
126
127 clock_nanosleep() is never restarted after being interrupted by a sig‐
128 nal handler, regardless of the use of the sigaction(2) SA_RESTART flag.
129
130 The remain argument is unused, and unnecessary, when flags is TIMER_AB‐
131 STIME. (An absolute sleep can be restarted using the same request ar‐
132 gument.)
133
134 POSIX.1 specifies that clock_nanosleep() has no effect on signals dis‐
135 positions or the signal mask.
136
137 POSIX.1 specifies that after changing the value of the CLOCK_REALTIME
138 clock via clock_settime(2), the new clock value shall be used to deter‐
139 mine the time at which a thread blocked on an absolute
140 clock_nanosleep() will wake up; if the new clock value falls past the
141 end of the sleep interval, then the clock_nanosleep() call will return
142 immediately.
143
144 POSIX.1 specifies that changing the value of the CLOCK_REALTIME clock
145 via clock_settime(2) shall have no effect on a thread that is blocked
146 on a relative clock_nanosleep().
147
149 clock_getres(2), nanosleep(2), restart_syscall(2), timer_create(2),
150 sleep(3), usleep(3), time(7)
151
153 This page is part of release 5.12 of the Linux man-pages project. A
154 description of the project, information about reporting bugs, and the
155 latest version of this page, can be found at
156 https://www.kernel.org/doc/man-pages/.
157
158
159
160Linux 2021-03-22 CLOCK_NANOSLEEP(2)