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 (date) "Linux man-pages (unreleased)"
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 *" t ,
23 .BI " struct timespec *_Nullable " 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 counting 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, 999999999] or
168 .RB ( CLOCK_THREAD_CPUTIME_ID
169 is not a permitted value for
173 The kernel does not support sleeping against this
182 If the interval specified in
184 is not an exact multiple of the granularity underlying clock (see
186 then the interval will be rounded up to the next multiple.
187 Furthermore, after the sleep completes, there may still be a delay before
188 the CPU becomes free to once again execute the calling thread.
190 Using an absolute timer is useful for preventing
191 timer drift problems of the type described in
193 (Such problems are exacerbated in programs that try to restart
194 a relative sleep that is repeatedly interrupted by signals.)
195 To perform a relative sleep that avoids these problems, call
196 .BR clock_gettime (2)
197 for the desired clock,
198 add the desired interval to the returned time value,
200 .BR clock_nanosleep ()
205 .BR clock_nanosleep ()
206 is never restarted after being interrupted by a signal handler,
207 regardless of the use of the
214 argument is unused, and unnecessary, when
218 (An absolute sleep can be restarted using the same
222 POSIX.1 specifies that
223 .BR clock_nanosleep ()
224 has no effect on signals dispositions or the signal mask.
226 POSIX.1 specifies that after changing the value of the
229 .BR clock_settime (2),
230 the new clock value shall be used to determine the time
231 at which a thread blocked on an absolute
232 .BR clock_nanosleep ()
234 if the new clock value falls past the end of the sleep interval, then the
235 .BR clock_nanosleep ()
236 call will return immediately.
238 POSIX.1 specifies that
239 changing the value of the
242 .BR clock_settime (2)
243 shall have no effect on a thread that is blocked on a relative
244 .BR clock_nanosleep ().
246 .BR clock_getres (2),
248 .BR restart_syscall (2),
249 .BR timer_create (2),