1 .\" Copyright (C) Markus Kuhn, 1996
2 .\" and Copyright (C) Linux Foundation, 2008, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
6 .\" This is free documentation; you can redistribute it and/or
7 .\" modify it under the terms of the GNU General Public License as
8 .\" published by the Free Software Foundation; either version 2 of
9 .\" the License, or (at your option) any later version.
11 .\" The GNU General Public License's references to "object code"
12 .\" and "executables" are to be interpreted as the output of any
13 .\" document formatting or typesetting system, including
14 .\" intermediate and printed output.
16 .\" This manual is distributed in the hope that it will be useful,
17 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
18 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 .\" GNU General Public License for more details.
21 .\" You should have received a copy of the GNU General Public
22 .\" License along with this manual; if not, see
23 .\" <http://www.gnu.org/licenses/>.
26 .\" 1996-04-10 Markus Kuhn <mskuhn@cip.informatik.uni-erlangen.de>
27 .\" First version written
28 .\" Modified, 2004-10-24, aeb
30 .\" Minor rewrites of some parts.
31 .\" NOTES: describe case where clock_nanosleep() can be preferable.
32 .\" NOTES: describe CLOCK_REALTIME versus CLOCK_NANOSLEEP
33 .\" Replace crufty discussion of HZ with a pointer to time(7).
34 .TH NANOSLEEP 2 2017-03-13 "Linux" "Linux Programmer's Manual"
36 nanosleep \- high-resolution sleep
40 .BI "int nanosleep(const struct timespec *" req ", struct timespec *" rem );
43 Feature Test Macro Requirements for glibc (see
44 .BR feature_test_macros (7)):
48 _POSIX_C_SOURCE\ >=\ 199309L
51 suspends the execution of the calling thread
52 until either at least the time specified in
54 has elapsed, or the delivery of a signal
55 that triggers the invocation of a handler in the calling thread or
56 that terminates the process.
58 If the call is interrupted by a signal handler,
64 and writes the remaining time into the structure pointed to by
71 can then be used to call
73 again and complete the specified pause (but see NOTES).
77 is used to specify intervals of time with nanosecond precision.
78 It is defined as follows:
83 time_t tv_sec; /* seconds */
84 long tv_nsec; /* nanoseconds */
89 The value of the nanoseconds field must be in the range 0 to 999999999.
96 has the following advantages:
97 it provides a higher resolution for specifying the sleep interval;
98 POSIX.1 explicitly specifies that it
99 does not interact with signals;
100 and it makes the task of resuming a sleep that has been
101 interrupted by a signal handler easier.
103 On successfully sleeping for the requested interval,
106 If the call is interrupted by a signal handler or encounters an error,
107 then it returns \-1, with
109 set to indicate the error.
113 Problem with copying information from user space.
116 The pause has been interrupted by a signal that was
117 delivered to the thread (see
119 The remaining sleep time has been written
122 so that the thread can easily call
124 again and continue with the pause.
129 field was not in the range 0 to 999999999 or
133 POSIX.1-2001, POSIX.1-2008.
135 If the interval specified in
137 is not an exact multiple of the granularity underlying clock (see
139 then the interval will be rounded up to the next multiple.
140 Furthermore, after the sleep completes, there may still be a delay before
141 the CPU becomes free to once again execute the calling thread.
145 sleeps for a relative interval can be problematic if the call
146 is repeatedly restarted after being interrupted by signals,
147 since the time between the interruptions and restarts of the call
148 will lead to drift in the time when the sleep finally completes.
149 This problem can be avoided by using
150 .BR clock_nanosleep (2)
151 with an absolute time value.
153 POSIX.1 specifies that
155 should measure time against the
158 However, Linux measures the time using the
161 .\" See also http://thread.gmane.org/gmane.linux.kernel/696854/
162 .\" Subject: nanosleep() uses CLOCK_MONOTONIC, should be CLOCK_REALTIME?
163 .\" Date: 2008-06-22 07:35:41 GMT
164 This probably does not matter, since the POSIX.1 specification for
165 .BR clock_settime (2)
166 says that discontinuous changes in
172 Setting the value of the
175 .BR clock_settime (2)
177 have no effect on threads that are blocked waiting for a relative time
178 service based upon this clock, including the
181 Consequently, these time services shall expire when the requested relative
182 interval elapses, independently of the new or old value of the clock.
185 In order to support applications requiring much more precise pauses
186 (e.g., in order to control some time-critical hardware),
188 would handle pauses of up to 2 milliseconds by busy waiting with microsecond
189 precision when called from a thread scheduled under a real-time policy
194 This special extension was removed in kernel 2.5.39,
195 and is thus not available in Linux 2.6.0 and later kernels.
197 If a program that catches signals and uses
199 receives signals at a very high rate,
200 then scheduling delays and rounding errors in the kernel's
201 calculation of the sleep interval and the returned
207 on successive restarts of the
210 To avoid such problems, use
211 .BR clock_nanosleep (2)
214 flag to sleep to an absolute deadline.
218 is stopped by a signal (e.g.,
220 then the call fails with the error
222 after the thread is resumed by a
225 If the system call is subsequently restarted,
226 then the time that the thread spent in the stopped state is
228 counted against the sleep interval.
229 This problem is fixed in Linux 2.6.0 and later kernels.
231 .BR clock_nanosleep (2),
232 .BR restart_syscall (2),
233 .BR sched_setscheduler (2),
234 .BR timer_create (2),