1 .\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH CLOCK_NANOSLEEP 2 2021-03-22 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
8 clock_nanosleep \- high-resolution sleep with specifiable clock
11 .RI ( libc ", " \-lc ),
16 .RI ( librt ", " \-lrt )
21 .BI "int clock_nanosleep(clockid_t " clockid ", int " flags ,
22 .BI " const struct timespec *" request ,
23 .BI " struct timespec *" remain );
27 Feature Test Macro Requirements for glibc (see
28 .BR feature_test_macros (7)):
31 .BR clock_nanosleep ():
33 _POSIX_C_SOURCE >= 200112L
38 .BR clock_nanosleep ()
39 allows the calling thread to sleep for an interval specified
40 with nanosecond precision.
41 It differs in allowing the caller to select the clock against
42 which the sleep interval is to be measured,
43 and in allowing the sleep interval to be specified as
44 either an absolute or a relative value.
46 The time values passed to and returned by this call are specified using
52 argument specifies the clock against which the sleep interval
54 This argument can have one of the following values:
55 .\" Look in time/posix-timers.c (kernel 5.6 sources) for the
56 .\" 'struct k_clock' structures that have an 'nsleep' method
59 A settable system-wide real-time clock.
61 .BR CLOCK_TAI " (since Linux 3.10)"
62 A system-wide clock derived from wall-clock time but ignoring leap seconds.
65 A nonsettable, monotonically increasing clock that measures time
66 since some unspecified point in the past that does not change after
68 .\" On Linux this clock measures time since boot.
70 .BR CLOCK_BOOTTIME " (since Linux 2.6.39)"
73 except that it also includes any time that the system is suspended.
75 .B CLOCK_PROCESS_CPUTIME_ID
76 A settable per-process clock that measures CPU time consumed
77 by all threads in the process.
78 .\" There is some trickery between glibc and the kernel
79 .\" to deal with the CLOCK_PROCESS_CPUTIME_ID case.
83 for further details on these clocks.
84 In addition, the CPU clock IDs returned by
85 .BR clock_getcpuclockid (3)
87 .BR pthread_getcpuclockid (3)
90 .\" Sleeping against CLOCK_REALTIME_ALARM and CLOCK_BOOTTIME_ALARM
91 .\" is also possible (tested), with CAP_WAKE_ALARM, but I'm not
92 .\" sure if this is useful or needs to be documented.
96 is 0, then the value specified in
98 is interpreted as an interval relative to the current
99 value of the clock specified by
108 is interpreted as an absolute time as measured by the clock,
112 is less than or equal to the current value of the clock,
114 .BR clock_nanosleep ()
115 returns immediately without suspending the calling thread.
117 .BR clock_nanosleep ()
118 suspends the execution of the calling thread
119 until either at least the time specified by
122 or a signal is delivered that causes a signal handler to be called or
123 that terminates the process.
125 If the call is interrupted by a signal handler,
126 .BR clock_nanosleep ()
135 it returns the remaining unslept time in
137 This value can then be used to call
138 .BR clock_nanosleep ()
139 again and complete a (relative) sleep.
141 On successfully sleeping for the requested interval,
142 .BR clock_nanosleep ()
144 If the call is interrupted by a signal handler or encounters an error,
145 then it returns one of the positive error number listed in ERRORS.
152 specified an invalid address.
155 The sleep was interrupted by a signal handler; see
161 field was not in the range 0 to 999999999 or
168 .RB ( CLOCK_THREAD_CPUTIME_ID
169 is not a permitted value for
173 The kernel does not support sleeping against this
177 .BR clock_nanosleep ()
178 system call first appeared in Linux 2.6.
179 Support is available in glibc since version 2.1.
181 POSIX.1-2001, POSIX.1-2008.
183 If the interval specified in
185 is not an exact multiple of the granularity underlying clock (see
187 then the interval will be rounded up to the next multiple.
188 Furthermore, after the sleep completes, there may still be a delay before
189 the CPU becomes free to once again execute the calling thread.
191 Using an absolute timer is useful for preventing
192 timer drift problems of the type described in
194 (Such problems are exacerbated in programs that try to restart
195 a relative sleep that is repeatedly interrupted by signals.)
196 To perform a relative sleep that avoids these problems, call
197 .BR clock_gettime (2)
198 for the desired clock,
199 add the desired interval to the returned time value,
201 .BR clock_nanosleep ()
206 .BR clock_nanosleep ()
207 is never restarted after being interrupted by a signal handler,
208 regardless of the use of the
215 argument is unused, and unnecessary, when
219 (An absolute sleep can be restarted using the same
223 POSIX.1 specifies that
224 .BR clock_nanosleep ()
225 has no effect on signals dispositions or the signal mask.
227 POSIX.1 specifies that after changing the value of the
230 .BR clock_settime (2),
231 the new clock value shall be used to determine the time
232 at which a thread blocked on an absolute
233 .BR clock_nanosleep ()
235 if the new clock value falls past the end of the sleep interval, then the
236 .BR clock_nanosleep ()
237 call will return immediately.
239 POSIX.1 specifies that
240 changing the value of the
243 .BR clock_settime (2)
244 shall have no effect on a thread that is blocked on a relative
245 .BR clock_nanosleep ().
247 .BR clock_getres (2),
249 .BR restart_syscall (2),
250 .BR timer_create (2),