2 .\" epoll by Davide Libenzi ( efficient event notification retrieval )
3 .\" Copyright (C) 2003 Davide Libenzi
5 .\" This program is free software; you can redistribute it and/or modify
6 .\" it under the terms of the GNU General Public License as published by
7 .\" the Free Software Foundation; either version 2 of the License, or
8 .\" (at your option) any later version.
10 .\" This program is distributed in the hope that it will be useful,
11 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
12 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 .\" GNU General Public License for more details.
15 .\" You should have received a copy of the GNU General Public License
16 .\" along with this program; if not, write to the Free Software
17 .\" Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 .\" Davide Libenzi <davidel@xmailserver.org>
21 .\" Japanese Version Copyright (c) 2004-2005 Yuichi SATO
22 .\" all rights reserved.
23 .\" Translated Sat Jun 19 07:50:04 JST 2004
24 .\" by Yuichi SATO <ysato444@yahoo.co.jp>
25 .\" Updated & Modified 2005-01-18, Yuichi SATO
26 .\" Updated 2006-07-14, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
27 .\" Catch up to LDP v2.34. epoll.4 is renamed to epoll.7.
28 .\" Updated 2007-09-07, Akihiro MOTOKI, LDP v2.64
29 .\" Updated 2008-04-08, Akihiro MOTOKI, LDP v2.79
30 .\" Updated 2009-02-23, Akihiro MOTOKI, LDP v3.19
32 .TH EPOLL 7 2009-02-01 "Linux" "Linux Programmer's Manual"
35 .\"O epoll \- I/O event notification facility
36 epoll \- I/O ¥¤¥Ù¥ó¥ÈÄÌÃε¡Ç½
39 .B #include <sys/epoll.h>
45 .\"O that can be used either as an edge-triggered or a level-triggered
46 .\"O interface and scales well to large numbers of watched file descriptors.
47 .\"O The following system calls are provided to
48 .\"O create and manage an
54 ¤Î°ì¼ï¤Ç¤¢¤ê¡¢¥¨¥Ã¥¸¥È¥ê¥¬¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤È
55 ¥ì¥Ù¥ë¥È¥ê¥¬¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤Î¤É¤Á¤é¤È¤·¤Æ¤â»ÈÍѤ¹¤ë¤³¤È¤¬¤Ç¤¡¢
56 ´Æ»ë¤¹¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¿ô¤¬Â¿¤¤¾ì¹ç¤Ë¤â»ÈÍѤǤ¤ë¡£
58 ¥¤¥ó¥¹¥¿¥ó¥¹¤ÎºîÀ®¤ä´ÉÍý¤ò¹Ô¤¦¤¿¤á¤Ë
59 °Ê²¼¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë¤¬Ä󶡤µ¤ì¤Æ¤¤¤ë:
63 .\"O instance created by
64 .\"O .BR epoll_create (2),
65 .\"O which returns a file descriptor referring to the epoll instance.
67 .\"O .BR epoll_create1 (2)
68 .\"O extends the functionality of
69 .\"O .BR epoll_create (2).)
75 ¤ÏºîÀ®¤·¤¿ epoll ¥¤¥ó¥¹¥¿¥ó¥¹¤ò»²¾È¤¹¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÊÖ¤¹¡£
80 ¤Îµ¡Ç½¤¬³ÈÄ¥¤µ¤ì¤Æ¤¤¤ë)¡£
82 .\"O Interest in particular file descriptors is then registered via
83 .\"O .BR epoll_ctl (2).
84 ÆÃÄê¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ËÂФ¹¤ë´Æ»ëÆâÍƤò
87 .\"O The set of file descriptors currently registered on an
89 .\"O instance is sometimes called an
93 ¥¤¥ó¥¹¥¿¥ó¥¹¤Ë¸½ºßÅÐÏ¿¤µ¤ì¤Æ¤¤¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î½¸¹ç¤Ï
95 ½¸¹ç¤È¸Æ¤Ð¤ì¤ë¤³¤È¤â¤¢¤ë¡£
97 .\"O Finally, the actual wait is started by
98 .\"O .BR epoll_wait (2).
101 ¤Ç¼ÂºÝ¤Î¥¤¥Ù¥ó¥ÈÂÔ¤Á¤ò³«»Ï¤¹¤ë¡£
102 .\"O .SS Level-Triggered and Edge-Triggered
103 .SS ¥ì¥Ù¥ë¥È¥ê¥¬¤È¥¨¥Ã¥¸¥È¥ê¥¬
106 .\"O event distribution interface is able to behave both as edge-triggered
107 .\"O (ET) and as level-triggered (LT).
108 .\"O The difference between the two mechanisms
109 .\"O can be described as follows.
111 .\"O this scenario happens:
113 ¥¤¥Ù¥ó¥ÈÇÛÁ÷ (distribution) ¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤Ï¡¢
114 ¥¨¥Ã¥¸¥È¥ê¥¬ (ET) ¤È¤·¤Æ¤â¥ì¥Ù¥ë¥È¥ê¥¬ (LT) ¤È¤·¤Æ¤âÆ°ºî¤µ¤»¤ë¤³¤È¤¬¤Ç¤¤ë¡£
115 Æó¤Ä¤ÎÇÛÁ÷µ¡¹½¤Î°ã¤¤¤Ï¡¢¼¡¤Î¤è¤¦¤ËÀâÌÀ¤Ç¤¤ë¡£
116 ¤³¤Î¤è¤¦¤Ê¥·¥Ê¥ê¥ª¤¬µ¯¤³¤Ã¤¿¤È¤·¤è¤¦:
118 .\"O The file descriptor that represents the read side of a pipe
120 .\"O is registered on the
123 ¥Ñ¥¤¥×¤ÎÆɤ߹þ¤ß¦¤òɽ¤¹¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿
127 ¥¤¥ó¥¹¥¿¥ó¥¹¤ËÅÐÏ¿¤µ¤ì¤ë¡£
129 .\"O A pipe writer writes 2 kB of data on the write side of the pipe.
130 ¥Ñ¥¤¥×¤Ø½ñ¤¹þ¤à¥×¥í¥°¥é¥à¤¬ 2 kB ¤Î¥Ç¡¼¥¿¤ò¥Ñ¥¤¥×¤Î½ñ¤¹þ¤ß¦¤Ø½ñ¤¹þ¤à¡£
133 .\"O .BR epoll_wait (2)
134 .\"O is done that will return
136 .\"O as a ready file descriptor.
138 ¤ò¸Æ¤Ó½Ð¤¹¤È¡¢Æɤ߹þ¤ß²Äǽ (ready) ¤Ê¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤È¤·¤Æ
142 .\"O The pipe reader reads 1 kB of data from
144 ¥Ñ¥¤¥×¤«¤éÆɤ߽Ф¹¥×¥í¥°¥é¥à¤¬¡¢1 kB ¤Î¥Ç¡¼¥¿¤ò
149 .\"O .BR epoll_wait (2)
152 ¤Î¸Æ¤Ó½Ð¤·¤¬¹Ô¤ï¤ì¤ë¡£
156 .\"O file descriptor has been added to the
158 .\"O interface using the
160 .\"O (edge-triggered)
161 .\"O flag, the call to
162 .\"O .BR epoll_wait (2)
165 .\"O will probably hang despite the available data still present in the file
167 .\"O meanwhile the remote peer might be expecting a response based on the
168 .\"O data it already sent.
169 .\"O The reason for this is that edge-triggered mode only
170 .\"O delivers events when changes occur on the monitored file descriptor.
172 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬
174 ¥Õ¥é¥° (¥¨¥Ã¥¸¥È¥ê¥¬) ¤ò»È¤Ã¤Æ
177 ÍøÍѲÄǽ¤Ê¥Ç¡¼¥¿¤¬¥Õ¥¡¥¤¥ëÆþÎϥХåե¡¤Ë¤Þ¤À¸ºß¤¹¤ë¤Ë¤â¤«¤«¤ï¤é¤º
182 ¤Î¸Æ¤Ó½Ð¤·¤Ç¥Ï¥ó¥°¤¹¤ë²ÄǽÀ¤¬¤¢¤ë¡£
183 ¤½¤Î°ìÊý¤Ç¡¢¥ê¥â¡¼¥È¤ÎÀܳÀè (peer) ¤Ï´û¤ËÁ÷¤é¤ì¤¿¥Ç¡¼¥¿¤Ë
184 ´ð¤Å¤¤¤Æ±þÅú¤ò´üÂÔ¤·¤Æ¤¤¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
185 ¤³¤Î¤è¤¦¤Ê¤³¤È¤¬µ¯¤³¤ëÍýͳ¤Ï¡¢¥¨¥Ã¥¸¥È¥ê¥¬¥¤¥Ù¥ó¥ÈÇÛÁ÷¤Ç¤Ï¡¢
186 ¥â¥Ë¥¿¤·¤Æ¤¤¤ë¥Õ¥¡¥¤¥ë¤Ç¥¤¥Ù¥ó¥È¤¬µ¯¤Ã¤¿¤È¤¤Ë¤Î¤ß¥¤¥Ù¥ó¥È¤¬
187 ÇÛÁ÷¤µ¤ì¤ë¤¿¤á¤Ç¤¢¤ë¡£
190 .\"O the caller might end up waiting for some data that is already present inside
191 .\"O the input buffer.
194 ¤Ç¤Ï¡¢¸Æ¤Ó½Ð¤·Â¦¤Ï·ë²ÌŪ¤Ë
195 ÆþÎϥХåե¡Æâ¤Ë¤¹¤Ç¸ºß¤¹¤ë¥Ç¡¼¥¿¤òÂԤĤ³¤È¤Ë¤Ê¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
196 .\"O In the above example, an event on
198 .\"O will be generated because of the write done in
200 .\"O and the event is consumed in
204 ¤Ç¹Ô¤ï¤ì¤¿½ñ¤¹þ¤ß¤Ë¤è¤Ã¤Æ
206 ¤Ë´Ø¤¹¤ë¥¤¥Ù¥ó¥È¤¬À¸À®¤µ¤ì¡¢
208 ¤Ç¥¤¥Ù¥ó¥È¤¬¾ÃÈñ (consume) ¤µ¤ì¤ë¡£
209 .\"O Since the read operation done in
211 .\"O does not consume the whole buffer data, the call to
212 .\"O .BR epoll_wait (2)
215 .\"O might block indefinitely.
217 ¤Ç¹Ô¤ï¤ì¤ëÆɤ߹þ¤ßÁàºî¤Ç¤Ï¡¢Á´Éô¤Î¥Ð¥Ã¥Õ¥¡¥Ç¡¼¥¿¤ò¾ÃÈñ¤·¤Ê¤¤¤Î¤Ç¡¢
223 ̵´ü¸Â¤ËÄä»ß (block) ¤¹¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
225 .\"O An application that employs the
227 .\"O flag should use nonblocking file descriptors to avoid having a blocking
228 .\"O read or write starve a task that is handling multiple file descriptors.
230 ¥Õ¥é¥°¤òºÎÍѤ¹¤ë¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ç¤Ï¡¢
231 ¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤Ï¥Ö¥í¥Ã¥¯¤·¤Ê¤¤ (nonblocking) ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò
233 ¤³¤ì¤Ï¡¢¥Ö¥í¥Ã¥¯¤µ¤ì¤ëÆɤ߹þ¤ß¤ä½ñ¤¹þ¤ß¤Ë¤è¤Ã¤Æ¡¢
234 Ê£¿ô¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò°·¤¦¥¿¥¹¥¯¤¬
235 Ää»ß¤·¤Æ¤·¤Þ¤¦¤Î¤òÈò¤±¤ë¤¿¤á¤Ç¤¢¤ë¡£
236 .\"O The suggested way to use
238 .\"O as an edge-triggered
240 .\"O interface is as follows:
244 ¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤È¤·¤Æ»È¤¦¤¿¤á¤ËÄó°Æ¤µ¤ì¤ëÊýË¡¤Ï°Ê²¼¤ÎÄ̤ê¤Ç¤¢¤ë¡£
248 .\"O with nonblocking file descriptors; and
249 ¥Ö¥í¥Ã¥¯¤·¤Ê¤¤¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤È¶¦¤Ë»È¤¦¡£
252 .\"O by waiting for an event only after
263 ¤òÊÖ¤·¤¿¸å¤Ç¤Î¤ß¡¢¥¤¥Ù¥ó¥È¤òÂԤġ£
266 .\"O By contrast, when used as a level-triggered interface
267 .\"O (the default, when
269 .\"O is not specified),
271 .\"O is simply a faster
273 .\"O and can be used wherever the latter is used since it shares the
275 °ìÊý¡¢¥ì¥Ù¥ë¥È¥ê¥¬¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤È¤·¤Æ»È¤¦¾ì¹ç
276 (¤³¤Á¤é¤¬¥Ç¥Õ¥©¥ë¥È¤Ç¤¢¤ë¡¢
278 ¤¬»ØÄꤵ¤ì¤Ê¤«¤Ã¤¿¾ì¹ç)¡¢
282 ¤Ç¤¢¤ê¡¢»È¤¤Êý¤¬Æ±¤¸¤Ê¤Î¤Ç¡¢
284 ¤¬»È¤ï¤ì¤Æ¤¤¤ë¤È¤³¤í¤Ç¤Ï¤É¤³¤Ç¤â»ÈÍѤ¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£
286 .\"O Since even with edge-triggered
288 .\"O multiple events can be generated upon receipt of multiple chunks of data,
289 .\"O the caller has the option to specify the
293 .\"O to disable the associated file descriptor after the receipt of an event with
294 .\"O .BR epoll_wait (2).
295 ¥¨¥Ã¥¸¥È¥ê¥¬¤ò»È¤Ã¤¿¾ì¹ç¤Ç¤â¡¢Ê£¿ô¤Î¥Ç¡¼¥¿¤ò¼õ¿®¤¹¤ë¤ÈÊ£¿ô¤Î
297 ¥¤¥Ù¥ó¥È¤¬À¸À®¤µ¤ì¤ë¤Î¤Ç¡¢
300 ¥Õ¥é¥°¤ò»ØÄꤹ¤ë¥ª¥×¥·¥ç¥ó¤¬¤¢¤ë¡£
305 ¤Ë¤è¤ë¥¤¥Ù¥ó¥È¤ò¼õ¿®¤·¤¿¸å¤Ç¡¢´ØÏ¢¤¹¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò̵¸ú¤Ë¤µ¤»¤ë¡£
308 .\"O flag is specified,
309 .\"O it is the caller's responsibility to rearm the file descriptor using
310 .\"O .BR epoll_ctl (2)
312 .\"O .BR EPOLL_CTL_MOD .
314 ¥Õ¥é¥°¤¬»ØÄꤵ¤ì¤¿¾ì¹ç¡¢
318 ¤ò»ØÄꤷ¤Æ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òºÆÅÙ»ÈÍѤǤ¤ë¤è¤¦¤Ë¤¹¤ë¤Î¤Ï¡¢
319 ¸Æ¤Ó½Ð¤·Â¦¤ÎÀÕǤ¤Ç¤¢¤ë¡£
320 .\"O .SS /proc interfaces
321 .SS /proc ¥¤¥ó¥¿¥Õ¥§¡¼¥¹
322 .\"O The following interfaces can be used to limit the amount of
323 .\"O kernel memory consumed by epoll:
324 epoll ¤¬¾ÃÈñ¤¹¤ë¥«¡¼¥Í¥ë¥á¥â¥ê¤ÎÎ̤òÀ©¸Â¤¹¤ë¤¿¤á¤Ë¡¢
325 °Ê²¼¤Î¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ò»ÈÍѤ¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£
327 .\" Following was added in 2.6.28, but them removed in 2.6.29
329 .\" .IR /proc/sys/fs/epoll/max_user_instances " (since Linux 2.6.28)"
330 .\" This specifies an upper limit on the number of epoll instances
331 .\" that can be created per real user ID.
333 .\"O .IR /proc/sys/fs/epoll/max_user_watches " (since Linux 2.6.28)"
334 .IR /proc/sys/fs/epoll/max_user_watches " (Linux 2.6.28 °Ê¹ß)"
335 .\"O This specifies a limit on the total number of
336 .\"O file descriptors that a user can register across
337 .\"O all epoll instances on the system.
338 .\"O The limit is per real user ID.
339 .\"O Each registered file descriptor costs roughly 90 bytes on a 32-bit kernel,
340 .\"O and roughly 160 bytes on a 64-bit kernel.
342 ¤³¤Î¥Õ¥¡¥¤¥ë¤Ï¡¢¤¢¤ë¥æ¡¼¥¶¤¬¥·¥¹¥Æ¥à¾å¤ÎÁ´¤Æ¤Î epoll ¥¤¥ó¥¹¥¿¥ó¥¹¤Ë
343 ÅÐÏ¿¤Ç¤¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÎÁí¿ô¤Î¾å¸Â¤òµ¬Äꤹ¤ë¡£
344 ¤³¤Î¾å¸Â¤Ï¼Â¥æ¡¼¥¶ ID ñ°Ì¤Ç¤¢¤ë¡£
345 ÅÐÏ¿¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿ 1 ¤Ä¤¬¾ÃÈñ¤¹¤ë¥á¥â¥êÎ̤ϡ¢
346 32 ¥Ó¥Ã¥È¥«¡¼¥Í¥ë¤Ç¤ª¤è¤½ 90 ¥Ð¥¤¥È¡¢
347 64 ¥Ó¥Ã¥È¥«¡¼¥Í¥ë¤Ç¤ª¤è¤½ 160 ¥Ð¥¤¥È¤Ç¤¢¤ë¡£
348 .\" 2.6.29 (in 2.6.28, the default was 1/32 of lowmem)
349 .\"O the default value for
350 .\"O .I max_user_watches
351 .\"O is 1/25 (4%) of the available low memory,
352 .\"O divided by the registration cost in bytes.
353 .\"O motoki: low memory ¤ÎÌõ¤Ë¼«¿®¤Ê¤·¡£
356 ¤Î¥Ç¥Õ¥©¥ë¥ÈÃͤϡ¢ÍøÍѲÄǽ¤Ê¥á¥â¥ê²¼¸Â¤Î 1/25 (4%) ¤Ç¤¢¤ê¡¢
357 ÅÐÏ¿¤Ç¾ÃÈñ¤µ¤ì¤ë¥á¥â¥êÎÌ (¥Ð¥¤¥Èñ°Ì) ¤Ç³ä¤Ã¤¿ÃͤȤʤ롣
358 .\"O .SS Example for Suggested Usage
360 .\"O While the usage of
362 .\"O when employed as a level-triggered interface does have the same
365 .\"O the edge-triggered usage requires more clarification to avoid stalls
366 .\"O in the application event loop.
367 ¥ì¥Ù¥ë¥È¥ê¥¬¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤È¤·¤Æ»ÈÍѤ¹¤ë¤È¤¤Î
372 ¤·¤«¤·¥¨¥Ã¥¸¥È¥ê¥¬¤È¤·¤Æ»È¤¦¾ì¹ç¤Ï¡¢
373 ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Î¥¤¥Ù¥ó¥È¥ë¡¼¥×¤Ç¥¹¥È¡¼¥ë (stall) ¤·¤Ê¤¤¤è¤¦¤Ë¡¢
374 »È¤¤Êý¤ò¤è¤êÌÀ³Î¤Ë¤·¤Æ¤ª¤¯É¬Íפ¬¤¢¤ë¡£
375 .\"O In this example, listener is a
376 .\"O nonblocking socket on which
378 .\"O has been called.
381 .\"O uses the new ready file descriptor until
383 .\"O is returned by either
387 ¤³¤ÎÎã¤Ç¤Ï¡¢¥ê¥¹¥Ê¤Ï¥Ö¥í¥Ã¥¯¤·¤Ê¤¤¥½¥±¥Ã¥È¤Ç¤¢¤ê¡¢
398 ¤¬ÊÖ¤µ¤ì¤ë¤Þ¤Ç¤Ï¡¢¿·¤·¤¤½àÈ÷ºÑ¤ß¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò»È¤¦¡£
399 .\"O An event-driven state machine application should, after having received
401 .\"O record its current state so that at the next call to
403 .\"O it will continue to
407 .\"O from where it stopped before.
408 ¥¤¥Ù¥ó¥È¶îÆ°¥¹¥Æ¡¼¥È¥Þ¥·¥ó¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ï¡¢
410 ¤ò¼õ¿®¤·¤¿¸å¡¢¥«¥ì¥ó¥È¤Î¾õÂÖ¤òµÏ¿¤·¤Æ¤ª¤¯¤Ù¤¤Ç¤¢¤ë¡£
413 ¸Æ¤Ó½Ð¤·¤Î¤È¤¤Ë¡¢°ÊÁ°¤ËÄä»ß¤·¤¿¤È¤³¤í¤«¤é
417 ¤ò·Ñ³¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£
421 #define MAX_EVENTS 10
422 struct epoll_event ev, events[MAX_EVENTS];
423 int listen_sock, conn_sock, nfds, epollfd;
425 /* Set up listening socket, \(aqlisten_sock\(aq (socket(),
428 epollfd = epoll_create(10);
429 if (epollfd == \-1) {
430 perror("epoll_create");
435 ev.data.fd = listen_sock;
436 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_sock, &ev) == \-1) {
437 perror("epoll_ctl: listen_sock");
442 nfds = epoll_wait(epollfd, events, MAX_EVENTS, \-1);
444 perror("epoll_pwait");
448 for (n = 0; n < nfds; ++n) {
449 if (events[n].data.fd == listen_sock) {
450 conn_sock = accept(listen_sock,
451 (struct sockaddr *) &local, &addrlen);
452 if (conn_sock == \-1) {
456 setnonblocking(conn_sock);
457 ev.events = EPOLLIN | EPOLLET;
458 ev.data.fd = conn_sock;
459 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock,
461 perror("epoll_ctl: conn_sock");
465 do_use_fd(events[n].data.fd);
472 .\"O When used as an edge-triggered interface, for performance reasons, it is
473 .\"O possible to add the file descriptor inside the
476 .\"O .RB ( EPOLL_CTL_ADD )
477 .\"O once by specifying
478 .\"O .RB ( EPOLLIN | EPOLLOUT ).
479 .\"O This allows you to avoid
480 .\"O continuously switching between
485 .\"O .BR epoll_ctl (2)
487 .\"O .BR EPOLL_CTL_MOD .
488 ¥¨¥Ã¥¸¥È¥ê¥¬¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤È¤·¤Æ»È¤¦¾ì¹ç¡¢Àǽ¾å¤ÎÍýͳ¤Ë¤è¤ê¡¢
490 .RB ( EPOLLIN | EPOLLOUT )
493 ¤Ç) ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò
495 ¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ËÄɲ乤뤳¤È¤¬¤Ç¤¤ë¡£
500 ¤ò»ØÄꤷ¤Æ¸Æ¤Ó½Ð¤¹¤³¤È¤Ç
504 ¤ÎϢ³Ū¤ÊÀÚ¤êÂؤ¨¤¬Èò¤±¤é¤ì¤ë¡£
505 .\"O .SS Questions and Answers
509 .\"O What is the key used to distinguish the file descriptors registered in an
513 ½¸¹çÆâ¤ÎÅÐÏ¿¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò¶èÊ̤¹¤ë¤Ë¤Ï¡¢
514 ²¿¤ò¥¡¼¤È¤·¤Æ»È¤¨¤Ð¤è¤¤¤«¡©
517 .\"O The key is the combination of the file descriptor number and
518 .\"O the open file description
519 .\"O (also known as an "open file handle",
520 .\"O the kernel's internal representation of an open file).
521 ¥¡¼¤Ï¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿ÈÖ¹æ¤È¥ª¡¼¥×¥ó¥Õ¥¡¥¤¥ëµ½Ò (open file
522 description) ¤ÎÁȤǤ¢¤ë (¥ª¡¼¥×¥ó¥Õ¥¡¥¤¥ëµ½Ò¤Ï "open file handle" ¤È¤â
523 ¸Æ¤Ð¤ì¡¢¥ª¡¼¥×¥ó¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¤Î¥«¡¼¥Í¥ë¤ÎÆâÉôɽ¸½¤Ç¤¢¤ë)¡£
526 .\"O What happens if you register the same file descriptor on an
531 ¥¤¥ó¥¹¥¿¥ó¥¹¤ËƱ¤¸¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò 2 ²óÅÐÏ¿¤¹¤ë¤È¤É¤¦¤Ê¤ë¤«¡©
534 .\"O You will probably get
536 .\"O However, it is possible to add a duplicate
541 .\"O descriptor to the same
549 ¥¤¥ó¥¹¥¿¥ó¥¹¤ËÂФ·¤ÆÊ£À½¤µ¤ì¤¿¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÄɲ乤뤳¤È¤Ï²Äǽ¤Ç¤¢¤ë
555 .\" But a descriptor duplicated by fork(2) can't be added to the
556 .\" set, because the [file *, fd] pair is already in the epoll set.
557 .\" That is a somewhat ugly inconsistency. On the one hand, a child process
558 .\" cannot add the duplicate file descriptor to the epoll set. (In every
559 .\" other case that I can think of, descriptors duplicated by fork have
560 .\" similar semantics to descriptors duplicated by dup() and friends.) On
561 .\" the other hand, the very fact that the child has a duplicate of the
562 .\" descriptor means that even if the parent closes its descriptor, then
563 .\" epoll_wait() in the parent will continue to receive notifications for
564 .\" that descriptor because of the duplicated descriptor in the child.
566 .\" See http://thread.gmane.org/gmane.linux.kernel/596462/
567 .\" "epoll design problems with common fork/exec patterns"
570 .\"O This can be a useful technique for filtering events,
571 .\"O if the duplicate file descriptors are registered with different
574 Ê£À½¤·¤¿¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò°Û¤Ê¤ë
576 ¥Þ¥¹¥¯¤ÇÅÐÏ¿¤¹¤ì¤Ð¡¢¥¤¥Ù¥ó¥È¤ò¥Õ¥£¥ë¥¿¥ê¥ó¥°¤¹¤ë¤Î¤Ë
577 ¤³¤Îµ¡Ç½¤ÏÍÍѤʼêË¡¤Ç¤¢¤ë¡£
582 .\"O instances wait for the same file descriptor?
583 .\"O If so, are events reported to both
585 .\"O file descriptors?
588 ¥¤¥ó¥¹¥¿¥ó¥¹¤¬Æ±¤¸¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÂÔ¤Á¼õ¤±¤ë¤³¤È¤Ï²Äǽ¤«¡©
589 ¤â¤·²Äǽ¤Ç¤¢¤ì¤Ð¡¢¥¤¥Ù¥ó¥È¤ÏξÊý¤Î
591 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ËÊó¹ð¤µ¤ì¤ë¤«¡©
594 .\"O Yes, and events would be reported to both.
595 .\"O However, careful programming may be needed to do this correctly.
596 ¥¤¥Ù¥ó¥È¤ÏξÊý¤ËÊó¹ð¤µ¤ì¤ë¡£
597 ¤·¤«¤·¤Ê¤¬¤é¡¢¤³¤ì¤òÀµ¤·¤¯°·¤¦¤Ë¤ÏÃí°Õ¿¼¤¯¥×¥í¥°¥é¥ß¥ó¥°¤¹¤ëɬÍפ¬
603 .\"O file descriptor itself poll/epoll/selectable?
605 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¼«¿È¤Ï poll/epoll/select ¤¬²Äǽ¤«¡©
611 .\"O file descriptor has events waiting then it will
612 .\"O indicate as being readable.
615 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ë½èÍýÂÔ¤Á¤Î¥¤¥Ù¥ó¥È¤¬¤¢¤ë¾ì¹ç¤Ï¡¢
616 Æɤ߽Ф·²Äǽ¤À¤ÈÄÌÃΤµ¤ì¤ë¤³¤È¤À¤í¤¦¡£
619 .\"O What happens if one attempts to put an
621 .\"O file descriptor into its own file descriptor set?
623 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò¼«¿È¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤Ë
624 Æþ¤ì¤è¤¦¤È¤¹¤ë¤È¤É¤¦¤Ê¤ë¤«¡©
628 .\"O .BR epoll_ctl (2)
631 .\"O However, you can add an
633 .\"O file descriptor inside another
635 .\"O file descriptor set.
642 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò¾¤Î
644 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤ÎÆâÉô¤ËÄɲ乤뤳¤È¤Ï²Äǽ¤Ç¤¢¤ë¡£
649 .\"O file descriptor over a UNIX domain socket to another process?
651 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò UNIX ¥É¥á¥¤¥ó¥½¥±¥Ã¥È¤Ç¾¤Î¥×¥í¥»¥¹¤ËÁ÷¤ë¤³¤È¤Ï²Äǽ¤«¡©
654 .\"O Yes, but it does not make sense to do this, since the receiving process
655 .\"O would not have copies of the file descriptors in the
658 ²Äǽ¤À¤¬¡¢¤³¤ì¤ò¤¹¤ë¤³¤È¤Ë°ÕÌ£¤Ï¤Ê¤¤¡£
659 ¤Ê¤¼¤Ê¤é¡¢¼õ¿®Â¦¤Î¥×¥í¥»¥¹¤¬
661 ½¸¹çÆâ¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¥³¥Ô¡¼¤ò»ý¤Ã¤Æ¤¤¤Ê¤¤¤«¤é¤Ç¤¢¤ë¡£
664 .\"O Will closing a file descriptor cause it to be removed from all
666 .\"O sets automatically?
667 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò¥¯¥í¡¼¥º¤¹¤ë¤È¡¢¤½¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÏÁ´¤Æ¤Î
669 ½¸¹ç¤«¤é¼«Æ°Åª¤Ëºï½ü¤µ¤ì¤ë¤«¡©
672 .\"O Yes, but be aware of the following point.
673 ºï½ü¤µ¤ì¤ë¤¬¡¢°Ê²¼¤ÎÅÀ¤ËÃí°Õ¤¬É¬ÍפǤ¢¤ë¡£
674 .\"O A file descriptor is a reference to an open file description (see
676 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ï¥ª¡¼¥×¥ó¥Õ¥¡¥¤¥ëµ½Ò
678 »²¾È) ¤Ø¤Î»²¾È¤Ç¤¢¤ë¡£
679 .\"O Whenever a descriptor is duplicated via
686 .\"O a new file descriptor referring to the same open file description is
688 ¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÎÊ£À½¤ò
696 ·Ðͳ¤Ç¹Ô¤¦Å٤ˡ¢Æ±¤¸¥ª¡¼¥×¥ó¥Õ¥¡¥¤¥ëµ½Ò¤ò»²¾È¤¹¤ë¿·µ¬¤Î¥Õ¥¡¥¤¥ë
697 ¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬À¸À®¤µ¤ì¤ë¡£
698 .\"O An open file description continues to exist until all
699 .\"O file descriptors referring to it have been closed.
700 ¥ª¡¼¥×¥ó¥Õ¥¡¥¤¥ëµ½Ò¼«ÂΤϡ¢¼«¿È¤ò»²¾È¤¹¤ëÁ´¤Æ¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿
701 ¤¬¥¯¥í¡¼¥º¤µ¤ì¤ë¤Þ¤Ç¸ºß¤·Â³¤±¤ë¡£
702 .\"O A file descriptor is removed from an
704 .\"O set only after all the file descriptors referring to the underlying
705 .\"O open file description have been closed
706 .\"O (or before if the descriptor is explicitly removed using
707 .\"O .BR epoll_ctl ()
708 .\"O .BR EPOLL_CTL_DEL ).
709 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬
711 ½¸¹ç¤«¤éºï½ü¤µ¤ì¤ë¤Î¤Ï¡¢Âбþ¤¹¤ë¥ª¡¼¥×¥ó¥Õ¥¡¥¤¥ëµ½Ò¤ò»²¾È¤·¤Æ¤¤¤ë
712 Á´¤Æ¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬¥¯¥í¡¼¥º¤µ¤ì¤¿¸å¤Ç¤¢¤ë
715 ¤ò»È¤Ã¤Æ¤½¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÌÀ¼¨Åª¤Ëºï½ü¤·¤¿¾ì¹ç¤Ë¤âºï½ü¤µ¤ì¤ë)¡£
716 .\"O This means that even after a file descriptor that is part of an
718 .\"O set has been closed,
719 .\"O events may be reported for that file descriptor if other file
720 .\"O descriptors referring to the same underlying file description remain open.
723 ½¸¹ç¤Ë°¤·¤Æ¤¤¤ë¤¢¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò¥¯¥í¡¼¥º¤·¤¿¸å¤Ç¤¢¤Ã¤Æ¤â¡¢
724 Ʊ¤¸¥Õ¥¡¥¤¥ëµ½Ò¤ò»²¾È¤¹¤ë¾¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬¥ª¡¼¥×¥ó¤µ¤ì¤Æ¤¤¤ë´Ö¤Ï¡¢
725 ¥¯¥í¡¼¥º¤·¤¿¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿°¸¤Ë¥¤¥Ù¥ó¥È¤¬Êó¹ð¤µ¤ì¤ë²ÄǽÀ¤¬¤¢¤ë¤È
729 .\"O If more than one event occurs between
730 .\"O .BR epoll_wait (2)
731 .\"O calls, are they combined or reported separately?
734 ¥³¡¼¥ë¤Î´Ö¤ËȯÀ¸¤·¤¿¾ì¹ç¡¢¤½¤ì¤é¤Ï¤Þ¤È¤á¤ÆÊó¹ð¤µ¤ì¤ë¤«¡¢
735 ¤½¤ì¤È¤âÊÌ¡¹¤ËÊó¹ð¤µ¤ì¤ë¤«¡©
738 .\"O They will be combined.
739 ¤Þ¤È¤á¤ÆÊó¹ð¤µ¤ì¤ë¤À¤í¤¦¡£
742 .\"O Does an operation on a file descriptor affect the
743 .\"O already collected but not yet reported events?
744 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ËÂФ¹¤ëÁàºî¤Ï¡¢
745 ´û¤Ë½¸¤á¤é¤ì¤Æ¤¤¤ë¤¬¤Þ¤ÀÊó¹ð¤µ¤ì¤Æ¤¤¤Ê¤¤¥¤¥Ù¥ó¥È¤Ë±Æ¶Á¤¹¤ë¤«¡©
748 .\"O You can do two operations on an existing file descriptor.
749 .\"O Remove would be meaningless for
751 .\"O Modify will reread available I/O.
752 ´û¸¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ËÂФ·¤Æ 2 ¤Ä¤ÎÁàºî¤ò¹Ô¤¦¤³¤È¤¬¤Ç¤¤ë¡£
753 ¤³¤Î¾ì¹ç¡¢ºï½ü¤Ë¤Ï°ÕÌ£¤¬¤Ê¤¤¡£
754 Êѹ¹¤¹¤ë¤È¡¢»ÈÍѲÄǽ¤Ê I/O ¤¬ºÆ¤ÓÆɤ߹þ¤Þ¤ì¤ë¡£
757 .\"O Do I need to continuously read/write a file descriptor
762 .\"O flag (edge-triggered behavior) ?
764 ¥Õ¥é¥° (¥¨¥Ã¥¸¥È¥ê¥¬Æ°ºî) ¤ò»È¤Ã¤Æ¤¤¤ë¾ì¹ç¡¢
767 ·Ñ³¤·¤Æ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÆɤ߽ñ¤¤¹¤ëɬÍפ¬¤¢¤ë¤«¡©
770 .\"O Receiving an event from
771 .\"O .BR epoll_wait (2)
772 .\"O should suggest to you that such
773 .\"O file descriptor is ready for the requested I/O operation.
775 ¤«¤é¥¤¥Ù¥ó¥È¤ò¼õ¤±¼è¤ë¤³¤È¤Ï¡¢
776 ¤½¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬Í׵ᤵ¤ì¤¿ I/O Áàºî¤ËÂФ·¤Æ½àÈ÷ºÑ¤ß¤Ç¤¢¤ë¡¢
777 ¤È¤¤¤¦¤³¤È¤ò¥æ¡¼¥¶¤Ë¼¨¤¹¤â¤Î¤Ç¤¢¤ë¡£
778 .\"O You must consider it ready until the next (nonblocking)
779 .\"O read/write yields
781 ¼¡¤Î (¥Ö¥í¥Ã¥¯¤·¤Ê¤¤) read/write ¤Ç
783 ¤ò¼õ¤±¼è¤ë¤Þ¤Ç¤Ï¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ï½àÈ÷ºÑ¤ß¤Ç¤¢¤ë¤È
784 ¹Í¤¨¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
785 .\"O When and how you will use the file descriptor is entirely up to you.
786 ¤½¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò¤¤¤Ä¤É¤Î¤è¤¦¤Ë»È¤¦¤«¤Ï¡¢
787 Á´¤¯¥æ¡¼¥¶¤ËǤ¤µ¤ì¤Æ¤ë¡£
789 .\"O For packet/token-oriented files (e.g., datagram socket,
790 .\"O terminal in canonical mode),
791 .\"O the only way to detect the end of the read/write I/O space
792 .\"O is to continue to read/write until
794 ¥Ñ¥±¥Ã¥È»Ø¸þ¤ä¥È¡¼¥¯¥ó»Ø¸þ¤Î¥Õ¥¡¥¤¥ë (Î㤨¤Ð¡¢¥Ç¡¼¥¿¥°¥é¥à¥½¥±¥Ã¥È¡¢
795 canonical ¥â¡¼¥É¤ÎüËö) ¤Ç¤Ï¡¢
796 Æɤ߹þ¤ßÍÑ / ½ñ¤¹þ¤ßÍѤΠI/O ¶õ´Ö¤ÎËöÈø¤ò¸¡ÃΤ¹¤ëÍ£°ì¤ÎÊýË¡¤Ï
798 ¤Ë¤Ê¤ë¤Þ¤Ç read/write ¤ò¹Ô¤¦¤³¤È¤Ç¤¢¤ë¡£
800 .\"O For stream-oriented files (e.g., pipe, FIFO, stream socket), the
801 .\"O condition that the read/write I/O space is exhausted can also be detected by
802 .\"O checking the amount of data read from / written to the target file
804 ¥¹¥È¥ê¡¼¥à»Ø¸þ¤Î¥Õ¥¡¥¤¥ë (Î㤨¤Ð¡¢¥Ñ¥¤¥×¡¢FIFO¡¢¥¹¥È¥ê¡¼¥à¥½¥±¥Ã¥È) ¤Ç¤Ï¡¢
805 Æɤ߹þ¤ßÍÑ / ½ñ¤¹þ¤ßÍѤΠI/O ¶õ´Ö¤¬»È¤¤¿Ô¤¯¤µ¤ì¤¿¾õÂ֤ϡ¢
806 ÂоݤȤʤë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤«¤éÆɤ߹þ¤ó¤À¥Ç¡¼¥¿Î̤ޤ¿¤Ï
807 ½ñ¤¹þ¤ó¤À¥Ç¡¼¥¿Î̤ò¥Á¥§¥Ã¥¯¤¹¤ë¤³¤È¤Ç¤â¸¡ÃΤǤ¤ë¡£
808 .\"O For example, if you call
810 .\"O by asking to read a certain amount of data and
812 .\"O returns a lower number of bytes, you
813 .\"O can be sure of having exhausted the read I/O space for the file
815 .\"O The same is true when writing using
817 .\"O (Avoid this latter technique if you cannot guarantee that
818 .\"O the monitored file descriptor always refers to a stream-oriented file.)
819 Î㤨¤Ð¡¢¤¢¤ëÆÃÄê¤ÎÎ̤Υǡ¼¥¿¤òÆɤ߹þ¤à¤¿¤á¤Ë
823 ¤¬ÊÖ¤·¤¿¥Ð¥¤¥È¿ô¤¬¤½¤ì¤è¤ê¾¯¤Ê¤«¤Ã¤¿¾ì¹ç¡¢
824 ¤½¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÎÆɤ߹þ¤ßÍÑ I/O ¶õ´Ö¤¬
825 »È¤¤¿Ô¤¯¤µ¤ì¤¿¤³¤È¤¬Ê¬¤«¤ë¡£
827 ¤ò»È¤Ã¤Æ½ñ¤¹þ¤ß¤ò¤¹¤ë¤È¤¤â¡¢Æ±¤¸¤³¤È¤¬¸À¤¨¤ë
828 (´Æ»ë¤·¤Æ¤¤¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬¾ï¤Ë¥¹¥È¥ê¡¼¥à»Ø¸þ¤Î¥Õ¥¡¥¤¥ë¤ò
829 »²¾È¤·¤Æ¤¤¤ë¤³¤È¤òÊݾڤǤ¤Ê¤¤¾ì¹ç¤Ë¤Ï¡¢¸å¼Ô¤Î¼êË¡¤Î»ÈÍѤòÈò¤±¤ë¤³¤È)¡£
830 .\"O .SS Possible Pitfalls and Ways to Avoid Them
831 .SS ¤¢¤ê¤¬¤Á¤ÊÍî¤È¤··ê¤È²óÈòÊýË¡
833 .\"O .B o Starvation (edge-triggered)
834 .B o µ²²î (starvation) (¥¨¥Ã¥¸¥È¥ê¥¬)
836 .\"O If there is a large amount of I/O space,
837 .\"O it is possible that by trying to drain
838 .\"O it the other files will not get processed causing starvation.
839 .\"O (This problem is not specific to
841 Âç¤¤Ê I/O ¶õ´Ö¤¬¤¢¤ë¾ì¹ç¡¢
842 ¤½¤Î I/O ¶õ´Ö¤Î¥Ç¡¼¥¿¤òÁ´¤Æ½èÍý (drain) ¤·¤è¤¦¤È¤¹¤ë¤È¡¢
843 ¾¤Î¥Õ¥¡¥¤¥ë¤¬½èÍý¤µ¤ì¤º¡¢µ²²î¤òȯÀ¸¤µ¤»¤ë¤³¤È¤¬¤¢¤ë
846 ¤Ë¸ÇͤΤâ¤Î¤Ç¤Ï¤Ê¤¤)¡£
848 .\"O The solution is to maintain a ready list
849 .\"O and mark the file descriptor as ready
850 .\"O in its associated data structure, thereby allowing the application to
851 .\"O remember which files need to be processed but still round robin amongst
852 .\"O all the ready files.
853 .\"O This also supports ignoring subsequent events you
854 .\"O receive for file descriptors that are already ready.
855 ¤³¤ÎÌäÂê¤Î²ò·èË¡¤Ï¡¢½àÈ÷ºÑ¤ß¾õÂ֤Υꥹ¥È¤ò´ÉÍý¤·¤Æ¡¢
856 ´ØÏ¢¤¹¤ë data ¹½Â¤ÂΤÎÃæ¤Ç¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬
857 ÍøÍѲÄǽ¤Ç¤¢¤ë¤È¥Þ¡¼¥¯¤¹¤ë¤³¤È¤Ç¤¢¤ë¡£
858 ¤½¤ì¤Ë¤è¤Ã¤Æ¡¢ÍøÍѲÄǽ¤Ê¤¹¤Ù¤Æ¤Î¥Õ¥¡¥¤¥ë¤ÎÃæ¤Ç
859 ¤É¤Î¥Õ¥¡¥¤¥ë¤ò½èÍý¤¹¤ëɬÍפ¬¤¢¤ë¤«¤ò²±¤¨¤ë¤³¤È¤¬¤Ç¤¡¢
860 ¤·¤«¤â½çÈ֤˽èÍý (round robin) ¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£
861 ´û¤ËÍøÍѲÄǽ¤Ç¤¢¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ËÂФ·¤Æ
862 ¤½¤ì°Ê¸å¤Ë¼õ¤±¼è¤ë¥¤¥Ù¥ó¥È¤ò̵»ë¤¹¤ë¤³¤È¤â¤Ç¤¤ë¡£
864 .\"O .B o If using an event cache...
865 .B o ¥¤¥Ù¥ó¥È¥¥ã¥Ã¥·¥å¤ò»È¤Ã¤Æ¤¤¤ë¾ì¹ç
867 .\"O If you use an event cache or store all the file descriptors returned from
868 .\"O .BR epoll_wait (2),
869 .\"O then make sure to provide a way to mark
870 .\"O its closure dynamically (i.e., caused by
871 .\"O a previous event's processing).
872 .\"O Suppose you receive 100 events from
873 .\"O .BR epoll_wait (2),
874 .\"O and in event #47 a condition causes event #13 to be closed.
875 ¥¤¥Ù¥ó¥È¥¥ã¥Ã¥·¥å¤ò»È¤Ã¤Æ¤¤¤ë¾ì¹ç¡¢
878 ¤«¤éÊÖ¤µ¤ì¤¿Á´¤Æ¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò³ÊǼ¤·¤Æ¤¤¤ë¾ì¹ç¡¢
879 ¥¯¥í¡¼¥º¤µ¤ì¤¿¤³¤È¤òưŪ¤Ë¥Þ¡¼¥¯¤¹¤ë
880 (¤Ä¤Þ¤êÁ°¤Î¥¤¥Ù¥ó¥È¤Î½èÍý¤Ë¤è¤Ã¤Æ¥Þ¡¼¥¯¤µ¤ì¤ë) ÊýË¡¤òÄ󶡤¹¤Ù¤¤Ç¤¢¤ë¡£
882 ¤«¤é 100 ¸Ä¤Î¥¤¥Ù¥ó¥È¤ò¼õ¤±¼è¤ê¡¢
883 ¥¤¥Ù¥ó¥È #47 ¤Ç¤Ï¤¢¤ë¾ò·ï¤Ç¥¤¥Ù¥ó¥È #13 ¤¬ÊĤ¸¤é¤ì¤ë¤È²¾Äꤹ¤ë¡£
884 .\"O If you remove the structure and
886 .\"O the file descriptor for event #13, then your
887 .\"O event cache might still say there are events waiting for that
888 .\"O file descriptor causing confusion.
889 ¥¤¥Ù¥ó¥È #13 ¤Î¹½Â¤ÂΤòºï½ü¤·¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò
891 ¤¹¤ë¤È¡¢¥¤¥Ù¥ó¥È¥¥ã¥Ã¥·¥å¤Ï¤½¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÂԤĥ¤¥Ù¥ó¥È¤¬
892 ¸ºß¤¹¤ë¤È¤¤¤Ã¤Æ¡¢º®Í𤬵¯¤¤ë¡£
894 .\"O One solution for this is to call, during the processing of event 47,
895 .\"O .BR epoll_ctl ( EPOLL_CTL_DEL )
896 .\"O to delete file descriptor 13 and
898 .\"O then mark its associated
899 .\"O data structure as removed and link it to a cleanup list.
900 .\"O If you find another
901 .\"O event for file descriptor 13 in your batch processing,
902 .\"O you will discover the file descriptor had been
903 .\"O previously removed and there will be no confusion.
904 ¤³¤ÎÌäÂê¤ò²ò·è¤¹¤ë 1 ¤Ä¤ÎÊýË¡¤Ï¡¢¥¤¥Ù¥ó¥È 47 ¤Î½èÍý¤ò¤·¤Æ¤¤¤ë´Ö¤Ë¡¢
905 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿ 13 ¤òºï½ü¤·¤Æ
908 .BR epoll_ctl ( EPOLL_CTL_DEL )
909 ¤ò¸Æ¤Ó½Ð¤·¡¢´ØÏ¢ÉÕ¤±¤é¤ì¤¿ data ¹½Â¤ÂΤòºï½üºÑ¤ß¤È¥Þ¡¼¥¯¤·¤Æ¡¢
910 ¥¯¥ê¡¼¥ó¥¢¥Ã¥×¥ê¥¹¥È¤Ë¥ê¥ó¥¯¤¹¤ë¤³¤È¤Ç¤¢¤ë¡£
911 ¥Ð¥Ã¥Á½èÍý¤ÎÃæ¤Ç¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿ 13 ¤Ë¤Ä¤¤¤Æ¤Î
912 ¾¤Î¥¤¥Ù¥ó¥È¤ò¸«¤Ä¤±¤¿¾ì¹ç¡¢
913 ¤½¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬°ÊÁ°¤Ëºï½ü¤µ¤ì¤¿¤â¤Î¤Ç¤¢¤ë¤Èʬ¤«¤ë¤Î¤Ç¡¢
919 .\"O API was introduced in Linux kernel 2.5.44.
921 API ¤Ï Linux ¥«¡¼¥Í¥ë 2.5.44 ¤ËƳÆþ¤µ¤ì¤¿¡£
922 .\"O .\" Its interface should be finalized in Linux kernel 2.5.66.
923 .\" ¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤Ï Linux ¥«¡¼¥Í¥ë 2.5.66 ¤Ç³ÎÄꤵ¤ì¤ë¤Ù¤¤Ç¤¢¤ë¡£
924 .\"O Support was added to glibc in version 2.3.2.
925 glibc ¤Ç¤Î¥µ¥Ý¡¼¥È¤Ï¥Ð¡¼¥¸¥ç¥ó 2.3.2 ¤ÇÄɲ䵤줿¡£
926 .\"O .SH CONFORMING TO
930 .\"O API is Linux-specific.
931 .\"O Some other systems provide similar
932 .\"O mechanisms, for example, FreeBSD has
937 API ¤Ï Linux ¸ÇͤǤ¢¤ë¡£
938 ¾¤Î¥·¥¹¥Æ¥à¤Ç¤âƱÍͤε¡¹½¤¬Ä󶡤µ¤ì¤Æ¤¤¤ë¾ì¹ç¤¬¤¢¤ë¡£
946 .BR epoll_create (2),
947 .BR epoll_create1 (2),