OSDN Git Service

LDP: Update original to LDP v3.68
[linuxjm/LDP_man-pages.git] / original / 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 2013-07-30 "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 .sp
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 .sp
38 Link with \fI\-lrt\fP (only for glibc versions before 2.17).
39 .sp
40 .ad l
41 .in -4n
42 Feature Test Macro Requirements for glibc (see
43 .BR feature_test_macros (7)):
44 .in
45 .sp
46 .BR clock_nanosleep ():
47 .RS 4
48 _XOPEN_SOURCE\ >=\ 600 || _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
62 The time values passed to and returned by this call are specified using
63 .I timespec
64 structures, defined as follows:
65 .sp
66 .in +4n
67 .nf
68 struct timespec {
69     time_t tv_sec;        /* seconds */
70     long   tv_nsec;       /* nanoseconds [0 .. 999999999] */
71 };
72 .fi
73 .in
74
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
100 If
101 .I flags
102 is 0, then the value specified in
103 .I request
104 is interpreted as an interval relative to the current
105 value of the clock specified by
106 .IR clock_id .
107
108 If
109 .I flags
110 is
111 .BR TIMER_ABSTIME ,
112 then
113 .I request
114 is interpreted as an absolute time as measured by the clock,
115 .IR clock_id .
116 If
117 .I request
118 is less than or equal to the current value of the clock,
119 then
120 .BR clock_nanosleep ()
121 returns immediately without suspending the calling thread.
122
123 .BR clock_nanosleep ()
124 suspends the execution of the calling thread
125 until either at least the time specified by
126 .IR request
127 has elapsed,
128 or a signal is delivered that causes a signal handler to be called or
129 that terminates the process.
130
131 If the call is interrupted by a signal handler,
132 .BR clock_nanosleep ()
133 fails with the error
134 .BR EINTR .
135 In addition, if
136 .I remain
137 is not NULL, and
138 .I flags
139 was not
140 .BR TIMER_ABSTIME ,
141 it returns the remaining unslept time in
142 .IR remain .
143 This value can then be used to call
144 .BR clock_nanosleep ()
145 again and complete a (relative) sleep.
146 .SH RETURN VALUE
147 On successfully sleeping for the requested interval,
148 .BR clock_nanosleep ()
149 returns 0.
150 If the call is interrupted by a signal handler or encounters an error,
151 then it returns one of the positive error number listed in ERRORS.
152 .SH ERRORS
153 .TP
154 .B EFAULT
155 .I request
156 or
157 .I remain
158 specified an invalid address.
159 .TP
160 .B EINTR
161 The sleep was interrupted by a signal handler.
162 .TP
163 .B EINVAL
164 The value in the
165 .I tv_nsec
166 field was not in the range 0 to 999999999 or
167 .I tv_sec
168 was negative.
169 .TP
170 .B EINVAL
171 .I clock_id
172 was invalid.
173 .RB ( CLOCK_THREAD_CPUTIME_ID
174 is not a permitted value for
175 .IR clock_id .)
176 .SH VERSIONS
177 The
178 .BR clock_nanosleep ()
179 system call first appeared in Linux 2.6.
180 Support is available in glibc since version 2.1.
181 .SH CONFORMING TO
182 POSIX.1-2001.
183 .SH NOTES
184 If the interval specified in
185 .I request
186 is not an exact multiple of the granularity underlying clock (see
187 .BR time (7)),
188 then the interval will be rounded up to the next multiple.
189 Furthermore, after the sleep completes, there may still be a delay before
190 the CPU becomes free to once again execute the calling thread.
191
192 Using an absolute timer is useful for preventing
193 timer drift problems of the type described in
194 .BR nanosleep (2).
195 (Such problems are exacerbated in programs that try to restart
196 a relative sleep that is repeatedly interrupted by signals.)
197 To perform a relative sleep that avoids these problems, call
198 .BR clock_gettime (2)
199 for the desired clock,
200 add the desired interval to the returned time value,
201 and then call
202 .BR clock_nanosleep ()
203 with the
204 .B TIMER_ABSTIME
205 flag.
206
207 .BR clock_nanosleep ()
208 is never restarted after being interrupted by a signal handler,
209 regardless of the use of the
210 .BR sigaction (2)
211 .B SA_RESTART
212 flag.
213
214 The
215 .I remain
216 argument is unused, and unnecessary, when
217 .I flags
218 is
219 .BR TIMER_ABSTIME .
220 (An absolute sleep can be restarted using the same
221 .I request
222 argument.)
223
224 POSIX.1 specifies that
225 .BR clock_nanosleep ()
226 has no effect on signals dispositions or the signal mask.
227
228 POSIX.1 specifies that after changing the value of the
229 .B CLOCK_REALTIME
230 clock via
231 .BR clock_settime (2),
232 the new clock value shall be used to determine the time
233 at which a thread blocked on an absolute
234 .BR clock_nanosleep ()
235 will wake up;
236 if the new clock value falls past the end of the sleep interval, then the
237 .BR clock_nanosleep ()
238 call will return immediately.
239
240 POSIX.1 specifies that
241 changing the value of the
242 .B CLOCK_REALTIME
243 clock via
244 .BR clock_settime (2)
245 shall have no effect on a thread that is blocked on a relative
246 .BR clock_nanosleep ().
247 .SH SEE ALSO
248 .BR clock_getres (2),
249 .BR nanosleep (2),
250 .BR restart_syscall (2),
251 .BR timer_create (2),
252 .BR sleep (3),
253 .BR usleep (3),
254 .BR time (7)
255 .SH COLOPHON
256 This page is part of release 3.68 of the Linux
257 .I man-pages
258 project.
259 A description of the project,
260 information about reporting bugs,
261 and the latest version of this page,
262 can be found at
263 \%http://www.kernel.org/doc/man\-pages/.