1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" This manpage is copyright (C) 1992 Drew Eckhardt,
4 .\" copyright (C) 1995 Michael Shields.
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
27 .\" Modified 1995-05-18 by Jim Van Zandt <jrv@vanzandt.mv.com>
28 .\" Sun Feb 11 14:07:00 MET 1996 Martin Schulze <joey@linux.de>
29 .\" * layout slightly modified
31 .\" Modified Mon Oct 21 23:05:29 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
32 .\" Modified Thu Feb 24 01:41:09 CET 2000 by aeb
33 .\" Modified Thu Feb 9 22:32:09 CET 2001 by bert hubert <ahu@ds9a.nl>, aeb
34 .\" Modified Mon Nov 11 14:35:00 PST 2002 by Ben Woodard <ben@zork.net>
35 .\" 2005-03-11, mtk, modified pselect() text (it is now a system
38 .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya all rights reserved.
39 .\" Translated 1997-02-23, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
40 .\" Modified 2000-03-12, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
41 .\" Updated & Modified 2001-06-04, Yuichi SATO <ysato@h4.dion.ne.jp>
42 .\" Updated & Modified 2001-07-01, Yuichi SATO
43 .\" Updated & Modifedd 2002-01-14, Yuichi SATO
44 .\" Updated 2002-09-24, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
45 .\" Updated 2003-01-19, Akihiro MOTOKI
46 .\" Updated 2005-03-17, Akihiro MOTOKI
47 .\" Updated 2006-04-16, Akihiro MOTOKI, LDP v2.28
48 .\" Updated 2006-07-23, Akihiro MOTOKI, LDP v2.36
49 .\" Updated 2007-09-08, Akihiro MOTOKI, LDP v2.64
50 .\" Updated 2008-08-06, Akihiro MOTOKI, LDP v3.05
52 .\"WORD: synchronous Ʊ´ü¤µ¤»¤ë
53 .\"WORD: multiplexing ¿½Å²½
54 .\"WORD: file descriptor ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿
55 .\"WORD: descriptor µ½Ò»Ò
57 .\"WORD: block Ää»ß(block)
58 .\"WORD: block ¶Ø»ß(block)
59 .\"WORD: signal ¥·¥°¥Ê¥ë
60 .\"WORD: timeout »þ´ÖÀÚ¤ì(timeout)
61 .\"WORD: portable °Ü¿¢À¤Î¤¢¤ë
63 .TH SELECT 2 2008-12-05 "Linux" "Linux Programmer's Manual"
66 .\"O select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO \-
67 .\"O synchronous I/O multiplexing
68 select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO \- ¿½Å²½¤µ¤ì¤¿ I/O ¤ÎƱ´ü¤ò¤È¤ë
72 .\"O /* According to POSIX.1-2001 */
73 /* POSIX.1-2001 ¤Ë½¾¤¦¾ì¹ç */
75 .B #include <sys/select.h>
77 .\"O /* According to earlier standards */
78 /* °ÊÁ°¤Îµ¬³Ê¤Ë½¾¤¦¾ì¹ç */
80 .B #include <sys/time.h>
82 .B #include <sys/types.h>
84 .B #include <unistd.h>
86 .BI "int select(int " nfds ", fd_set *" readfds ", fd_set *" writefds ,
87 .BI " fd_set *" exceptfds ", struct timeval *" timeout );
89 .BI "void FD_CLR(int " fd ", fd_set *" set );
91 .BI "int FD_ISSET(int " fd ", fd_set *" set );
93 .BI "void FD_SET(int " fd ", fd_set *" set );
95 .BI "void FD_ZERO(fd_set *" set );
97 .B #include <sys/select.h>
99 .BI "int pselect(int " nfds ", fd_set *" readfds ", fd_set *" writefds ,
100 .BI " fd_set *" exceptfds ", const struct timespec *" timeout ,
101 .BI " const sigset_t *" sigmask );
105 .\"O Feature Test Macro Requirements for glibc (see
106 .\"O .BR feature_test_macros (7)):
107 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
108 .RB ( feature_test_macros (7)
113 _POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600
119 .\"O allow a program to monitor multiple file descriptors,
120 .\"O waiting until one or more of the file descriptors become "ready"
121 .\"O for some class of I/O operation (e.g., input possible).
122 .\"O A file descriptor is considered ready if it is possible to
123 .\"O perform the corresponding I/O operation (e.g.,
125 .\"O without blocking.
129 ¤ò»È¤¦¤È¡¢¥×¥í¥°¥é¥à¤ÇÊ£¿ô¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò´Æ»ë¤·¡¢
130 °ì¤Ä°Ê¾å¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬¤¢¤ë¼ï¤Î I/O Áàºî¤Î
131 ¡Öready (½àÈ÷¤¬¤Ç¤¤¿)¡×¾õÂÖ (Î㤨¤Ð¡¢Æɤ߹þ¤ß²Äǽ¤Ë¤Ê¤Ã¤¿¾õÂÖ)
132 ¤Ë¤Ê¤ë¤Þ¤ÇÂԤĤ³¤È¤¬¤Ç¤¤ë¡£
133 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬ ready (½àÈ÷¤¬¤Ç¤¤¿) ¤È¤Ï¡¢
134 Âбþ¤¹¤ë I/O Áàºî (Î㤨¤Ð
136 ¤Ê¤É) ¤¬Ää»ß (block) ¤Ê¤·¤Ë¼Â¹Ô²Äǽ¤Ê¾õÂ֤ˤ¢¤ë¤³¤È¤ò°ÕÌ£¤¹¤ë¡£
138 .\"O The operation of
142 .\"O is identical, with three differences:
146 ¤ÎÆ°ºî¤ÏƱ¤¸¤Ç¤¢¤ë¤¬¡¢°Ê²¼¤Î 3 ÅÀ¤¬°Û¤Ê¤ë:
150 .\"O uses a timeout that is a
151 .\"O .I struct timeval
152 .\"O (with seconds and microseconds), while
155 .\"O .I struct timespec
156 .\"O (with seconds and nanoseconds).
158 ¤Ç¤Ï¡¢¥¿¥¤¥à¥¢¥¦¥È»þ´Ö¤Î»ØÄê¤Ë¹½Â¤ÂÎ
160 (Éᦥޥ¤¥¯¥íÉÃñ°Ì) ¤òÍѤ¤¤ë¡£
165 (ÉᦥʥÎÉÃñ°Ì) ¤òÍѤ¤¤ë¡£
171 .\"O argument to indicate how much time was left.
173 .\"O does not change this argument.
177 °ú¤¿ô¤ò¹¹¿·¤¹¤ë¤³¤È¤¬¤¢¤ë¡£
179 ¤Ï¤³¤Î°ú¤¿ô¤òÊѹ¹¤·¤Ê¤¤¡£
186 .\"O argument, and behaves as
188 .\"O called with NULL
193 °ú¤¿ô¤ò»ý¤¿¤Ê¤¤¡£¤½¤ÎÆ°ºî¤Ï
195 ¤Ë NULL ¤ò»ØÄꤷ¤¿¾ì¹ç¤Î
199 .\"O Three independent sets of file descriptors are watched.
202 .\"O will be watched to see if characters become
203 .\"O available for reading (more precisely, to see if a read will not
204 .\"O block; in particular, a file descriptor is also ready on end-of-file),
207 .\"O will be watched to see if a write will not block, and
210 .\"O will be watched for exceptions.
211 .\"O On exit, the sets are modified in place
212 .\"O to indicate which file descriptors actually changed status.
213 .\"O Each of the three file descriptor sets may be specified as NULL
214 .\"O if no file descriptors are to be watched for the corresponding class
216 3 ¤Ä¤ÎÆÈΩ¤·¤¿¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤Î´Æ»ë¤ò¹Ô¤¦¡£
218 ¤ËÆþ¤ì¤é¤ì¤¿¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ë¤Ä¤¤¤Æ¤Ï¡¢Æɤ߹þ¤ß¤¬²Äǽ¤«¤É¤¦¤«¤ò
219 ´Æ»ë¤¹¤ë (¤è¤êÀµ³Î¤Ë¤¤¤¦¤È¡¢Ää»ß (block) ¤Ê¤·¤ÇÆɤळ¤È¤¬¤Ç¤¤ë¤«¤ò
220 Ä´¤Ù¤ë¡£¥Õ¥¡¥¤¥ë¤Î½ªÃ¼ (end-of-file) ¤Î¾ì¹ç¤â¡¢
221 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÏÆɤ߹þ¤ß²Äǽ¤È¤·¤Æ°·¤ï¤ì¤ë)¡£
223 ¤ËÆþ¤ì¤é¤ì¤¿¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ë¤Ä¤¤¤Æ¤Ï¡¢Ää»ß¤»¤º¤Ë½ñ¤¹þ¤ß¤¬
224 ²Äǽ¤«¤É¤¦¤«¤ò´Æ»ë¤¹¤ë¡£
226 ¤Ë¤¢¤ë¤â¤Î¤Ë¤Ä¤¤¤Æ¤Ï¡¢Îã³°¤Î´Æ»ë¤ò¹Ô¤Ê¤¦¡£¥·¥¹¥Æ¥à¥³¡¼¥ë½ªÎ»»þ¤Ë¡¢
227 ¤É¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¾õÂÖ¤¬¼ÂºÝ¤ËÊѲ½¤·¤¿¤«¼¨¤¹¤¿¤á¤Ë¡¢
228 ½¸¹ç¤ÎÆâÍƤ¬Êѹ¹¤µ¤ì¤ë¡£
229 ¤¢¤ë¼ïÊ̤Υ¤¥Ù¥ó¥È¤ò´Æ»ë¤·¤¿¤¤¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬°ì¤Ä¤â¤Ê¤¤¾ì¹ç¤Ë¤Ï¡¢
230 Âбþ¤¹¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤Ë NULL ¤ò»ØÄꤹ¤ë¤³¤È¤¬¤Ç¤¤ë¡£
232 .\"O Four macros are provided to manipulate the sets.
233 ½¸¹ç¤òÁàºî¤¹¤ë¤¿¤á¤Ë 4 ¤Ä¤Î¥Þ¥¯¥í¤¬Ä󶡤µ¤ì¤Æ¤¤¤ë¡£
241 .\"O respectively add and remove a given file descriptor from a set.
245 ¤Ï¤½¤ì¤¾¤ì»ØÄꤷ¤¿¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î½¸¹ç¤Ø¤ÎÄɲᢺï½ü¤ò¹Ô¤¦¡£
247 .\"O tests to see if a file descriptor is part of the set;
248 .\"O this is useful after
252 ¤Ï½¸¹ç¤Ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬¤¢¤ë¤«¤É¤¦¤«Ä´¤Ù¤ë;
255 ¤¬½ªÎ»¤·¤¿¸å¤Ë»È¤¦¤ÈÊØÍø¤Ç¤¢¤ë¡£
258 .\"O is the highest-numbered file descriptor in any of the three sets, plus 1.
260 ¤Ï 3 ¤Ä¤Î½¸¹ç¤Ë´Þ¤Þ¤ì¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÎºÇÂçÃÍ¤Ë 1 ¤ò¤·¤¿¤â¤Î¤Ç¤¢¤ë¡£
263 .\"O is an upper bound on the amount of time elapsed before
266 .\"O If both fields of the
268 .\"O stucture are zero, then
270 .\"O returns immediately.
271 .\"O (This is useful for polling.)
274 .\"O is NULL (no timeout),
276 .\"O can block indefinitely.
280 ¤¬Éüµ¢¤¹¤ë¤Þ¤Ç¤Î·Ð²á»þ´Ö¤Î¾å¸Â¤Ç¤¢¤ë¡£
282 ¹½Â¤ÂΤÎξÊý¤Î¥Õ¥£¡¼¥ë¥É¤¬ 0 ¤Î¾ì¹ç¡¢
285 (¤³¤Îµ¡Ç½¤Ï¥Ý¡¼¥ê¥ó¥° (polling) ¤ò¹Ô¤¦¤Î¤ËÊØÍø¤Ç¤¢¤ë)¡£
287 ¤Ë NULL (¥¿¥¤¥à¥¢¥¦¥È¤Ê¤·)
290 ¤Ï̵´ü¸Â¤ËÄä»ß (block) ¤¹¤ë¡£
293 .\"O is a pointer to a signal mask (see
294 .\"O .BR sigprocmask (2));
295 .\"O if it is not NULL, then
297 .\"O first replaces the current signal mask by the one pointed to by
299 .\"O then does the "select" function, and then restores the original
303 .RB ( sigprocmask (2)
304 ¤ò»²¾È) ¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
310 ¤¬»Ø¤·¤Æ¤¤¤ë¥·¥°¥Ê¥ë¥Þ¥¹¥¯¤Ç¸½ºß¤Î¥·¥°¥Ê¥ë¥Þ¥¹¥¯¤òÃÖ¤´¹¤¨¤Æ¤«¤é¡¢
311 "select" ´Ø¿ô¤ò¼Â¹Ô¤·¡¢
312 ½ªÎ»¸å¤Ë¥·¥°¥Ê¥ë¥Þ¥¹¥¯¤ò¸µ¤Î¥·¥°¥Ê¥ë¥Þ¥¹¥¯¤ËÌ᤹¡£
314 .\"O Other than the difference in the precision of the
316 .\"O argument, the following
320 °ú¤¿ô¤ÎÀºÅ٤ΰ㤤¤ò½ü¤¯¤È¡¢°Ê²¼¤Î
325 ready = pselect(nfds, &readfds, &writefds, &exceptfds,
329 .\"O is equivalent to
331 .\"O executing the following calls:
334 ¤Ë¼Â¹Ô¤¹¤ë¤Î¤ÈÅù²Á¤Ç¤¢¤ë¡£
339 sigprocmask(SIG_SETMASK, &sigmask, &origmask);
340 ready = select(nfds, &readfds, &writefds, &exceptfds, timeout);
341 sigprocmask(SIG_SETMASK, &origmask, NULL);
346 .\"O is needed is that if one wants to wait for either a signal
347 .\"O or for a file descriptor to become ready, then
348 .\"O an atomic test is needed to prevent race conditions.
349 .\"O (Suppose the signal handler sets a global flag and
351 .\"O Then a test of this global flag followed by a call of
353 .\"O could hang indefinitely if the signal arrived just after the test
354 .\"O but just before the call.
356 ¤¬É¬ÍפˤʤëÍýͳ¤Ï¡¢¥·¥°¥Ê¥ë¤ä¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¾õÂÖÊѲ½¤ò
357 ÂÔ¤Á¤¿¤¤¤È¤¤Ë¤Ï¡¢¶¥¹ç¾õÂÖ¤òÈò¤±¤ë¤¿¤á¤Ë atomic ¤Ê¥Æ¥¹¥È¤¬É¬Íפˤʤë
359 (¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤¬Âç°è¥Õ¥é¥°¤òÀßÄꤷ¤ÆÌá¤ë¾ì¹ç¤ò¹Í¤¨¤Æ¤ß¤è¤¦¡£
360 ¤³¤ÎÂç°è¥Õ¥é¥°¤Î¥Æ¥¹¥È¤Ë³¤±¤Æ
363 ¥·¥°¥Ê¥ë¤¬¥Æ¥¹¥È¤Îľ¸å¤«¤Ä¸Æ¤Ó½Ð¤·¤ÎľÁ°¤ËÆϤ¤¤¿»þ¤Ë¤Ï
365 ¤Ï±Êµ×¤Ë¥Ï¥ó¥°¤·¤Æ¤·¤Þ¤¦¤«¤â¤·¤ì¤Ê¤¤¡£
368 .\"O allows one to first block signals, handle the signals that have come in,
371 .\"O with the desired
373 .\"O avoiding the race.)
376 ¤ò»È¤¦¤È¡¢¤Þ¤º¥·¥°¥Ê¥ë¤ò¶Ø»ß (block) ¤·¤Æ¡¢Æþ¤Ã¤Æ¤¯¤ë¥·¥°¥Ê¥ë¤òÁàºî¤·¡¢
381 ¤ò¸Æ¤Ó½Ð¤¹¤³¤È¤Ç¡¢Á°µ¤Î¶¥¹ç¤òÈò¤±¤ë¤³¤È¤¬¤Ç¤¤ë¡£)
382 .\"O .SS "The timeout"
384 .\"O The time structures involved are defined in
387 ¤³¤ì¤é¤Î´Ø¿ô¤Ç»ÈÍѤµ¤ì¤ë»þ´Ö´ØÏ¢¤Î¹½Â¤ÂΤϡ¢
389 ¤Ç°Ê²¼¤Î¤è¤¦¤ËÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£
393 .\"O struct timeval {
394 .\"O long tv_sec; /* seconds */
395 .\"O long tv_usec; /* microseconds */
402 long tv_sec; /* ÉÃ */
403 long tv_usec; /* ¥Þ¥¤¥¯¥íÉà */
412 .\"O struct timespec {
413 .\"O long tv_sec; /* seconds */
414 .\"O long tv_nsec; /* nanoseconds */
421 long tv_sec; /* ÉÃ */
422 long tv_nsec; /* ¥Ê¥ÎÉà */
427 .\"O (However, see below on the POSIX.1-2001 versions.)
428 (POSIX.1-2001 ¤Ç¤ÎÄêµÁ¤Ë¤Ä¤¤¤Æ¤Ï²¼µ¤Î¡ÖÃí°Õ¡×¤ò»²¾È)
432 .\"O with all three sets empty,
434 .\"O zero, and a non-NULL
436 .\"O as a fairly portable way to sleep with subsecond precision.
437 ÉÃñ°Ì°Ê²¼¤ÎÀºÅ٤ǥ¹¥ê¡¼¥×¤ò¼Â¸½¤¹¤ë
438 °Ü¿¢À¤Î¹â¤¤ÊýË¡¤È¤·¤Æ¡¢
443 ¤ò NULL ¤Ç¤Ê¤¤ÃͤËÀßÄꤷ¤Æ
445 ¤ò¸Æ¤Ó½Ð¤¹¤È¤¤¤¦ÊýË¡¤ò»È¤Ã¤Æ¤¤¤ë¥³¡¼¥É¤â¤¢¤ë¡£
451 .\"O to reflect the amount of time not slept; most other implementations
453 .\"O (POSIX.1-2001 permits either behavior.)
454 .\"O This causes problems both when Linux code which reads
456 .\"O is ported to other operating systems, and when code is ported to Linux
457 .\"O that reuses a \fIstruct timeval\fP for multiple
459 .\"O in a loop without reinitializing it.
462 .\"O to be undefined after
469 ¤òÊѹ¹¤·¡¢»Ä¤ê¤ÎÄä»ß»þ´Ö¤òÈ¿±Ç¤¹¤ë¤è¤¦¤Ë¤Ê¤Ã¤Æ¤¤¤ë¤¬¡¢
470 ¾¤Î¤Û¤È¤ó¤É¤Î¼ÂÁõ¤Ç¤Ï¤³¤Î¤è¤¦¤Ë¤Ê¤Ã¤Æ¤¤¤Ê¤¤
471 (POSIX.1-2001 ¤Ï¤É¤Á¤é¤ÎÆ°ºî¤âǧ¤á¤Æ¤¤¤ë)¡£
474 ¤ò»²¾È¤·¤Æ¤¤¤ë Linux ¤Î¥³¡¼¥É¤ò¾¤Î¥ª¥Ú¥ì¡¼¥Æ¥£¥ó¥°¡¦¥·¥¹¥Æ¥à¤Ø
475 °Ü¿¢¤¹¤ë¾ì¹ç¡¢ÌäÂ꤬µ¯¤³¤ë¡£
476 ¤Þ¤¿¡¢¥ë¡¼¥×¤ÎÃæ¤Ç \fItimeval\fP ¹½Â¤ÂΤò½é´ü²½¤»¤º¤Ë¤½¤Î¤Þ¤ÞºÆÍøÍѤ·¤Æ
478 ¤òÊ£¿ô²ó¹Ô¤Ê¤Ã¤Æ¤¤¤ë¥³¡¼¥É¤ò Linux ¤Ø°Ü¿¢¤¹¤ë¾ì¹ç¤Ë¤â¡¢ÌäÂ꤬µ¯¤³¤ë¡£
482 ¤Ï̤ÄêµÁ¤Ç¤¢¤ë¤È¹Í¤¨¤ë¤Ù¤¤Ç¤¢¤ë¡£
483 .\"O .\" .PP - it is rumored that:
484 .\"O .\" On BSD, when a timeout occurs, the file descriptor bits are not changed.
485 .\"O .\" - it is certainly true that:
486 .\"O .\" Linux follows SUSv2 and sets the bit masks to zero upon a timeout.
488 .\" ¡ÖBSD ¤Ç¤Ï¥¿¥¤¥à¥¢¥¦¥È¤¬µ¯¤³¤Ã¤Æ¤â¡¢
489 .\" ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¥Ó¥Ã¥È¤ÏÊѤï¤é¤Ê¤¤¡×¤Èʹ¤¤¤¿¤³¤È¤¬¤¢¤ë¡£
490 .\" ¡ÖLinux ¤Ï SUSv2 ¤Ë½¾¤Ã¤Æ¤ª¤ê¡¢
491 .\" ¥¿¥¤¥à¥¢¥¦¥È¤ÎºÝ¤Ë¤Ï¥Ó¥Ã¥È¥Þ¥¹¥¯¤ò 0 ¤Ë¤¹¤ë¡×¤È¤¤¤¦¤Î¤Ï³Î¤«¤Ë»ö¼Â¤Ç¤¢¤ë¡£
492 .\"O .SH "RETURN VALUE"
498 .\"O return the number of file descriptors contained in the three returned
499 .\"O descriptor sets (that is, the total number of bits that are set in
503 .\"O which may be zero if the timeout expires before anything interesting happens.
508 ¤Ï¹¹¿·¤µ¤ì¤¿ 3 ¤Ä¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤Ë´Þ¤Þ¤ì¤Æ¤¤¤ë
509 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¿ô (¤Ä¤Þ¤ê¡¢
513 Ãæ¤Î 1 ¤Ë¤Ê¤Ã¤Æ¤¤¤ë¥Ó¥Ã¥È¤ÎÁí¿ô) ¤òÊÖ¤¹¡£
514 ²¿¤âµ¯¤³¤é¤º¤Ë»þ´ÖÀÚ¤ì¤Ë¤Ê¤Ã¤¿¾ì¹ç¡¢
515 ¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¿ô¤Ï 0 ¤Ë¤Ê¤ë¤³¤È¤â¤¢¤ë¡£
516 .\"O On error, \-1 is returned, and
518 .\"O is set appropriately; the sets and
520 .\"O become undefined, so do not
521 .\"O rely on their contents after an error.
522 ¥¨¥é¡¼¤Ê¤é¤Ð \-1 ¤òÊÖ¤·¡¢
524 ¤ËŬÀÚ¤ÊÃͤ¬ÀßÄꤵ¤ì¤ë; ½¸¹ç¤È
526 ¤Ï̤ÄêµÁ¤È¤Ê¤ë¤Î¤Ç¡¢¥¨¥é¡¼¤¬µ¯¤³¤Ã¤¿¸å¤Ï¤½¤ì¤é¤ÎÆâÍƤò¿®Íꤷ¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£
531 .\"O An invalid file descriptor was given in one of the sets.
532 .\"O (Perhaps a file descriptor that was already closed,
533 .\"O or one on which an error has occurred.)
534 ¤¤¤º¤ì¤«¤Î½¸¹ç¤Ë̵¸ú¤Ê¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬»ØÄꤵ¤ì¤¿
535 (¤ª¤½¤é¤¯¤Ï¡¢¤¹¤Ç¤Ë¥¯¥í¡¼¥º¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤«¡¢
536 ¥¨¥é¡¼¤¬È¯À¸¤·¤¿¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬»ØÄꤵ¤ì¤¿)¡£
539 .\"O A signal was caught; see
545 .\"O is negative or the value contained within
551 ¤ËÆþ¤Ã¤Æ¤¤¤ëÃͤ¬ÉÔÀµ¤Ç¤¢¤ë¡£
554 .\"O unable to allocate memory for internal tables.
555 ÆâÉô¥Æ¡¼¥Ö¥ë¤Ë¥á¥â¥ê¤ò³ä¤êÅö¤Æ¤ë¤³¤È¤¬¤Ç¤¤Ê¤«¤Ã¤¿¡£
559 .\"O was added to Linux in kernel 2.6.16.
561 ¤Ï¥«¡¼¥Í¥ë 2.6.16 ¤Ç Linux ¤ËÄɲ䵤줿¡£
564 .\"O was emulated in glibc (but see BUGS).
567 ¤Ï glibc ¤Ç¥¨¥ß¥å¥ì¡¼¥È¤µ¤ì¤Æ¤¤¤¿ (¡Ö¥Ð¥°¡×¤Î¾Ï¤ò»²¾È)¡£
568 .\"O .SH "CONFORMING TO"
571 .\"O conforms to POSIX.1-2001 and
574 .\"O first appeared in 4.2BSD).
575 .\"O Generally portable to/from
576 .\"O non-BSD systems supporting clones of the BSD socket layer (including
577 .\"O System V variants).
578 .\"O However, note that the System V variant typically
579 .\"O sets the timeout variable before exit, but the BSD variant does not.
581 ¤Ï POSIX.1-2001 ¤È 4.4BSD
583 ¤Ï 4.2BSD ¤ÇºÇ½é¤ËÅо줷¤¿) ¤Ë½àµò¤¹¤ë¡£
584 BSD ¥½¥±¥Ã¥ÈÁؤΥ¯¥í¡¼¥ó¤ò¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤ëÈó BSD ¥·¥¹¥Æ¥à
585 (System V ·Ï¤â´Þ¤à) ¤È¤Î´Ö¤Ç¤À¤¤¤¿¤¤°Ü¿¢À¤¬¤¢¤ë¡£¤·¤«¤· System V ·Ï¤Ç¤Ï
586 ¤¿¤¤¤¬¤¤ timeout ÊÑ¿ô¤ò exit ¤ÎÁ°¤Ë¥»¥Ã¥È¤¹¤ë¤¬¡¢
587 BSD ·Ï¤Ç¤Ï¤½¤¦¤Ç¤Ê¤¤¤Î¤ÇÃí°Õ¤¹¤ë¤³¤È¡£
590 .\"O is defined in POSIX.1g, and in
593 ¤Ï POSIX.1g ¤È POSIX.1-2001 ¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£
598 .\"O is a fixed size buffer.
605 .\"O that is negative or is equal to or larger than
608 .\"O in undefined behavior.
609 .\"O Moreover, POSIX requires
611 .\"O to be a valid file descriptor.
613 ¤Ï¸ÇÄꥵ¥¤¥º¤Î¥Ð¥Ã¥Õ¥¡¤Ç¤¢¤ë¡£
623 ¤É¤Î¤è¤¦¤ÊÆ°ºî¤ò¤¹¤ë¤«¤ÏÄêµÁ¤µ¤ì¤Æ¤¤¤Ê¤¤¡£
626 ¤Ï͸ú¤Ê¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¤Èµ¬Äꤵ¤ì¤Æ¤¤¤ë¡£
628 .\"O Concerning the types involved, the classical situation is that
629 .\"O the two fields of a
631 .\"O structure are typed as
633 .\"O (as shown above), and the structure is defined in
634 .\"O .IR <sys/time.h> .
635 ·¿Àë¸À¤Ë´Ø¤·¤Æ¤Ï¡¢ÀΤʤ¬¤é¤Î¾õ¶·¤Ç¤Ï
637 ¹½Â¤ÂΤΠ2 ¤Ä¤Î¥Õ¥£¡¼¥ë¥É¤Ï
638 (¾åµ¤Î¤è¤¦¤Ë) ξÊý¤È¤â
643 .\"O The POSIX.1-2001 situation is
647 .\"O struct timeval {
648 .\"O time_t tv_sec; /* seconds */
649 .\"O suseconds_t tv_usec; /* microseconds */
654 .\"O where the structure is defined in
655 .\"O .I <sys/select.h>
656 .\"O and the data types
661 .\"O .IR <sys/types.h> .
662 POSIX.1-2001 ¤Î²¼¤Ç¤Ï¡¢°Ê²¼¤Î¤è¤¦¤Ë¤Ê¤Ã¤Æ¤¤¤ë¡£
667 time_t tv_sec; /* ÉÃ */
668 suseconds_t tv_usec; /* ¥Þ¥¤¥¯¥íÉà */
675 ¤ÇÄêµÁ¤µ¤ì¤Æ¤ª¤ê¡¢¥Ç¡¼¥¿·¿
683 .\"O Concerning prototypes, the classical situation is that one should
688 ¥×¥í¥È¥¿¥¤¥×¤Ë´Ø¤·¤Æ¤Ï¡¢ÀΤʤ¬¤é¤Î¾õ¶·¤Ç
692 ¤ò¥¤¥ó¥¯¥ë¡¼¥É¤¹¤ì¤Ð¤è¤¤¡£
693 .\"O The POSIX.1-2001 situation is that one should include
694 .\"O .I <sys/select.h>
699 POSIX.1-2001 ¤Î´Ä¶¤Ç
705 ¤ò¥¤¥ó¥¯¥ë¡¼¥É¤¹¤ì¤Ð¤è¤¤¡£
707 .\"O Libc4 and libc5 do not have a
708 .\"O .I <sys/select.h>
709 .\"O header; under glibc 2.0 and later this header exists.
712 ¤Ï libc4 ¤È libc5 ¤Ë¤Ï¤Ê¤¯¡¢glibc 2.0 °Ê¹ß¤Ë¸ºß¤¹¤ë¡£
713 .\"O Under glibc 2.0 it unconditionally gives the wrong prototype for
715 .\"O Under glibc 2.1 to 2.2.1 it gives
720 .\"O Since glibc 2.2.2 the requirements are as shown in the SYNOPSIS.
721 °¤¤¤³¤È¤Ë glibc 2.0 °ÊÁ°¤Ç¤Ï
723 ¤Î¥×¥í¥È¥¿¥¤¥×¤¬´Ö°ã¤Ã¤Æ¤¤¤ë¡£
724 glibc 2.1 ¤«¤é 2.2.1 ¤Ç¤Ï
726 ¤¬ÄêµÁ¤µ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ë¡¢
729 glibc 2.2.2 °Ê¹ß¤Ç¤Ï¡¢
731 ¤ò»ÈÍѤ¹¤ë¤Ë¤Ï¡¢¡Ö½ñ¼°¡×¤ËµºÜ¤µ¤ì¤¿Í×·ï¤òËþ¤¿¤¹É¬Íפ¬¤¢¤ë¡£
732 .\"O .SS "LINUX NOTES"
736 .\"O system call modifies its
744 .\"O However, the glibc wrapper function hides this behavior
745 .\"O by using a local variable for the timeout argument that
746 .\"O is passed to the system call.
749 .\"O function does not modify its timeout argument;
750 .\"O this is the behavior required by POSIX.1-2001.
751 ¤·¤«¤·¡¢ glibc ¤Î¥é¥Ã¥Ñ¡¼´Ø¿ô¤Ï¡¢¥·¥¹¥Æ¥à¥³¡¼¥ë¤ËÅϤ¹ timeout °ú¤¿ô
752 ¤È¤·¤Æ¥í¡¼¥«¥ëÊÑ¿ô¤ò»È¤¦¤³¤È¤Ç¤³¤ÎÆ°ºî¤ò±£Ê䷤Ƥ¤¤ë¡£
755 ´Ø¿ô¤Ï timeout °ú¤¿ô¤òÊѹ¹¤·¤Ê¤¤¡£
756 ¤³¤ì¤¬ POSIX.1-2001 ¤¬Í׵ᤷ¤Æ¤¤¤ëÆ°ºî¤Ç¤¢¤ë¡£
759 .\"O Glibc 2.0 provided a version of
761 .\"O that did not take a
766 °ú¤¿ô¤ò¼è¤é¤Ê¤¤¥Ð¡¼¥¸¥ç¥ó¤Î
770 .\"O Since version 2.1, glibc has provided an emulation of
772 .\"O that is implemented using
773 .\"O .BR sigprocmask (2)
776 .\"O This implementation remains vulnerable to the very race condition that
778 .\"O was designed to prevent.
779 ¥Ð¡¼¥¸¥ç¥ó 2.1 °Ê¹ß¤Î glibc ¤Ç¤Ï¡¢
785 ¤ò»È¤Ã¤Æ¥¨¥ß¥å¥ì¡¼¥È¤µ¤ì¤Æ¤¤¤¿¡£
786 ¤³¤Î¼ÂÁõ¤Ë¤Ï¤¤ï¤É¤¤¶¥¹ç¾ò·ï¤Ë¤ª¤¤¤ÆÀȼåÀ¤¬»Ä¤Ã¤Æ¤¤¤ë¡£
787 ¤³¤Î¶¥¹ç¾ò·ï¤Ë¤ª¤±¤ëÌäÂê¤òËɻߤ¹¤ë¤¿¤á¤Ë
789 ¤ÏÀ߷פµ¤ì¤¿¤Î¤Ç¤¢¤ë¡£
790 .\"O On systems that lack
792 .\"O reliable (and more portable) signal trapping can be achieved
793 .\"O using the self-pipe trick
794 .\"O (where a signal handler writes a byte to a pipe whose other end
797 .\"O in the main program.)
799 ¤¬¤Ê¤¤¥·¥¹¥Æ¥à¤Ë¤ª¤¤¤Æ¡¢
800 ¥·¥°¥Ê¥ë¤ÎÊ᪤ò¿®ÍêÀ¤¬¤¢¤ê (°Ü¿¢À¤â¹â¤¤) ÊýË¡¤Ç¹Ô¤¦¤Ë¤Ï¡¢
801 ¼«¸Ê¥Ñ¥¤¥× (self-pipe) ¤È¤¤¤¦µ»¤ò»È¤¦¤È¤è¤¤ (¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ï¥Ñ¥¤¥×¤Ø
802 1 ¥Ð¥¤¥È¤Î¥Ç¡¼¥¿¤ò½ñ¤¹þ¤ß¡¢Æ±¤¸¥Ñ¥¤¥×¤Î¤â¤¦°ìü¤ò¥á¥¤¥ó¥×¥í¥°¥é¥à¤Î
804 ¤Ç´Æ»ë¤¹¤ë¤È¤¤¤¦ÊýË¡¤Ç¤¢¤ë)¡£
808 .\"O may report a socket file descriptor as "ready for reading", while
809 .\"O nevertheless a subsequent read blocks.
810 .\"O This could for example
811 .\"O happen when data has arrived but upon examination has wrong
812 .\"O checksum and is discarded.
813 .\"O There may be other circumstances
814 .\"O in which a file descriptor is spuriously reported as ready.
815 .\"O .\" Stevens discusses a case where accept can block after select
816 .\"O .\" returns successfully because of an intervening RST from the client.
817 .\"O Thus it may be safer to use
819 .\"O on sockets that should not block.
822 ¤¬¥½¥±¥Ã¥È¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ç "Æɤ߹þ¤ß¤Î½àÈ÷¤¬¤Ç¤¤¿" ¤ÈÊó¹ð¤·¤¿¾ì¹ç¤Ç¤â¡¢
823 ¤³¤Î¸å¤Ç read ¤ò¹Ô¤¦¤ÈÄä»ß (block) ¤¹¤ë¤³¤È¤¬¤¢¤ë¡£¤³¤Î¤è¤¦¤Ê¾õ¶·¤Ï¡¢
824 Î㤨¤Ð¡¢¥Ç¡¼¥¿¤¬ÅþÃ夷¤¿¤¬¡¢¸¡ºº¤Ç¥Á¥§¥Ã¥¯¥µ¥à°Û¾ï¤¬¸«¤Ä¤«¤êÇÑ´þ¤µ¤ì¤¿»þ
825 ¤Ê¤É¤Ëµ¯¤³¤ê¤¨¤ë¡£Â¾¤Ë¤â¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬½àÈ÷¤Ç¤¤¿¤È´Ö°ã¤Ã¤Æ
826 Êó¹ð¤µ¤ì¤ë¾õ¶·¤¬µ¯¤³¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
827 .\" Stevens ¤¬µó¤²¤¿¤Î¤Ï¡¢select ¤¬À®¸ù¤·¤ÆÉüµ¢¤·¤Æ¤«¤é accept ¤¬
828 .\" ¸Æ¤Ð¤ì¤ë¤Þ¤Ç¤Î´Ö¤Ë¡¢¥¯¥é¥¤¥¢¥ó¥È¤«¤é RST ¤¬Á÷¤é¤ì¤ë¤È¡¢
829 .\" accept ¤¬Ää»ß¤¹¤ë¾ì¹ç¤Ç¤¢¤ë¡£
830 ¤·¤¿¤¬¤Ã¤Æ¡¢Ää»ß¤¹¤Ù¤¤Ç¤Ï¤Ê¤¤¥½¥±¥Ã¥È¤ËÂФ·¤Æ¤Ï
832 ¤ò»È¤¦¤È¤è¤ê°ÂÁ´¤Ç¤¢¤í¤¦¡£
833 .\"O .\" Maybe the kernel should have returned EIO in such a situation?
834 .\" ¿ʬ¡¢¤³¤Î¤è¤¦¤Ê¾õ¶·¤Ç¤Ï¥«¡¼¥Í¥ë¤Ï EIO ¤òÊÖ¤·¤Æ¤·¤Þ¤¦?
840 .\"O if the call is interrupted by a signal handler (i.e., the
843 .\"O This is not permitted by POSIX.1-2001.
846 .\"O system call has the same behavior,
847 .\"O but the glibc wrapper hides this behavior by internally copying the
849 .\"O to a local variable and passing that variable to the system call.
852 ¤¬¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ë¤è¤ê³ä¤ê¹þ¤Þ¤ì¤¿¾ì¹ç (¤Ä¤Þ¤ê
857 ¤³¤ì¤Ï POSIX.1-2001 ¤Ç¤Ïǧ¤á¤é¤ì¤Æ¤¤¤Ê¤¤µóÆ°¤Ç¤¢¤ë¡£
860 ¥·¥¹¥Æ¥à¥³¡¼¥ë¤âƱ¤¸µóÆ°¤ò¤¹¤ë¤¬¡¢
861 glibc ¤Î¥é¥Ã¥Ñ¡¼´Ø¿ô¤¬¤³¤ÎµóÆ°¤ò±£Ê䷤Ƥ¤¤ë¡£
862 ¶ñÂÎŪ¤Ë¤Ï¡¢glibc ¤Î¥é¥Ã¥Ñ¡¼´Ø¿ô¤ÎÆâÉô¤Ç¡¢
864 ¤ò¥í¡¼¥«¥ëÊÑ¿ô¤Ë¥³¥Ô¡¼¤·¡¢
865 ¤³¤Î¥í¡¼¥«¥ëÊÑ¿ô¤ò¥·¥¹¥Æ¥à¥³¡¼¥ë¤ËÅϤ·¤Æ¤¤¤ë¡£
871 #include <sys/time.h>
872 #include <sys/types.h>
879 .\"O struct timeval tv;
882 .\"O /* Watch stdin (fd 0) to see when it has input. */
884 .\"O FD_SET(0, &rfds);
886 .\"O /* Wait up to five seconds. */
890 .\"O retval = select(1, &rfds, NULL, NULL, &tv);
891 .\"O /* Don't rely on the value of tv now! */
893 .\"O if (retval == \-1)
894 .\"O perror("select()");
895 .\"O else if (retval)
896 .\"O printf("Data is available now.\\n");
897 .\"O /* FD_ISSET(0, &rfds) will be true. */
899 .\"O printf("No data within five seconds.\\n");
901 .\"O exit(EXIT_SUCCESS);
910 /* stdin (fd 0) ¤ò´Æ»ë¤·¡¢ÆþÎϤ¬¤¢¤Ã¤¿¾ì¹ç¤Ëɽ¼¨¤¹¤ë¡£*/
914 /* 5 Éôִƻ뤹¤ë¡£*/
918 retval = select(1, &rfds, NULL, NULL, &tv);
919 /* ¤³¤Î»þÅÀ¤Ç¤Î tv ¤ÎÃͤò¿®Íꤷ¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£*/
924 printf("º£¡¢¥Ç¡¼¥¿¤¬¼èÆÀ¤Ç¤¤Þ¤·¤¿¡£\\n");
925 /* FD_ISSET(0, &rfds) ¤¬ true ¤Ë¤Ê¤ë¡£*/
927 printf("5 ÉðÊÆâ¤Ë¥Ç¡¼¥¿¤¬ÆþÎϤµ¤ì¤Þ¤»¤ó¤Ç¤·¤¿¡£\\n");
934 .\"O For a tutorial with discussion and examples, see
935 .\"O .BR select_tut (2).
936 ¹Í»¡¤È»ÈÍÑÎã¤Î½ñ¤«¤ì¤¿¥Á¥å¡¼¥È¥ê¥¢¥ë¤È¤·¤Æ¡¢
940 .\"O For vaguely related stuff, see
941 ´Ø·¸¤¬¤¢¤ê¤½¤¦¤Ê¤â¤Î¤òµó¤²¤Æ¤ª¤¯: