]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/clock_nanosleep.2
mknod.2: tfix
[thirdparty/man-pages.git] / man2 / clock_nanosleep.2
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 2020-04-11 "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 .ad l
41 .in -4n
42 Feature Test Macro Requirements for glibc (see
43 .BR feature_test_macros (7)):
44 .in
45 .PP
46 .BR clock_nanosleep ():
47 .RS 4
48 _POSIX_C_SOURCE\ >=\ 200112L
49 .RE
50 .ad
51 .SH DESCRIPTION
52 Like
53 .BR nanosleep (2),
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.
61 .PP
62 The time values passed to and returned by this call are specified using
63 .I timespec
64 structures, defined as follows:
65 .PP
66 .in +4n
67 .EX
68 struct timespec {
69 time_t tv_sec; /* seconds */
70 long tv_nsec; /* nanoseconds [0 .. 999999999] */
71 };
72 .EE
73 .in
74 .PP
75 The
76 .I clockid
77 argument specifies the clock against which the sleep interval
78 is to be measured.
79 This argument can have one of the following values:
80 .\" Look in time/posix-timers.c (kernel 5.6 sources) for the
81 .\" 'struct k_clock' structures that have an 'nsleep' method
82 .TP
83 .BR CLOCK_REALTIME
84 A settable system-wide real-time clock.
85 .TP
86 .BR CLOCK_TAI " (since Linux 3.10)"
87 A system-wide clock derived from wall-clock time but ignoring leap seconds.
88 .TP
89 .BR CLOCK_MONOTONIC
90 A nonsettable, monotonically increasing clock that measures time
91 since some unspecified point in the past that does not change after
92 system startup.
93 .\" On Linux this clock measures time since boot.
94 .TP
95 .BR CLOCK_BOOTIME " (since Linux 2.6.39)"
96 Identical to
97 .BR CLOCK_MONOTONIC ,
98 except that it also includes any time that the system is suspended.
99 .TP
100 .BR CLOCK_PROCESS_CPUTIME_ID
101 A settable per-process clock that measures CPU time consumed
102 by all threads in the process.
103 .\" There is some trickery between glibc and the kernel
104 .\" to deal with the CLOCK_PROCESS_CPUTIME_ID case.
105 .PP
106 See
107 .BR clock_getres (2)
108 for further details on these clocks.
109 In addition, the CPU clock IDs returned by
110 .BR clock_getcpuclockid (3)
111 and
112 .BR pthread_getcpuclockid (3)
113 can also be passed in
114 .IR clockid .
115 .\" Sleeping against CLOCK_REALTIME_ALARM and CLOCK_BOOTTIME_ALARM
116 .\" is also possible (tested), with CAP_WAKE_ALARM, but I'm not
117 .\" sure if this is useful or needs to be documented.
118 .PP
119 If
120 .I flags
121 is 0, then the value specified in
122 .I request
123 is interpreted as an interval relative to the current
124 value of the clock specified by
125 .IR clockid .
126 .PP
127 If
128 .I flags
129 is
130 .BR TIMER_ABSTIME ,
131 then
132 .I request
133 is interpreted as an absolute time as measured by the clock,
134 .IR clockid .
135 If
136 .I request
137 is less than or equal to the current value of the clock,
138 then
139 .BR clock_nanosleep ()
140 returns immediately without suspending the calling thread.
141 .PP
142 .BR clock_nanosleep ()
143 suspends the execution of the calling thread
144 until either at least the time specified by
145 .IR request
146 has elapsed,
147 or a signal is delivered that causes a signal handler to be called or
148 that terminates the process.
149 .PP
150 If the call is interrupted by a signal handler,
151 .BR clock_nanosleep ()
152 fails with the error
153 .BR EINTR .
154 In addition, if
155 .I remain
156 is not NULL, and
157 .I flags
158 was not
159 .BR TIMER_ABSTIME ,
160 it returns the remaining unslept time in
161 .IR remain .
162 This value can then be used to call
163 .BR clock_nanosleep ()
164 again and complete a (relative) sleep.
165 .SH RETURN VALUE
166 On successfully sleeping for the requested interval,
167 .BR clock_nanosleep ()
168 returns 0.
169 If the call is interrupted by a signal handler or encounters an error,
170 then it returns one of the positive error number listed in ERRORS.
171 .SH ERRORS
172 .TP
173 .B EFAULT
174 .I request
175 or
176 .I remain
177 specified an invalid address.
178 .TP
179 .B EINTR
180 The sleep was interrupted by a signal handler; see
181 .BR signal (7).
182 .TP
183 .B EINVAL
184 The value in the
185 .I tv_nsec
186 field was not in the range 0 to 999999999 or
187 .I tv_sec
188 was negative.
189 .TP
190 .B EINVAL
191 .I clockid
192 was invalid.
193 .RB ( CLOCK_THREAD_CPUTIME_ID
194 is not a permitted value for
195 .IR clockid .)
196 .TP
197 .B ENOTSUP
198 The kernel does not support sleeping against this
199 .IR clockid .
200 .SH VERSIONS
201 The
202 .BR clock_nanosleep ()
203 system call first appeared in Linux 2.6.
204 Support is available in glibc since version 2.1.
205 .SH CONFORMING TO
206 POSIX.1-2001, POSIX.1-2008.
207 .SH NOTES
208 If the interval specified in
209 .I request
210 is not an exact multiple of the granularity underlying clock (see
211 .BR time (7)),
212 then the interval will be rounded up to the next multiple.
213 Furthermore, after the sleep completes, there may still be a delay before
214 the CPU becomes free to once again execute the calling thread.
215 .PP
216 Using an absolute timer is useful for preventing
217 timer drift problems of the type described in
218 .BR nanosleep (2).
219 (Such problems are exacerbated in programs that try to restart
220 a relative sleep that is repeatedly interrupted by signals.)
221 To perform a relative sleep that avoids these problems, call
222 .BR clock_gettime (2)
223 for the desired clock,
224 add the desired interval to the returned time value,
225 and then call
226 .BR clock_nanosleep ()
227 with the
228 .B TIMER_ABSTIME
229 flag.
230 .PP
231 .BR clock_nanosleep ()
232 is never restarted after being interrupted by a signal handler,
233 regardless of the use of the
234 .BR sigaction (2)
235 .B SA_RESTART
236 flag.
237 .PP
238 The
239 .I remain
240 argument is unused, and unnecessary, when
241 .I flags
242 is
243 .BR TIMER_ABSTIME .
244 (An absolute sleep can be restarted using the same
245 .I request
246 argument.)
247 .PP
248 POSIX.1 specifies that
249 .BR clock_nanosleep ()
250 has no effect on signals dispositions or the signal mask.
251 .PP
252 POSIX.1 specifies that after changing the value of the
253 .B CLOCK_REALTIME
254 clock via
255 .BR clock_settime (2),
256 the new clock value shall be used to determine the time
257 at which a thread blocked on an absolute
258 .BR clock_nanosleep ()
259 will wake up;
260 if the new clock value falls past the end of the sleep interval, then the
261 .BR clock_nanosleep ()
262 call will return immediately.
263 .PP
264 POSIX.1 specifies that
265 changing the value of the
266 .B CLOCK_REALTIME
267 clock via
268 .BR clock_settime (2)
269 shall have no effect on a thread that is blocked on a relative
270 .BR clock_nanosleep ().
271 .SH SEE ALSO
272 .BR clock_getres (2),
273 .BR nanosleep (2),
274 .BR restart_syscall (2),
275 .BR timer_create (2),
276 .BR sleep (3),
277 .BR usleep (3),
278 .BR time (7)