1 .\" Copyright (C) Markus Kuhn, 1996
2 .\" and Copyright (C) Linux Foundation, 2008, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: GPL-2.0-or-later
7 .\" 1996-04-10 Markus Kuhn <mskuhn@cip.informatik.uni-erlangen.de>
8 .\" First version written
9 .\" Modified, 2004-10-24, aeb
11 .\" Minor rewrites of some parts.
12 .\" NOTES: describe case where clock_nanosleep() can be preferable.
13 .\" NOTES: describe CLOCK_REALTIME versus CLOCK_NANOSLEEP
14 .\" Replace crufty discussion of HZ with a pointer to time(7).
15 .TH nanosleep 2 (date) "Linux man-pages (unreleased)"
17 nanosleep \- high-resolution sleep
20 .RI ( libc ", " \-lc )
25 .BI "int nanosleep(const struct timespec *" duration ,
26 .BI " struct timespec *_Nullable " rem );
30 Feature Test Macro Requirements for glibc (see
31 .BR feature_test_macros (7)):
36 _POSIX_C_SOURCE >= 199309L
40 suspends the execution of the calling thread
41 until either at least the time specified in
43 has elapsed, or the delivery of a signal
44 that triggers the invocation of a handler in the calling thread or
45 that terminates the process.
47 If the call is interrupted by a signal handler,
53 and writes the remaining time into the structure pointed to by
60 can then be used to call
62 again and complete the specified pause (but see NOTES).
67 is used to specify intervals of time with nanosecond precision.
69 The value of the nanoseconds field must be in the range [0, 999999999].
76 has the following advantages:
77 it provides a higher resolution for specifying the sleep interval;
78 POSIX.1 explicitly specifies that it
79 does not interact with signals;
80 and it makes the task of resuming a sleep that has been
81 interrupted by a signal handler easier.
83 On successfully sleeping for the requested duration,
86 If the call is interrupted by a signal handler or encounters an error,
87 then it returns \-1, with
89 set to indicate the error.
93 Problem with copying information from user space.
96 The pause has been interrupted by a signal that was
97 delivered to the thread (see
99 The remaining sleep time has been written
102 so that the thread can easily call
104 again and continue with the pause.
109 field was not in the range [0, 999999999] or
113 POSIX.1 specifies that
115 should measure time against the
118 However, Linux measures the time using the
121 .\" See also http://thread.gmane.org/gmane.linux.kernel/696854/
122 .\" Subject: nanosleep() uses CLOCK_MONOTONIC, should be CLOCK_REALTIME?
123 .\" Date: 2008-06-22 07:35:41 GMT
124 This probably does not matter, since the POSIX.1 specification for
125 .BR clock_settime (2)
126 says that discontinuous changes in
132 Setting the value of the
135 .BR clock_settime (2)
137 have no effect on threads that are blocked waiting for a relative time
138 service based upon this clock, including the
142 these time services shall expire when the requested duration elapses,
143 independently of the new or old value of the clock.
150 In order to support applications requiring much more precise pauses
151 (e.g., in order to control some time-critical hardware),
153 would handle pauses of up to 2 milliseconds by busy waiting with microsecond
154 precision when called from a thread scheduled under a real-time policy
159 This special extension was removed in Linux 2.5.39,
160 and is thus not available in Linux 2.6.0 and later kernels.
164 is not an exact multiple of the granularity underlying clock (see
166 then the interval will be rounded up to the next multiple.
167 Furthermore, after the sleep completes, there may still be a delay before
168 the CPU becomes free to once again execute the calling thread.
172 sleeps for a relative interval can be problematic if the call
173 is repeatedly restarted after being interrupted by signals,
174 since the time between the interruptions and restarts of the call
175 will lead to drift in the time when the sleep finally completes.
176 This problem can be avoided by using
177 .BR clock_nanosleep (2)
178 with an absolute time value.
180 If a program that catches signals and uses
182 receives signals at a very high rate,
183 then scheduling delays and rounding errors in the kernel's
184 calculation of the sleep interval and the returned
190 on successive restarts of the
193 To avoid such problems, use
194 .BR clock_nanosleep (2)
197 flag to sleep to an absolute deadline.
201 is stopped by a signal (e.g.,
203 then the call fails with the error
205 after the thread is resumed by a
208 If the system call is subsequently restarted,
209 then the time that the thread spent in the stopped state is
211 counted against the sleep interval.
212 This problem is fixed in Linux 2.6.0 and later kernels.
214 .BR clock_nanosleep (2),
215 .BR restart_syscall (2),
216 .BR sched_setscheduler (2),
217 .BR timer_create (2),