1 .\" This manpage is copyright (C) 2001 Paul Sheer.
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.
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.
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
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" very minor changes, aeb
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
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
36 .\"WORD: file descriptor ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿
37 .\"WORD: exceptional condition Îã³°¾õÂÖ
39 .TH SELECT_TUT 2 2009-01-26 "Linux" "Linux Programmer's Manual"
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 ¤Î¿½Å²½
48 .\"O /* According to POSIX.1-2001 */
49 /* POSIX.1-2001 ¤Ë½¾¤¦¾ì¹ç */
51 .B #include <sys/select.h>
53 .\"O /* According to earlier standards */
54 /* °ÊÁ°¤Îµ¬³Ê¤Ë½¾¤¦¾ì¹ç */
56 .B #include <sys/time.h>
58 .B #include <sys/types.h>
60 .B #include <unistd.h>
62 .BI "int select(int " nfds ", fd_set *" readfds ", fd_set *" writefds ,
63 .BI " fd_set *" exceptfds ", struct timeval *" utimeout );
65 .BI "void FD_CLR(int " fd ", fd_set *" set );
67 .BI "int FD_ISSET(int " fd ", fd_set *" set );
69 .BI "void FD_SET(int " fd ", fd_set *" set );
71 .BI "void FD_ZERO(fd_set *" set );
73 .B #include <sys/select.h>
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 );
81 .\"O Feature Test Macro Requirements for glibc (see
82 .\"O .BR feature_test_macros (7)):
83 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
84 .RB ( feature_test_macros (7)
89 _POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600
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.
102 ¤ò»È¤¦¤È¡¢¸úΨŪ¤ËÊ£¿ô¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò´Æ»ë¤·¡¢
103 ¤½¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¤¤¤º¤ì¤«¤¬
104 ¡Öready (½àÈ÷¤¬¤Ç¤¤¿)¡×¾õÂÖ¡¢¤Ä¤Þ¤ê I/O (Æþ½ÐÎÏ) ¤¬²Äǽ¤Ë¤Ê¤Ã¤Æ¤¤¤ë¤«¤ä¡¢
105 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¤¤¤º¤ì¤«¤¬
106 ¡ÖÎã³°¾õÂÖ (exceptional condition)¡×¤¬È¯À¸¤·¤¿¤«¡¢¤òÄ´¤Ù¤ë¤³¤È¤¬¤Ç¤¤ë¡£
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
114 .\"O and its contents can be manipulated with the macros
116 .\"O .BR FD_ISSET (),
120 .\"O A newly declared set should first be cleared using
124 ¤È¤·¤ÆÀë¸À¤µ¤ì¡¢¤½¤ÎÆâÍƤÏ
129 ¤È¤¤¤Ã¤¿¥Þ¥¯¥í¤Ë¤è¤Ã¤ÆÁàºî¤Ç¤¤ë¡£
130 ¿·¤·¤¯Àë¸À¤µ¤ì¤¿½¸¹ç¤Ï¡¢¤Þ¤ººÇ½é¤Ë
132 ¤ò»È¤Ã¤Æ¥¯¥ê¥¢¤¹¤Ù¤¤Ç¤¢¤ë¡£
134 .\"O modifies the contents of the sets according to the rules
135 .\"O described below; after calling
137 .\"O you can test if a file descriptor is still present in a set with the
141 .\"O returns nonzero if a specified file descriptor is present in a set
142 .\"O and zero if it is not.
144 .\"O removes a file descriptor from a set.
146 ¤Ï¤³¤ì¤é¤Î½¸¹ç¤ÎÆâÍƤò¡¢°Ê¹ß¤Ë½Ò¤Ù¤ëµ¬Â§¤Ë½¾¤Ã¤Æ½¤Àµ¤¹¤ë¡£
148 ¤ò¸Æ¤ó¤À¸å¡¢¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬¤Þ¤À½¸¹ç¤Ë¸ºß¤·¤Æ¤¤¤ë¤«¤É¤¦¤«¤Ï¡¢
150 ¥Þ¥¯¥í¤Ë¤è¤Ã¤ÆÄ´¤Ù¤ë¤³¤È¤¬¤Ç¤¤ë¡£
152 ¤Ï»ØÄꤵ¤ì¤¿¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬½¸¹ç¤Ë¸ºß¤·¤Æ¤¤¤ì¤Ð 0 °Ê³°¤ÎÃͤòÊÖ¤·¡¢
153 ¸ºß¤·¤Ê¤±¤ì¤Ð 0 ¤òÊÖ¤¹¡£
155 ¤Ï½¸¹ç¤«¤é¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Îºï½ü¤ò¹Ô¤¦¡£
160 .\"O This set is watched to see if data is available for reading from any of
161 .\"O its file descriptors.
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 ¥Ç¡¼¥¿¤ÎÆɤ߹þ¤ß¤¬²Äǽ¤Ë¤Ê¤Ã¤¿¤«¤É¤¦¤«¤ò´Æ»ë¤¹¤ë¡£
170 ¤«¤éÌá¤ë»þ¤Ë¡¢\fIreadfds\fP ¤Î¤¦¤Á¡¢
171 ľ¤Á¤ËÆɤ߹þ¤ß²Äǽ¤Ê¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿°Ê³°¤Ï
175 .\"O This set is watched to see if there is space to write data to any of
176 .\"O its file descriptors.
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 ¥Ç¡¼¥¿¤ò½ñ¤¹þ¤à¥¹¥Ú¡¼¥¹¤¬¤¢¤ë¤«¤É¤¦¤«¤ò´Æ»ë¤¹¤ë¡£
185 ¤«¤éÌá¤ë»þ¤Ë¡¢\fIwritefds\fP ¤Î¤¦¤Á¡¢
186 ľ¤Á¤Ë½ñ¤¹þ¤ß²Äǽ¤Ê¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿°Ê³°¤Ï
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.
199 .\"O for more details about OOB data.
200 .\"O (One other less common case where
202 .\"O indicates an exceptional condition occurs with pseudo-terminals
203 .\"O in packet mode; see
204 .\"O .BR tty_ioctl (4).)
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 ¥Ç¡¼¥¿¤Î¾ÜºÙ¤Ë¤Ä¤¤¤Æ¤Ï¡¢
220 (¤³¤ì°Ê³°¤Ç¤Ï¡¢¤Þ¤ì¤Ê¤³¤È¤À¤¬¡¢
221 ¥Ñ¥±¥Ã¥È¥â¡¼¥É¤Îµ¼»÷üËö (pseudo-terminals) ¤Ç
223 ¤¬Îã³°¾õÂÖ¤ò¼¨¤¹¤³¤È¤¬¤¢¤ë¡£)
225 ¤¬ÊÖ¤ë»þ¤Ë¡¢\fIexceptfds\fP ¤Î¤¦¤Á¡¢
226 Îã³°¾õÂÖ¤¬È¯À¸¤·¤¿¥Ç¥£¥¹¥¯¥ê¥×¥¿°Ê³°¤Ï½¸¹ç¤«¤éºï½ü¤µ¤ì¤ë¡£
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 ¤È¤·¤ÆÅϤµ¤Ê¤¤¤È¤¤¤±¤Ê¤¤¡¢¤È¤¤¤¦¤³¤È¤À¡£
241 .\"O This is the longest time
243 .\"O may wait before returning, even if nothing interesting happened.
244 .\"O If this value is passed as NULL, then
246 .\"O blocks indefinitely waiting for a file descriptor to become ready.
247 .\"O \fIutimeout\fP can be set to zero seconds, which causes
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 (²¿¤âµ¯¤³¤é¤Ê¤«¤Ã¤¿¾ì¹ç¤Ë)
254 ¤¬Ìá¤ëÁ°¤ËÂԤĺÇÂç»þ´Ö¤Ç¤¢¤ë¡£
255 ¤³¤ÎÃÍ¤Ë NULL ¤òÅϤ¹¤È¡¢
257 ¤Ï¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¤¤¤º¤ì¤«¤¬ ready (½àÈ÷¤¬¤Ç¤¤¿) ¾õÂÖ¤Ë
258 ¤Ê¤ë¤Þ¤ÇÂÔ¤Á³¤±¤Æ¤º¤Ã¤ÈÄä»ß¤¹¤ë¡£
259 \fIutimeout\fP ¤Ï 0 Éäˤ¹¤ë¤³¤È¤â¤Ç¤¡¢
262 ¤Ïľ¤Á¤ËÊ֤ꡢ¸Æ¤Ó½Ð¤·»þÅÀ¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¾õÂÖ¤Ë
263 ´Ø¤¹¤ë¾ðÊó¤¬ÊÖ¤µ¤ì¤ë¡£
264 ¹½Â¤ÂÎ \fIstruct timeval\fP ¤Ï¼¡¤Î¤è¤¦¤ËÄêµÁ¤µ¤ì¤Æ¤¤¤ë:
269 time_t tv_sec; /* seconds */
270 long tv_usec; /* microseconds */
276 .\"O This argument for
278 .\"O has the same meaning as
281 .\"O .I "struct timespec"
282 .\"O has nanosecond precision as follows:
288 ¤Ï¼¡¤Ë¼¨¤¹¤è¤¦¤Ë¥Ê¥ÎÉäÎÀºÅÙ¤ò»ý¤Ä¡£
292 long tv_sec; /* seconds */
293 long tv_nsec; /* nanoseconds */
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
304 .\"O .BR sigaddset (3)
306 .\"O .BR sigprocmask (2)).
308 .\"O in which case the call does not modify the signal mask on
309 .\"O entry and exit to the function.
312 .\"O will then behave just like
314 ¤³¤Î°ú¤¿ô¤Ï¡¢¸Æ¤Ó½Ð¤·Â¦¤¬
316 ÆâÉô¤ÇÄä»ß¤·¤Æ¤¤¤ë´Ö¤Ë¡¢¥«¡¼¥Í¥ë¤¬ÄÌÃΤòµö²Ä¤¹¤Ù¤¥·¥°¥Ê¥ë½¸¹ç
317 (¤¹¤Ê¤ï¤Á¡¢¸Æ¤Ó½Ð¤·¤¿¥¹¥ì¥Ã¥É¤Î¥·¥°¥Ê¥ë¥Þ¥¹¥¯¤«¤éºï½ü¤¹¤Ù¤¥·¥°¥Ê¥ë½¸¹ç)
323 ¤³¤Î°ú¤¿ô¤Ï NULL ¤Ë¤¹¤ë¤³¤È¤â¤Ç¤¡¢¤½¤Î¾ì¹ç¤Ï¤³¤Î´Ø¿ô¤Ø
324 Æþ¤ë¤È¤¡¦½Ð¤ë¤È¤¤Ë¥·¥°¥Ê¥ë¥Þ¥¹¥¯¤òÊѹ¹¤·¤Ê¤¤¡£
329 ¤ÈÁ´¤¯Æ±¤¸Æ°ºî¤È¤Ê¤ë¡£
330 .\"O .SS Combining Signal and Data Events
331 .SS ¥·¥°¥Ê¥ë¤È¥Ç¡¼¥¿¥¤¥Ù¥ó¥È¤òÁȤ߹ç¤ï¤»¤ë
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 ¥·¥°¥Ê¥ë¤âÂÔ¤Á¤¿¤¤¾ì¹ç¤Ë¤Ï¡¢
343 ¥·¥°¥Ê¥ë¤ò¼õ¿®¤¹¤ë¥×¥í¥°¥é¥à¤Ï¡¢Ä̾ï¤Ï¡¢
344 ¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤ò¥°¥í¡¼¥Ð¥ë¤Ê¥Õ¥é¥°¤òΩ¤Æ¤ë¤¿¤á¤À¤±¤Ë»È¤¦¡£
345 ¤³¤Î¥°¥í¡¼¥Ð¥ë¤Ê¥Õ¥é¥°¤Ï¡¢
346 ¤½¤Î¥¤¥Ù¥ó¥È¤ò¥×¥í¥°¥é¥à¤Î¥á¥¤¥ó¥ë¡¼¥×¤Ç
347 ½èÍý¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¤³¤È¤ò¼¨¤¹¡£
348 .\"O A signal will cause the
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
356 .\"O would block indefinitely.
362 \fIerrno\fP ¤Ë \fBEINTR\fP ¤ò¥»¥Ã¥È¤·¤ÆÌá¤ë¤³¤È¤Ë¤Ê¤ë¡£
363 ¥·¥°¥Ê¥ë¤¬¥×¥í¥°¥é¥à¤Î¥á¥¤¥ó¥ë¡¼¥×¤Ç½èÍý¤µ¤ì¤ë¤¿¤á¤Ë¤Ï¤³¤ÎÆ°ºî¤¬ÉԲķç¤Ç¡¢
366 ¤Ï±Ê±ó¤ËÄä»ß¤·Â³¤±¤ë¤³¤È¤Ë¤Ê¤ë¡£
368 .\"O in the main loop will be a conditional to check the global flag.
370 .\"O what if a signal arrives after the conditional, but before the
373 .\"O The answer is that
375 .\"O would block indefinitely, even though an event is actually pending.
376 ¤µ¤Æ¡¢¥á¥¤¥ó¥ë¡¼¥×¤Î¤É¤³¤«¤Ë¤³¤Î¥°¥í¡¼¥Ð¥ë¥Õ¥é¥°¤ò¥Á¥§¥Ã¥¯¤¹¤ë
377 ¾ò·ïʸ¤¬¤¢¤ë¤È¤·¤è¤¦¡£¤³¤³¤Ç¾¯¤·¹Í¤¨¤Æ¤ß¤Ê¤¤¤È¤¤¤±¤Ê¤¤¡£
378 ¡Ö¥·¥°¥Ê¥ë¤¬¾ò·ïʸ¤Î¸å¡¢¤·¤«¤·
380 ¥³¡¼¥ë¤ÎÁ°¤ËÅþÃ夷¤¿¤é
384 ¤Ï¡¢¤¿¤È¤¨²ò·èÂÔ¤Á¤Î¥¤¥Ù¥ó¥È¤¬¤¢¤Ã¤¿¤È¤·¤Æ¤â¡¢
385 ±Ê±ó¤ËÄä»ß¤¹¤ë¡×¤Ç¤¢¤ë¡£
386 .\"O This race condition is solved by the
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
395 ¥³¡¼¥ë¤Ë¤è¤Ã¤Æ²ò·è¤Ç¤¤ë¡£
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).
406 .\"O call would enable
408 .\"O by using an empty signal mask.
409 .\"O Our program would look like:
410 Î㤨¤Ð¡¢ÌäÂê¤È¤Ê¤Ã¤Æ¤¤¤ë¥¤¥Ù¥ó¥È¤¬»Ò¥×¥í¥»¥¹¤Î½ªÎ»¤Î¾ì¹ç¤ò¹Í¤¨¤è¤¦¡£
411 ¥á¥¤¥ó¥ë¡¼¥×¤¬»Ï¤Þ¤ëÁ°¤Ë¡¢
419 ¤ò¡¢¤â¤È¤â¤È¤Î¥·¥°¥Ê¥ë¥Þ¥¹¥¯¤ò»È¤Ã¤Æ͸ú¤Ë¤¹¤ë¤Î¤À¡£
420 ¤³¤Î¥×¥í¥°¥é¥à¤Ï¼¡¤Î¤è¤¦¤Ë¤Ê¤ë¡£
423 static volatile sig_atomic_t got_SIGCHLD = 0;
426 child_sig_handler(int sig)
432 main(int argc, char *argv[])
434 sigset_t sigmask, empty_mask;
436 fd_set readfds, writefds, exceptfds;
439 sigemptyset(&sigmask);
440 sigaddset(&sigmask, SIGCHLD);
441 if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == \-1) {
442 perror("sigprocmask");
447 sa.sa_handler = child_sig_handler;
448 sigemptyset(&sa.sa_mask);
449 if (sigaction(SIGCHLD, &sa, NULL) == \-1) {
454 sigemptyset(&empty_mask);
456 for (;;) { /* main loop */
457 /* Initialize readfds, writefds, and exceptfds
458 before the pselect() call. (Code omitted.) */
460 r = pselect(nfds, &readfds, &writefds, &exceptfds,
462 if (r == \-1 && errno != EINTR) {
469 /* Handle signalled event here; e.g., wait() for all
470 terminated children. (Code omitted.) */
473 /* main body of program */
479 .\"O So what is the point of
481 .\"O Can't I just read and write to my descriptors whenever I want?
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
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.
498 .\"O efficiently copes with this situation.
501 ¤ÎÂç»ö¤ÊÅÀ¤Ï²¿¤Ê¤Î¤«¡©
502 ¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ï¹¥¤¤Ê¤È¤¤ËÆɤ߽ñ¤¤Ç¤¤ë¤ó¤¸¤ã¤Ê¤¤¤Î¡©
504 ¤Î½ÅÍפʤȤ³¤í¤Ï¡¢Ê£¿ô¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òƱ»þ¤Ë´Æ»ë¤Ç¤¡¢
505 ¤Ê¤ó¤ÎÆ°¤¤â¤Ê¤±¤ì¤Ð¥×¥í¥»¥¹¤òŬÀڤ˥¹¥ê¡¼¥×¾õÂ֤˰ܹԤ¹¤ë¤È¤³¤í¤Ë¤¢¤ë¤Î¤À¡£
507 Ê£¿ô¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÎÆþ½ÐÎϤòƱ»þ¤Ë°·¤ï¤Í¤Ð¤Ê¤é¤º¡¢
508 ¤·¤«¤â¥Ç¡¼¥¿¤Îή¤ì¤Ï´Ö·çŪ¤Ç¤¢¤ë¡¢¤È¤¤¤¦¾õ¶·¤Ë¤è¤¯½Ð²ñ¤¦¡£
513 ¥³¡¼¥ë¤Î¥·¡¼¥±¥ó¥¹¤òºî¤ë¤À¤±¤Ç¤Ï¡¢¤½¤ì¤é¤Î¥³¡¼¥ë¤Î¤É¤ì¤«¤¬
514 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤«¤é¤Î¥Ç¡¼¥¿¤òÂԤäƥ֥í¥Ã¥¯¤·¤Æ¤ª¤ê¡¢
515 Ê̤Υե¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ë¤Ï I/O ¤¬²Äǽ¤Ê¤Î¤Ë»È¤¨¤Ê¤¤¡¢
516 ¤È¤¤¤¦¤³¤È¤Ë¤Ê¤Ã¤Æ¤·¤Þ¤¦¤À¤í¤¦¡£
518 ¤ò»È¤¦¤È¤³¤Î¾õ¶·¤Ë¸ú²ÌŪ¤ËÂнè¤Ç¤¤ë¡£
521 .\"O Many people who try to use
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
531 .\"O so here is a list of essentials to watch for when using
534 ¤ò»È¤ª¤¦¤È¤·¤¿Â¿¤¯¤Î¿Í¤Ï¡¢Íý²ò¤·¤Ë¤¯¤¤µóÆ°¤Ë½Ð¤¯¤ï¤·¡¢·ë²ÌŪ¤Ë
535 ¤Ç¤¤¿¤â¤Î¤Ï°Ü¿¢À¤¬¤Ê¤¤¤«¡¢¤è¤¯¤Æ¤â¥®¥ê¥®¥ê¤Î¤â¤Î¤Ë¤Ê¤Ã¤Æ¤·¤Þ¤¦¡£
536 Î㤨¤Ð¡¢¾åµ¤Î¥×¥í¥°¥é¥à¤Ï¡¢
537 ½¸¹ç¤Ë´Þ¤Þ¤ì¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÈóÄä»ß (nonblocking) ¥â¡¼¥É
538 ¤Ë¤·¤Ê¤¯¤Æ¤â¡¢¤É¤³¤Ë¤â¥Ö¥í¥Ã¥¯¤¬À¸¤¸¤Ê¤¤¤è¤¦Ãí°Õ¤·¤Æ½ñ¤«¤ì¤Æ¤¤¤ë¡£
539 Èù̯¤Ê´Ö°ã¤¤¤Ë¤è¤Ã¤Æ¡¢
541 ¤ò»È¤¦ÍøÅÀ¤Ï´Êñ¤Ë¼º¤ï¤ì¤Æ¤·¤Þ¤¦¡£
544 ¥³¡¼¥ë¤ò»È¤¦¤È¤¤ËÃí°Õ¤¹¤Ù¤½ÅÍ×»ö¹à¤òÎóµó¤·¤Æ¤ª¤¯¤³¤È¤Ë¤¹¤ë¡£
547 .\"O You should always try to use
549 .\"O without a timeout.
551 .\"O should have nothing to do if there is no data available.
553 .\"O depends on timeouts is not usually portable and is difficult to debug.
555 ¤ò»È¤¦¤È¤¤Ï¡¢¥¿¥¤¥à¥¢¥¦¥È¤ÏÀßÄꤹ¤Ù¤¤Ç¤Ê¤¤¡£
556 ½èÍý¤¹¤ë¥Ç¡¼¥¿¤¬Ìµ¤¤¤È¤¤Ë¤Ï¡¢
557 ¤¢¤Ê¤¿¤Î¥×¥í¥°¥é¥à¤Ë¤Ï²¿¤â¤¹¤ë¤³¤È¤Ï̵¤¤¤Ï¤º¤Ç¤¢¤ë¡£
558 ¥¿¥¤¥à¥¢¥¦¥È¤Ë°Í¸¤·¤¿¥³¡¼¥É¤ÏÄ̾ï°Ü¿¢À¤¬¤Ê¤¯¡¢
559 ¥Ç¥Ð¥Ã¥°¤âÆñ¤·¤¯¤Ê¤ë¡£
562 .\"O The value \fInfds\fP must be properly calculated for efficiency as
563 .\"O explained above.
565 ¸úΨŪ¤Ê¥×¥í¥°¥é¥à¤ò½ñ¤¯¤Ë¤Ï
567 ¤ÎÃͤòŬÀڤ˷׻»¤·¤ÆÍ¿¤¨¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
570 .\"O No file descriptor must be added to any set if you do not intend
571 .\"O to check its result after the
573 .\"O call, and respond appropriately.
576 ¥³¡¼¥ë¤Î½ªÎ»¸å¤Ë·ë²Ì¤ò¥Á¥§¥Ã¥¯¤·¤Æ¡¢
577 ŬÀÚ¤ËÂбþ¤¹¤ë¤Ä¤â¤ê¤Î¤Ê¤¤¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ï¡¢
578 ¤É¤Î½¸¹ç¤Ë¤â²Ã¤¨¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£
584 .\"O returns, all file descriptors in all sets
585 .\"O should be checked to see if they are ready.
587 ¤«¤éÊ֤俸å¤Ë¤Ï¡¢Á´¤Æ¤Î½¸¹ç¤ÎÁ´¤Æ¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ë¤Ä¤¤¤Æ
588 Æɤ߽ñ¤²Äǽ¤Ê¾õÂ֤ˤʤäƤ¤¤ë¤«¤ò¥Á¥§¥Ã¥¯¤¹¤Ù¤¤Ç¤¢¤ë¡£
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.
608 ¤È¤¤¤Ã¤¿´Ø¿ô¤Ï¡¢¤³¤Á¤é¤¬Í׵ᤷ¤¿Á´¥Ç¡¼¥¿¤òÆɤ߽ñ¤¤¹¤ëɬÍפÏ\fI¤Ê¤¤\fP¡£
609 ¤â¤·Á´¥Ç¡¼¥¿¤òÆɤ߽ñ¤¤¹¤ë¤Ê¤é¡¢¤½¤ì¤Ï¥È¥é¥Õ¥£¥Ã¥¯¤ÎÉé²Ù¤¬¾®¤µ¤¯¡¢
610 ¥¹¥È¥ê¡¼¥à¤¬Â®¤¤¾ì¹ç¤À¤í¤¦¡£¤³¤Î¾ò·ï¤Ï¾ï¤ËËþ¤¿¤µ¤ì¤ë¤È¤Ï¸Â¤é¤Ê¤¤¡£
611 ¤³¤ì¤é¤Î´Ø¿ô¤¬´èÄ¥¤Ã¤Æ¤â 1 ¥Ð¥¤¥È¤·¤«Á÷¼õ¿®¤Ç¤¤Ê¤¤¤è¤¦¤Ê¾ì¹ç¤â
612 ¹Íθ¤ËÆþ¤ì¤Æ¤ä¤é¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
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.
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 ¤³¤Î¥µ¥¤¥º¤òÂ礤¯¤¹¤ë¤Î¤Ï´Êñ¤À¤í¤¦¡£
636 .\"O call can return \-1 with
638 .\"O set to \fBEINTR\fP,
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.
657 .B EINTR " ¤ä " EAGAIN
659 ¤Ë¤·¤Æ \-1 ¤òÊÖ¤¹¤³¤È¤¬¤¢¤ë¡£
660 ¤³¤Î¤è¤¦¤Ê·ë²Ì¤ËÂФ·¤ÆŬÀÚ¤ËÂбþ¤·¤Æ¤ä¤é¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤
661 (¾åµ¤ÎÎã¤Ç¤Ï¤·¤Æ¤¤¤Ê¤¤)¡£
662 ½ñ¤¤¤Æ¤¤¤ë¥×¥í¥°¥é¥à¤¬¥·¥°¥Ê¥ë¤ò¼õ¤±¤ëͽÄ꤬¤Ê¤±¤ì¤Ð¡¢
664 ¤¬ÊÖ¤µ¤ì¤ë¤³¤È¤Ï¤¢¤Þ¤ê¹Í¤¨¤é¤ì¤Ê¤¤¡£
665 ½ñ¤¤¤Æ¤¤¤ë¥×¥í¥°¥é¥à¤ÇÈó¥Ö¥í¥Ã¥¯ I/O ¤ò¥»¥Ã¥È¤·¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¡¢
667 ¤¬ÊÖ¤µ¤ì¤ë¤³¤È¤Ï¤Ê¤¤¤À¤í¤¦¡£
668 .\" ¤½¤ì¤Ç¤â¤Ê¤ª¡¢´°Á´¤ò´ü¤¹¤ë¤Ê¤é¤Ð¡¢
669 .\" ¤³¤ì¤é¤Î¥¨¥é¡¼¤ò¹Íθ¤ËÆþ¤ì¤ëɬÍפ¬¤¢¤ë¡£
678 .\"O with a buffer length of zero.
679 ·è¤·¤Æ¡¢°ú¤¿ô¤ËŤµ 0 ¤Î¥Ð¥Ã¥Õ¥¡¤ò»ØÄꤷ¤Æ
684 ¤ò¸Æ¤Ó½Ð¤·¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£
687 .\"O If the functions
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
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.
706 \fB7.\fP ¤Ë¼¨¤·¤¿°Ê³°¤Î¥¨¥é¡¼¤Ç¼ºÇÔ¤·¤¿¾ì¹ç¤ä¡¢
707 ÆþÎϷϤδؿô¤Î°ì¤Ä¤¬¥Õ¥¡¥¤¥ëËöÈø¤òɽ¤¹ 0 ¤òÊÖ¤·¤¿¾ì¹ç¤Ï¡¢
708 ¤½¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò¤â¤¦°ìÅÙ select ¤ËÅϤ·¤Æ¤Ï\fI¤Ê¤é¤Ê¤¤\fP¡£
709 ²¼µ¤ÎÎã¤Ç¤Ï¡¢¤½¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò¤¿¤À¤Á¤Ë¥¯¥í¡¼¥º¤·¡¢
710 ¤½¤³¤Ë¤Ï \-1 ¤ò¥»¥Ã¥È¤·¤Æ¡¢
711 ¤½¤ì¤¬½¸¹ç¤Ë´Þ¤Þ¤ì³¤±¤ë¤Î¤òµö¤µ¤Ê¤¤¤è¤¦¤Ë¤·¤Æ¤¤¤ë¡£
714 .\"O The timeout value must be initialized with each new call to
716 .\"O since some operating systems modify the structure.
718 .\"O however does not modify its timeout structure.
721 ¤ò¸Æ¤Ö¤¿¤Ó¤Ë½é´ü²½¤¹¤Ù¤¤Ç¤¢¤ë¡£
722 OS ¤Ë¤è¤Ã¤Æ¤Ï timeout ¹½Â¤ÂΤ¬Êѹ¹¤µ¤ì¤ë¾ì¹ç¤¬¤¢¤ë¤«¤é¤Ç¤¢¤ë¡£
725 ¤Ï¼«Ê¬¤Î timeout ¹½Â¤ÂΤòÊѹ¹¤¹¤ë¤³¤È¤Ï¤Ê¤¤¡£
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.
734 ¤Ï¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤òÊѹ¹¤¹¤ë¤Î¤Ç¡¢
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.
742 .\"O .\" I have heard that the Windows socket layer does not cope with OOB data
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 .\" ¤Þ¤¿¡¢¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬Á´¤¯¥»¥Ã¥È¤µ¤ì¤Æ¤¤¤Ê¤¤¤è¤¦¤Ê
754 .\" ¥³¡¼¥ë¤âÀµ¤·¤¯½èÍý¤·¤Ê¤¤¤é¤·¤¤¡£
755 .\" ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò°ìÀÚÀßÄꤷ¤Ê¤¤¤Ç timeout ¤ò»È¤¦¤ä¤êÊý¤Ï¡¢
756 .\" 1 Éðʲ¼¤ÎÀºÅÙ¤Ç¥×¥í¥»¥¹¤ò¥¹¥ê¡¼¥×¤µ¤»¤ë¤Ë¤ÏÊØÍø¤ÊÊýË¡¤Ê¤Î¤À¤¬
758 .\"O .SS Usleep Emulation
759 .SS usleep ¥¨¥ß¥å¥ì¡¼¥·¥ç¥ó
760 .\"O On systems that do not have a
762 .\"O function, you can call
764 .\"O with a finite timeout and no file descriptors as
767 ´Ø¿ô¤ò»ý¤¿¤Ê¤¤¥·¥¹¥Æ¥à¤Ç¤Ï¡¢
768 ͸¤Υ¿¥¤¥à¥¢¥¦¥È¤ò»ØÄꤷ¡¢¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÁ´¤¯¥»¥Ã¥È¤»¤º¤Ë
770 ¤ò¸Æ¤Ó½Ð¤¹¤³¤È¤Ç¡¢¤³¤ì¤òÂåÍѤǤ¤ë¡£
776 tv.tv_usec = 200000; /* 0.2 seconds */
777 select(0, NULL, NULL, NULL, &tv);
780 .\"O This is only guaranteed to work on Unix systems, however.
781 ⤷¡¢¤³¤ì¤¬Æ°¤¯¤ÈÊݾڤµ¤ì¤Æ¤¤¤ë¤Î¤Ï Unix ¥·¥¹¥Æ¥à¤Ë¸Â¤é¤ì¤ë¡£
782 .\"O .SH RETURN VALUE
786 .\"O returns the total number of file descriptors
787 .\"O still present in the file descriptor sets.
790 ¤Ï¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤Ë»Ä¤Ã¤Æ¤¤¤ë
791 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÎÁí¿ô¤òÊÖ¤¹¡£
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).
799 ¤¬¥¿¥¤¥à¥¢¥¦¥È¤¹¤ë¤È¡¢ÊÖ¤êÃÍ¤Ï 0 ¤Ë¤Ê¤ë¡£
800 ¤½¤Î»þ¡¢¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤Ï¤¹¤Ù¤Æ¶õ¤Ç¤¢¤ë
801 (¤·¤«¤·¤½¤¦¤Ê¤é¤Ê¤¤¥·¥¹¥Æ¥à¤â¤¢¤ë)¡£
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.
808 .\"O however never modifies \fIntimeout\fP.
809 ÊÖ¤êÃͤ¬ \-1 ¤Î¾ì¹ç¤Ï¥¨¥é¡¼¤ò°ÕÌ£¤·¡¢
811 ¤¬Å¬Àڤ˥»¥Ã¥È¤µ¤ì¤ë¡£¥¨¥é¡¼¤¬µ¯¤³¤Ã¤¿¾ì¹ç¡¢
812 ÊÖ¤µ¤ì¤¿½¸¹ç¤ÎÆâÍƤ乽¤ÂÎ \fIstruct timeout\fP ¤ÎÆâÍƤÏ
813 ̤ÄêµÁ¤È¤Ê¤Ã¤Æ¤ª¤ê¡¢»ÈÍѤ¹¤Ù¤¤Ç¤Ï¤Ê¤¤¡£
821 .\"O Generally speaking,
822 .\"O all operating systems that support sockets also support
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 °ìÈÌŪ¤Ë¸À¤Ã¤Æ¡¢¥½¥±¥Ã¥È¤ò¥µ¥Ý¡¼¥È¤¹¤ëÁ´¤Æ¤Î¥ª¥Ú¥ì¡¼¥Æ¥£¥ó¥°¥·¥¹¥Æ¥à¤Ï
833 ¤ò»È¤¦¤È¡¢¥×¥í¥°¥é¥Þ¤¬¥¹¥ì¥Ã¥É¡¢¥Õ¥©¡¼¥¯¡¢IPC¡¢¥·¥°¥Ê¥ë¡¢¥á¥â¥ê¶¦Í¡¢
834 Åù¡¹¤ò»È¤Ã¤Æ¤â¤Ã¤ÈÊ£»¨¤ÊÊýË¡¤Ç²ò·è¤·¤è¤¦¤È¤¹¤ë¿¤¯¤ÎÌäÂ꤬¡¢
835 °Ü¿¢À¤¬¤¢¤ê¤«¤Ä¸úΨŪ¤ÊÊýË¡¤Ç²ò·è¤Ç¤¤ë¡£
839 .\"O system call has the same functionality as
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
848 ¤ÈƱ¤¸µ¡Ç½¤ò»ý¤Ã¤Æ¤ª¤ê¡¢
849 ¤Þ¤Ð¤é¤Ê¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤ò´Æ»ë¤¹¤ë¾ì¹ç¤Ë
851 ¸½ºß¤Ç¤Ï¹¤¯ÍøÍѲÄǽ¤Ç¤¢¤ë¤¬¡¢°ÊÁ°¤Ï
853 ¤è¤ê°Ü¿¢À¤ÎÌ̤ÇÎô¤Ã¤Æ¤¤¤¿¡£
855 .\"O The Linux-specific
857 .\"O API provides an interface that is more efficient than
861 .\"O when monitoring large numbers of file descriptors.
864 API ¤Ï¡¢Â¿¿ô¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò´Æ»ë¤¹¤ë¾ì¹ç¤Ë
868 ¤è¤ê¤â¸úΨŪ¤Ê¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤òÄ󶡤·¤Æ¤¤¤ë¡£
871 .\"O Here is an example that better demonstrates the true utility of
873 .\"O The listing below a TCP forwarding program that forwards
874 .\"O from one TCP port to another.
876 ¤ÎËÜÅö¤ËÊØÍø¤ÊÅÀ¤ò¼¨¤¹¡¢¤è¤¤Îã¤ò¾Ò²ð¤¹¤ë¡£
877 °Ê²¼¤Î¥ê¥¹¥È¤Ï¡¢¤¢¤ë TCP ¥Ý¡¼¥È¤«¤éÊ̤Υݡ¼¥È¤ØžÁ÷¤ò¹Ô¤¦
878 TCP ¥Õ¥©¥ï¡¼¥É¥×¥í¥°¥é¥à¤Ç¤¢¤ë¡£
884 #include <sys/time.h>
885 #include <sys/types.h>
888 #include <sys/socket.h>
889 #include <netinet/in.h>
890 #include <arpa/inet.h>
893 static int forward_port;
896 #define max(x,y) ((x) > (y) ? (x) : (y))
899 listen_socket(int listen_port)
901 struct sockaddr_in a;
905 if ((s = socket(AF_INET, SOCK_STREAM, 0)) == \-1) {
910 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
911 (char *) &yes, sizeof(yes)) == \-1) {
912 perror("setsockopt");
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) {
924 printf("accepting connections on port %d\\n", listen_port);
930 connect_socket(int connect_port, char *address)
932 struct sockaddr_in a;
935 if ((s = socket(AF_INET, SOCK_STREAM, 0)) == \-1) {
941 memset(&a, 0, sizeof(a));
942 a.sin_port = htons(connect_port);
943 a.sin_family = AF_INET;
945 if (!inet_aton(address, (struct in_addr *) &a.sin_addr.s_addr)) {
946 perror("bad IP address format");
951 if (connect(s, (struct sockaddr *) &a, sizeof(a)) == \-1) {
953 shutdown(s, SHUT_RDWR);
960 #define SHUT_FD1 do { \\
962 shutdown(fd1, SHUT_RDWR); \\
968 #define SHUT_FD2 do { \\
970 shutdown(fd2, SHUT_RDWR); \\
976 #define BUF_SIZE 1024
979 main(int argc, char *argv[])
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;
988 fprintf(stderr, "Usage\\n\\tfwd <listen-port> "
989 "<forward-to-port> <forward-to-ip-address>\\n");
993 signal(SIGPIPE, SIG_IGN);
995 forward_port = atoi(argv[2]);
997 h = listen_socket(atoi(argv[1]));
1009 nfds = max(nfds, h);
1010 if (fd1 > 0 && buf1_avail < BUF_SIZE) {
1012 nfds = max(nfds, fd1);
1014 if (fd2 > 0 && buf2_avail < BUF_SIZE) {
1016 nfds = max(nfds, fd2);
1018 if (fd1 > 0 && buf2_avail \- buf2_written > 0) {
1020 nfds = max(nfds, fd1);
1022 if (fd2 > 0 && buf1_avail \- buf1_written > 0) {
1024 nfds = max(nfds, fd2);
1028 nfds = max(nfds, fd1);
1032 nfds = max(nfds, fd2);
1035 r = select(nfds + 1, &rd, &wr, &er, NULL);
1037 if (r == \-1 && errno == EINTR)
1045 if (FD_ISSET(h, &rd)) {
1047 struct sockaddr_in client_address;
1049 memset(&client_address, 0, l = sizeof(client_address));
1050 r = accept(h, (struct sockaddr *) &client_address, &l);
1056 buf1_avail = buf1_written = 0;
1057 buf2_avail = buf2_written = 0;
1059 fd2 = connect_socket(forward_port, argv[3]);
1063 printf("connect from %s\\n",
1064 inet_ntoa(client_address.sin_addr));
1068 /* NB: read oob data before normal reads */
1071 if (FD_ISSET(fd1, &er)) {
1074 r = recv(fd1, &c, 1, MSG_OOB);
1078 send(fd2, &c, 1, MSG_OOB);
1081 if (FD_ISSET(fd2, &er)) {
1084 r = recv(fd2, &c, 1, MSG_OOB);
1088 send(fd1, &c, 1, MSG_OOB);
1091 if (FD_ISSET(fd1, &rd)) {
1092 r = read(fd1, buf1 + buf1_avail,
1093 BUF_SIZE \- buf1_avail);
1100 if (FD_ISSET(fd2, &rd)) {
1101 r = read(fd2, buf2 + buf2_avail,
1102 BUF_SIZE \- buf2_avail);
1109 if (FD_ISSET(fd1, &wr)) {
1110 r = write(fd1, buf2 + buf2_written,
1111 buf2_avail \- buf2_written);
1118 if (FD_ISSET(fd2, &wr)) {
1119 r = write(fd2, buf1 + buf1_written,
1120 buf1_avail \- buf1_written);
1127 /* check if write data has caught read data */
1129 if (buf1_written == buf1_avail)
1130 buf1_written = buf1_avail = 0;
1131 if (buf2_written == buf2_avail)
1132 buf2_written = buf2_avail = 0;
1134 /* one side has closed the connection, keep
1135 writing to the other side until empty */
1137 if (fd1 < 0 && buf1_avail \- buf1_written == 0)
1139 if (fd2 < 0 && buf2_avail \- buf2_written == 0)
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
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
1156 .\"O This also has its problems because you end up using
1157 .\"O inefficient timeouts.
1158 ¾åµ¤Î¥×¥í¥°¥é¥à¤Ï¡¢¤Û¤È¤ó¤É¤Î¼ïÎà¤Î TCP Àܳ¤ò¥Õ¥©¥ï¡¼¥É¤¹¤ë¡£
1160 ¥µ¡¼¥Ð¤Ë¤è¤Ã¤ÆÃæ·Ñ¤µ¤ì¤ë OOB ¥·¥°¥Ê¥ë¥Ç¡¼¥¿¤â°·¤¨¤ë¡£
1161 ¤³¤Î¥×¥í¥°¥é¥à¤Ï¡¢¥Ç¡¼¥¿¥Õ¥í¡¼¤òÁÐÊý¸þ¤ËƱ»þ¤ËÁ÷¤ë¤È¤¤¤¦¡¢
1162 ¤ä¤ä¤³¤·¤¤ÌäÂê¤â½èÍý¤Ç¤¤ë¡£
1164 ¥³¡¼¥ë¤ò»È¤Ã¤Æ¡¢³Æ¥¹¥È¥ê¡¼¥à¤´¤È¤ËÀìÍѤΥ¹¥ì¥Ã¥É¤òÍѤ¤¤ë¤Û¤¦¤¬¸úΨŪ¤À¡¢
1165 ¤È¤¤¤¦¿Í¤â¤¤¤ë¤«¤â¤·¤ì¤Ê¤¤¡£¤·¤«¤·¡¢¤³¤ì¤Ï¹Í¤¨¤Æ¤¤¤ë¤è¤ê¤º¤Ã¤È¤ä¤ä¤³¤·¤¤¡£
1168 ¤ò»È¤Ã¤ÆÈó¥Ö¥í¥Ã¥¯ I/O ¤ò¥»¥Ã¥È¤¹¤ì¤ÐÎɤ¤¡¢¤È¤¤¤¦¥¢¥¤¥Ç¥¢¤â¤¢¤ë¤À¤í¤¦¡£
1169 ¤³¤ì¤Ë¤â¼ÂºÝ¤Ë¤ÏÌäÂ꤬¤¢¤ê¡¢¥¿¥¤¥à¥¢¥¦¥È¤¬Èó¸úΨŪ¤Ëµ¯¤³¤Ã¤Æ¤·¤Þ¤¦¡£
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 ¸½»þÅÀ¤Î¤â¤Î¤Ç¤Ï¡¢¿·¤·¤¤Àܳ¤¬¤¯¤ë¤È¸Å¤¤Àܳ¤ÏÍî¤Á¤Æ¤·¤Þ¤¦¡£
1190 .BR sigprocmask (2),
1194 .BR sigemptyset (3),
1196 .BR sigismember (3),
1199 .\" This man page was written by Paul Sheer.