OSDN Git Service

49ed827934bf81312464b3314023f1d915ff7c76
[linuxjm/LDP_man-pages.git] / draft / man2 / select.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" This manpage is copyright (C) 1992 Drew Eckhardt,
4 .\"                 copyright (C) 1995 Michael Shields.
5 .\"
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.
9 .\"
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.
14 .\"
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
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\"
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
30 .\"
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
36 .\"     call in 2.6.16.
37 .\"
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
51 .\"
52 .\"WORD:        synchronous             Æ±´ü¤µ¤»¤ë
53 .\"WORD:        multiplexing            Â¿½Å²½
54 .\"WORD:        file descriptor         ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿
55 .\"WORD:        descriptor              µ­½Ò»Ò
56 .\"WORD:        macro                   ¥Þ¥¯¥í
57 .\"WORD:        block                   Ää»ß(block)
58 .\"WORD:        block                   ¶Ø»ß(block)
59 .\"WORD:        signal                  ¥·¥°¥Ê¥ë
60 .\"WORD:        timeout                 »þ´ÖÀÚ¤ì(timeout)
61 .\"WORD:        portable                °Ü¿¢À­¤Î¤¢¤ë
62 .\"
63 .TH SELECT 2 2008-12-05 "Linux" "Linux Programmer's Manual"
64 .\"O .SH NAME
65 .SH Ì¾Á°
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 ¤ÎƱ´ü¤ò¤È¤ë
69 .\"O .SH SYNOPSIS
70 .SH ½ñ¼°
71 .nf
72 .\"O /* According to POSIX.1-2001 */
73 /* POSIX.1-2001 ¤Ë½¾¤¦¾ì¹ç */
74 .br
75 .B #include <sys/select.h>
76 .sp
77 .\"O /* According to earlier standards */
78 /* °ÊÁ°¤Îµ¬³Ê¤Ë½¾¤¦¾ì¹ç */
79 .br
80 .B #include <sys/time.h>
81 .br
82 .B #include <sys/types.h>
83 .br
84 .B #include <unistd.h>
85 .sp
86 .BI "int select(int " nfds ", fd_set *" readfds ", fd_set *" writefds ,
87 .BI "           fd_set *" exceptfds ", struct timeval *" timeout );
88 .sp
89 .BI "void FD_CLR(int " fd ", fd_set *" set );
90 .br
91 .BI "int  FD_ISSET(int " fd ", fd_set *" set );
92 .br
93 .BI "void FD_SET(int " fd ", fd_set *" set );
94 .br
95 .BI "void FD_ZERO(fd_set *" set );
96 .sp
97 .B #include <sys/select.h>
98 .sp
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 );
102 .fi
103 .sp
104 .in -4n
105 .\"O Feature Test Macro Requirements for glibc (see
106 .\"O .BR feature_test_macros (7)):
107 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
108 .RB ( feature_test_macros (7)
109 »²¾È):
110 .in
111 .sp
112 .BR pselect ():
113 _POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600
114 .\"O .SH DESCRIPTION
115 .SH ÀâÌÀ
116 .\"O .BR select ()
117 .\"O and
118 .\"O .BR pselect ()
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.,
124 .\"O .BR read (2))
125 .\"O without blocking.
126 .BR select ()
127 ¤ä
128 .BR pselect ()
129 ¤ò»È¤¦¤È¡¢¥×¥í¥°¥é¥à¤ÇÊ£¿ô¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò´Æ»ë¤·¡¢
130 °ì¤Ä°Ê¾å¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬¤¢¤ë¼ï¤Î I/O Áàºî¤Î
131 ¡Öready (½àÈ÷¤¬¤Ç¤­¤¿)¡×¾õÂÖ (Î㤨¤Ð¡¢Æɤ߹þ¤ß²Äǽ¤Ë¤Ê¤Ã¤¿¾õÂÖ)
132 ¤Ë¤Ê¤ë¤Þ¤ÇÂԤĤ³¤È¤¬¤Ç¤­¤ë¡£
133 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬ ready (½àÈ÷¤¬¤Ç¤­¤¿) ¤È¤Ï¡¢
134 Âбþ¤¹¤ë I/O Áàºî (Î㤨¤Ð
135 .BR read (2)
136 ¤Ê¤É) ¤¬Ää»ß (block) ¤Ê¤·¤Ë¼Â¹Ô²Äǽ¤Ê¾õÂ֤ˤ¢¤ë¤³¤È¤ò°ÕÌ£¤¹¤ë¡£
137 .PP
138 .\"O The operation of
139 .\"O .BR select ()
140 .\"O and
141 .\"O .BR pselect ()
142 .\"O is identical, with three differences:
143 .BR select ()
144 ¤È
145 .BR pselect ()
146 ¤ÎÆ°ºî¤ÏƱ¤¸¤Ç¤¢¤ë¤¬¡¢°Ê²¼¤Î 3 ÅÀ¤¬°Û¤Ê¤ë:
147 .TP
148 (i)
149 .\"O .BR select ()
150 .\"O uses a timeout that is a
151 .\"O .I struct timeval
152 .\"O (with seconds and microseconds), while
153 .\"O .BR pselect ()
154 .\"O uses a
155 .\"O .I struct timespec
156 .\"O (with seconds and nanoseconds).
157 .BR select ()
158 ¤Ç¤Ï¡¢¥¿¥¤¥à¥¢¥¦¥È»þ´Ö¤Î»ØÄê¤Ë¹½Â¤ÂÎ
159 .I struct timeval
160 (Éᦥޥ¤¥¯¥íÉÃñ°Ì) ¤òÍѤ¤¤ë¡£
161 °ìÊý¡¢
162 .BR pselect ()
163 ´Ø¿ô¤Ç¤Ï¡¢¹½Â¤ÂÎ
164 .I struct timespec
165 (ÉᦥʥÎÉÃñ°Ì) ¤òÍѤ¤¤ë¡£
166 .TP
167 (ii)
168 .\"O .BR select ()
169 .\"O may update the
170 .\"O .I timeout
171 .\"O argument to indicate how much time was left.
172 .\"O .BR pselect ()
173 .\"O does not change this argument.
174 .BR select ()
175 ¤Ï»Ä¤ê»þ´Ö¤ò¼¨¤¹
176 .I timeout
177 °ú¤­¿ô¤ò¹¹¿·¤¹¤ë¤³¤È¤¬¤¢¤ë¡£
178 .BR pselect ()
179 ¤Ï¤³¤Î°ú¤­¿ô¤òÊѹ¹¤·¤Ê¤¤¡£
180 .TP
181 (iii)
182 .\"O The
183 .\"O .BR select ()
184 .\"O has no
185 .\"O .I sigmask
186 .\"O argument, and behaves as
187 .\"O .BR pselect ()
188 .\"O called with NULL
189 .\"O .IR sigmask .
190 .BR select ()
191 ¤Ï
192 .I sigmask
193 °ú¤­¿ô¤ò»ý¤¿¤Ê¤¤¡£¤½¤ÎÆ°ºî¤Ï
194 .I sigmask
195 ¤Ë NULL ¤ò»ØÄꤷ¤¿¾ì¹ç¤Î
196 .BR pselect ()
197 ¤ÈƱ¤¸¤Ç¤¢¤ë¡£
198 .PP
199 .\"O Three independent sets of file descriptors are watched.
200 .\"O Those listed in
201 .\"O .I readfds
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),
205 .\"O those in
206 .\"O .I writefds
207 .\"O will be watched to see if a write will not block, and
208 .\"O those in
209 .\"O .I exceptfds
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
215 .\"O of events.
216 3 ¤Ä¤ÎÆÈΩ¤·¤¿¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤Î´Æ»ë¤ò¹Ô¤¦¡£
217 .I readfds
218 ¤ËÆþ¤ì¤é¤ì¤¿¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ë¤Ä¤¤¤Æ¤Ï¡¢Æɤ߹þ¤ß¤¬²Äǽ¤«¤É¤¦¤«¤ò
219 ´Æ»ë¤¹¤ë (¤è¤êÀµ³Î¤Ë¤¤¤¦¤È¡¢Ää»ß (block) ¤Ê¤·¤ÇÆɤळ¤È¤¬¤Ç¤­¤ë¤«¤ò
220 Ä´¤Ù¤ë¡£¥Õ¥¡¥¤¥ë¤Î½ªÃ¼ (end-of-file) ¤Î¾ì¹ç¤â¡¢
221 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÏÆɤ߹þ¤ß²Äǽ¤È¤·¤Æ°·¤ï¤ì¤ë)¡£
222 .I writefds
223 ¤ËÆþ¤ì¤é¤ì¤¿¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ë¤Ä¤¤¤Æ¤Ï¡¢Ää»ß¤»¤º¤Ë½ñ¤­¹þ¤ß¤¬
224 ²Äǽ¤«¤É¤¦¤«¤ò´Æ»ë¤¹¤ë¡£
225 .I exceptfds
226 ¤Ë¤¢¤ë¤â¤Î¤Ë¤Ä¤¤¤Æ¤Ï¡¢Îã³°¤Î´Æ»ë¤ò¹Ô¤Ê¤¦¡£¥·¥¹¥Æ¥à¥³¡¼¥ë½ªÎ»»þ¤Ë¡¢
227 ¤É¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¾õÂÖ¤¬¼ÂºÝ¤ËÊѲ½¤·¤¿¤«¼¨¤¹¤¿¤á¤Ë¡¢
228 ½¸¹ç¤ÎÆâÍƤ¬Êѹ¹¤µ¤ì¤ë¡£
229 ¤¢¤ë¼ïÊ̤Υ¤¥Ù¥ó¥È¤ò´Æ»ë¤·¤¿¤¤¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬°ì¤Ä¤â¤Ê¤¤¾ì¹ç¤Ë¤Ï¡¢
230 Âбþ¤¹¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤Ë NULL ¤ò»ØÄꤹ¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
231 .PP
232 .\"O Four macros are provided to manipulate the sets.
233 ½¸¹ç¤òÁàºî¤¹¤ë¤¿¤á¤Ë 4 ¤Ä¤Î¥Þ¥¯¥í¤¬Ä󶡤µ¤ì¤Æ¤¤¤ë¡£
234 .\"O .BR FD_ZERO ()
235 .\"O clears a set.
236 .BR FD_ZERO ()
237 ¤Ï½¸¹ç¤ò¾Ãµî¤¹¤ë¡£
238 .\"O .BR FD_SET ()
239 .\"O and
240 .\"O .BR FD_CLR ()
241 .\"O respectively add and remove a given file descriptor from a set.
242 .BR FD_SET ()
243 ¤È
244 .BR FD_CLR ()
245 ¤Ï¤½¤ì¤¾¤ì»ØÄꤷ¤¿¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î½¸¹ç¤Ø¤ÎÄɲᢺï½ü¤ò¹Ô¤¦¡£
246 .\"O .BR FD_ISSET ()
247 .\"O tests to see if a file descriptor is part of the set;
248 .\"O this is useful after
249 .\"O .BR select ()
250 .\"O returns.
251 .BR FD_ISSET ()
252 ¤Ï½¸¹ç¤Ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬¤¢¤ë¤«¤É¤¦¤«Ä´¤Ù¤ë; 
253 ¤³¤Î¥Þ¥¯¥í¤Ï
254 .BR select ()
255 ¤¬½ªÎ»¤·¤¿¸å¤Ë»È¤¦¤ÈÊØÍø¤Ç¤¢¤ë¡£
256 .PP
257 .\"O .I nfds
258 .\"O is the highest-numbered file descriptor in any of the three sets, plus 1.
259 .I nfds
260 ¤Ï 3 ¤Ä¤Î½¸¹ç¤Ë´Þ¤Þ¤ì¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÎºÇÂçÃͤˠ1 ¤ò­¤·¤¿¤â¤Î¤Ç¤¢¤ë¡£
261 .PP
262 .\"O .I timeout
263 .\"O is an upper bound on the amount of time elapsed before
264 .\"O .BR select ()
265 .\"O returns.
266 .\"O If both fields of the
267 .\"O .I timeval
268 .\"O stucture are zero, then
269 .\"O .BR select ()
270 .\"O returns immediately.
271 .\"O (This is useful for polling.)
272 .\"O If
273 .\"O .I timeout
274 .\"O is NULL (no timeout),
275 .\"O .BR select ()
276 .\"O can block indefinitely.
277 .I timeout
278 ¤Ï
279 .BR select ()
280 ¤¬Éüµ¢¤¹¤ë¤Þ¤Ç¤Î·Ð²á»þ´Ö¤Î¾å¸Â¤Ç¤¢¤ë¡£
281 .I timeval
282 ¹½Â¤ÂΤÎξÊý¤Î¥Õ¥£¡¼¥ë¥É¤¬ 0 ¤Î¾ì¹ç¡¢
283 .BR select ()
284 ¤Ï¤¹¤°¤ËÉüµ¢¤¹¤ë
285 (¤³¤Îµ¡Ç½¤Ï¥Ý¡¼¥ê¥ó¥° (polling) ¤ò¹Ô¤¦¤Î¤ËÊØÍø¤Ç¤¢¤ë)¡£
286 .I timeout
287 ¤Ë NULL (¥¿¥¤¥à¥¢¥¦¥È¤Ê¤·)
288 ¤¬»ØÄꤵ¤ì¤ë¤È¡¢
289 .BR select ()
290 ¤Ï̵´ü¸Â¤ËÄä»ß (block) ¤¹¤ë¡£
291 .PP
292 .\"O .I sigmask
293 .\"O is a pointer to a signal mask (see
294 .\"O .BR sigprocmask (2));
295 .\"O if it is not NULL, then
296 .\"O .BR pselect ()
297 .\"O first replaces the current signal mask by the one pointed to by
298 .\"O .IR sigmask ,
299 .\"O then does the "select" function, and then restores the original
300 .\"O signal mask.
301 .I sigmask
302 ¤Ï¡¢¥·¥°¥Ê¥ë¥Þ¥¹¥¯
303 .RB ( sigprocmask (2)
304 ¤ò»²¾È) ¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
305 .I sigmask
306 ¤¬ NULL ¤Ç¤Ê¤¤¾ì¹ç¡¢
307 .BR pselect ()
308 ¤Ï
309 .I sigmask
310 ¤¬»Ø¤·¤Æ¤¤¤ë¥·¥°¥Ê¥ë¥Þ¥¹¥¯¤Ç¸½ºß¤Î¥·¥°¥Ê¥ë¥Þ¥¹¥¯¤òÃÖ¤­´¹¤¨¤Æ¤«¤é¡¢
311 "select" ´Ø¿ô¤ò¼Â¹Ô¤·¡¢
312 ½ªÎ»¸å¤Ë¥·¥°¥Ê¥ë¥Þ¥¹¥¯¤ò¸µ¤Î¥·¥°¥Ê¥ë¥Þ¥¹¥¯¤ËÌ᤹¡£
313 .PP
314 .\"O Other than the difference in the precision of the
315 .\"O .I timeout
316 .\"O argument, the following
317 .\"O .BR pselect ()
318 .\"O call:
319 .I timeout
320 °ú¤­¿ô¤ÎÀºÅ٤ΰ㤤¤ò½ü¤¯¤È¡¢°Ê²¼¤Î
321 .BR pselect ()
322 ¤Î¸Æ¤Ó½Ð¤·¤Ï¡¢
323 .nf
324
325     ready = pselect(nfds, &readfds, &writefds, &exceptfds,
326                     timeout, &sigmask);
327
328 .fi
329 .\"O is equivalent to
330 .\"O .I atomically
331 .\"O executing the following calls:
332 ¼¡¤Î¥³¡¼¥ë¤ò
333 .I atomic
334 ¤Ë¼Â¹Ô¤¹¤ë¤Î¤ÈÅù²Á¤Ç¤¢¤ë¡£
335 .nf
336
337     sigset_t origmask;
338
339     sigprocmask(SIG_SETMASK, &sigmask, &origmask);
340     ready = select(nfds, &readfds, &writefds, &exceptfds, timeout);
341     sigprocmask(SIG_SETMASK, &origmask, NULL);
342 .fi
343 .PP
344 .\"O The reason that
345 .\"O .BR pselect ()
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
350 .\"O returns.
351 .\"O Then a test of this global flag followed by a call of
352 .\"O .BR select ()
353 .\"O could hang indefinitely if the signal arrived just after the test
354 .\"O but just before the call.
355 .BR pselect ()
356 ¤¬É¬ÍפˤʤëÍýͳ¤Ï¡¢¥·¥°¥Ê¥ë¤ä¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¾õÂÖÊѲ½¤ò
357 ÂÔ¤Á¤¿¤¤¤È¤­¤Ë¤Ï¡¢¶¥¹ç¾õÂÖ¤òÈò¤±¤ë¤¿¤á¤Ë atomic ¤Ê¥Æ¥¹¥È¤¬É¬Íפˤʤë
358 ¤«¤é¤Ç¤¢¤ë¡£
359 (¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤¬Âç°è¥Õ¥é¥°¤òÀßÄꤷ¤ÆÌá¤ë¾ì¹ç¤ò¹Í¤¨¤Æ¤ß¤è¤¦¡£
360 ¤³¤ÎÂç°è¥Õ¥é¥°¤Î¥Æ¥¹¥È¤Ë³¤±¤Æ
361 .BR select ()
362 ¤ò¸Æ¤Ó½Ð¤¹¤È¡¢
363 ¥·¥°¥Ê¥ë¤¬¥Æ¥¹¥È¤Îľ¸å¤«¤Ä¸Æ¤Ó½Ð¤·¤ÎľÁ°¤ËÆϤ¤¤¿»þ¤Ë¤Ï
364 .BR select ()
365 ¤Ï±Êµ×¤Ë¥Ï¥ó¥°¤·¤Æ¤·¤Þ¤¦¤«¤â¤·¤ì¤Ê¤¤¡£
366 .\"O By contrast,
367 .\"O .BR pselect ()
368 .\"O allows one to first block signals, handle the signals that have come in,
369 .\"O then call
370 .\"O .BR pselect ()
371 .\"O with the desired
372 .\"O .IR sigmask ,
373 .\"O avoiding the race.)
374 °ìÊý¡¢
375 .BR pselect ()
376 ¤ò»È¤¦¤È¡¢¤Þ¤º¥·¥°¥Ê¥ë¤ò¶Ø»ß (block) ¤·¤Æ¡¢Æþ¤Ã¤Æ¤¯¤ë¥·¥°¥Ê¥ë¤òÁàºî¤·¡¢
377 ˾¤ß¤Î
378 .I sigmask
379 ¤Ç
380 .BR pselect ()
381 ¤ò¸Æ¤Ó½Ð¤¹¤³¤È¤Ç¡¢Á°µ­¤Î¶¥¹ç¤òÈò¤±¤ë¤³¤È¤¬¤Ç¤­¤ë¡£)
382 .\"O .SS "The timeout"
383 .SS ¥¿¥¤¥à¥¢¥¦¥È
384 .\"O The time structures involved are defined in
385 .\"O .I <sys/time.h>
386 .\"O and look like
387 ¤³¤ì¤é¤Î´Ø¿ô¤Ç»ÈÍѤµ¤ì¤ë»þ´Ö´ØÏ¢¤Î¹½Â¤ÂΤϡ¢
388 .I <sys/time.h>
389 ¤Ç°Ê²¼¤Î¤è¤¦¤ËÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£
390
391 .\"O .in +4n
392 .\"O .nf
393 .\"O struct timeval {
394 .\"O     long    tv_sec;         /* seconds */
395 .\"O     long    tv_usec;        /* microseconds */
396 .\"O };
397 .\"O .fi
398 .\"O .in
399 .in +4n
400 .nf
401 struct timeval {
402     long    tv_sec;         /* Éà*/
403     long    tv_usec;        /* ¥Þ¥¤¥¯¥íÉà*/
404 };
405 .fi
406 .in
407
408 .\"O and
409 .\"O
410 .\"O .in +4n
411 .\"O .nf
412 .\"O struct timespec {
413 .\"O     long    tv_sec;         /* seconds */
414 .\"O     long    tv_nsec;        /* nanoseconds */
415 .\"O };
416 .\"O .fi
417 .\"O .in
418 .in +4n
419 .nf
420 struct timespec {
421     long    tv_sec;         /* Éà*/
422     long    tv_nsec;        /* ¥Ê¥ÎÉà*/
423 };
424 .fi
425 .in
426
427 .\"O (However, see below on the POSIX.1-2001 versions.)
428 (POSIX.1-2001 ¤Ç¤ÎÄêµÁ¤Ë¤Ä¤¤¤Æ¤Ï²¼µ­¤Î¡ÖÃí°Õ¡×¤ò»²¾È)
429 .PP
430 .\"O Some code calls
431 .\"O .BR select ()
432 .\"O with all three sets empty,
433 .\"O .I nfds
434 .\"O zero, and a non-NULL
435 .\"O .I timeout
436 .\"O as a fairly portable way to sleep with subsecond precision.
437 ÉÃñ°Ì°Ê²¼¤ÎÀºÅ٤ǥ¹¥ê¡¼¥×¤ò¼Â¸½¤¹¤ë
438 °Ü¿¢À­¤Î¹â¤¤ÊýË¡¤È¤·¤Æ¡¢
439 3 ¤Ä¤Î½¸¹çÁ´¤Æ¤ò¶õ¡¢
440 .I nfds
441 ¤ò 0 ¡¢
442 .I timeout
443 ¤ò NULL ¤Ç¤Ê¤¤ÃͤËÀßÄꤷ¤Æ
444 .BR select ()
445 ¤ò¸Æ¤Ó½Ð¤¹¤È¤¤¤¦ÊýË¡¤ò»È¤Ã¤Æ¤¤¤ë¥³¡¼¥É¤â¤¢¤ë¡£
446 .PP
447 .\"O On Linux,
448 .\"O .BR select ()
449 .\"O modifies
450 .\"O .I timeout
451 .\"O to reflect the amount of time not slept; most other implementations
452 .\"O do not do this.
453 .\"O (POSIX.1-2001 permits either behavior.)
454 .\"O This causes problems both when Linux code which reads
455 .\"O .I timeout
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
458 .\"O .BR select ()s
459 .\"O in a loop without reinitializing it.
460 .\"O Consider
461 .\"O .I timeout
462 .\"O to be undefined after
463 .\"O .BR select ()
464 .\"O returns.
465 Linux ¤Ç¤Ï¡¢
466 .BR select ()
467 ¤Ï
468 .I timeout
469 ¤òÊѹ¹¤·¡¢»Ä¤ê¤ÎÄä»ß»þ´Ö¤òÈ¿±Ç¤¹¤ë¤è¤¦¤Ë¤Ê¤Ã¤Æ¤¤¤ë¤¬¡¢
470 ¾¤Î¤Û¤È¤ó¤É¤Î¼ÂÁõ¤Ç¤Ï¤³¤Î¤è¤¦¤Ë¤Ê¤Ã¤Æ¤¤¤Ê¤¤
471 (POSIX.1-2001 ¤Ï¤É¤Á¤é¤ÎÆ°ºî¤âǧ¤á¤Æ¤¤¤ë)¡£
472 ¤³¤Î¤¿¤á¡¢
473 .I timeout
474 ¤ò»²¾È¤·¤Æ¤¤¤ë Linux ¤Î¥³¡¼¥É¤ò¾¤Î¥ª¥Ú¥ì¡¼¥Æ¥£¥ó¥°¡¦¥·¥¹¥Æ¥à¤Ø
475 °Ü¿¢¤¹¤ë¾ì¹ç¡¢ÌäÂ꤬µ¯¤³¤ë¡£
476 ¤Þ¤¿¡¢¥ë¡¼¥×¤ÎÃæ¤Ç \fItimeval\fP ¹½Â¤ÂΤò½é´ü²½¤»¤º¤Ë¤½¤Î¤Þ¤ÞºÆÍøÍѤ·¤Æ
477 .BR select ()
478 ¤òÊ£¿ô²ó¹Ô¤Ê¤Ã¤Æ¤¤¤ë¥³¡¼¥É¤ò Linux ¤Ø°Ü¿¢¤¹¤ë¾ì¹ç¤Ë¤â¡¢ÌäÂ꤬µ¯¤³¤ë¡£
479 .BR select ()
480 ¤«¤éÉüµ¢¤·¤¿¸å¤Ï
481 .I timeout
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.
487 .\" .PP
488 .\" ¡ÖBSD ¤Ç¤Ï¥¿¥¤¥à¥¢¥¦¥È¤¬µ¯¤³¤Ã¤Æ¤â¡¢
489 .\" ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¥Ó¥Ã¥È¤ÏÊѤï¤é¤Ê¤¤¡×¤Èʹ¤¤¤¿¤³¤È¤¬¤¢¤ë¡£
490 .\" ¡ÖLinux ¤Ï SUSv2 ¤Ë½¾¤Ã¤Æ¤ª¤ê¡¢
491 .\" ¥¿¥¤¥à¥¢¥¦¥È¤ÎºÝ¤Ë¤Ï¥Ó¥Ã¥È¥Þ¥¹¥¯¤ò 0 ¤Ë¤¹¤ë¡×¤È¤¤¤¦¤Î¤Ï³Î¤«¤Ë»ö¼Â¤Ç¤¢¤ë¡£
492 .\"O .SH "RETURN VALUE"
493 .SH ÊÖ¤êÃÍ
494 .\"O On success,
495 .\"O .BR select ()
496 .\"O and
497 .\"O .BR pselect ()
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
500 .\"O .IR readfds ,
501 .\"O .IR writefds ,
502 .\"O .IR exceptfds )
503 .\"O which may be zero if the timeout expires before anything interesting happens.
504 À®¸ù¤·¤¿¾ì¹ç¡¢
505 .BR select ()
506 ¤È
507 .BR pselect ()
508 ¤Ï¹¹¿·¤µ¤ì¤¿ 3 ¤Ä¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤Ë´Þ¤Þ¤ì¤Æ¤¤¤ë
509 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¿ô (¤Ä¤Þ¤ê¡¢
510 .IR readfds ,
511 .IR writefds ,
512 .I exceptfds
513 Ãæ¤Î 1 ¤Ë¤Ê¤Ã¤Æ¤¤¤ë¥Ó¥Ã¥È¤ÎÁí¿ô) ¤òÊÖ¤¹¡£
514 ²¿¤âµ¯¤³¤é¤º¤Ë»þ´ÖÀÚ¤ì¤Ë¤Ê¤Ã¤¿¾ì¹ç¡¢
515 ¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¿ô¤Ï 0 ¤Ë¤Ê¤ë¤³¤È¤â¤¢¤ë¡£
516 .\"O On error, \-1 is returned, and
517 .\"O .I errno
518 .\"O is set appropriately; the sets and
519 .\"O .I timeout
520 .\"O become undefined, so do not
521 .\"O rely on their contents after an error.
522 ¥¨¥é¡¼¤Ê¤é¤Ð \-1 ¤òÊÖ¤·¡¢
523 .I errno
524 ¤ËŬÀÚ¤ÊÃͤ¬ÀßÄꤵ¤ì¤ë; ½¸¹ç¤È
525 .I timeout
526 ¤Ï̤ÄêµÁ¤È¤Ê¤ë¤Î¤Ç¡¢¥¨¥é¡¼¤¬µ¯¤³¤Ã¤¿¸å¤Ï¤½¤ì¤é¤ÎÆâÍƤò¿®Íꤷ¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£
527 .\"O .SH ERRORS
528 .SH ¥¨¥é¡¼
529 .TP
530 .B EBADF
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 ¥¨¥é¡¼¤¬È¯À¸¤·¤¿¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬»ØÄꤵ¤ì¤¿)¡£
537 .TP
538 .B EINTR
539 .\"O A signal was caught; see
540 .\"O .BR signal (7).
541 ¥·¥°¥Ê¥ë¤ò¼õ¿®¤·¤¿¡£
542 .TP
543 .B EINVAL
544 .\"O .I nfds
545 .\"O is negative or the value contained within
546 .\"O .I timeout
547 .\"O is invalid.
548 .I n
549 ¤¬Éé¡¢¤Þ¤¿¤Ï
550 .I timeout
551 ¤ËÆþ¤Ã¤Æ¤¤¤ëÃͤ¬ÉÔÀµ¤Ç¤¢¤ë¡£
552 .TP
553 .B ENOMEM
554 .\"O unable to allocate memory for internal tables.
555 ÆâÉô¥Æ¡¼¥Ö¥ë¤Ë¥á¥â¥ê¤ò³ä¤êÅö¤Æ¤ë¤³¤È¤¬¤Ç¤­¤Ê¤«¤Ã¤¿¡£
556 .\"O .SH VERSIONS
557 .SH ¥Ð¡¼¥¸¥ç¥ó
558 .\"O .BR pselect ()
559 .\"O was added to Linux in kernel 2.6.16.
560 .BR pselect ()
561 ¤Ï¥«¡¼¥Í¥ë 2.6.16 ¤Ç Linux ¤ËÄɲ䵤줿¡£
562 .\"O Prior to this,
563 .\"O .BR pselect ()
564 .\"O was emulated in glibc (but see BUGS).
565 ¤½¤ì°ÊÁ°¤Ï¡¢
566 .BR pselect ()
567 ¤Ï glibc ¤Ç¥¨¥ß¥å¥ì¡¼¥È¤µ¤ì¤Æ¤¤¤¿ (¡Ö¥Ð¥°¡×¤Î¾Ï¤ò»²¾È)¡£
568 .\"O .SH "CONFORMING TO"
569 .SH ½àµò
570 .\"O .BR select ()
571 .\"O conforms to POSIX.1-2001 and
572 .\"O 4.4BSD
573 .\"O .RB ( select ()
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.
580 .BR select ()
581 ¤Ï POSIX.1-2001 ¤È 4.4BSD
582 .RB ( select ()
583 ¤Ï 4.2BSD ¤ÇºÇ½é¤ËÅо줷¤¿) ¤Ë½àµò¤¹¤ë¡£
584 BSD ¥½¥±¥Ã¥ÈÁؤΥ¯¥í¡¼¥ó¤ò¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤ëÈó BSD ¥·¥¹¥Æ¥à
585 (System V ·Ï¤â´Þ¤à) ¤È¤Î´Ö¤Ç¤À¤¤¤¿¤¤°Ü¿¢À­¤¬¤¢¤ë¡£¤·¤«¤· System V ·Ï¤Ç¤Ï
586 ¤¿¤¤¤¬¤¤ timeout ÊÑ¿ô¤ò exit ¤ÎÁ°¤Ë¥»¥Ã¥È¤¹¤ë¤¬¡¢
587 BSD ·Ï¤Ç¤Ï¤½¤¦¤Ç¤Ê¤¤¤Î¤ÇÃí°Õ¤¹¤ë¤³¤È¡£
588 .PP
589 .\"O .BR pselect ()
590 .\"O is defined in POSIX.1g, and in
591 .\"O POSIX.1-2001.
592 .BR pselect ()
593 ¤Ï POSIX.1g ¤È POSIX.1-2001 ¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£
594 .\"O .SH NOTES
595 .SH Ãí°Õ
596 .\"O An
597 .\"O .I fd_set
598 .\"O is a fixed size buffer.
599 .\"O Executing
600 .\"O .BR FD_CLR ()
601 .\"O or
602 .\"O .BR FD_SET ()
603 .\"O with a value of
604 .\"O .I fd
605 .\"O that is negative or is equal to or larger than
606 .\"O .B FD_SETSIZE
607 .\"O will result
608 .\"O in undefined behavior.
609 .\"O Moreover, POSIX requires
610 .\"O .I fd
611 .\"O to be a valid file descriptor.
612 .I fd_set
613 ¤Ï¸ÇÄꥵ¥¤¥º¤Î¥Ð¥Ã¥Õ¥¡¤Ç¤¢¤ë¡£
614 Éé¤ä
615 .B FD_SETSIZE
616 °Ê¾å¤ÎÃͤò»ý¤Ä
617 .I fd
618 ¤ËÂФ·¤Æ
619 .BR FD_CLR ()
620 ¤ä
621 .BR FD_SET ()
622 ¤ò¼Â¹Ô¤·¤¿¾ì¹ç¡¢
623 ¤É¤Î¤è¤¦¤ÊÆ°ºî¤ò¤¹¤ë¤«¤ÏÄêµÁ¤µ¤ì¤Æ¤¤¤Ê¤¤¡£
624 ¤Þ¤¿¡¢ POSIX ¤Ç¤Ï
625 .I fd
626 ¤ÏÍ­¸ú¤Ê¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¤Èµ¬Äꤵ¤ì¤Æ¤¤¤ë¡£
627
628 .\"O Concerning the types involved, the classical situation is that
629 .\"O the two fields of a
630 .\"O .I timeval
631 .\"O structure are typed as
632 .\"O .I long
633 .\"O (as shown above), and the structure is defined in
634 .\"O .IR <sys/time.h> .
635 ·¿Àë¸À¤Ë´Ø¤·¤Æ¤Ï¡¢ÀΤʤ¬¤é¤Î¾õ¶·¤Ç¤Ï
636 .I timeval
637 ¹½Â¤ÂΤΠ2 ¤Ä¤Î¥Õ¥£¡¼¥ë¥É¤Ï
638 (¾åµ­¤Î¤è¤¦¤Ë) Î¾Êý¤È¤â
639 .I long
640 ·¿¤Ç¤¢¤ê¡¢¹½Â¤ÂΤÏ
641 .I <sys/time.h>
642 ¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£
643 .\"O The POSIX.1-2001 situation is
644 .\"O
645 .\"O .in +4n
646 .\"O .nf
647 .\"O struct timeval {
648 .\"O     time_t         tv_sec;     /* seconds */
649 .\"O     suseconds_t    tv_usec;    /* microseconds */
650 .\"O };
651 .\"O .fi
652 .\"O .in
653 .\"O
654 .\"O where the structure is defined in
655 .\"O .I <sys/select.h>
656 .\"O and the data types
657 .\"O .I time_t
658 .\"O and
659 .\"O .I suseconds_t
660 .\"O are defined in
661 .\"O .IR <sys/types.h> .
662 POSIX.1-2001 ¤Î²¼¤Ç¤Ï¡¢°Ê²¼¤Î¤è¤¦¤Ë¤Ê¤Ã¤Æ¤¤¤ë¡£
663
664 .in +4n
665 .nf
666 struct timeval {
667          time_t         tv_sec;     /* Éà*/
668          suseconds_t    tv_usec;    /* ¥Þ¥¤¥¯¥íÉà*/
669 };
670 .fi
671 .in
672
673 ¤³¤Î¹½Â¤ÂΤÏ
674 .I <sys/select.h>
675 ¤ÇÄêµÁ¤µ¤ì¤Æ¤ª¤ê¡¢¥Ç¡¼¥¿·¿
676 .I time_t
677 ¤È
678 .I suseconds_t
679 ¤Ï
680 .I <sys/types.h>
681 ¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£
682 .LP
683 .\"O Concerning prototypes, the classical situation is that one should
684 .\"O include
685 .\"O .I <time.h>
686 .\"O for
687 .\"O .BR select ().
688 ¥×¥í¥È¥¿¥¤¥×¤Ë´Ø¤·¤Æ¤Ï¡¢ÀΤʤ¬¤é¤Î¾õ¶·¤Ç
689 .BR select ()
690 ¤ò»È¤¤¤¿¤¤¾ì¹ç¤Ï¡¢
691 .I <time.h>
692 ¤ò¥¤¥ó¥¯¥ë¡¼¥É¤¹¤ì¤Ð¤è¤¤¡£
693 .\"O The POSIX.1-2001 situation is that one should include
694 .\"O .I <sys/select.h>
695 .\"O for
696 .\"O .BR select ()
697 .\"O and
698 .\"O .BR pselect ().
699 POSIX.1-2001 ¤Î´Ä¶­¤Ç
700 .BR select ()
701 ¤È
702 .BR pselect ()
703 ¤ò»È¤¤¤¿¤¤¾ì¹ç¤Ï¡¢
704 .I <sys/select.h>
705 ¤ò¥¤¥ó¥¯¥ë¡¼¥É¤¹¤ì¤Ð¤è¤¤¡£
706
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.
710 ¥Ø¥Ã¥À¥Õ¥¡¥¤¥ë
711 .I <sys/select.h>
712 ¤Ï libc4 ¤È libc5 ¤Ë¤Ï¤Ê¤¯¡¢glibc 2.0 °Ê¹ß¤Ë¸ºß¤¹¤ë¡£
713 .\"O Under glibc 2.0 it unconditionally gives the wrong prototype for
714 .\"O .BR pselect ().
715 .\"O Under glibc 2.1 to 2.2.1 it gives
716 .\"O .BR pselect ()
717 .\"O when
718 .\"O .B _GNU_SOURCE
719 .\"O is defined.
720 .\"O Since glibc 2.2.2 the requirements are as shown in the SYNOPSIS.
721 °­¤¤¤³¤È¤Ë glibc 2.0 °ÊÁ°¤Ç¤Ï
722 .BR pselect ()
723 ¤Î¥×¥í¥È¥¿¥¤¥×¤¬´Ö°ã¤Ã¤Æ¤¤¤ë¡£
724 glibc 2.1 ¤«¤é 2.2.1 ¤Ç¤Ï
725 .B _GNU_SOURCE
726 ¤¬ÄêµÁ¤µ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ë¡¢
727 .BR pselect ()
728 ¤¬Ä󶡤µ¤ì¤ë¡£
729 glibc 2.2.2 °Ê¹ß¤Ç¤Ï¡¢
730 .BR pselect ()
731 ¤ò»ÈÍѤ¹¤ë¤Ë¤Ï¡¢¡Ö½ñ¼°¡×¤Ëµ­ºÜ¤µ¤ì¤¿Í×·ï¤òËþ¤¿¤¹É¬Íפ¬¤¢¤ë¡£
732 .\"O .SS "LINUX NOTES"
733 .SS "Linux ¤Ç¤ÎÃí°Õ"
734 .\"O The Linux
735 .\"O .BR pselect ()
736 .\"O system call modifies its
737 .\"O .I timeout
738 .\"O argument.
739 Linux ¤Î
740 .BR pselect ()
741 ¥·¥¹¥Æ¥à¥³¡¼¥ë¤Ï
742 .I timeout
743 °ú¤­¿ô¤òÊѹ¹¤¹¤ë¡£
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.
747 .\"O Thus, the glibc
748 .\"O .BR pselect ()
749 .\"O function does not modify its timeout argument;
750 .\"O this is the behavior required by POSIX.1-2001.
751 ¤·¤«¤·¡¢ glibc ¤Î¥é¥Ã¥Ñ¡¼´Ø¿ô¤Ï¡¢¥·¥¹¥Æ¥à¥³¡¼¥ë¤ËÅϤ¹ timeout °ú¤­¿ô
752 ¤È¤·¤Æ¥í¡¼¥«¥ëÊÑ¿ô¤ò»È¤¦¤³¤È¤Ç¤³¤ÎÆ°ºî¤ò±£Ê䷤Ƥ¤¤ë¡£
753 ¤³¤Î¤¿¤á¡¢glibc ¤Î
754 .BR pselect ()
755 ´Ø¿ô¤Ï timeout °ú¤­¿ô¤òÊѹ¹¤·¤Ê¤¤¡£
756 ¤³¤ì¤¬ POSIX.1-2001 ¤¬Í׵ᤷ¤Æ¤¤¤ëÆ°ºî¤Ç¤¢¤ë¡£
757 .\"O .SH BUGS
758 .SH ¥Ð¥°
759 .\"O Glibc 2.0 provided a version of
760 .\"O .BR pselect ()
761 .\"O that did not take a
762 .\"O .I sigmask
763 .\"O argument.
764 glibc 2.0 ¤Ç¤Ï¡¢
765 .I sigmask
766 °ú¤­¿ô¤ò¼è¤é¤Ê¤¤¥Ð¡¼¥¸¥ç¥ó¤Î
767 .BR pselect ()
768 ¤¬Ä󶡤µ¤ì¤Æ¤¤¤¿¡£
769
770 .\"O Since version 2.1, glibc has provided an emulation of
771 .\"O .BR pselect ()
772 .\"O that is implemented using
773 .\"O .BR sigprocmask (2)
774 .\"O and
775 .\"O .BR select ().
776 .\"O This implementation remains vulnerable to the very race condition that
777 .\"O .BR pselect ()
778 .\"O was designed to prevent.
779 ¥Ð¡¼¥¸¥ç¥ó 2.1 °Ê¹ß¤Î glibc ¤Ç¤Ï¡¢
780 .BR pselect ()
781 ¤Ï
782 .BR sigprocmask (2)
783 ¤È
784 .BR select ()
785 ¤ò»È¤Ã¤Æ¥¨¥ß¥å¥ì¡¼¥È¤µ¤ì¤Æ¤¤¤¿¡£
786 ¤³¤Î¼ÂÁõ¤Ë¤Ï¤­¤ï¤É¤¤¶¥¹ç¾ò·ï¤Ë¤ª¤¤¤ÆÀȼåÀ­¤¬»Ä¤Ã¤Æ¤¤¤ë¡£
787 ¤³¤Î¶¥¹ç¾ò·ï¤Ë¤ª¤±¤ëÌäÂê¤òËɻߤ¹¤ë¤¿¤á¤Ë
788 .BR pselect ()
789 ¤ÏÀ߷פµ¤ì¤¿¤Î¤Ç¤¢¤ë¡£
790 .\"O On systems that lack
791 .\"O .BR pselect (),
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
795 .\"O is monitored by
796 .\"O .BR select ()
797 .\"O in the main program.)
798 .BR pselect ()
799 ¤¬¤Ê¤¤¥·¥¹¥Æ¥à¤Ë¤ª¤¤¤Æ¡¢
800 ¥·¥°¥Ê¥ë¤ÎÊ᪤ò¿®ÍêÀ­¤¬¤¢¤ê (°Ü¿¢À­¤â¹â¤¤) ÊýË¡¤Ç¹Ô¤¦¤Ë¤Ï¡¢
801 ¼«¸Ê¥Ñ¥¤¥× (self-pipe) ¤È¤¤¤¦µ»¤ò»È¤¦¤È¤è¤¤ (¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ï¥Ñ¥¤¥×¤Ø
802 1 ¥Ð¥¤¥È¤Î¥Ç¡¼¥¿¤ò½ñ¤­¹þ¤ß¡¢Æ±¤¸¥Ñ¥¤¥×¤Î¤â¤¦°ìü¤ò¥á¥¤¥ó¥×¥í¥°¥é¥à¤Î
803 .BR select ()
804 ¤Ç´Æ»ë¤¹¤ë¤È¤¤¤¦ÊýË¡¤Ç¤¢¤ë)¡£
805
806 .\"O Under Linux,
807 .\"O .BR select ()
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
818 .\"O .B O_NONBLOCK
819 .\"O on sockets that should not block.
820 Linux ¤Ç¤Ï¡¢
821 .BR select ()
822 ¤¬¥½¥±¥Ã¥È¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ç "Æɤ߹þ¤ß¤Î½àÈ÷¤¬¤Ç¤­¤¿" ¤ÈÊó¹ð¤·¤¿¾ì¹ç¤Ç¤â¡¢
823 ¤³¤Î¸å¤Ç read ¤ò¹Ô¤¦¤ÈÄä»ß (block) ¤¹¤ë¤³¤È¤¬¤¢¤ë¡£¤³¤Î¤è¤¦¤Ê¾õ¶·¤Ï¡¢
824 Î㤨¤Ð¡¢¥Ç¡¼¥¿¤¬ÅþÃ夷¤¿¤¬¡¢¸¡ºº¤Ç¥Á¥§¥Ã¥¯¥µ¥à°Û¾ï¤¬¸«¤Ä¤«¤êÇÑ´þ¤µ¤ì¤¿»þ
825 ¤Ê¤É¤Ëµ¯¤³¤ê¤¨¤ë¡£Â¾¤Ë¤â¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬½àÈ÷¤Ç¤­¤¿¤È´Ö°ã¤Ã¤Æ
826 Êó¹ð¤µ¤ì¤ë¾õ¶·¤¬µ¯¤³¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
827 .\" Stevens ¤¬µó¤²¤¿¤Î¤Ï¡¢select ¤¬À®¸ù¤·¤ÆÉüµ¢¤·¤Æ¤«¤é accept ¤¬
828 .\" ¸Æ¤Ð¤ì¤ë¤Þ¤Ç¤Î´Ö¤Ë¡¢¥¯¥é¥¤¥¢¥ó¥È¤«¤é RST ¤¬Á÷¤é¤ì¤ë¤È¡¢
829 .\" accept ¤¬Ää»ß¤¹¤ë¾ì¹ç¤Ç¤¢¤ë¡£
830 ¤·¤¿¤¬¤Ã¤Æ¡¢Ää»ß¤¹¤Ù¤­¤Ç¤Ï¤Ê¤¤¥½¥±¥Ã¥È¤ËÂФ·¤Æ¤Ï
831 .B O_NONBLOCK
832 ¤ò»È¤¦¤È¤è¤ê°ÂÁ´¤Ç¤¢¤í¤¦¡£
833 .\"O .\" Maybe the kernel should have returned EIO in such a situation?
834 .\" Â¿Ê¬¡¢¤³¤Î¤è¤¦¤Ê¾õ¶·¤Ç¤Ï¥«¡¼¥Í¥ë¤Ï EIO ¤òÊÖ¤·¤Æ¤·¤Þ¤¦?
835
836 .\"O On Linux,
837 .\"O .BR select ()
838 .\"O also modifies
839 .\"O .I timeout
840 .\"O if the call is interrupted by a signal handler (i.e., the
841 .\"O .B EINTR
842 .\"O error return).
843 .\"O This is not permitted by POSIX.1-2001.
844 .\"O The Linux
845 .\"O .BR pselect ()
846 .\"O system call has the same behavior,
847 .\"O but the glibc wrapper hides this behavior by internally copying the
848 .\"O .I timeout
849 .\"O to a local variable and passing that variable to the system call.
850 Linux ¤Ç¤Ï¡¢
851 .BR select ()
852 ¤¬¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ë¤è¤ê³ä¤ê¹þ¤Þ¤ì¤¿¾ì¹ç (¤Ä¤Þ¤ê
853 .B EINTR
854 ¥¨¥é¡¼¤¬ÊÖ¤ë¾ì¹ç)¡¢
855 .I timeout
856 ¤âÊѹ¹¤¹¤ë¡£
857 ¤³¤ì¤Ï POSIX.1-2001 ¤Ç¤Ïǧ¤á¤é¤ì¤Æ¤¤¤Ê¤¤µóÆ°¤Ç¤¢¤ë¡£
858 Linux ¤Î
859 .BR pselect ()
860 ¥·¥¹¥Æ¥à¥³¡¼¥ë¤âƱ¤¸µóÆ°¤ò¤¹¤ë¤¬¡¢
861 glibc ¤Î¥é¥Ã¥Ñ¡¼´Ø¿ô¤¬¤³¤ÎµóÆ°¤ò±£Ê䷤Ƥ¤¤ë¡£
862 ¶ñÂÎŪ¤Ë¤Ï¡¢glibc ¤Î¥é¥Ã¥Ñ¡¼´Ø¿ô¤ÎÆâÉô¤Ç¡¢
863 .I timeout
864 ¤ò¥í¡¼¥«¥ëÊÑ¿ô¤Ë¥³¥Ô¡¼¤·¡¢
865 ¤³¤Î¥í¡¼¥«¥ëÊÑ¿ô¤ò¥·¥¹¥Æ¥à¥³¡¼¥ë¤ËÅϤ·¤Æ¤¤¤ë¡£
866 .\"O .SH EXAMPLE
867 .SH Îã
868 .nf
869 #include <stdio.h>
870 #include <stdlib.h>
871 #include <sys/time.h>
872 #include <sys/types.h>
873 #include <unistd.h>
874
875 .\"O int
876 .\"O main(void)
877 .\"O {
878 .\"O     fd_set rfds;
879 .\"O     struct timeval tv;
880 .\"O     int retval;
881 .\"O
882 .\"O     /* Watch stdin (fd 0) to see when it has input. */
883 .\"O     FD_ZERO(&rfds);
884 .\"O     FD_SET(0, &rfds);
885 .\"O
886 .\"O     /* Wait up to five seconds. */
887 .\"O     tv.tv_sec = 5;
888 .\"O     tv.tv_usec = 0;
889 .\"O
890 .\"O     retval = select(1, &rfds, NULL, NULL, &tv);
891 .\"O     /* Don't rely on the value of tv now! */
892 .\"O
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. */
898 .\"O     else
899 .\"O         printf("No data within five seconds.\\n");
900 .\"O
901 .\"O     exit(EXIT_SUCCESS);
902 .\"O }
903 int
904 main(void)
905 {
906     fd_set rfds;
907     struct timeval tv;
908     int retval;
909
910     /* stdin (fd 0) ¤ò´Æ»ë¤·¡¢ÆþÎϤ¬¤¢¤Ã¤¿¾ì¹ç¤Ëɽ¼¨¤¹¤ë¡£*/
911     FD_ZERO(&rfds);
912     FD_SET(0, &rfds);
913
914     /* 5 Éôִƻ뤹¤ë¡£*/
915     tv.tv_sec = 5;
916     tv.tv_usec = 0;
917
918     retval = select(1, &rfds, NULL, NULL, &tv);
919     /* ¤³¤Î»þÅÀ¤Ç¤Î tv ¤ÎÃͤò¿®Íꤷ¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£*/
920
921     if (retval == \-1)
922         perror("select()");
923     else if (retval)
924         printf("º£¡¢¥Ç¡¼¥¿¤¬¼èÆÀ¤Ç¤­¤Þ¤·¤¿¡£\\n");
925         /* FD_ISSET(0, &rfds) ¤¬ true ¤Ë¤Ê¤ë¡£*/
926     else
927         printf("5 ÉðÊÆâ¤Ë¥Ç¡¼¥¿¤¬ÆþÎϤµ¤ì¤Þ¤»¤ó¤Ç¤·¤¿¡£\\n");
928
929     exit(EXIT_SUCCESS);
930 }
931 .fi
932 .\"O .SH "SEE ALSO"
933 .SH ´ØÏ¢¹àÌÜ
934 .\"O For a tutorial with discussion and examples, see
935 .\"O .BR select_tut (2).
936 ¹Í»¡¤È»ÈÍÑÎã¤Î½ñ¤«¤ì¤¿¥Á¥å¡¼¥È¥ê¥¢¥ë¤È¤·¤Æ¡¢
937 .BR select_tut (2)
938 ¤¬¤¢¤ë¡£
939 .LP
940 .\"O For vaguely related stuff, see
941 ´Ø·¸¤¬¤¢¤ê¤½¤¦¤Ê¤â¤Î¤òµó¤²¤Æ¤ª¤¯:
942 .BR accept (2),
943 .BR connect (2),
944 .BR poll (2),
945 .BR read (2),
946 .BR recv (2),
947 .BR send (2),
948 .BR sigprocmask (2),
949 .BR write (2),
950 .BR epoll (7),
951 .BR time (7)