]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/clock_nanosleep.2
proc.5: 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 2017-09-15 "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 " clock_id ", 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 clock_id
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 .TP 17
81 .BR CLOCK_REALTIME
82 A settable system-wide real-time clock.
83 .TP
84 .BR CLOCK_MONOTONIC
85 A nonsettable, monotonically increasing clock that measures time
86 since some unspecified point in the past that does not change after
87 system startup.
88 .\" On Linux this clock measures time since boot.
89 .TP
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.
95 .PP
96 See
97 .BR clock_getres (2)
98 for further details on these clocks.
99 In addition, the CPU clock IDs returned by
100 .BR clock_getcpuclockid (3)
101 and
102 .BR pthread_getcpuclockid (3)
103 can also be passed in
104 .IR clock_id .
105 .PP
106 If
107 .I flags
108 is 0, then the value specified in
109 .I request
110 is interpreted as an interval relative to the current
111 value of the clock specified by
112 .IR clock_id .
113 .PP
114 If
115 .I flags
116 is
117 .BR TIMER_ABSTIME ,
118 then
119 .I request
120 is interpreted as an absolute time as measured by the clock,
121 .IR clock_id .
122 If
123 .I request
124 is less than or equal to the current value of the clock,
125 then
126 .BR clock_nanosleep ()
127 returns immediately without suspending the calling thread.
128 .PP
129 .BR clock_nanosleep ()
130 suspends the execution of the calling thread
131 until either at least the time specified by
132 .IR request
133 has elapsed,
134 or a signal is delivered that causes a signal handler to be called or
135 that terminates the process.
136 .PP
137 If the call is interrupted by a signal handler,
138 .BR clock_nanosleep ()
139 fails with the error
140 .BR EINTR .
141 In addition, if
142 .I remain
143 is not NULL, and
144 .I flags
145 was not
146 .BR TIMER_ABSTIME ,
147 it returns the remaining unslept time in
148 .IR remain .
149 This value can then be used to call
150 .BR clock_nanosleep ()
151 again and complete a (relative) sleep.
152 .SH RETURN VALUE
153 On successfully sleeping for the requested interval,
154 .BR clock_nanosleep ()
155 returns 0.
156 If the call is interrupted by a signal handler or encounters an error,
157 then it returns one of the positive error number listed in ERRORS.
158 .SH ERRORS
159 .TP
160 .B EFAULT
161 .I request
162 or
163 .I remain
164 specified an invalid address.
165 .TP
166 .B EINTR
167 The sleep was interrupted by a signal handler; see
168 .BR signal (7).
169 .TP
170 .B EINVAL
171 The value in the
172 .I tv_nsec
173 field was not in the range 0 to 999999999 or
174 .I tv_sec
175 was negative.
176 .TP
177 .B EINVAL
178 .I clock_id
179 was invalid.
180 .RB ( CLOCK_THREAD_CPUTIME_ID
181 is not a permitted value for
182 .IR clock_id .)
183 .SH VERSIONS
184 The
185 .BR clock_nanosleep ()
186 system call first appeared in Linux 2.6.
187 Support is available in glibc since version 2.1.
188 .SH CONFORMING TO
189 POSIX.1-2001, POSIX.1-2008.
190 .SH NOTES
191 If the interval specified in
192 .I request
193 is not an exact multiple of the granularity underlying clock (see
194 .BR time (7)),
195 then the interval will be rounded up to the next multiple.
196 Furthermore, after the sleep completes, there may still be a delay before
197 the CPU becomes free to once again execute the calling thread.
198 .PP
199 Using an absolute timer is useful for preventing
200 timer drift problems of the type described in
201 .BR nanosleep (2).
202 (Such problems are exacerbated in programs that try to restart
203 a relative sleep that is repeatedly interrupted by signals.)
204 To perform a relative sleep that avoids these problems, call
205 .BR clock_gettime (2)
206 for the desired clock,
207 add the desired interval to the returned time value,
208 and then call
209 .BR clock_nanosleep ()
210 with the
211 .B TIMER_ABSTIME
212 flag.
213 .PP
214 .BR clock_nanosleep ()
215 is never restarted after being interrupted by a signal handler,
216 regardless of the use of the
217 .BR sigaction (2)
218 .B SA_RESTART
219 flag.
220 .PP
221 The
222 .I remain
223 argument is unused, and unnecessary, when
224 .I flags
225 is
226 .BR TIMER_ABSTIME .
227 (An absolute sleep can be restarted using the same
228 .I request
229 argument.)
230 .PP
231 POSIX.1 specifies that
232 .BR clock_nanosleep ()
233 has no effect on signals dispositions or the signal mask.
234 .PP
235 POSIX.1 specifies that after changing the value of the
236 .B CLOCK_REALTIME
237 clock via
238 .BR clock_settime (2),
239 the new clock value shall be used to determine the time
240 at which a thread blocked on an absolute
241 .BR clock_nanosleep ()
242 will wake up;
243 if the new clock value falls past the end of the sleep interval, then the
244 .BR clock_nanosleep ()
245 call will return immediately.
246 .PP
247 POSIX.1 specifies that
248 changing the value of the
249 .B CLOCK_REALTIME
250 clock via
251 .BR clock_settime (2)
252 shall have no effect on a thread that is blocked on a relative
253 .BR clock_nanosleep ().
254 .SH SEE ALSO
255 .BR clock_getres (2),
256 .BR nanosleep (2),
257 .BR restart_syscall (2),
258 .BR timer_create (2),
259 .BR sleep (3),
260 .BR usleep (3),
261 .BR time (7)