1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" Copyright (C) Markus Kuhn, 1996
4 .\" and Copyright (C) Linux Foundation, 2008, written by Michael Kerrisk
5 .\" <mtk.manpages@gmail.com>
7 .\" This is free documentation; you can redistribute it and/or
8 .\" modify it under the terms of the GNU General Public License as
9 .\" published by the Free Software Foundation; either version 2 of
10 .\" the License, or (at your option) any later version.
12 .\" The GNU General Public License's references to "object code"
13 .\" and "executables" are to be interpreted as the output of any
14 .\" document formatting or typesetting system, including
15 .\" intermediate and printed output.
17 .\" This manual is distributed in the hope that it will be useful,
18 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
19 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 .\" GNU General Public License for more details.
22 .\" You should have received a copy of the GNU General Public
23 .\" License along with this manual; if not, write to the Free
24 .\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
27 .\" 1996-04-10 Markus Kuhn <mskuhn@cip.informatik.uni-erlangen.de>
28 .\" First version written
29 .\" Modified, 2004-10-24, aeb
31 .\" Minor rewrites of some parts.
32 .\" NOTES: describe case where clock_nanosleep() can be preferable.
33 .\" NOTES: describe CLOCK_REALTIME versus CLOCK_NANOSLEEP
34 .\" Replace crufty discussion of HZ with a pointer to time(7).
36 .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya
37 .\" all rights reserved.
38 .\" Translated, 1997-02-23, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
39 .\" Updated 1999-02-27, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
40 .\" Updated 2003-09-15, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
41 .\" Updated 2005-02-10, Akihiro MOTOKI
42 .\" Updated 2006-07-23, Akihiro MOTOKI, LDP v2.36
43 .\" Updated 2006-08-13, Akihiro MOTOKI, LDP v2.39
44 .\" Updated 2008-08-08, Akihiro MOTOKI, LDP v3.05
48 .\"WORD: signal ¥·¥°¥Ê¥ë
50 .\"WORD: block ¶Ø»ß(block)
51 .\"WORD: kernel ¥«¡¼¥Í¥ë
52 .\"WORD: real-time ¥ê¥¢¥ë¡¦¥¿¥¤¥à
53 .\"WORD: schedule ¥¹¥±¥¸¥å¡¼¥ë
54 .\"WORD: busy wait ¥Ó¥·¡¼¡¦¥¦¥§¥¤¥È
56 .TH NANOSLEEP 2 2009-01-19 "Linux" "Linux Programmer's Manual"
59 .\"O nanosleep \- high-resolution sleep
60 nanosleep \- ¹âÀºÅ٤ʥ¹¥ê¡¼¥×
65 .BI "int nanosleep(const struct timespec *" req ", struct timespec *" rem );
68 .\"O Feature Test Macro Requirements for glibc (see
69 .\"O .BR feature_test_macros (7)):
70 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
71 .RB ( feature_test_macros (7)
76 _POSIX_C_SOURCE\ >=\ 199309L
80 .\"O suspends the execution of the calling thread
81 .\"O until either at least the time specified in
83 .\"O has elapsed, or the delivery of a signal
84 .\"O that triggers the invocation of a handler in the calling thread or
85 .\"O that terminates the process.
89 ¤Ç»ØÄꤵ¤ì¤¿»þ´Ö¤Î´Ö¡¢¥×¥í¥°¥é¥à¤Î¼Â¹Ô¤òÃٱ䤵¤»¤ë¡£
91 ¤Ï¡¢¸Æ¤Ó½Ð¤·¤¿¥¹¥ì¥Ã¥É¤Î¼Â¹Ô¤ò¡¢
94 ¤Ç»ØÄꤵ¤ì¤¿»þ´Ö¤Î´Ö¡¢¤â¤·¤¯¤Ï¸Æ¤Ó½Ð¤·¤¿¥¹¥ì¥Ã¥É¤Ç¥Ï¥ó¥É¥é¤Îµ¯Æ°¤Î
95 ¤¤Ã¤«¤±¤È¤Ê¤ë¥·¥°¥Ê¥ë¡¢¤Þ¤¿¤Ï¥×¥í¥»¥¹¤ò½ªÎ»¤µ¤»¤ë¥·¥°¥Ê¥ë¤ÎÇÛÁ÷¤¬
96 ¹Ô¤ï¤ì¤ë¤Þ¤Ç°ì»þÄä»ß¤¹¤ë¡£
98 .\"O If the call is interrupted by a signal handler,
100 .\"O returns \-1, sets \fIerrno\fP to
102 .\"O and writes the remaining time into the structure pointed to by
107 ¸Æ¤Ó½Ð¤·¤¬¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ë¤è¤ê³ä¤ê¹þ¤Þ¤ì¤¿¾ì¹ç¡¢
118 ¤¬»Ø¤¹¹½Â¤ÂΤ˳ÊǼ¤¹¤ë¡£
121 .\"O can then be used to call
122 .\"O .BR nanosleep ()
123 .\"O again and complete the specified pause (but see NOTES).
127 ¤ò¤â¤¦°ìÅٸƤӽФ·¤Æ¡¢»ØÄꤷ¤¿»þ´Ö¤ÎÄä»ß¤ò
128 ´°Î»¤µ¤»¤ë¤³¤È¤¬¤Ç¤¤ë (⤷¡¢¡ÖÃí°Õ¡×¤ÎÀá¤ò»²¾È¤Î¤³¤È)¡£
132 .\"O is used to specify intervals of time with nanosecond precision.
133 .\"O It is defined as follows:
134 ¥Ê¥ÎÉùï¤ß¤Î»þ´Ö´Ö³Ö¤ò»ØÄꤹ¤ë¤Î¤Ë
136 ¹½Â¤ÂΤ¬»ÈÍѤµ¤ì¤ë¡£¤³¤Î¹½Â¤ÂΤϼ¡¤Î¤è¤¦¤ËÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£
140 .\"O struct timespec {
141 .\"O time_t tv_sec; /* seconds */
142 .\"O long tv_nsec; /* nanoseconds */
143 time_t tv_sec; /* ÉÃ */
144 long tv_nsec; /* ¥Ê¥ÎÉà */
149 .\"O The value of the nanoseconds field must be in the range 0 to 999999999.
150 ¥Ê¥ÎÉäΥե£¡¼¥ë¥É¤ÎÃÍ¤Ï 0 ¤«¤é 999999999 ¤ÎÈϰϤˤʤ±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
156 .\"O .BR nanosleep ()
157 .\"O has the following advantages:
158 .\"O it provides a higher resolution for specifying the sleep interval;
159 .\"O POSIX.1 explicitly specifies that it
160 .\"O does not interact with signals;
161 .\"O and it makes the task of resuming a sleep that has been
162 .\"O interrupted by a signal handler easier.
168 ¤Ë¤Ï°Ê²¼¤Î¤è¤¦¤ÊÍøÅÀ¤¬¤¢¤ë:
169 Ää»ß´ü´Ö¤Î»ØÄê¤Ë´Ø¤·¤Æ¹â¤¤»þ´Öʬ²òǽ¤¬Ä󶡤µ¤ì¤Æ¤¤¤ë¡£
170 ¥·¥°¥Ê¥ë¤È¸ß¤¤¤Ë±Æ¶Á¤òµÚ¤Ü¤¹¤³¤È¤¬¤Ê¤¤¤È
171 POSIX.1 ¤ÇÌÀ¼¨Åª¤Ëµ¬Äꤵ¤ì¤Æ¤¤¤ë¡£
172 ¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ë¤è¤Ã¤Æ³ä¤ê¹þ¤Þ¤ì¤¿ºÝ¤Ë¡¢Ää»ß¤òºÆ³«¤¹¤ë¤Î¤¬
174 .\"O .SH "RETURN VALUE"
176 .\"O On successfully sleeping for the requested interval,
177 .\"O .BR nanosleep ()
179 .\"O If the call is interrupted by a signal handler or encounters an error,
180 .\"O then it returns \-1, with
182 .\"O set to indicate the error.
183 Í׵ᤵ¤ì¤¿´ü´Ö¤ÎÄä»ß¤ËÀ®¸ù¤·¤¿¾ì¹ç¡¢
185 ¤Ï 0 ¤òÊÖ¤¹¡£¸Æ¤Ó½Ð¤·¤¬¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ë¤è¤ê³ä¤ê¹þ¤Þ¤ì¤¿¤ê¡¢
186 ¥¨¥é¡¼¤¬È¯À¸¤·¤¿¾ì¹ç¤Ï¡¢\-1 ¤òÊÖ¤·¡¢
188 ¤Ë¥¨¥é¡¼ÆâÍƤò¼¨¤¹ÃͤòÀßÄꤹ¤ë¡£
193 .\"O Problem with copying information from user space.
194 ¥æ¡¼¥¶¶õ´Ö¤«¤é¤Î¾ðÊó¤Î¥³¥Ô¡¼¤ÇÌäÂ꤬¤¢¤Ã¤¿¡£
197 .\"O The pause has been interrupted by a signal that was
198 .\"O delivered to the thread.
199 .\"O The remaining sleep time has been written
200 .\"O into \fI*rem\fP so that the thread can easily call
201 .\"O .BR nanosleep ()
202 .\"O again and continue with the pause.
203 ¤½¤Î¥¹¥ì¥Ã¥É¤ËÇÛÁ÷¤µ¤ì¤¿¥·¥°¥Ê¥ë¤Ë¤è¤êÄä»ß¤¬ÃæÃǤµ¤ì¤¿¡£
206 ¤òºÆ¤Ó¸Æ¤Ó½Ð¤·¤ÆÄä»ß¤ò³¤±¤ë¤³¤È¤¬¤Ç¤¤ë¤è¤¦¤Ë¡¢
207 »Ä¤ê¤ÎÄä»ß»þ´Ö¤¬ \fI*rem\fP ¤Ë³ÊǼ¤µ¤ì¤ë¡£
210 .\"O The value in the
212 .\"O field was not in the range 0 to 999999999 or
216 ¥Õ¥£¡¼¥ë¥É¤ÎÃͤ¬ 0 ¤«¤é 999999999 ¤Þ¤Ç¤ÎÈϰϤˤʤ«¤Ã¤¿¤«¡¢
219 .\"O .SH "CONFORMING TO"
224 .\"O If the interval specified in
226 .\"O is not an exact multiple of the granularity underlying clock (see
228 .\"O then the interval will be rounded up to the next multiple.
229 .\"O Furthermore, after the sleep completes, there may still be a delay before
230 .\"O the CPU becomes free to once again execute the calling thread.
232 ¤Ç»ØÄꤵ¤ì¤¿´ü´Ö¤¬¡¢ÆâÉô¤Ç»ÈÍѤµ¤ì¤ë¥¯¥í¥Ã¥¯¤ÎγÅÙ¤ÎÇÜ¿ô¤Ë¤Ê¤Ã¤Æ¤¤¤Ê¤¤
233 ¾ì¹ç¡¢´ü´Ö¤Ï°ìÈֶᤤÇÜ¿ô¤ËÀÚ¤ê¾å¤²¤é¤ì¤ë¡£
234 ¤Þ¤¿¡¢Ää»ß¤¬´°Î»¤·¤¿¸å¡¢CPU ¤¬¸Æ¤Ó½Ð¤·¸µ¤Î¥¹¥ì¥Ã¥É¤òºÆ¤Ó¼Â¹Ô¤Ç¤¤ë¤è¤¦¤Ë
235 ¤Ê¤ë¤Þ¤Ç¤Ë¤ÏÃٱ䤬Æþ¤ë¡£
238 .\"O .BR nanosleep ()
239 .\"O sleeps for a relative interval can be problematic if the call
240 .\"O is repeatedly restarted after being interrupted by signals,
241 .\"O since the time between the interruptions and restarts of the call
242 .\"O will lead to drift in the time when the sleep finally completes.
243 .\"O This problem can be avoided by using
244 .\"O .BR clock_nanosleep (2)
245 .\"O with an absolute time value.
246 ¥·¥°¥Ê¥ë¤Ë¤è¤ë³ä¤ê¹þ¤ß¸å¤Ë·«¤êÊÖ¤·ºÆ³«¤µ¤ì¤¿¾ì¹ç¡¢
248 ¤ÎÄä»ß¤¬ÁêÂÐŪ¤Ê´ü´Ö¤Ç¤¢¤ë¤³¤È¤ÏÌäÂê¤È¤Ê¤ë¤³¤È¤¬¤¢¤ë¡£
249 ¤³¤ì¤Ï¡¢¸Æ¤Ó½Ð¤·¤Î³ä¤ê¹þ¤ß¤«¤éºÆ³«¤Þ¤Ç¤Î´Ö¤Î»þ´Ö¤¬¸¶°ø¤Ç
250 Ää»ß¤¬ºÇ½ªÅª¤Ë´°Î»¤·¤¿ºÝ¤Ë»þ´Ö¤Ë¤º¤ì¤¬È¯À¸¤¹¤ë¤«¤é¤Ç¤¢¤ë¡£
251 ¤³¤ÎÌäÂê¤Ï¡¢ÀäÂлþ¹ï¤¬»ØÄê¤Ç¤¤ë
252 .BR clock_nanosleep (2)
253 ¤ò»È¤¦¤³¤È¤Ç²óÈò¤Ç¤¤ë¡£
255 .\"O POSIX.1 specifies that
256 .\"O .BR nanosleep ()
257 .\"O should measure time against the
258 .\"O .B CLOCK_REALTIME
260 .\"O However, Linux measures the time using the
261 .\"O .B CLOCK_MONOTONIC
267 ¤ËÂФ·¤Æ»þ¹ï¤ò·×¬¤¹¤ë¤Ù¤¤À¤Èµ¬Äꤷ¤Æ¤¤¤ë¡£
268 ¤·¤«¤·¤Ê¤¬¤é¡¢Linux ¤Ï
270 ¥¯¥í¥Ã¥¯¤òÍѤ¤¤Æ»þ¹ï¤ò·×¬¤·¤Æ¤¤¤ë¡£
271 .\" See also http://thread.gmane.org/gmane.linux.kernel/696854/
272 .\" Subject: nanosleep() uses CLOCK_MONOTONIC, should be CLOCK_REALTIME?
273 .\" Date: 2008-06-22 07:35:41 GMT
274 .\"O This probably does not matter, since the POSIX.1 specification for
275 .\"O .BR clock_settime ()
276 .\"O says that discontinuous changes in
277 .\"O .B CLOCK_REALTIME
278 .\"O should not affect
279 .\"O .BR nanosleep ():
280 ¤³¤Î¤³¤È¤Ï¤ª¤½¤é¤¯ÌäÂê¤Ë¤Ê¤é¤Ê¤¤¤À¤í¤¦¡£
287 ¤Ë±Æ¶Á¤¹¤Ù¤¤Ç¤Ï¤Ê¤¤¡¢¤È½ñ¤«¤ì¤Æ¤¤¤ë¤«¤é¤Ç¤¢¤ë¡£
290 .\"O Setting the value of the
291 .\"O .B CLOCK_REALTIME
293 .\"O .BR clock_settime ()
295 .\"O have no effect on threads that are blocked waiting for a relative time
296 .\"O service based upon this clock, including the
297 .\"O .BR nanosleep ()
299 .\"O Consequently, these time services shall expire when the requested relative
300 .\"O interval elapses, independently of the new or old value of the clock.
304 ¥¯¥í¥Ã¥¯¤ÎÃͤòÀßÄꤷ¤Æ¤â¡¢
308 ¤Ë´ð¤Å¤¯¥µ¡¼¥Ó¥¹¤Ë¤è¤êÁêÂÐŪ¤Ê´ü´Ö¤À¤±¼Â¹Ô¤òÄä»ß¤¹¤ë¥¹¥ì¥Ã¥É¤Ë¤Ï±Æ¶Á¤Ï¤Ê¤¤¡£
309 ·ë²Ì¤È¤·¤Æ¡¢¥¯¥í¥Ã¥¯Ãͤ¬¹¹¿·Á°¤«¸å¤«¤Ë´Ø¤ï¤é¤º¡¢Í׵ᤵ¤ì¤¿ÁêÂÐŪ¤Ê»þ´Ö¤¬
310 ·Ð²á¤¹¤ë¤ÈËþλ¤¹¤ë¤³¤È¤Ë¤Ê¤ë¡£
312 .\"O .SS "Old behavior"
314 .\"O In order to support applications requiring much more precise pauses
315 .\"O (e.g., in order to control some time-critical hardware),
316 .\"O .BR nanosleep ()
317 .\"O would handle pauses of up to 2\ ms by busy waiting with microsecond
318 .\"O precision when called from a thread scheduled under a real-time policy
323 .\"O This special extension was removed in kernel 2.5.39,
324 .\"O hence is still present in
325 .\"O current 2.4 kernels, but not in 2.6 kernels.
326 (Î㤨¤Ð¡¢»þ´Ö¤¬½ÅÍפʰÕÌ£¤ò»ý¤Ä¥Ï¡¼¥É¥¦¥§¥¢¤òÀ©¸æ¤¹¤ë¾ì¹ç¤Ê¤É)
327 ¤è¤êÀµ³Î¤ÊÄä»ß¤òɬÍפȤ¹¤ë¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ËÂбþ¤¹¤ë¤¿¤á¤Ë¡¢
329 ¤Ï¡¢¥Þ¥¤¥¯¥íÉÃÀºÅ٤Υӥ¸¡¼¡¦¥¦¥§¥¤¥È¤òÍøÍѤ¹¤ë¤³¤È¤Ç¡¢
330 2\ ms °Ê²¼¤ÎÄä»ß¤ò¹Ô¤¦¤³¤È¤¬¤Ç¤¤¿¡£
331 ⤷¡¢¤³¤Îµ¡Ç½¤òÍøÍѤ¹¤ë¤Ë¤Ï¡¢¸Æ¤Ó½Ð¤·¸µ¤Î¥¹¥ì¥Ã¥É¤¬
335 ¤È¤¤¤Ã¤¿¥ê¥¢¥ë¥¿¥¤¥à¡¦¥Ý¥ê¥·¡¼¤Î¸µ¤Ç¥¹¥±¥¸¥å¡¼¥ê¥ó¥°¤µ¤ì¤Æ¤¤¤ë
337 ¤³¤ÎÆÃÊ̤ʳÈÄ¥¤Ï¥«¡¼¥Í¥ë 2.5.39 ¤Çºï½ü¤µ¤ì¤¿¡£¤·¤¿¤¬¤Ã¤Æ¡¢
338 ¸½ºß¤Î 2.4 ·ÏÎó¤Î¥«¡¼¥Í¥ë¤Ë¤Ï¤³¤Îµ¡Ç½¤¬Â¸ºß¤¹¤ë¤¬¡¢
339 2.6·ÏÎó¤Î¥«¡¼¥Í¥ë¤Ë¤Ï¤Ê¤¤¡£
342 .\"O In Linux 2.4, if
343 .\"O .BR nanosleep ()
344 .\"O is stopped by a signal (e.g.,
346 .\"O then the call fails with the error
348 .\"O after the thread is resumed by a
351 .\"O If the system call is subsequently restarted,
352 .\"O then the time that the thread spent in the stopped state is
353 .\"O \fInot\fP counted against the sleep interval.
358 ¤Ê¤É¤Î) ¥·¥°¥Ê¥ë¤Ë¤è¤êÄä»ß¤µ¤ì¤¿¾ì¹ç¡¢
362 ¥·¥°¥Ê¥ë¤Ë¤è¤ë¥¹¥ì¥Ã¥É¤ÎºÆ³«¸å¤Ë
365 ¥·¥¹¥Æ¥à¥³¡¼¥ë¤¬¤³¤Î¸å¤ÇºÆ¥¹¥¿¡¼¥È¤µ¤ì¤¿¾ì¹ç¡¢
366 ¥¹¥ì¥Ã¥É¤¬Ää»ß¾õÂ֤ˤ¢¤ë´Ö¤Ë·Ð²á¤·¤¿»þ´Ö¤Ï
367 Ää»ß´ü´Ö¤È¤·¤Æ¥«¥¦¥ó¥È¡Ö¤µ¤ì¤Ê¤¤¡×¡£
370 .BR clock_nanosleep (2),
371 .BR sched_setscheduler (2),
373 .BR timer_create (2),