OSDN Git Service

54ce470081fb89e1f77fdcb6d5797eb7aac1607d
[linuxjm/LDP_man-pages.git] / draft / man2 / select_tut.2
1 .\" This manpage is copyright (C) 2001 Paul Sheer.
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein.  The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .\" very minor changes, aeb
24 .\"
25 .\" Modified 5 June 2002, Michael Kerrisk <mtk.manpages@gmail.com>
26 .\" 2006-05-13, mtk, removed much material that is redundant with select.2
27 .\"             various other changes
28 .\" 2008-01-26, mtk, substantial changes and rewrites
29 .\"
30 .\" Translated 2002-02-21, NAKANO Takeo <nakano@apm.seikei.ac.jp>
31 .\" Updated 2002-09-23, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
32 .\" Updated 2005-03-15, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
33 .\" Updated 2006-07-21, Akihiro MOTOKI, LDP v2.36
34 .\" Updated 2009-02-13, Akihiro MOTOKI, LDP v3.18
35 .\"
36 .\"WORD:        file descriptor         ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿
37 .\"WORD:        exceptional condition   Îã³°¾õÂÖ
38 .\"
39 .TH SELECT_TUT 2 2010-06-10 "Linux" "Linux Programmer's Manual"
40 .\"O .SH NAME
41 .SH Ì¾Á°
42 .\"O select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO \-
43 .\"O synchronous I/O multiplexing
44 select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO \- Æ±´ü I/O ¤Î¿½Å²½
45 .\"O .SH SYNOPSIS
46 .SH ½ñ¼°
47 .nf
48 .\"O /* According to POSIX.1-2001 */
49 /* POSIX.1-2001 ¤Ë½¾¤¦¾ì¹ç */
50 .br
51 .B #include <sys/select.h>
52 .sp
53 .\"O /* According to earlier standards */
54 /* °ÊÁ°¤Îµ¬³Ê¤Ë½¾¤¦¾ì¹ç */
55 .br
56 .B #include <sys/time.h>
57 .br
58 .B #include <sys/types.h>
59 .br
60 .B #include <unistd.h>
61 .sp
62 .BI "int select(int " nfds ", fd_set *" readfds ", fd_set *" writefds ,
63 .BI "           fd_set *" exceptfds ", struct timeval *" utimeout );
64 .sp
65 .BI "void FD_CLR(int " fd ", fd_set *" set );
66 .br
67 .BI "int  FD_ISSET(int " fd ", fd_set *" set );
68 .br
69 .BI "void FD_SET(int " fd ", fd_set *" set );
70 .br
71 .BI "void FD_ZERO(fd_set *" set );
72 .sp
73 .B #include <sys/select.h>
74 .sp
75 .BI "int pselect(int " nfds ", fd_set *" readfds ", fd_set *" writefds ,
76 .BI "            fd_set *" exceptfds ", const struct timespec *" ntimeout ,
77 .BI "            const sigset_t *" sigmask );
78 .fi
79 .sp
80 .in -4n
81 .\"O Feature Test Macro Requirements for glibc (see
82 .\"O .BR feature_test_macros (7)):
83 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
84 .RB ( feature_test_macros (7)
85 »²¾È):
86 .in
87 .sp
88 .BR pselect ():
89 _POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600
90 .\"O .SH DESCRIPTION
91 .SH ÀâÌÀ
92 .\"O .BR select ()
93 .\"O (or
94 .\"O .BR pselect ())
95 .\"O is used to efficiently monitor multiple file descriptors,
96 .\"O to see if any of them is, or becomes, "ready";
97 .\"O that is, to see whether I/O becomes possible,
98 .\"O or an "exceptional condition" has occurred on any of the descriptors.
99 .BR select ()
100 (¤ä
101 .BR pselect ())
102 ¤ò»È¤¦¤È¡¢¸úΨŪ¤ËÊ£¿ô¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò´Æ»ë¤·¡¢
103 ¤½¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¤¤¤º¤ì¤«¤¬
104 ¡Öready (½àÈ÷¤¬¤Ç¤­¤¿)¡×¾õÂÖ¡¢¤Ä¤Þ¤ê I/O (Æþ½ÐÎÏ) ¤¬²Äǽ¤Ë¤Ê¤Ã¤Æ¤¤¤ë¤«¤ä¡¢
105 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¤¤¤º¤ì¤«¤¬
106 ¡ÖÎã³°¾õÂÖ (exceptional condition)¡×¤¬È¯À¸¤·¤¿¤«¡¢¤òÄ´¤Ù¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
107
108 .\"O Its principal arguments are three "sets" of file descriptors:
109 .\"O \fIreadfds\fP, \fIwritefds\fP, and \fIexceptfds\fP.
110 ¤³¤Î´Ø¿ô¤Î¼çÍפʰú¤­¿ô¤Ï¡¢3¼ïÎà¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¡Ö½¸¹ç¡×
111 \fIreadfds\fP, \fIwritefds\fP, \fIexceptfds\fP ¤Ç¤¢¤ë¡£
112 .\"O Each set is declared as type
113 .\"O .IR fd_set ,
114 .\"O and its contents can be manipulated with the macros
115 .\"O .BR FD_CLR (),
116 .\"O .BR FD_ISSET (),
117 .\"O .BR FD_SET (),
118 .\"O and
119 .\"O .BR FD_ZERO ().
120 .\"O A newly declared set should first be cleared using
121 .\"O .BR FD_ZERO ().
122 ³Æ¡¹¤Î½¸¹ç¤Ï
123 .B fd_set
124 ¤È¤·¤ÆÀë¸À¤µ¤ì¡¢¤½¤ÎÆâÍƤÏ
125 .BR FD_CLR (),
126 .BR FD_ISSET (),
127 .BR FD_SET (),
128 .BR FD_ZERO ()
129 ¤È¤¤¤Ã¤¿¥Þ¥¯¥í¤Ë¤è¤Ã¤ÆÁàºî¤Ç¤­¤ë¡£
130 ¿·¤·¤¯Àë¸À¤µ¤ì¤¿½¸¹ç¤Ï¡¢¤Þ¤ººÇ½é¤Ë
131 .BR FD_ZERO ()
132 ¤ò»È¤Ã¤Æ¥¯¥ê¥¢¤¹¤Ù¤­¤Ç¤¢¤ë¡£
133 .\"O .BR select ()
134 .\"O modifies the contents of the sets according to the rules
135 .\"O described below; after calling
136 .\"O .BR select ()
137 .\"O you can test if a file descriptor is still present in a set with the
138 .\"O .BR FD_ISSET ()
139 .\"O macro.
140 .\"O .BR FD_ISSET ()
141 .\"O returns nonzero if a specified file descriptor is present in a set
142 .\"O and zero if it is not.
143 .\"O .BR FD_CLR ()
144 .\"O removes a file descriptor from a set.
145 .BR select ()
146 ¤Ï¤³¤ì¤é¤Î½¸¹ç¤ÎÆâÍƤò¡¢°Ê¹ß¤Ë½Ò¤Ù¤ëµ¬Â§¤Ë½¾¤Ã¤Æ½¤Àµ¤¹¤ë¡£
147 .BR select ()
148 ¤ò¸Æ¤ó¤À¸å¡¢¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬¤Þ¤À½¸¹ç¤Ë¸ºß¤·¤Æ¤¤¤ë¤«¤É¤¦¤«¤Ï¡¢
149 .BR FD_ISSET ()
150 ¥Þ¥¯¥í¤Ë¤è¤Ã¤ÆÄ´¤Ù¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
151 .BR FD_ISSET ()
152 ¤Ï»ØÄꤵ¤ì¤¿¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬½¸¹ç¤Ë¸ºß¤·¤Æ¤¤¤ì¤Ð 0 °Ê³°¤ÎÃͤòÊÖ¤·¡¢
153 ¸ºß¤·¤Ê¤±¤ì¤Ð 0 ¤òÊÖ¤¹¡£
154 .BR FD_CLR ()
155 ¤Ï½¸¹ç¤«¤é¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Îºï½ü¤ò¹Ô¤¦¡£
156 .\"O .SS Arguments
157 .SS °ú¤­¿ô
158 .TP
159 \fIreadfds\fP
160 .\"O This set is watched to see if data is available for reading from any of
161 .\"O its file descriptors.
162 .\"O After
163 .\"O .BR select ()
164 .\"O has returned, \fIreadfds\fP will be
165 .\"O cleared of all file descriptors except for those that
166 .\"O are immediately available for reading.
167 ¤³¤Î½¸¹ç¤Ë´Þ¤Þ¤ì¤ë¤¤¤º¤ì¤«¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ç¡¢
168 ¥Ç¡¼¥¿¤ÎÆɤ߹þ¤ß¤¬²Äǽ¤Ë¤Ê¤Ã¤¿¤«¤É¤¦¤«¤ò´Æ»ë¤¹¤ë¡£
169 .BR select ()
170 ¤«¤éÌá¤ë»þ¤Ë¡¢\fIreadfds\fP ¤Î¤¦¤Á¡¢
171 ľ¤Á¤ËÆɤ߹þ¤ß²Äǽ¤Ê¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿°Ê³°¤Ï
172 ½¸¹ç¤«¤éºï½ü¤µ¤ì¤ë¡£
173 .TP
174 \fIwritefds\fP
175 .\"O This set is watched to see if there is space to write data to any of
176 .\"O its file descriptors.
177 .\"O After
178 .\"O .BR select ()
179 .\"O has returned, \fIwritefds\fP will be
180 .\"O cleared of all file descriptors except for those that
181 .\"O are immediately available for writing.
182 ¤³¤Î½¸¹ç¤Ë´Þ¤Þ¤ì¤ë¤¤¤º¤ì¤«¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ç¡¢
183 ¥Ç¡¼¥¿¤ò½ñ¤­¹þ¤à¥¹¥Ú¡¼¥¹¤¬¤¢¤ë¤«¤É¤¦¤«¤ò´Æ»ë¤¹¤ë¡£
184 .BR select ()
185 ¤«¤éÌá¤ë»þ¤Ë¡¢\fIwritefds\fP ¤Î¤¦¤Á¡¢
186 ľ¤Á¤Ë½ñ¤­¹þ¤ß²Äǽ¤Ê¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿°Ê³°¤Ï
187 ½¸¹ç¤«¤éºï½ü¤µ¤ì¤ë¡£
188 .TP
189 \fIexceptfds\fP
190 .\"O This set is watched for "exceptional conditions".
191 .\"O In practice, only one such exceptional condition is common:
192 .\"O the availability of \fIout-of-band\fP (OOB) data for reading
193 .\"O from a TCP socket.
194 .\"O See
195 .\"O .BR recv (2),
196 .\"O .BR send (2),
197 .\"O and
198 .\"O .BR tcp (7)
199 .\"O for more details about OOB data.
200 .\"O (One other less common case where
201 .\"O .BR select (2)
202 .\"O indicates an exceptional condition occurs with pseudoterminals
203 .\"O in packet mode; see
204 .\"O .BR tty_ioctl (4).)
205 .\"O After
206 .\"O .BR select ()
207 .\"O has returned,
208 .\"O \fIexceptfds\fP will be cleared of all file descriptors except for those
209 .\"O for which an exceptional condition has occurred.
210 ¤³¤Î½¸¹ç¤Ë´Þ¤Þ¤ì¤ë¤¤¤º¤ì¤«¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ç¡¢
211 ¡ÖÎã³°¾õÂÖ (exceptional condition)¡×¤¬È¯À¸¤·¤¿¤«¤É¤¦¤«¤ò´Æ»ë¤¹¤ë¡£
212 ¼ÂºÝ¤ÎÆ°ºî¤Ç¤Ï¡¢ÉáÄ̤˵¯¤³¤êÆÀ¤ëÎã³°¾õÂ֤ϰì¤Ä¤À¤±¤Ç¤¢¤ê¡¢
213 ¤½¤ì¤Ï TCP ¥½¥±¥Ã¥È¤Ç \fIÂÓ°è³° (out-of-band; OOB)\fP ¥Ç¡¼¥¿¤¬
214 Æɤ߹þ¤ß²Äǽ¤Ê¾ì¹ç¤Ç¤¢¤ë¡£
215 OOB ¥Ç¡¼¥¿¤Î¾ÜºÙ¤Ë¤Ä¤¤¤Æ¤Ï¡¢
216 .BR recv (2),
217 .BR send (2),
218 .BR tcp (7)
219 ¤ò»²¾È¤Î¤³¤È¡£
220 (¤³¤ì°Ê³°¤Ç¤Ï¡¢¤Þ¤ì¤Ê¤³¤È¤À¤¬¡¢
221 ¥Ñ¥±¥Ã¥È¥â¡¼¥É¤Îµ¼»÷üËö (pseudoterminals) ¤Ç
222 .BR select ()
223 ¤¬Îã³°¾õÂÖ¤ò¼¨¤¹¤³¤È¤¬¤¢¤ë¡£)
224 .BR select ()
225 ¤¬ÊÖ¤ë»þ¤Ë¡¢\fIexceptfds\fP ¤Î¤¦¤Á¡¢
226 Îã³°¾õÂÖ¤¬È¯À¸¤·¤¿¥Ç¥£¥¹¥¯¥ê¥×¥¿°Ê³°¤Ï½¸¹ç¤«¤éºï½ü¤µ¤ì¤ë¡£
227 .TP
228 \fInfds\fP
229 .\"O This is an integer one more than the maximum of any file descriptor in
230 .\"O any of the sets.
231 .\"O In other words, while adding file descriptors to each of the sets,
232 .\"O you must calculate the maximum integer value of all of them,
233 .\"O then increment this value by one, and then pass this as \fInfds\fP.
234 Á´¤Æ¤Î½¸¹ç¤Ë´Þ¤Þ¤ì¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¤¦¤Á¡¢
235 Ãͤ¬ºÇÂç¤Î¤â¤Î¤Ë 1 ¤ò­¤·¤¿À°¿ô¤Ç¤¢¤ë¡£
236 ¤¹¤Ê¤ï¤Á¡¢¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò³Æ½¸¹ç¤Ë²Ã¤¨¤ëºî¶È¤ÎÅÓÃæ¤Ç¡¢
237 Á´¤Æ¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò¸«¤ÆºÇÂçÃͤòµá¤á¡¢
238 ¤½¤ì¤Ë 1 ¤ò²Ã¤¨¤Æ \fInfds\fP ¤È¤·¤ÆÅϤµ¤Ê¤¤¤È¤¤¤±¤Ê¤¤¡¢¤È¤¤¤¦¤³¤È¤À¡£
239 .TP
240 \fIutimeout\fP
241 .\"O This is the longest time
242 .\"O .BR select ()
243 .\"O may wait before returning, even if nothing interesting happened.
244 .\"O If this value is passed as NULL, then
245 .\"O .BR select ()
246 .\"O blocks indefinitely waiting for a file descriptor to become ready.
247 .\"O \fIutimeout\fP can be set to zero seconds, which causes
248 .\"O .BR select ()
249 .\"O to return immediately, with information about the readiness
250 .\"O of file descriptors at the time of the call.
251 .\"O The structure \fIstruct timeval\fP is defined as:
252 (²¿¤âµ¯¤³¤é¤Ê¤«¤Ã¤¿¾ì¹ç¤Ë)
253 .BR select ()
254 ¤¬Ìá¤ëÁ°¤ËÂԤĺÇÂç»þ´Ö¤Ç¤¢¤ë¡£
255 ¤³¤ÎÃͤˠNULL ¤òÅϤ¹¤È¡¢
256 .BR select ()
257 ¤Ï¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¤¤¤º¤ì¤«¤¬ ready (½àÈ÷¤¬¤Ç¤­¤¿) ¾õÂÖ¤Ë
258 ¤Ê¤ë¤Þ¤ÇÂÔ¤Á³¤±¤Æ¤º¤Ã¤ÈÄä»ß¤¹¤ë¡£
259 \fIutimeout\fP ¤Ï 0 Éäˤ¹¤ë¤³¤È¤â¤Ç¤­¡¢
260 ¤³¤Î¾ì¹ç
261 .BR select ()
262 ¤Ïľ¤Á¤ËÊ֤ꡢ¸Æ¤Ó½Ð¤·»þÅÀ¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¾õÂÖ¤Ë
263 ´Ø¤¹¤ë¾ðÊó¤¬ÊÖ¤µ¤ì¤ë¡£
264 ¹½Â¤ÂΠ\fIstruct timeval\fP ¤Ï¼¡¤Î¤è¤¦¤ËÄêµÁ¤µ¤ì¤Æ¤¤¤ë:
265 .IP
266 .in +4n
267 .nf
268 struct timeval {
269     time_t tv_sec;    /* seconds */
270     long tv_usec;     /* microseconds */
271 };
272 .fi
273 .in
274 .TP
275 \fIntimeout\fP
276 .\"O This argument for
277 .\"O .BR pselect ()
278 .\"O has the same meaning as
279 .\"O .IR utimeout ,
280 .\"O but
281 .\"O .I "struct timespec"
282 .\"O has nanosecond precision as follows:
283 .BR pselect ()
284 ¤Î¤³¤Î°ú¤­¿ô¤Ï
285 .I utimeout
286 ¤ÈƱ¤¸°ÕÌ£¤ò»ý¤Ä¤¬¡¢
287 .I "struct timespec"
288 ¤Ï¼¡¤Ë¼¨¤¹¤è¤¦¤Ë¥Ê¥ÎÉäÎÀºÅÙ¤ò»ý¤Ä¡£
289 .in +4n
290 .nf
291 struct timespec {
292     long tv_sec;    /* seconds */
293     long tv_nsec;   /* nanoseconds */
294 };
295 .fi
296 .in
297 .TP
298 \fIsigmask\fP
299 .\"O This argument holds a set of signals that the kernel should unblock
300 .\"O (i.e., remove from the signal mask of the calling thread),
301 .\"O while the caller is blocked inside the
302 .\"O .BR pselect ()
303 .\"O call (see
304 .\"O .BR sigaddset (3)
305 .\"O and
306 .\"O .BR sigprocmask (2)).
307 .\"O It may be NULL,
308 .\"O in which case the call does not modify the signal mask on
309 .\"O entry and exit to the function.
310 .\"O In this case,
311 .\"O .BR pselect ()
312 .\"O will then behave just like
313 .\"O .BR select ().
314 ¤³¤Î°ú¤­¿ô¤Ï¡¢¸Æ¤Ó½Ð¤·Â¦¤¬
315 .BR pselect ()
316 ÆâÉô¤ÇÄä»ß¤·¤Æ¤¤¤ë´Ö¤Ë¡¢¥«¡¼¥Í¥ë¤¬ÄÌÃΤòµö²Ä¤¹¤Ù¤­¥·¥°¥Ê¥ë½¸¹ç
317 (¤¹¤Ê¤ï¤Á¡¢¸Æ¤Ó½Ð¤·¤¿¥¹¥ì¥Ã¥É¤Î¥·¥°¥Ê¥ë¥Þ¥¹¥¯¤«¤éºï½ü¤¹¤Ù¤­¥·¥°¥Ê¥ë½¸¹ç)
318 ¤òÊÝ»ý¤¹¤ë
319 .RB ( sigaddset (3)
320 ¤È
321 .BR sigprocmask (2)
322 ¤ò»²¾È)¡£
323 ¤³¤Î°ú¤­¿ô¤Ï NULL ¤Ë¤¹¤ë¤³¤È¤â¤Ç¤­¡¢¤½¤Î¾ì¹ç¤Ï¤³¤Î´Ø¿ô¤Ø
324 Æþ¤ë¤È¤­¡¦½Ð¤ë¤È¤­¤Ë¥·¥°¥Ê¥ë¥Þ¥¹¥¯¤òÊѹ¹¤·¤Ê¤¤¡£
325 ¤³¤Î¾ì¹ç¡¢
326 .BR pselect ()
327 ¤Ï
328 .BR select ()
329 ¤ÈÁ´¤¯Æ±¤¸Æ°ºî¤È¤Ê¤ë¡£
330 .\"O .SS Combining Signal and Data Events
331 .SS ¥·¥°¥Ê¥ë¤È¥Ç¡¼¥¿¥¤¥Ù¥ó¥È¤òÁȤ߹ç¤ï¤»¤ë
332 .\"O .BR pselect ()
333 .\"O is useful if you are waiting for a signal as well as
334 .\"O for file descriptor(s) to become ready for I/O.
335 .\"O Programs that receive signals
336 .\"O normally use the signal handler only to raise a global flag.
337 .\"O The global flag will indicate that the event must be processed
338 .\"O in the main loop of the program.
339 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬ I/O ²Äǽ¤Ê¾õÂ֤ˤʤë¤Î¤ÈƱ»þ¤Ë
340 ¥·¥°¥Ê¥ë¤âÂÔ¤Á¤¿¤¤¾ì¹ç¤Ë¤Ï¡¢
341 .BR pselect ()
342 ¤¬ÊØÍø¤Ç¤¢¤ë¡£
343 ¥·¥°¥Ê¥ë¤ò¼õ¿®¤¹¤ë¥×¥í¥°¥é¥à¤Ï¡¢Ä̾ï¤Ï¡¢
344 ¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤ò¥°¥í¡¼¥Ð¥ë¤Ê¥Õ¥é¥°¤òΩ¤Æ¤ë¤¿¤á¤À¤±¤Ë»È¤¦¡£
345 ¤³¤Î¥°¥í¡¼¥Ð¥ë¤Ê¥Õ¥é¥°¤Ï¡¢
346 ¤½¤Î¥¤¥Ù¥ó¥È¤ò¥×¥í¥°¥é¥à¤Î¥á¥¤¥ó¥ë¡¼¥×¤Ç
347 ½èÍý¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¤³¤È¤ò¼¨¤¹¡£
348 .\"O A signal will cause the
349 .\"O .BR select ()
350 .\"O (or
351 .\"O .BR pselect ())
352 .\"O call to return with \fIerrno\fP set to \fBEINTR\fP.
353 .\"O This behavior is essential so that signals can be processed
354 .\"O in the main loop of the program, otherwise
355 .\"O .BR select ()
356 .\"O would block indefinitely.
357 ¥·¥°¥Ê¥ë¤ò¼õ¤±¤ë¤È
358 .BR select ()
359 (¤ä
360 .BR pselect ())
361 ¤Ï
362 \fIerrno\fP ¤Ë \fBEINTR\fP ¤ò¥»¥Ã¥È¤·¤ÆÌá¤ë¤³¤È¤Ë¤Ê¤ë¡£
363 ¥·¥°¥Ê¥ë¤¬¥×¥í¥°¥é¥à¤Î¥á¥¤¥ó¥ë¡¼¥×¤Ç½èÍý¤µ¤ì¤ë¤¿¤á¤Ë¤Ï¤³¤ÎÆ°ºî¤¬ÉԲķç¤Ç¡¢
364 ¤³¤ì¤¬¤Ê¤¤¤È
365 .BR select ()
366 ¤Ï±Ê±ó¤ËÄä»ß¤·Â³¤±¤ë¤³¤È¤Ë¤Ê¤ë¡£
367 .\"O Now, somewhere
368 .\"O in the main loop will be a conditional to check the global flag.
369 .\"O So we must ask:
370 .\"O what if a signal arrives after the conditional, but before the
371 .\"O .BR select ()
372 .\"O call?
373 .\"O The answer is that
374 .\"O .BR select ()
375 .\"O would block indefinitely, even though an event is actually pending.
376 ¤µ¤Æ¡¢¥á¥¤¥ó¥ë¡¼¥×¤Î¤É¤³¤«¤Ë¤³¤Î¥°¥í¡¼¥Ð¥ë¥Õ¥é¥°¤ò¥Á¥§¥Ã¥¯¤¹¤ë
377 ¾ò·ïʸ¤¬¤¢¤ë¤È¤·¤è¤¦¡£¤³¤³¤Ç¾¯¤·¹Í¤¨¤Æ¤ß¤Ê¤¤¤È¤¤¤±¤Ê¤¤¡£
378 ¡Ö¥·¥°¥Ê¥ë¤¬¾ò·ïʸ¤Î¸å¡¢¤·¤«¤·
379 .BR select ()
380 ¥³¡¼¥ë¤ÎÁ°¤ËÅþÃ夷¤¿¤é
381 ¤É¤¦¤Ê¤ë¤Î¤«¡©¡×
382 Åú¤¨¤Ï¡Ö¤½¤Î
383 .BR select ()
384 ¤Ï¡¢¤¿¤È¤¨²ò·èÂÔ¤Á¤Î¥¤¥Ù¥ó¥È¤¬¤¢¤Ã¤¿¤È¤·¤Æ¤â¡¢
385 ±Ê±ó¤ËÄä»ß¤¹¤ë¡×¤Ç¤¢¤ë¡£
386 .\"O This race condition is solved by the
387 .\"O .BR pselect ()
388 .\"O call.
389 .\"O This call can be used to set the signal mask to a set of signals
390 .\"O that are only to be received within the
391 .\"O .BR pselect ()
392 .\"O call.
393 ¤³¤Î¶¥¹ç¾õÂÖ¤Ï
394 .BR pselect ()
395 ¥³¡¼¥ë¤Ë¤è¤Ã¤Æ²ò·è¤Ç¤­¤ë¡£
396 ¤³¤Î¥³¡¼¥ë¤ò»È¤¦¤È¡¢
397 .BR pselect ()
398 ¤Ç¼õ¿®¤·¤¿¤¤¥·¥°¥Ê¥ë¤Î½¸¹ç¤À¤±¤ò¥·¥°¥Ê¥ë¥Þ¥¹¥¯¤ËÀßÄꤹ¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
399 .\"O For instance, let us say that the event in question
400 .\"O was the exit of a child process.
401 .\"O Before the start of the main loop, we
402 .\"O would block \fBSIGCHLD\fP using
403 .\"O .BR sigprocmask (2).
404 .\"O Our
405 .\"O .BR pselect ()
406 .\"O call would enable
407 .\"O .B SIGCHLD
408 .\"O by using an empty signal mask.
409 .\"O Our program would look like:
410 Î㤨¤Ð¡¢ÌäÂê¤È¤Ê¤Ã¤Æ¤¤¤ë¥¤¥Ù¥ó¥È¤¬»Ò¥×¥í¥»¥¹¤Î½ªÎ»¤Î¾ì¹ç¤ò¹Í¤¨¤è¤¦¡£
411 ¥á¥¤¥ó¥ë¡¼¥×¤¬»Ï¤Þ¤ëÁ°¤Ë¡¢
412 .B SIGCHLD
413 ¤ò
414 .BR sigprocmask (2)
415 ¤Ç¥Ö¥í¥Ã¥¯¤¹¤ë¡£
416 .BR pselect ()
417 ¥³¡¼¥ë¤Ç¤Ï
418 .B SIGCHLD
419 ¤ò¡¢¤â¤È¤â¤È¤Î¥·¥°¥Ê¥ë¥Þ¥¹¥¯¤ò»È¤Ã¤ÆÍ­¸ú¤Ë¤¹¤ë¤Î¤À¡£
420 ¤³¤Î¥×¥í¥°¥é¥à¤Ï¼¡¤Î¤è¤¦¤Ë¤Ê¤ë¡£
421 .PP
422 .nf
423 static volatile sig_atomic_t got_SIGCHLD = 0;
424
425 static void
426 child_sig_handler(int sig)
427 {
428     got_SIGCHLD = 1;
429 }
430
431 int
432 main(int argc, char *argv[])
433 {
434     sigset_t sigmask, empty_mask;
435     struct sigaction sa;
436     fd_set readfds, writefds, exceptfds;
437     int r;
438
439     sigemptyset(&sigmask);
440     sigaddset(&sigmask, SIGCHLD);
441     if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == \-1) {
442         perror("sigprocmask");
443         exit(EXIT_FAILURE);
444     }
445
446     sa.sa_flags = 0;
447     sa.sa_handler = child_sig_handler;
448     sigemptyset(&sa.sa_mask);
449     if (sigaction(SIGCHLD, &sa, NULL) == \-1) {
450         perror("sigaction");
451         exit(EXIT_FAILURE);
452     }
453
454     sigemptyset(&empty_mask);
455
456     for (;;) {          /* main loop */
457         /* Initialize readfds, writefds, and exceptfds
458            before the pselect() call. (Code omitted.) */
459
460         r = pselect(nfds, &readfds, &writefds, &exceptfds,
461                     NULL, &empty_mask);
462         if (r == \-1 && errno != EINTR) {
463             /* Handle error */
464         }
465
466         if (got_SIGCHLD) {
467             got_SIGCHLD = 0;
468
469             /* Handle signalled event here; e.g., wait() for all
470                terminated children. (Code omitted.) */
471         }
472
473         /* main body of program */
474     }
475 }
476 .fi
477 .\"O .SS Practical
478 .SS ¼ÂÎã
479 .\"O So what is the point of
480 .\"O .BR select ()?
481 .\"O Can't I just read and write to my descriptors whenever I want?
482 .\"O The point of
483 .\"O .BR select ()
484 .\"O is that it watches
485 .\"O multiple descriptors at the same time and properly puts the process to
486 .\"O sleep if there is no activity.
487 .\"O UNIX programmers often find
488 .\"O themselves in a position where they have to handle I/O from more than one
489 .\"O file descriptor where the data flow may be intermittent.
490 .\"O If you were to merely create a sequence of
491 .\"O .BR read (2)
492 .\"O and
493 .\"O .BR write (2)
494 .\"O calls, you would
495 .\"O find that one of your calls may block waiting for data from/to a file
496 .\"O descriptor, while another file descriptor is unused though ready for I/O.
497 .\"O .BR select ()
498 .\"O efficiently copes with this situation.
499 ¼ÂºÝ¤Î¤È¤³¤í
500 .BR select ()
501 ¤ÎÂç»ö¤ÊÅÀ¤Ï²¿¤Ê¤Î¤«¡©
502 ¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ï¹¥¤­¤Ê¤È¤­¤ËÆɤ߽ñ¤­¤Ç¤­¤ë¤ó¤¸¤ã¤Ê¤¤¤Î¡©
503 .BR select ()
504 ¤Î½ÅÍפʤȤ³¤í¤Ï¡¢Ê£¿ô¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òƱ»þ¤Ë´Æ»ë¤Ç¤­¡¢
505 ¤Ê¤ó¤ÎÆ°¤­¤â¤Ê¤±¤ì¤Ð¥×¥í¥»¥¹¤òŬÀڤ˥¹¥ê¡¼¥×¾õÂ֤˰ܹԤ¹¤ë¤È¤³¤í¤Ë¤¢¤ë¤Î¤À¡£
506 UNIX ¥×¥í¥°¥é¥Þ¤Ï¡¢
507 Ê£¿ô¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÎÆþ½ÐÎϤòƱ»þ¤Ë°·¤ï¤Í¤Ð¤Ê¤é¤º¡¢
508 ¤·¤«¤â¥Ç¡¼¥¿¤Îή¤ì¤Ï´Ö·çŪ¤Ç¤¢¤ë¡¢¤È¤¤¤¦¾õ¶·¤Ë¤è¤¯½Ð²ñ¤¦¡£
509 ñ¤Ë
510 .BR read (2)
511 ¤ä
512 .BR write (2)
513 ¥³¡¼¥ë¤Î¥·¡¼¥±¥ó¥¹¤òºî¤ë¤À¤±¤Ç¤Ï¡¢¤½¤ì¤é¤Î¥³¡¼¥ë¤Î¤É¤ì¤«¤¬
514 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤«¤é¤Î¥Ç¡¼¥¿¤òÂԤäƥ֥í¥Ã¥¯¤·¤Æ¤ª¤ê¡¢
515 Ê̤Υե¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ë¤Ï I/O ¤¬²Äǽ¤Ê¤Î¤Ë»È¤¨¤Ê¤¤¡¢
516 ¤È¤¤¤¦¤³¤È¤Ë¤Ê¤Ã¤Æ¤·¤Þ¤¦¤À¤í¤¦¡£
517 .BR select ()
518 ¤ò»È¤¦¤È¤³¤Î¾õ¶·¤Ë¸ú²ÌŪ¤ËÂнè¤Ç¤­¤ë¡£
519 .\"O .SS Select Law
520 .SS SELECT ¤ÎÙÝ
521 .\"O Many people who try to use
522 .\"O .BR select ()
523 .\"O come across behavior that is
524 .\"O difficult to understand and produces nonportable or borderline results.
525 .\"O For instance, the above program is carefully written not to
526 .\"O block at any point, even though it does not set its file descriptors to
527 .\"O nonblocking mode.
528 .\"O It is easy to introduce
529 .\"O subtle errors that will remove the advantage of using
530 .\"O .BR select (),
531 .\"O so here is a list of essentials to watch for when using
532 .\"O .BR select ().
533 .BR select ()
534 ¤ò»È¤ª¤¦¤È¤·¤¿Â¿¤¯¤Î¿Í¤Ï¡¢Íý²ò¤·¤Ë¤¯¤¤µóÆ°¤Ë½Ð¤¯¤ï¤·¡¢·ë²ÌŪ¤Ë
535 ¤Ç¤­¤¿¤â¤Î¤Ï°Ü¿¢À­¤¬¤Ê¤¤¤«¡¢¤è¤¯¤Æ¤â¥®¥ê¥®¥ê¤Î¤â¤Î¤Ë¤Ê¤Ã¤Æ¤·¤Þ¤¦¡£
536 Î㤨¤Ð¡¢¾åµ­¤Î¥×¥í¥°¥é¥à¤Ï¡¢
537 ½¸¹ç¤Ë´Þ¤Þ¤ì¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÈóÄä»ß (nonblocking) ¥â¡¼¥É
538 ¤Ë¤·¤Ê¤¯¤Æ¤â¡¢¤É¤³¤Ë¤â¥Ö¥í¥Ã¥¯¤¬À¸¤¸¤Ê¤¤¤è¤¦Ãí°Õ¤·¤Æ½ñ¤«¤ì¤Æ¤¤¤ë¡£
539 Èù̯¤Ê´Ö°ã¤¤¤Ë¤è¤Ã¤Æ¡¢
540 .BR select ()
541 ¤ò»È¤¦ÍøÅÀ¤Ï´Êñ¤Ë¼º¤ï¤ì¤Æ¤·¤Þ¤¦¡£
542 ¤½¤³¤Ç¡¢
543 .BR select ()
544 ¥³¡¼¥ë¤ò»È¤¦¤È¤­¤ËÃí°Õ¤¹¤Ù¤­½ÅÍ×»ö¹à¤òÎóµó¤·¤Æ¤ª¤¯¤³¤È¤Ë¤¹¤ë¡£
545 .TP 4
546 1.
547 .\"O You should always try to use
548 .\"O .BR select ()
549 .\"O without a timeout.
550 .\"O Your program
551 .\"O should have nothing to do if there is no data available.
552 .\"O Code that
553 .\"O depends on timeouts is not usually portable and is difficult to debug.
554 .BR select ()
555 ¤ò»È¤¦¤È¤­¤Ï¡¢¥¿¥¤¥à¥¢¥¦¥È¤ÏÀßÄꤹ¤Ù¤­¤Ç¤Ê¤¤¡£
556 ½èÍý¤¹¤ë¥Ç¡¼¥¿¤¬Ìµ¤¤¤È¤­¤Ë¤Ï¡¢
557 ¤¢¤Ê¤¿¤Î¥×¥í¥°¥é¥à¤Ë¤Ï²¿¤â¤¹¤ë¤³¤È¤Ï̵¤¤¤Ï¤º¤Ç¤¢¤ë¡£
558 ¥¿¥¤¥à¥¢¥¦¥È¤Ë°Í¸¤·¤¿¥³¡¼¥É¤ÏÄ̾ï°Ü¿¢À­¤¬¤Ê¤¯¡¢
559 ¥Ç¥Ð¥Ã¥°¤âÆñ¤·¤¯¤Ê¤ë¡£
560 .TP
561 2.
562 .\"O The value \fInfds\fP must be properly calculated for efficiency as
563 .\"O explained above.
564 ¾å½Ò¤·¤¿¤è¤¦¤Ë¡¢
565 ¸úΨŪ¤Ê¥×¥í¥°¥é¥à¤ò½ñ¤¯¤Ë¤Ï
566 .I nfds
567 ¤ÎÃͤòŬÀڤ˷׻»¤·¤ÆÍ¿¤¨¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
568 .TP
569 3.
570 .\"O No file descriptor must be added to any set if you do not intend
571 .\"O to check its result after the
572 .\"O .BR select ()
573 .\"O call, and respond appropriately.
574 .\"O See next rule.
575 .BR select ()
576 ¥³¡¼¥ë¤Î½ªÎ»¸å¤Ë·ë²Ì¤ò¥Á¥§¥Ã¥¯¤·¤Æ¡¢
577 ŬÀÚ¤ËÂбþ¤¹¤ë¤Ä¤â¤ê¤Î¤Ê¤¤¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ï¡¢
578 ¤É¤Î½¸¹ç¤Ë¤â²Ã¤¨¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£
579 ¼¡¤Î¥ë¡¼¥ë¤â»²¾È¡£
580 .TP
581 4.
582 .\"O After
583 .\"O .BR select ()
584 .\"O returns, all file descriptors in all sets
585 .\"O should be checked to see if they are ready.
586 .BR select ()
587 ¤«¤éÊ֤俸å¤Ë¤Ï¡¢Á´¤Æ¤Î½¸¹ç¤ÎÁ´¤Æ¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ë¤Ä¤¤¤Æ
588 Æɤ߽ñ¤­²Äǽ¤Ê¾õÂ֤ˤʤäƤ¤¤ë¤«¤ò¥Á¥§¥Ã¥¯¤¹¤Ù¤­¤Ç¤¢¤ë¡£
589 .TP
590 5.
591 .\"O The functions
592 .\"O .BR read (2),
593 .\"O .BR recv (2),
594 .\"O .BR write (2),
595 .\"O and
596 .\"O .BR send (2)
597 .\"O do \fInot\fP necessarily read/write the full amount of data
598 .\"O that you have requested.
599 .\"O If they do read/write the full amount, it's
600 .\"O because you have a low traffic load and a fast stream.
601 .\"O This is not always going to be the case.
602 .\"O You should cope with the case of your
603 .\"O functions only managing to send or receive a single byte.
604 .BR read (2),
605 .BR recv (2),
606 .BR write (2),
607 .BR send (2)
608 ¤È¤¤¤Ã¤¿´Ø¿ô¤Ï¡¢¤³¤Á¤é¤¬Í׵ᤷ¤¿Á´¥Ç¡¼¥¿¤òÆɤ߽ñ¤­¤¹¤ëɬÍפÏ\fI¤Ê¤¤\fP¡£
609 ¤â¤·Á´¥Ç¡¼¥¿¤òÆɤ߽ñ¤­¤¹¤ë¤Ê¤é¡¢¤½¤ì¤Ï¥È¥é¥Õ¥£¥Ã¥¯¤ÎÉé²Ù¤¬¾®¤µ¤¯¡¢
610 ¥¹¥È¥ê¡¼¥à¤¬Â®¤¤¾ì¹ç¤À¤í¤¦¡£¤³¤Î¾ò·ï¤Ï¾ï¤ËËþ¤¿¤µ¤ì¤ë¤È¤Ï¸Â¤é¤Ê¤¤¡£
611 ¤³¤ì¤é¤Î´Ø¿ô¤¬´èÄ¥¤Ã¤Æ¤â 1 ¥Ð¥¤¥È¤·¤«Á÷¼õ¿®¤Ç¤­¤Ê¤¤¤è¤¦¤Ê¾ì¹ç¤â
612 ¹Íθ¤ËÆþ¤ì¤Æ¤ä¤é¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
613 .TP
614 6.
615 .\"O Never read/write only in single bytes at a time unless you are really
616 .\"O sure that you have a small amount of data to process.
617 .\"O It is extremely
618 .\"O inefficient not to read/write as much data as you can buffer each time.
619 .\"O The buffers in the example below are 1024 bytes although they could
620 .\"O easily be made larger.
621 ½èÍý¤¹¤ë¥Ç¡¼¥¿Î̤¬¾®¤µ¤¤¤³¤È¤¬¤Ï¤Ã¤­¤ê¤È¤ï¤«¤Ã¤Æ¤¤¤ë¾ì¹ç¤ò½ü¤¤¤Æ¡¢
622 °ìÅ٤ˠ1 ¥Ð¥¤¥È¤º¤ÄÆɤ߽ñ¤­¤¹¤ë¤è¤¦¤Ê¤³¤È¤Ï¤·¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£
623 ¥Ð¥Ã¥Õ¥¡¤Îµö¤¹¤«¤®¤ê¤Î¥Ç¡¼¥¿¤ò¤Þ¤È¤á¤ÆÆɤ߽ñ¤­¤·¤Ê¤¤¤È¡¢
624 Èó¾ï¤Ë¸úΨ¤¬°­¤¤¡£²¼µ­¤ÎÎã¤Ç¤Ï¥Ð¥Ã¥Õ¥¡¤Ï 1024 ¥Ð¥¤¥È¤Ë¤·¤Æ¤¤¤ë¤¬¡¢
625 ¤³¤Î¥µ¥¤¥º¤òÂ礭¤¯¤¹¤ë¤Î¤Ï´Êñ¤À¤í¤¦¡£
626 .TP
627 7.
628 .\"O The functions
629 .\"O .BR read (2),
630 .\"O .BR recv (2),
631 .\"O .BR write (2),
632 .\"O and
633 .\"O .BR send (2)
634 .\"O as well as the
635 .\"O .BR select ()
636 .\"O call can return \-1 with
637 .\"O .I errno
638 .\"O set to \fBEINTR\fP,
639 .\"O or with
640 .\"O .I errno
641 .\"O set to \fBEAGAIN\fP (\fBEWOULDBLOCK\fP).
642 .\"O These results must be properly managed (not done properly above).
643 .\"O If your program is not going to receive any signals, then
644 .\"O it is unlikely you will get \fBEINTR\fP.
645 .\"O If your program does not set nonblocking I/O,
646 .\"O you will not get \fBEAGAIN\fP.
647 .\"O .\" Nonetheless, you should still cope with these errors for completeness.
648 .BR read (2),
649 .BR recv (2),
650 .BR write (2),
651 .BR send (2)
652 ¤Ê¤É¤Î´Ø¿ô¤ä
653 .BR select ()
654 ¥³¡¼¥ë¤Ï¡¢
655 .I errno
656 ¤ò
657 .B EINTR " ¤ä " EAGAIN
658 .RB ( EWOULDBLOCK )
659 ¤Ë¤·¤Æ \-1 ¤òÊÖ¤¹¤³¤È¤¬¤¢¤ë¡£
660 ¤³¤Î¤è¤¦¤Ê·ë²Ì¤ËÂФ·¤ÆŬÀÚ¤ËÂбþ¤·¤Æ¤ä¤é¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤
661 (¾åµ­¤ÎÎã¤Ç¤Ï¤·¤Æ¤¤¤Ê¤¤)¡£
662 ½ñ¤¤¤Æ¤¤¤ë¥×¥í¥°¥é¥à¤¬¥·¥°¥Ê¥ë¤ò¼õ¤±¤ëͽÄ꤬¤Ê¤±¤ì¤Ð¡¢
663 .B EINTR
664 ¤¬ÊÖ¤µ¤ì¤ë¤³¤È¤Ï¤¢¤Þ¤ê¹Í¤¨¤é¤ì¤Ê¤¤¡£
665 ½ñ¤¤¤Æ¤¤¤ë¥×¥í¥°¥é¥à¤ÇÈó¥Ö¥í¥Ã¥¯ I/O ¤ò¥»¥Ã¥È¤·¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¡¢
666 .B EAGAIN
667 ¤¬ÊÖ¤µ¤ì¤ë¤³¤È¤Ï¤Ê¤¤¤À¤í¤¦¡£
668 .\" ¤½¤ì¤Ç¤â¤Ê¤ª¡¢´°Á´¤ò´ü¤¹¤ë¤Ê¤é¤Ð¡¢
669 .\" ¤³¤ì¤é¤Î¥¨¥é¡¼¤ò¹Íθ¤ËÆþ¤ì¤ëɬÍפ¬¤¢¤ë¡£
670 .TP
671 8.
672 .\"O Never call
673 .\"O .BR read (2),
674 .\"O .BR recv (2),
675 .\"O .BR write (2),
676 .\"O or
677 .\"O .BR send (2)
678 .\"O with a buffer length of zero.
679 ·è¤·¤Æ¡¢°ú¤­¿ô¤ËŤµ 0 ¤Î¥Ð¥Ã¥Õ¥¡¤ò»ØÄꤷ¤Æ
680 .BR read (2),
681 .BR recv (2),
682 .BR write (2),
683 .BR send (2)
684 ¤ò¸Æ¤Ó½Ð¤·¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£
685 .TP
686 9.
687 .\"O If the functions
688 .\"O .BR read (2),
689 .\"O .BR recv (2),
690 .\"O .BR write (2),
691 .\"O and
692 .\"O .BR send (2)
693 .\"O fail with errors other than those listed in \fB7.\fP,
694 .\"O or one of the input functions returns 0, indicating end of file,
695 .\"O then you should \fInot\fP pass that descriptor to
696 .\"O .BR select ()
697 .\"O again.
698 .\"O In the example below,
699 .\"O I close the descriptor immediately, and then set it to \-1
700 .\"O to prevent it being included in a set.
701 .BR read (2),
702 .BR recv (2),
703 .BR write (2),
704 .BR send (2)
705 ¤¬
706 \fB7.\fP ¤Ë¼¨¤·¤¿°Ê³°¤Î¥¨¥é¡¼¤Ç¼ºÇÔ¤·¤¿¾ì¹ç¤ä¡¢
707 ÆþÎϷϤδؿô¤Î°ì¤Ä¤¬¥Õ¥¡¥¤¥ëËöÈø¤òɽ¤¹ 0 ¤òÊÖ¤·¤¿¾ì¹ç¤Ï¡¢
708 ¤½¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò¤â¤¦°ìÅÙ select ¤ËÅϤ·¤Æ¤Ï\fI¤Ê¤é¤Ê¤¤\fP¡£
709 ²¼µ­¤ÎÎã¤Ç¤Ï¡¢¤½¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò¤¿¤À¤Á¤Ë¥¯¥í¡¼¥º¤·¡¢
710 ¤½¤³¤Ë¤Ï \-1 ¤ò¥»¥Ã¥È¤·¤Æ¡¢
711 ¤½¤ì¤¬½¸¹ç¤Ë´Þ¤Þ¤ì³¤±¤ë¤Î¤òµö¤µ¤Ê¤¤¤è¤¦¤Ë¤·¤Æ¤¤¤ë¡£
712 .TP
713 10.
714 .\"O The timeout value must be initialized with each new call to
715 .\"O .BR select (),
716 .\"O since some operating systems modify the structure.
717 .\"O .BR pselect ()
718 .\"O however does not modify its timeout structure.
719 ¥¿¥¤¥à¥¢¥¦¥È¤ÎÃͤÏ
720 .BR select ()
721 ¤ò¸Æ¤Ö¤¿¤Ó¤Ë½é´ü²½¤¹¤Ù¤­¤Ç¤¢¤ë¡£
722 OS ¤Ë¤è¤Ã¤Æ¤Ï timeout ¹½Â¤ÂΤ¬Êѹ¹¤µ¤ì¤ë¾ì¹ç¤¬¤¢¤ë¤«¤é¤Ç¤¢¤ë¡£
723 ⤷¡¢
724 .BR pselect ()
725 ¤Ï¼«Ê¬¤Î timeout ¹½Â¤ÂΤòÊѹ¹¤¹¤ë¤³¤È¤Ï¤Ê¤¤¡£
726 .TP
727 11.
728 .\"O Since
729 .\"O .BR select ()
730 .\"O modifies its file descriptor sets,
731 .\"O if the call is being used in a loop,
732 .\"O then the sets must be reinitialized before each call.
733 .BR select ()
734 ¤Ï¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤òÊѹ¹¤¹¤ë¤Î¤Ç¡¢
735 .BR select ()
736 ¤¬¥ë¡¼¥×¤ÎÃæ¤Ç»ÈÍѤµ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ë¤Ï¡¢¸Æ¤Ó½Ð¤·¤ò¹Ô¤¦Á°¤ËËè²ó
737 ¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤ò½é´ü²½¤·Ä¾¤µ¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
738 .\" "I have heard" does not fill me with confidence, and doesn't
739 .\" belong in a man page, so I've commented this point out.
740 .\" .TP
741 .\"O .\" 11.
742 .\"O .\" I have heard that the Windows socket layer does not cope with OOB data
743 .\"O .\" properly.
744 .\"O .\" It also does not cope with
745 .\"O .\" .BR select ()
746 .\"O .\" calls when no file descriptors are set at all.
747 .\"O .\" Having no file descriptors set is a useful
748 .\"O .\" way to sleep the process with subsecond precision by using the timeout.
749 .\"O .\" (See further on.)
750 .\" Ê¹¤¤¤¿¤È¤³¤í¤Ç¤Ï¡¢Windows ¤Î¥½¥±¥Ã¥ÈÁؤÏ
751 .\" OOB ¥Ç¡¼¥¿¤òÀµ¤·¤¯½èÍý¤·¤Ê¤¤¤é¤·¤¤¡£
752 .\" ¤Þ¤¿¡¢¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬Á´¤¯¥»¥Ã¥È¤µ¤ì¤Æ¤¤¤Ê¤¤¤è¤¦¤Ê
753 .\" .BR select ()
754 .\" ¥³¡¼¥ë¤âÀµ¤·¤¯½èÍý¤·¤Ê¤¤¤é¤·¤¤¡£
755 .\" ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò°ìÀÚÀßÄꤷ¤Ê¤¤¤Ç timeout ¤ò»È¤¦¤ä¤êÊý¤Ï¡¢
756 .\" 1 Éðʲ¼¤ÎÀºÅÙ¤Ç¥×¥í¥»¥¹¤ò¥¹¥ê¡¼¥×¤µ¤»¤ë¤Ë¤ÏÊØÍø¤ÊÊýË¡¤Ê¤Î¤À¤¬
757 .\" (³¤­¤ò¸«¤è)¡£
758 .\"O .SS Usleep Emulation
759 .SS usleep ¥¨¥ß¥å¥ì¡¼¥·¥ç¥ó
760 .\"O On systems that do not have a
761 .\"O .BR usleep (3)
762 .\"O function, you can call
763 .\"O .BR select ()
764 .\"O with a finite timeout and no file descriptors as
765 .\"O follows:
766 .BR usleep (3)
767 ´Ø¿ô¤ò»ý¤¿¤Ê¤¤¥·¥¹¥Æ¥à¤Ç¤Ï¡¢
768 Í­¸Â¤Î¥¿¥¤¥à¥¢¥¦¥È¤ò»ØÄꤷ¡¢¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÁ´¤¯¥»¥Ã¥È¤»¤º¤Ë
769 .BR select ()
770 ¤ò¸Æ¤Ó½Ð¤¹¤³¤È¤Ç¡¢¤³¤ì¤òÂåÍѤǤ­¤ë¡£
771 °Ê²¼¤Î¤è¤¦¤Ë¤¹¤ë¡£
772 .PP
773 .nf
774     struct timeval tv;
775     tv.tv_sec = 0;
776     tv.tv_usec = 200000;  /* 0.2 seconds */
777     select(0, NULL, NULL, NULL, &tv);
778 .fi
779 .PP
780 .\"O This is only guaranteed to work on UNIX systems, however.
781 ⤷¡¢¤³¤ì¤¬Æ°¤¯¤ÈÊݾڤµ¤ì¤Æ¤¤¤ë¤Î¤Ï UNIX ¥·¥¹¥Æ¥à¤Ë¸Â¤é¤ì¤ë¡£
782 .\"O .SH RETURN VALUE
783 .SH ÊÖ¤êÃÍ
784 .\"O On success,
785 .\"O .BR select ()
786 .\"O returns the total number of file descriptors
787 .\"O still present in the file descriptor sets.
788 À®¸ù¤¹¤ë¤È¡¢
789 .BR select ()
790 ¤Ï¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤Ë»Ä¤Ã¤Æ¤¤¤ë
791 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÎÁí¿ô¤òÊÖ¤¹¡£
792
793 .\"O If
794 .\"O .BR select ()
795 .\"O timed out, then the return value will be zero.
796 .\"O The file descriptors set should be all
797 .\"O empty (but may not be on some systems).
798 .BR select ()
799 ¤¬¥¿¥¤¥à¥¢¥¦¥È¤¹¤ë¤È¡¢ÊÖ¤êÃͤϠ0 ¤Ë¤Ê¤ë¡£
800 ¤½¤Î»þ¡¢¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤Ï¤¹¤Ù¤Æ¶õ¤Ç¤¢¤ë
801 (¤·¤«¤·¤½¤¦¤Ê¤é¤Ê¤¤¥·¥¹¥Æ¥à¤â¤¢¤ë)¡£
802
803 .\"O A return value of \-1 indicates an error, with \fIerrno\fP being
804 .\"O set appropriately.
805 .\"O In the case of an error, the contents of the returned sets and
806 .\"O the \fIstruct timeout\fP contents are undefined and should not be used.
807 .\"O .BR pselect ()
808 .\"O however never modifies \fIntimeout\fP.
809 ÊÖ¤êÃͤ¬ \-1 ¤Î¾ì¹ç¤Ï¥¨¥é¡¼¤ò°ÕÌ£¤·¡¢
810 .I errno
811 ¤¬Å¬Àڤ˥»¥Ã¥È¤µ¤ì¤ë¡£¥¨¥é¡¼¤¬µ¯¤³¤Ã¤¿¾ì¹ç¡¢
812 ÊÖ¤µ¤ì¤¿½¸¹ç¤ÎÆâÍƤ乽¤ÂΠ\fIstruct timeout\fP ¤ÎÆâÍƤÏ
813 ̤ÄêµÁ¤È¤Ê¤Ã¤Æ¤ª¤ê¡¢»ÈÍѤ¹¤Ù¤­¤Ç¤Ï¤Ê¤¤¡£
814 ¤·¤«¤·
815 .BR pselect ()
816 ¤Ï·è¤·¤Æ
817 .I ntimeout
818 ¤òÊѹ¹¤·¤Ê¤¤¡£
819 .\"O .SH NOTES
820 .SH Ãí°Õ
821 .\"O Generally speaking,
822 .\"O all operating systems that support sockets also support
823 .\"O .BR select ().
824 .\"O .BR select ()
825 .\"O can be used to solve
826 .\"O many problems in a portable and efficient way that naive programmers try
827 .\"O to solve in a more complicated manner using
828 .\"O threads, forking, IPCs, signals, memory sharing, and so on.
829 °ìÈÌŪ¤Ë¸À¤Ã¤Æ¡¢¥½¥±¥Ã¥È¤ò¥µ¥Ý¡¼¥È¤¹¤ëÁ´¤Æ¤Î¥ª¥Ú¥ì¡¼¥Æ¥£¥ó¥°¥·¥¹¥Æ¥à¤Ï
830 .BR select ()
831 ¤â¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤ë¡£
832 .BR select ()
833 ¤ò»È¤¦¤È¡¢¥×¥í¥°¥é¥Þ¤¬¥¹¥ì¥Ã¥É¡¢¥Õ¥©¡¼¥¯¡¢IPC¡¢¥·¥°¥Ê¥ë¡¢¥á¥â¥ê¶¦Í­¡¢
834 Åù¡¹¤ò»È¤Ã¤Æ¤â¤Ã¤ÈÊ£»¨¤ÊÊýË¡¤Ç²ò·è¤·¤è¤¦¤È¤¹¤ë¿¤¯¤ÎÌäÂ꤬¡¢
835 °Ü¿¢À­¤¬¤¢¤ê¤«¤Ä¸úΨŪ¤ÊÊýË¡¤Ç²ò·è¤Ç¤­¤ë¡£
836 .PP
837 .\"O The
838 .\"O .BR poll (2)
839 .\"O system call has the same functionality as
840 .\"O .BR select (),
841 .\"O and is somewhat more efficient when monitoring sparse
842 .\"O file descriptor sets.
843 .\"O It is nowadays widely available, but historically was less portable than
844 .\"O .BR select ().
845 .BR poll (2)
846 ¥·¥¹¥Æ¥à¥³¡¼¥ë¤Ï
847 .BR select ()
848 ¤ÈƱ¤¸µ¡Ç½¤ò»ý¤Ã¤Æ¤ª¤ê¡¢
849 ¤Þ¤Ð¤é¤Ê¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤ò´Æ»ë¤¹¤ë¾ì¹ç¤Ë
850 ¤¤¤¯¤é¤«¸úΨ¤¬¤è¤¤¡£
851 ¸½ºß¤Ç¤Ï¹­¤¯ÍøÍѲÄǽ¤Ç¤¢¤ë¤¬¡¢°ÊÁ°¤Ï
852 .BR select ()
853 ¤è¤ê°Ü¿¢À­¤ÎÌ̤ÇÎô¤Ã¤Æ¤¤¤¿¡£
854 .PP
855 .\"O The Linux-specific
856 .\"O .BR epoll (7)
857 .\"O API provides an interface that is more efficient than
858 .\"O .BR select (2)
859 .\"O and
860 .\"O .BR poll (2)
861 .\"O when monitoring large numbers of file descriptors.
862 Linux Æȼ«¤Î
863 .BR epoll (7)
864 API ¤Ï¡¢Â¿¿ô¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò´Æ»ë¤¹¤ë¾ì¹ç¤Ë
865 .BR select (2)
866 ¤ä
867 .BR poll (2)
868 ¤è¤ê¤â¸úΨŪ¤Ê¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤òÄ󶡤·¤Æ¤¤¤ë¡£
869 .\"O .SH EXAMPLE
870 .SH Îã
871 .\"O Here is an example that better demonstrates the true utility of
872 .\"O .BR select ().
873 .\"O The listing below a TCP forwarding program that forwards
874 .\"O from one TCP port to another.
875 .BR select ()
876 ¤ÎËÜÅö¤ËÊØÍø¤ÊÅÀ¤ò¼¨¤¹¡¢¤è¤¤Îã¤ò¾Ò²ð¤¹¤ë¡£
877 °Ê²¼¤Î¥ê¥¹¥È¤Ï¡¢¤¢¤ë TCP ¥Ý¡¼¥È¤«¤éÊ̤Υݡ¼¥È¤ØžÁ÷¤ò¹Ô¤¦
878 TCP ¥Õ¥©¥ï¡¼¥É¥×¥í¥°¥é¥à¤Ç¤¢¤ë¡£
879 .PP
880 .nf
881 #include <stdlib.h>
882 #include <stdio.h>
883 #include <unistd.h>
884 #include <sys/time.h>
885 #include <sys/types.h>
886 #include <string.h>
887 #include <signal.h>
888 #include <sys/socket.h>
889 #include <netinet/in.h>
890 #include <arpa/inet.h>
891 #include <errno.h>
892
893 static int forward_port;
894
895 #undef max
896 #define max(x,y) ((x) > (y) ? (x) : (y))
897
898 static int
899 listen_socket(int listen_port)
900 {
901     struct sockaddr_in a;
902     int s;
903     int yes;
904
905     if ((s = socket(AF_INET, SOCK_STREAM, 0)) == \-1) {
906         perror("socket");
907         return \-1;
908     }
909     yes = 1;
910     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
911             (char *) &yes, sizeof(yes)) == \-1) {
912         perror("setsockopt");
913         close(s);
914         return \-1;
915     }
916     memset(&a, 0, sizeof(a));
917     a.sin_port = htons(listen_port);
918     a.sin_family = AF_INET;
919     if (bind(s, (struct sockaddr *) &a, sizeof(a)) == \-1) {
920         perror("bind");
921         close(s);
922         return \-1;
923     }
924     printf("accepting connections on port %d\\n", listen_port);
925     listen(s, 10);
926     return s;
927 }
928
929 static int
930 connect_socket(int connect_port, char *address)
931 {
932     struct sockaddr_in a;
933     int s;
934
935     if ((s = socket(AF_INET, SOCK_STREAM, 0)) == \-1) {
936         perror("socket");
937         close(s);
938         return \-1;
939     }
940
941     memset(&a, 0, sizeof(a));
942     a.sin_port = htons(connect_port);
943     a.sin_family = AF_INET;
944
945     if (!inet_aton(address, (struct in_addr *) &a.sin_addr.s_addr)) {
946         perror("bad IP address format");
947         close(s);
948         return \-1;
949     }
950
951     if (connect(s, (struct sockaddr *) &a, sizeof(a)) == \-1) {
952         perror("connect()");
953         shutdown(s, SHUT_RDWR);
954         close(s);
955         return \-1;
956     }
957     return s;
958 }
959
960 #define SHUT_FD1 do {                                \\
961                      if (fd1 >= 0) {                 \\
962                          shutdown(fd1, SHUT_RDWR);   \\
963                          close(fd1);                 \\
964                          fd1 = \-1;                   \\
965                      }                               \\
966                  } while (0)
967
968 #define SHUT_FD2 do {                                \\
969                      if (fd2 >= 0) {                 \\
970                          shutdown(fd2, SHUT_RDWR);   \\
971                          close(fd2);                 \\
972                          fd2 = \-1;                   \\
973                      }                               \\
974                  } while (0)
975
976 #define BUF_SIZE 1024
977
978 int
979 main(int argc, char *argv[])
980 {
981     int h;
982     int fd1 = \-1, fd2 = \-1;
983     char buf1[BUF_SIZE], buf2[BUF_SIZE];
984     int buf1_avail, buf1_written;
985     int buf2_avail, buf2_written;
986
987     if (argc != 4) {
988         fprintf(stderr, "Usage\\n\\tfwd <listen-port> "
989                  "<forward-to-port> <forward-to-ip-address>\\n");
990         exit(EXIT_FAILURE);
991     }
992
993     signal(SIGPIPE, SIG_IGN);
994
995     forward_port = atoi(argv[2]);
996
997     h = listen_socket(atoi(argv[1]));
998     if (h == \-1)
999         exit(EXIT_FAILURE);
1000
1001     for (;;) {
1002         int r, nfds = 0;
1003         fd_set rd, wr, er;
1004
1005         FD_ZERO(&rd);
1006         FD_ZERO(&wr);
1007         FD_ZERO(&er);
1008         FD_SET(h, &rd);
1009         nfds = max(nfds, h);
1010         if (fd1 > 0 && buf1_avail < BUF_SIZE) {
1011             FD_SET(fd1, &rd);
1012             nfds = max(nfds, fd1);
1013         }
1014         if (fd2 > 0 && buf2_avail < BUF_SIZE) {
1015             FD_SET(fd2, &rd);
1016             nfds = max(nfds, fd2);
1017         }
1018         if (fd1 > 0 && buf2_avail \- buf2_written > 0) {
1019             FD_SET(fd1, &wr);
1020             nfds = max(nfds, fd1);
1021         }
1022         if (fd2 > 0 && buf1_avail \- buf1_written > 0) {
1023             FD_SET(fd2, &wr);
1024             nfds = max(nfds, fd2);
1025         }
1026         if (fd1 > 0) {
1027             FD_SET(fd1, &er);
1028             nfds = max(nfds, fd1);
1029         }
1030         if (fd2 > 0) {
1031             FD_SET(fd2, &er);
1032             nfds = max(nfds, fd2);
1033         }
1034
1035         r = select(nfds + 1, &rd, &wr, &er, NULL);
1036
1037         if (r == \-1 && errno == EINTR)
1038             continue;
1039
1040         if (r == \-1) {
1041             perror("select()");
1042             exit(EXIT_FAILURE);
1043         }
1044
1045         if (FD_ISSET(h, &rd)) {
1046             unsigned int l;
1047             struct sockaddr_in client_address;
1048
1049             memset(&client_address, 0, l = sizeof(client_address));
1050             r = accept(h, (struct sockaddr *) &client_address, &l);
1051             if (r == \-1) {
1052                 perror("accept()");
1053             } else {
1054                 SHUT_FD1;
1055                 SHUT_FD2;
1056                 buf1_avail = buf1_written = 0;
1057                 buf2_avail = buf2_written = 0;
1058                 fd1 = r;
1059                 fd2 = connect_socket(forward_port, argv[3]);
1060                 if (fd2 == \-1)
1061                     SHUT_FD1;
1062                 else
1063                     printf("connect from %s\\n",
1064                             inet_ntoa(client_address.sin_addr));
1065             }
1066         }
1067
1068         /* NB: read oob data before normal reads */
1069
1070         if (fd1 > 0)
1071             if (FD_ISSET(fd1, &er)) {
1072                 char c;
1073
1074                 r = recv(fd1, &c, 1, MSG_OOB);
1075                 if (r < 1)
1076                     SHUT_FD1;
1077                 else
1078                     send(fd2, &c, 1, MSG_OOB);
1079             }
1080         if (fd2 > 0)
1081             if (FD_ISSET(fd2, &er)) {
1082                 char c;
1083
1084                 r = recv(fd2, &c, 1, MSG_OOB);
1085                 if (r < 1)
1086                     SHUT_FD2;
1087                 else
1088                     send(fd1, &c, 1, MSG_OOB);
1089             }
1090         if (fd1 > 0)
1091             if (FD_ISSET(fd1, &rd)) {
1092                 r = read(fd1, buf1 + buf1_avail,
1093                           BUF_SIZE \- buf1_avail);
1094                 if (r < 1)
1095                     SHUT_FD1;
1096                 else
1097                     buf1_avail += r;
1098             }
1099         if (fd2 > 0)
1100             if (FD_ISSET(fd2, &rd)) {
1101                 r = read(fd2, buf2 + buf2_avail,
1102                           BUF_SIZE \- buf2_avail);
1103                 if (r < 1)
1104                     SHUT_FD2;
1105                 else
1106                     buf2_avail += r;
1107             }
1108         if (fd1 > 0)
1109             if (FD_ISSET(fd1, &wr)) {
1110                 r = write(fd1, buf2 + buf2_written,
1111                            buf2_avail \- buf2_written);
1112                 if (r < 1)
1113                     SHUT_FD1;
1114                 else
1115                     buf2_written += r;
1116             }
1117         if (fd2 > 0)
1118             if (FD_ISSET(fd2, &wr)) {
1119                 r = write(fd2, buf1 + buf1_written,
1120                            buf1_avail \- buf1_written);
1121                 if (r < 1)
1122                     SHUT_FD2;
1123                 else
1124                     buf1_written += r;
1125             }
1126
1127         /* check if write data has caught read data */
1128
1129         if (buf1_written == buf1_avail)
1130             buf1_written = buf1_avail = 0;
1131         if (buf2_written == buf2_avail)
1132             buf2_written = buf2_avail = 0;
1133
1134         /* one side has closed the connection, keep
1135            writing to the other side until empty */
1136
1137         if (fd1 < 0 && buf1_avail \- buf1_written == 0)
1138             SHUT_FD2;
1139         if (fd2 < 0 && buf2_avail \- buf2_written == 0)
1140             SHUT_FD1;
1141     }
1142     exit(EXIT_SUCCESS);
1143 }
1144 .fi
1145 .PP
1146 .\"O The above program properly forwards most kinds of TCP connections
1147 .\"O including OOB signal data transmitted by \fBtelnet\fP servers.
1148 .\"O It handles the tricky problem of having data flow in both directions
1149 .\"O simultaneously.
1150 .\"O You might think it more efficient to use a
1151 .\"O .BR fork (2)
1152 .\"O call and devote a thread to each stream.
1153 .\"O This becomes more tricky than you might suspect.
1154 .\"O Another idea is to set nonblocking I/O using
1155 .\"O .BR fcntl (2).
1156 .\"O This also has its problems because you end up using
1157 .\"O inefficient timeouts.
1158 ¾åµ­¤Î¥×¥í¥°¥é¥à¤Ï¡¢¤Û¤È¤ó¤É¤Î¼ïÎà¤Î TCP Àܳ¤ò¥Õ¥©¥ï¡¼¥É¤¹¤ë¡£
1159 .B telnet
1160 ¥µ¡¼¥Ð¤Ë¤è¤Ã¤ÆÃæ·Ñ¤µ¤ì¤ë OOB ¥·¥°¥Ê¥ë¥Ç¡¼¥¿¤â°·¤¨¤ë¡£
1161 ¤³¤Î¥×¥í¥°¥é¥à¤Ï¡¢¥Ç¡¼¥¿¥Õ¥í¡¼¤òÁÐÊý¸þ¤ËƱ»þ¤ËÁ÷¤ë¤È¤¤¤¦¡¢
1162 ¤ä¤ä¤³¤·¤¤ÌäÂê¤â½èÍý¤Ç¤­¤ë¡£
1163 .BR fork (2)
1164 ¥³¡¼¥ë¤ò»È¤Ã¤Æ¡¢³Æ¥¹¥È¥ê¡¼¥à¤´¤È¤ËÀìÍѤΥ¹¥ì¥Ã¥É¤òÍѤ¤¤ë¤Û¤¦¤¬¸úΨŪ¤À¡¢
1165 ¤È¤¤¤¦¿Í¤â¤¤¤ë¤«¤â¤·¤ì¤Ê¤¤¡£¤·¤«¤·¡¢¤³¤ì¤Ï¹Í¤¨¤Æ¤¤¤ë¤è¤ê¤º¤Ã¤È¤ä¤ä¤³¤·¤¤¡£
1166 ¤¢¤ë¤¤¤Ï¡¢
1167 .BR fcntl (2)
1168 ¤ò»È¤Ã¤ÆÈó¥Ö¥í¥Ã¥¯ I/O ¤ò¥»¥Ã¥È¤¹¤ì¤ÐÎɤ¤¡¢¤È¤¤¤¦¥¢¥¤¥Ç¥¢¤â¤¢¤ë¤À¤í¤¦¡£
1169 ¤³¤ì¤Ë¤â¼ÂºÝ¤Ë¤ÏÌäÂ꤬¤¢¤ê¡¢¥¿¥¤¥à¥¢¥¦¥È¤¬Èó¸úΨŪ¤Ëµ¯¤³¤Ã¤Æ¤·¤Þ¤¦¡£
1170
1171 .\"O The program does not handle more than one simultaneous connection at a
1172 .\"O time, although it could easily be extended to do this with a linked list
1173 .\"O of buffers \(em one for each connection.
1174 .\"O At the moment, new
1175 .\"O connections cause the current connection to be dropped.
1176 ¤³¤Î¥×¥í¥°¥é¥à¤Ï°ìÅ٤ˤҤȤİʾå¤ÎƱ»þÀܳ¤ò°·¤¦¤³¤È¤Ï¤Ç¤­¤Ê¤¤¤¬¡¢
1177 ¤½¤ÎÍͤ˳ÈÄ¥¤¹¤ë¤Î¤Ï´Êñ¤Ç¡¢¥Ð¥Ã¥Õ¥¡¤Î¥ê¥ó¥¯¥ê¥¹¥È¤ò
1178 (Àܳ¤´¤È¤Ë¤Ò¤È¤Ä¤º¤Ä) »È¤¨¤Ð¤è¤¤¡£
1179 ¸½»þÅÀ¤Î¤â¤Î¤Ç¤Ï¡¢¿·¤·¤¤Àܳ¤¬¤¯¤ë¤È¸Å¤¤Àܳ¤ÏÍî¤Á¤Æ¤·¤Þ¤¦¡£
1180 .\"O .SH SEE ALSO
1181 .SH ´ØÏ¢¹àÌÜ
1182 .BR accept (2),
1183 .BR connect (2),
1184 .BR ioctl (2),
1185 .BR poll (2),
1186 .BR read (2),
1187 .BR recv (2),
1188 .BR select (2),
1189 .BR send (2),
1190 .BR sigprocmask (2),
1191 .BR write (2),
1192 .BR sigaddset (3),
1193 .BR sigdelset (3),
1194 .BR sigemptyset (3),
1195 .BR sigfillset (3),
1196 .BR sigismember (3),
1197 .BR epoll (7)
1198 .\" .SH AUTHORS
1199 .\" This man page was written by Paul Sheer.