1 .\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH CLOCK_NANOSLEEP 2 2021-03-22 "Linux" "Linux Programmer's Manual"
28 clock_nanosleep \- high-resolution sleep with specifiable clock
33 .BI "int clock_nanosleep(clockid_t " clockid ", int " flags ,
34 .BI " const struct timespec *" request ,
35 .BI " struct timespec *" remain );
38 Link with \fI\-lrt\fP (only for glibc versions before 2.17).
41 Feature Test Macro Requirements for glibc (see
42 .BR feature_test_macros (7)):
45 .BR clock_nanosleep ():
47 _POSIX_C_SOURCE >= 200112L
52 .BR clock_nanosleep ()
53 allows the calling thread to sleep for an interval specified
54 with nanosecond precision.
55 It differs in allowing the caller to select the clock against
56 which the sleep interval is to be measured,
57 and in allowing the sleep interval to be specified as
58 either an absolute or a relative value.
60 The time values passed to and returned by this call are specified using
62 structures, defined as follows:
67 time_t tv_sec; /* seconds */
68 long tv_nsec; /* nanoseconds [0 .. 999999999] */
75 argument specifies the clock against which the sleep interval
77 This argument can have one of the following values:
78 .\" Look in time/posix-timers.c (kernel 5.6 sources) for the
79 .\" 'struct k_clock' structures that have an 'nsleep' method
82 A settable system-wide real-time clock.
84 .BR CLOCK_TAI " (since Linux 3.10)"
85 A system-wide clock derived from wall-clock time but ignoring leap seconds.
88 A nonsettable, monotonically increasing clock that measures time
89 since some unspecified point in the past that does not change after
91 .\" On Linux this clock measures time since boot.
93 .BR CLOCK_BOOTIME " (since Linux 2.6.39)"
96 except that it also includes any time that the system is suspended.
98 .BR CLOCK_PROCESS_CPUTIME_ID
99 A settable per-process clock that measures CPU time consumed
100 by all threads in the process.
101 .\" There is some trickery between glibc and the kernel
102 .\" to deal with the CLOCK_PROCESS_CPUTIME_ID case.
106 for further details on these clocks.
107 In addition, the CPU clock IDs returned by
108 .BR clock_getcpuclockid (3)
110 .BR pthread_getcpuclockid (3)
111 can also be passed in
113 .\" Sleeping against CLOCK_REALTIME_ALARM and CLOCK_BOOTTIME_ALARM
114 .\" is also possible (tested), with CAP_WAKE_ALARM, but I'm not
115 .\" sure if this is useful or needs to be documented.
119 is 0, then the value specified in
121 is interpreted as an interval relative to the current
122 value of the clock specified by
131 is interpreted as an absolute time as measured by the clock,
135 is less than or equal to the current value of the clock,
137 .BR clock_nanosleep ()
138 returns immediately without suspending the calling thread.
140 .BR clock_nanosleep ()
141 suspends the execution of the calling thread
142 until either at least the time specified by
145 or a signal is delivered that causes a signal handler to be called or
146 that terminates the process.
148 If the call is interrupted by a signal handler,
149 .BR clock_nanosleep ()
158 it returns the remaining unslept time in
160 This value can then be used to call
161 .BR clock_nanosleep ()
162 again and complete a (relative) sleep.
164 On successfully sleeping for the requested interval,
165 .BR clock_nanosleep ()
167 If the call is interrupted by a signal handler or encounters an error,
168 then it returns one of the positive error number listed in ERRORS.
175 specified an invalid address.
178 The sleep was interrupted by a signal handler; see
184 field was not in the range 0 to 999999999 or
191 .RB ( CLOCK_THREAD_CPUTIME_ID
192 is not a permitted value for
196 The kernel does not support sleeping against this
200 .BR clock_nanosleep ()
201 system call first appeared in Linux 2.6.
202 Support is available in glibc since version 2.1.
204 POSIX.1-2001, POSIX.1-2008.
206 If the interval specified in
208 is not an exact multiple of the granularity underlying clock (see
210 then the interval will be rounded up to the next multiple.
211 Furthermore, after the sleep completes, there may still be a delay before
212 the CPU becomes free to once again execute the calling thread.
214 Using an absolute timer is useful for preventing
215 timer drift problems of the type described in
217 (Such problems are exacerbated in programs that try to restart
218 a relative sleep that is repeatedly interrupted by signals.)
219 To perform a relative sleep that avoids these problems, call
220 .BR clock_gettime (2)
221 for the desired clock,
222 add the desired interval to the returned time value,
224 .BR clock_nanosleep ()
229 .BR clock_nanosleep ()
230 is never restarted after being interrupted by a signal handler,
231 regardless of the use of the
238 argument is unused, and unnecessary, when
242 (An absolute sleep can be restarted using the same
246 POSIX.1 specifies that
247 .BR clock_nanosleep ()
248 has no effect on signals dispositions or the signal mask.
250 POSIX.1 specifies that after changing the value of the
253 .BR clock_settime (2),
254 the new clock value shall be used to determine the time
255 at which a thread blocked on an absolute
256 .BR clock_nanosleep ()
258 if the new clock value falls past the end of the sleep interval, then the
259 .BR clock_nanosleep ()
260 call will return immediately.
262 POSIX.1 specifies that
263 changing the value of the
266 .BR clock_settime (2)
267 shall have no effect on a thread that is blocked on a relative
268 .BR clock_nanosleep ().
270 .BR clock_getres (2),
272 .BR restart_syscall (2),
273 .BR timer_create (2),