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 2013-07-30 "Linux" "Linux Programmer's Manual"
28 clock_nanosleep \- high-resolution sleep with specifiable clock
33 .BI "int clock_nanosleep(clockid_t " clock_id ", 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).
42 Feature Test Macro Requirements for glibc (see
43 .BR feature_test_macros (7)):
46 .BR clock_nanosleep ():
48 _XOPEN_SOURCE\ >=\ 600 || _POSIX_C_SOURCE\ >=\ 200112L
54 .BR clock_nanosleep ()
55 allows the calling thread to sleep for an interval specified
56 with nanosecond precision.
57 It differs in allowing the caller to select the clock against
58 which the sleep interval is to be measured,
59 and in allowing the sleep interval to be specified as
60 either an absolute or a relative value.
62 The time values passed to and returned by this call are specified using
64 structures, defined as follows:
69 time_t tv_sec; /* seconds */
70 long tv_nsec; /* nanoseconds [0 .. 999999999] */
77 argument specifies the clock against which the sleep interval
79 This argument can have one of the following values:
82 A settable system-wide real-time clock.
85 A nonsettable, monotonically increasing clock that measures time
86 since some unspecified point in the past that does not change after
88 .\" On Linux this clock measures time since boot.
90 .BR CLOCK_PROCESS_CPUTIME_ID
91 A settable per-process clock that measures CPU time consumed
92 by all threads in the process.
93 .\" There is some trickery between glibc and the kernel
94 .\" to deal with the CLOCK_PROCESS_CPUTIME_ID case.
98 for further details on these clocks.
102 is 0, then the value specified in
104 is interpreted as an interval relative to the current
105 value of the clock specified by
114 is interpreted as an absolute time as measured by the clock,
118 is less than or equal to the current value of the clock,
120 .BR clock_nanosleep ()
121 returns immediately without suspending the calling thread.
123 .BR clock_nanosleep ()
124 suspends the execution of the calling thread
125 until either at least the time specified by
128 or a signal is delivered that causes a signal handler to be called or
129 that terminates the process.
131 If the call is interrupted by a signal handler,
132 .BR clock_nanosleep ()
141 it returns the remaining unslept time in
143 This value can then be used to call
144 .BR clock_nanosleep ()
145 again and complete a (relative) sleep.
147 On successfully sleeping for the requested interval,
148 .BR clock_nanosleep ()
150 If the call is interrupted by a signal handler or encounters an error,
151 then it returns one of the positive error number listed in ERRORS.
158 specified an invalid address.
161 The sleep was interrupted by a signal handler.
166 field was not in the range 0 to 999999999 or
173 .RB ( CLOCK_THREAD_CPUTIME_ID
174 is not a permitted value for
178 .BR clock_nanosleep ()
179 system call first appeared in Linux 2.6.
180 Support is available in glibc since version 2.1.
184 If the interval specified in
186 is not an exact multiple of the granularity underlying clock (see
188 then the interval will be rounded up to the next multiple.
189 Furthermore, after the sleep completes, there may still be a delay before
190 the CPU becomes free to once again execute the calling thread.
192 Using an absolute timer is useful for preventing
193 timer drift problems of the type described in
195 (Such problems are exacerbated in programs that try to restart
196 a relative sleep that is repeatedly interrupted by signals.)
197 To perform a relative sleep that avoids these problems, call
198 .BR clock_gettime (2)
199 for the desired clock,
200 add the desired interval to the returned time value,
202 .BR clock_nanosleep ()
207 .BR clock_nanosleep ()
208 is never restarted after being interrupted by a signal handler,
209 regardless of the use of the
216 argument is unused, and unnecessary, when
220 (An absolute sleep can be restarted using the same
224 POSIX.1 specifies that
225 .BR clock_nanosleep ()
226 has no effect on signals dispositions or the signal mask.
228 POSIX.1 specifies that after changing the value of the
231 .BR clock_settime (2),
232 the new clock value shall be used to determine the time
233 at which a thread blocked on an absolute
234 .BR clock_nanosleep ()
236 if the new clock value falls past the end of the sleep interval, then the
237 .BR clock_nanosleep ()
238 call will return immediately.
240 POSIX.1 specifies that
241 changing the value of the
244 .BR clock_settime (2)
245 shall have no effect on a thread that is blocked on a relative
246 .BR clock_nanosleep ().
248 .BR clock_getres (2),
250 .BR restart_syscall (2),
251 .BR timer_create (2),
256 This page is part of release 3.68 of the Linux
259 A description of the project,
260 information about reporting bugs,
261 and the latest version of this page,
263 \%http://www.kernel.org/doc/man\-pages/.