Start of man-pages-5.14: updating Changes and Changes.old
[man-pages.git] / man2 / clock_nanosleep.2
blobb8c4afc2c83692c75d3feb2cf5c2d7cbbed2fd8c
1 .\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
3 .\"
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.
8 .\"
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.
13 .\"
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
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .TH CLOCK_NANOSLEEP 2 2021-03-22 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 clock_nanosleep \- high-resolution sleep with specifiable clock
29 .SH SYNOPSIS
30 .B #include <time.h>
31 .nf
32 .PP
33 .BI "int clock_nanosleep(clockid_t " clockid ", int " flags ,
34 .BI "                    const struct timespec *" request ,
35 .BI "                    struct timespec *" remain );
36 .fi
37 .PP
38 Link with \fI\-lrt\fP (only for glibc versions before 2.17).
39 .PP
40 .RS -4
41 Feature Test Macro Requirements for glibc (see
42 .BR feature_test_macros (7)):
43 .RE
44 .PP
45 .BR clock_nanosleep ():
46 .nf
47     _POSIX_C_SOURCE >= 200112L
48 .fi
49 .SH DESCRIPTION
50 Like
51 .BR nanosleep (2),
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.
59 .PP
60 The time values passed to and returned by this call are specified using
61 .I timespec
62 structures, defined as follows:
63 .PP
64 .in +4n
65 .EX
66 struct timespec {
67     time_t tv_sec;        /* seconds */
68     long   tv_nsec;       /* nanoseconds [0 .. 999999999] */
70 .EE
71 .in
72 .PP
73 The
74 .I clockid
75 argument specifies the clock against which the sleep interval
76 is to be measured.
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
80 .TP
81 .BR CLOCK_REALTIME
82 A settable system-wide real-time clock.
83 .TP
84 .BR CLOCK_TAI " (since Linux 3.10)"
85 A system-wide clock derived from wall-clock time but ignoring leap seconds.
86 .TP
87 .BR CLOCK_MONOTONIC
88 A nonsettable, monotonically increasing clock that measures time
89 since some unspecified point in the past that does not change after
90 system startup.
91 .\" On Linux this clock measures time since boot.
92 .TP
93 .BR CLOCK_BOOTIME " (since Linux 2.6.39)"
94 Identical to
95 .BR CLOCK_MONOTONIC ,
96 except that it also includes any time that the system is suspended.
97 .TP
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.
105 .BR clock_getres (2)
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
112 .IR clockid .
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.
118 .I flags
119 is 0, then the value specified in
120 .I request
121 is interpreted as an interval relative to the current
122 value of the clock specified by
123 .IR clockid .
126 .I flags
128 .BR TIMER_ABSTIME ,
129 then
130 .I request
131 is interpreted as an absolute time as measured by the clock,
132 .IR clockid .
134 .I request
135 is less than or equal to the current value of the clock,
136 then
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
143 .IR request
144 has elapsed,
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 ()
150 fails with the error
151 .BR EINTR .
152 In addition, if
153 .I remain
154 is not NULL, and
155 .I flags
156 was not
157 .BR TIMER_ABSTIME ,
158 it returns the remaining unslept time in
159 .IR remain .
160 This value can then be used to call
161 .BR clock_nanosleep ()
162 again and complete a (relative) sleep.
163 .SH RETURN VALUE
164 On successfully sleeping for the requested interval,
165 .BR clock_nanosleep ()
166 returns 0.
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.
169 .SH ERRORS
171 .B EFAULT
172 .I request
174 .I remain
175 specified an invalid address.
177 .B EINTR
178 The sleep was interrupted by a signal handler; see
179 .BR signal (7).
181 .B EINVAL
182 The value in the
183 .I tv_nsec
184 field was not in the range 0 to 999999999 or
185 .I tv_sec
186 was negative.
188 .B EINVAL
189 .I clockid
190 was invalid.
191 .RB ( CLOCK_THREAD_CPUTIME_ID
192 is not a permitted value for
193 .IR clockid .)
195 .B ENOTSUP
196 The kernel does not support sleeping against this
197 .IR clockid .
198 .SH VERSIONS
200 .BR clock_nanosleep ()
201 system call first appeared in Linux 2.6.
202 Support is available in glibc since version 2.1.
203 .SH CONFORMING TO
204 POSIX.1-2001, POSIX.1-2008.
205 .SH NOTES
206 If the interval specified in
207 .I request
208 is not an exact multiple of the granularity underlying clock (see
209 .BR time (7)),
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
216 .BR nanosleep (2).
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,
223 and then call
224 .BR clock_nanosleep ()
225 with the
226 .B TIMER_ABSTIME
227 flag.
229 .BR clock_nanosleep ()
230 is never restarted after being interrupted by a signal handler,
231 regardless of the use of the
232 .BR sigaction (2)
233 .B SA_RESTART
234 flag.
237 .I remain
238 argument is unused, and unnecessary, when
239 .I flags
241 .BR TIMER_ABSTIME .
242 (An absolute sleep can be restarted using the same
243 .I request
244 argument.)
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
251 .B CLOCK_REALTIME
252 clock via
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 ()
257 will wake up;
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
264 .B CLOCK_REALTIME
265 clock via
266 .BR clock_settime (2)
267 shall have no effect on a thread that is blocked on a relative
268 .BR clock_nanosleep ().
269 .SH SEE ALSO
270 .BR clock_getres (2),
271 .BR nanosleep (2),
272 .BR restart_syscall (2),
273 .BR timer_create (2),
274 .BR sleep (3),
275 .BR usleep (3),
276 .BR time (7)