OSDN Git Service

2c90ce11b2c1499fe7fec651c8c2c5ae4590a099
[linuxjm/LDP_man-pages.git] / draft / man7 / epoll.7
1 .\"
2 .\"  epoll by Davide Libenzi ( efficient event notification retrieval )
3 .\"  Copyright (C) 2003  Davide Libenzi
4 .\"
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.
9 .\"
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.
14 .\"
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
18 .\"
19 .\"  Davide Libenzi <davidel@xmailserver.org>
20 .\"
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
31 .\"
32 .TH EPOLL 7 2009-02-01 "Linux" "Linux Programmer's Manual"
33 .\"O .SH NAME
34 .SH Ì¾Á°
35 .\"O epoll \- I/O event notification facility
36 epoll \- I/O ¥¤¥Ù¥ó¥ÈÄÌÃε¡Ç½
37 .\"O .SH SYNOPSIS
38 .SH ½ñ¼°
39 .B #include <sys/epoll.h>
40 .\"O .SH DESCRIPTION
41 .SH ÀâÌÀ
42 .\"O .B epoll
43 .\"O is a variant of
44 .\"O .BR poll (2)
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
49 .\"O .B epoll
50 .\"O instance:
51 .B epoll
52 ¤Ï
53 .BR poll (2)
54 ¤Î°ì¼ï¤Ç¤¢¤ê¡¢¥¨¥Ã¥¸¥È¥ê¥¬¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤È
55 ¥ì¥Ù¥ë¥È¥ê¥¬¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤Î¤É¤Á¤é¤È¤·¤Æ¤â»ÈÍѤ¹¤ë¤³¤È¤¬¤Ç¤­¡¢
56 ´Æ»ë¤¹¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¿ô¤¬Â¿¤¤¾ì¹ç¤Ë¤â»ÈÍѤǤ­¤ë¡£
57 .B epoll
58 ¥¤¥ó¥¹¥¿¥ó¥¹¤ÎºîÀ®¤ä´ÉÍý¤ò¹Ô¤¦¤¿¤á¤Ë
59 °Ê²¼¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë¤¬Ä󶡤µ¤ì¤Æ¤¤¤ë:
60 .IP * 3
61 .\"O An
62 .\"O .B epoll
63 .\"O instance created by
64 .\"O .BR epoll_create (2),
65 .\"O which returns a file descriptor referring to the epoll instance.
66 .\"O (The more recent
67 .\"O .BR epoll_create1 (2)
68 .\"O extends the functionality of
69 .\"O .BR epoll_create (2).)
70 .B epoll
71 ¥¤¥ó¥¹¥¿¥ó¥¹¤Ï
72 .BR epoll_create (2)
73 ¤ÇºîÀ®¤µ¤ì¤ë¡£
74 .BR epoll_create (2)
75 ¤ÏºîÀ®¤·¤¿ epoll ¥¤¥ó¥¹¥¿¥ó¥¹¤ò»²¾È¤¹¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÊÖ¤¹¡£
76 (¤â¤Ã¤È¿·¤·¤¤
77 .BR epoll_create1 (2)
78 ¤Ç¤Ï¡¢
79 .BR epoll_create (2)
80 ¤Îµ¡Ç½¤¬³ÈÄ¥¤µ¤ì¤Æ¤¤¤ë)¡£
81 .IP *
82 .\"O Interest in particular file descriptors is then registered via
83 .\"O .BR epoll_ctl (2).
84 ÆÃÄê¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ËÂФ¹¤ë´Æ»ëÆâÍƤò
85 .BR epoll_ctl (2)
86 ¤ÇÅÐÏ¿¤¹¤ë¡£
87 .\"O The set of file descriptors currently registered on an
88 .\"O .B epoll
89 .\"O instance is sometimes called an
90 .\"O .I epoll
91 .\"O set.
92 .B epoll
93 ¥¤¥ó¥¹¥¿¥ó¥¹¤Ë¸½ºßÅÐÏ¿¤µ¤ì¤Æ¤¤¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î½¸¹ç¤Ï
94 .I epoll
95 ½¸¹ç¤È¸Æ¤Ð¤ì¤ë¤³¤È¤â¤¢¤ë¡£
96 .IP *
97 .\"O Finally, the actual wait is started by
98 .\"O .BR epoll_wait (2).
99 ºÇ¸å¤Ë
100 .BR epoll_wait (2)
101 ¤Ç¼ÂºÝ¤Î¥¤¥Ù¥ó¥ÈÂÔ¤Á¤ò³«»Ï¤¹¤ë¡£
102 .\"O .SS Level-Triggered and Edge-Triggered
103 .SS ¥ì¥Ù¥ë¥È¥ê¥¬¤È¥¨¥Ã¥¸¥È¥ê¥¬
104 .\"O The
105 .\"O .B epoll
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.
110 .\"O Suppose that
111 .\"O this scenario happens:
112 .B epoll
113 ¥¤¥Ù¥ó¥ÈÇÛÁ÷ (distribution) ¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤Ï¡¢
114 ¥¨¥Ã¥¸¥È¥ê¥¬ (ET) ¤È¤·¤Æ¤â¥ì¥Ù¥ë¥È¥ê¥¬ (LT) ¤È¤·¤Æ¤âÆ°ºî¤µ¤»¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
115 Æó¤Ä¤ÎÇÛÁ÷µ¡¹½¤Î°ã¤¤¤Ï¡¢¼¡¤Î¤è¤¦¤ËÀâÌÀ¤Ç¤­¤ë¡£
116 ¤³¤Î¤è¤¦¤Ê¥·¥Ê¥ê¥ª¤¬µ¯¤³¤Ã¤¿¤È¤·¤è¤¦:
117 .IP 1. 3
118 .\"O The file descriptor that represents the read side of a pipe
119 .\"O .RI ( rfd )
120 .\"O is registered on the
121 .\"O .B epoll
122 .\"O instance.
123 ¥Ñ¥¤¥×¤ÎÆɤ߹þ¤ß¦¤òɽ¤¹¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿
124 .RI ( rfd )
125 ¤¬
126 .B epoll
127 ¥¤¥ó¥¹¥¿¥ó¥¹¤ËÅÐÏ¿¤µ¤ì¤ë¡£
128 .IP 2.
129 .\"O A pipe writer writes 2 kB of data on the write side of the pipe.
130 ¥Ñ¥¤¥×¤Ø½ñ¤­¹þ¤à¥×¥í¥°¥é¥à¤¬ 2 kB ¤Î¥Ç¡¼¥¿¤ò¥Ñ¥¤¥×¤Î½ñ¤­¹þ¤ß¦¤Ø½ñ¤­¹þ¤à¡£
131 .IP 3.
132 .\"O A call to
133 .\"O .BR epoll_wait (2)
134 .\"O is done that will return
135 .\"O .I rfd
136 .\"O as a ready file descriptor.
137 .BR epoll_wait (2)
138 ¤ò¸Æ¤Ó½Ð¤¹¤È¡¢Æɤ߹þ¤ß²Äǽ (ready) ¤Ê¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤È¤·¤Æ
139 .I rfd
140 ¤¬Ê֤롣
141 .IP 4.
142 .\"O The pipe reader reads 1 kB of data from
143 .\"O .IR rfd .
144 ¥Ñ¥¤¥×¤«¤éÆɤ߽Ф¹¥×¥í¥°¥é¥à¤¬¡¢1 kB ¤Î¥Ç¡¼¥¿¤ò
145 .I rfd
146 ¤«¤éÆɤ߽Ф¹¡£
147 .IP 5.
148 .\"O A call to
149 .\"O .BR epoll_wait (2)
150 .\"O is done.
151 .BR epoll_wait (2)
152 ¤Î¸Æ¤Ó½Ð¤·¤¬¹Ô¤ï¤ì¤ë¡£
153 .PP
154 .\"O If the
155 .\"O .I rfd
156 .\"O file descriptor has been added to the
157 .\"O .B epoll
158 .\"O interface using the
159 .\"O .B EPOLLET
160 .\"O (edge-triggered)
161 .\"O flag, the call to
162 .\"O .BR epoll_wait (2)
163 .\"O done in step
164 .\"O .B 5
165 .\"O will probably hang despite the available data still present in the file
166 .\"O input buffer;
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.
171 .I rfd
172 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬
173 .B EPOLLET
174 ¥Õ¥é¥° (¥¨¥Ã¥¸¥È¥ê¥¬) ¤ò»È¤Ã¤Æ
175 .B epoll
176 ¤ËÄɲ䵤ì¤Æ¤¤¤ë¤È¡¢
177 ÍøÍѲÄǽ¤Ê¥Ç¡¼¥¿¤¬¥Õ¥¡¥¤¥ëÆþÎϥХåե¡¤Ë¤Þ¤À¸ºß¤¹¤ë¤Ë¤â¤«¤«¤ï¤é¤º
178 ¥¹¥Æ¥Ã¥×
179 .B 5
180 ¤Î
181 .BR epoll_wait (2)
182 ¤Î¸Æ¤Ó½Ð¤·¤Ç¥Ï¥ó¥°¤¹¤ë²ÄǽÀ­¤¬¤¢¤ë¡£
183 ¤½¤Î°ìÊý¤Ç¡¢¥ê¥â¡¼¥È¤ÎÀܳÀè (peer) ¤Ï´û¤ËÁ÷¤é¤ì¤¿¥Ç¡¼¥¿¤Ë
184 ´ð¤Å¤¤¤Æ±þÅú¤ò´üÂÔ¤·¤Æ¤¤¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
185 ¤³¤Î¤è¤¦¤Ê¤³¤È¤¬µ¯¤³¤ëÍýͳ¤Ï¡¢¥¨¥Ã¥¸¥È¥ê¥¬¥¤¥Ù¥ó¥ÈÇÛÁ÷¤Ç¤Ï¡¢
186 ¥â¥Ë¥¿¤·¤Æ¤¤¤ë¥Õ¥¡¥¤¥ë¤Ç¥¤¥Ù¥ó¥È¤¬µ¯¤Ã¤¿¤È¤­¤Ë¤Î¤ß¥¤¥Ù¥ó¥È¤¬
187 ÇÛÁ÷¤µ¤ì¤ë¤¿¤á¤Ç¤¢¤ë¡£
188 .\"O So, in step
189 .\"O .B 5
190 .\"O the caller might end up waiting for some data that is already present inside
191 .\"O the input buffer.
192 ¤·¤¿¤¬¤Ã¤Æ¡¢¥¹¥Æ¥Ã¥×
193 .B 5
194 ¤Ç¤Ï¡¢¸Æ¤Ó½Ð¤·Â¦¤Ï·ë²ÌŪ¤Ë
195 ÆþÎϥХåե¡Æâ¤Ë¤¹¤Ç¸ºß¤¹¤ë¥Ç¡¼¥¿¤òÂԤĤ³¤È¤Ë¤Ê¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
196 .\"O In the above example, an event on
197 .\"O .I rfd
198 .\"O will be generated because of the write done in
199 .\"O .B 2
200 .\"O and the event is consumed in
201 .\"O .BR 3 .
202 ¾åµ­¤ÎÎã¤Ç¤Ï¡¢
203 .B 2
204 ¤Ç¹Ô¤ï¤ì¤¿½ñ¤­¹þ¤ß¤Ë¤è¤Ã¤Æ
205 .I rfd
206 ¤Ë´Ø¤¹¤ë¥¤¥Ù¥ó¥È¤¬À¸À®¤µ¤ì¡¢
207 .B 3
208 ¤Ç¥¤¥Ù¥ó¥È¤¬¾ÃÈñ (consume) ¤µ¤ì¤ë¡£
209 .\"O Since the read operation done in
210 .\"O .B 4
211 .\"O does not consume the whole buffer data, the call to
212 .\"O .BR epoll_wait (2)
213 .\"O done in step
214 .\"O .B 5
215 .\"O might block indefinitely.
216 .B 4
217 ¤Ç¹Ô¤ï¤ì¤ëÆɤ߹þ¤ßÁàºî¤Ç¤Ï¡¢Á´Éô¤Î¥Ð¥Ã¥Õ¥¡¥Ç¡¼¥¿¤ò¾ÃÈñ¤·¤Ê¤¤¤Î¤Ç¡¢
218 ¥¹¥Æ¥Ã¥×
219 .B 5
220 ¤Ç¹Ô¤ï¤ì¤ë
221 .BR epoll_wait (2)
222 ¤Î¸Æ¤Ó½Ð¤·¤¬
223 ̵´ü¸Â¤ËÄä»ß (block) ¤¹¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
224
225 .\"O An application that employs the
226 .\"O .B EPOLLET
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.
229 .B EPOLLET
230 ¥Õ¥é¥°¤òºÎÍѤ¹¤ë¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ç¤Ï¡¢
231 ¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤Ï¥Ö¥í¥Ã¥¯¤·¤Ê¤¤ (nonblocking) ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò
232 »È¤¦¤Ù¤­¤Ç¤¢¤ë¡£
233 ¤³¤ì¤Ï¡¢¥Ö¥í¥Ã¥¯¤µ¤ì¤ëÆɤ߹þ¤ß¤ä½ñ¤­¹þ¤ß¤Ë¤è¤Ã¤Æ¡¢
234 Ê£¿ô¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò°·¤¦¥¿¥¹¥¯¤¬
235 Ää»ß¤·¤Æ¤·¤Þ¤¦¤Î¤òÈò¤±¤ë¤¿¤á¤Ç¤¢¤ë¡£
236 .\"O The suggested way to use
237 .\"O .B epoll
238 .\"O as an edge-triggered
239 .\"O .RB ( EPOLLET )
240 .\"O interface is as follows:
241 .B epoll
242 ¤ò¥¨¥Ã¥¸¥È¥ê¥¬
243 .RB ( EPOLLET )
244 ¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤È¤·¤Æ»È¤¦¤¿¤á¤ËÄó°Æ¤µ¤ì¤ëÊýË¡¤Ï°Ê²¼¤ÎÄ̤ê¤Ç¤¢¤ë¡£
245 .RS
246 .TP 4
247 .B i
248 .\"O with nonblocking file descriptors; and
249 ¥Ö¥í¥Ã¥¯¤·¤Ê¤¤¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤È¶¦¤Ë»È¤¦¡£
250 .TP
251 .B ii
252 .\"O by waiting for an event only after
253 .\"O .BR read (2)
254 .\"O or
255 .\"O .BR write (2)
256 .\"O return
257 .\"O .BR EAGAIN .
258 .BR read (2)
259 ¤Þ¤¿¤Ï
260 .BR write (2)
261 ¤¬
262 .B EAGAIN
263 ¤òÊÖ¤·¤¿¸å¤Ç¤Î¤ß¡¢¥¤¥Ù¥ó¥È¤òÂԤġ£
264 .RE
265 .PP
266 .\"O By contrast, when used as a level-triggered interface
267 .\"O (the default, when
268 .\"O .B EPOLLET
269 .\"O is not specified),
270 .\"O .B epoll
271 .\"O is simply a faster
272 .\"O .BR poll (2),
273 .\"O and can be used wherever the latter is used since it shares the
274 .\"O same semantics.
275 °ìÊý¡¢¥ì¥Ù¥ë¥È¥ê¥¬¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤È¤·¤Æ»È¤¦¾ì¹ç
276  (¤³¤Á¤é¤¬¥Ç¥Õ¥©¥ë¥È¤Ç¤¢¤ë¡¢
277 .B EPOLLET
278 ¤¬»ØÄꤵ¤ì¤Ê¤«¤Ã¤¿¾ì¹ç)¡¢
279 .B epoll
280 ¤Ïñ¤Ë¹â®¤Ê
281 .BR poll (2)
282 ¤Ç¤¢¤ê¡¢»È¤¤Êý¤¬Æ±¤¸¤Ê¤Î¤Ç¡¢
283 .BR poll (2)
284 ¤¬»È¤ï¤ì¤Æ¤¤¤ë¤È¤³¤í¤Ç¤Ï¤É¤³¤Ç¤â»ÈÍѤ¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
285
286 .\"O Since even with edge-triggered
287 .\"O .BR epoll ,
288 .\"O multiple events can be generated upon receipt of multiple chunks of data,
289 .\"O the caller has the option to specify the
290 .\"O .B EPOLLONESHOT
291 .\"O flag, to tell
292 .\"O .B epoll
293 .\"O to disable the associated file descriptor after the receipt of an event with
294 .\"O .BR epoll_wait (2).
295 ¥¨¥Ã¥¸¥È¥ê¥¬¤ò»È¤Ã¤¿¾ì¹ç¤Ç¤â¡¢Ê£¿ô¤Î¥Ç¡¼¥¿¤ò¼õ¿®¤¹¤ë¤ÈÊ£¿ô¤Î
296 .B epoll
297 ¥¤¥Ù¥ó¥È¤¬À¸À®¤µ¤ì¤ë¤Î¤Ç¡¢
298 ¸Æ¤Ó½Ð¤·Â¦¤Ë¤Ï
299 .B EPOLLONESHOT
300 ¥Õ¥é¥°¤ò»ØÄꤹ¤ë¥ª¥×¥·¥ç¥ó¤¬¤¢¤ë¡£
301 ¤³¤Î¥Õ¥é¥°¤Ï
302 .B epoll
303 ¤ËÂФ·¤Æ¡¢
304 .BR epoll_wait (2)
305 ¤Ë¤è¤ë¥¤¥Ù¥ó¥È¤ò¼õ¿®¤·¤¿¸å¤Ç¡¢´ØÏ¢¤¹¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò̵¸ú¤Ë¤µ¤»¤ë¡£
306 .\"O When the
307 .\"O .B EPOLLONESHOT
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)
311 .\"O with
312 .\"O .BR EPOLL_CTL_MOD .
313 .B EPOLLONESHOT
314 ¥Õ¥é¥°¤¬»ØÄꤵ¤ì¤¿¾ì¹ç¡¢
315 .BR epoll_ctl (2)
316 ¤Ë
317 .B EPOLL_CTL_MOD
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 °Ê²¼¤Î¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ò»ÈÍѤ¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
326 .TP
327 .\" Following was added in 2.6.28, but them removed in 2.6.29
328 .\" .TP
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.
332 .TP
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.
341 .\"O Currently,
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 ¤ÎÌõ¤Ë¼«¿®¤Ê¤·¡£
354 ¸½ºß¤Î¤È¤³¤í¡¢
355 .I max_user_watches
356 ¤Î¥Ç¥Õ¥©¥ë¥ÈÃͤϡ¢ÍøÍѲÄǽ¤Ê¥á¥â¥ê²¼¸Â¤Î 1/25 (4%) ¤Ç¤¢¤ê¡¢
357 ÅÐÏ¿¤Ç¾ÃÈñ¤µ¤ì¤ë¥á¥â¥êÎÌ (¥Ð¥¤¥Èñ°Ì) ¤Ç³ä¤Ã¤¿ÃͤȤʤ롣
358 .\"O .SS Example for Suggested Usage
359 .SS ¤ª¤¹¤¹¤á¤Ê»ÈÍÑÎã
360 .\"O While the usage of
361 .\"O .B epoll
362 .\"O when employed as a level-triggered interface does have the same
363 .\"O semantics as
364 .\"O .BR poll (2),
365 .\"O the edge-triggered usage requires more clarification to avoid stalls
366 .\"O in the application event loop.
367 ¥ì¥Ù¥ë¥È¥ê¥¬¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤È¤·¤Æ»ÈÍѤ¹¤ë¤È¤­¤Î
368 .B epoll
369 ¤Î»È¤¤Êý¤Ï
370 .BR poll (2)
371 ¤ÈƱ¤¸¤Ç¤¢¤ë¡£
372 ¤·¤«¤·¥¨¥Ã¥¸¥È¥ê¥¬¤È¤·¤Æ»È¤¦¾ì¹ç¤Ï¡¢
373 ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Î¥¤¥Ù¥ó¥È¥ë¡¼¥×¤Ç¥¹¥È¡¼¥ë (stall) ¤·¤Ê¤¤¤è¤¦¤Ë¡¢
374 »È¤¤Êý¤ò¤è¤êÌÀ³Î¤Ë¤·¤Æ¤ª¤¯É¬Íפ¬¤¢¤ë¡£
375 .\"O In this example, listener is a
376 .\"O nonblocking socket on which
377 .\"O .BR listen (2)
378 .\"O has been called.
379 .\"O The function
380 .\"O .I do_use_fd()
381 .\"O uses the new ready file descriptor until
382 .\"O .B EAGAIN
383 .\"O is returned by either
384 .\"O .BR read (2)
385 .\"O or
386 .\"O .BR write (2).
387 ¤³¤ÎÎã¤Ç¤Ï¡¢¥ê¥¹¥Ê¤Ï¥Ö¥í¥Ã¥¯¤·¤Ê¤¤¥½¥±¥Ã¥È¤Ç¤¢¤ê¡¢
388 .BR listen (2)
389 ¤¬¸Æ¤Ð¤ì¤Æ¤¤¤ë¡£
390 ´Ø¿ô
391 .I do_use_fd()
392 ¤Ï¡¢
393 .BR read (2)
394 ¤Þ¤¿¤Ï
395 .BR write (2)
396 ¤Ë¤è¤Ã¤Æ
397 .B EAGAIN
398 ¤¬ÊÖ¤µ¤ì¤ë¤Þ¤Ç¤Ï¡¢¿·¤·¤¤½àÈ÷ºÑ¤ß¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò»È¤¦¡£
399 .\"O An event-driven state machine application should, after having received
400 .\"O .BR EAGAIN ,
401 .\"O record its current state so that at the next call to
402 .\"O .I do_use_fd()
403 .\"O it will continue to
404 .\"O .BR read (2)
405 .\"O or
406 .\"O .BR write (2)
407 .\"O from where it stopped before.
408 ¥¤¥Ù¥ó¥È¶îÆ°¥¹¥Æ¡¼¥È¥Þ¥·¥ó¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ï¡¢
409 .B EAGAIN
410 ¤ò¼õ¿®¤·¤¿¸å¡¢¥«¥ì¥ó¥È¤Î¾õÂÖ¤òµ­Ï¿¤·¤Æ¤ª¤¯¤Ù¤­¤Ç¤¢¤ë¡£
411 ¤³¤ì¤Ë¤è¤ê¡¢¼¡¤Î
412 .I do_use_fd()
413 ¸Æ¤Ó½Ð¤·¤Î¤È¤­¤Ë¡¢°ÊÁ°¤ËÄä»ß¤·¤¿¤È¤³¤í¤«¤é
414 .BR read (2)
415 ¤Þ¤¿¤Ï
416 .BR write (2)
417 ¤ò·Ñ³¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
418
419 .in +4n
420 .nf
421 #define MAX_EVENTS 10
422 struct epoll_event ev, events[MAX_EVENTS];
423 int listen_sock, conn_sock, nfds, epollfd;
424
425 /* Set up listening socket, \(aqlisten_sock\(aq (socket(),
426    bind(), listen()) */
427
428 epollfd = epoll_create(10);
429 if (epollfd == \-1) {
430     perror("epoll_create");
431     exit(EXIT_FAILURE);
432 }
433
434 ev.events = EPOLLIN;
435 ev.data.fd = listen_sock;
436 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_sock, &ev) == \-1) {
437     perror("epoll_ctl: listen_sock");
438     exit(EXIT_FAILURE);
439 }
440
441 for (;;) {
442     nfds = epoll_wait(epollfd, events, MAX_EVENTS, \-1);
443     if (nfds == \-1) {
444         perror("epoll_pwait");
445         exit(EXIT_FAILURE);
446     }
447
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) {
453                 perror("accept");
454                 exit(EXIT_FAILURE);
455             }
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,
460                         &ev) == \-1) {
461                 perror("epoll_ctl: conn_sock");
462                 exit(EXIT_FAILURE);
463             }
464         } else {
465             do_use_fd(events[n].data.fd);
466         }
467     }
468 }
469 .fi
470 .in
471
472 .\"O When used as an edge-triggered interface, for performance reasons, it is
473 .\"O possible to add the file descriptor inside the
474 .\"O .B epoll
475 .\"O interface
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
481 .\"O .B EPOLLIN
482 .\"O and
483 .\"O .B EPOLLOUT
484 .\"O calling
485 .\"O .BR epoll_ctl (2)
486 .\"O with
487 .\"O .BR EPOLL_CTL_MOD .
488 ¥¨¥Ã¥¸¥È¥ê¥¬¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤È¤·¤Æ»È¤¦¾ì¹ç¡¢À­Ç½¾å¤ÎÍýͳ¤Ë¤è¤ê¡¢
489 °ìÅÙ
490 .RB ( EPOLLIN | EPOLLOUT )
491 ¤ò»ØÄꤷ¤Æ¤«¤é
492 .RB ( EPOLL_CTL_ADD
493 ¤Ç) ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò
494 .B epoll
495 ¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ËÄɲ乤뤳¤È¤¬¤Ç¤­¤ë¡£
496 ¤³¤ì¤Ë¤è¤ê¡¢
497 .BR epoll_ctl (2)
498 ¤Ë
499 .B EPOLL_CTL_MOD
500 ¤ò»ØÄꤷ¤Æ¸Æ¤Ó½Ð¤¹¤³¤È¤Ç
501 .B EPOLLIN
502 ¤È
503 .B EPOLLOUT
504 ¤ÎϢ³Ū¤ÊÀÚ¤êÂؤ¨¤¬Èò¤±¤é¤ì¤ë¡£
505 .\"O .SS Questions and Answers
506 .SS ¼ÁÌä¤È²òÅú
507 .TP 4
508 .B Q0
509 .\"O What is the key used to distinguish the file descriptors registered in an
510 .\"O .B epoll
511 .\"O set?
512 .B epoll
513 ½¸¹çÆâ¤ÎÅÐÏ¿¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò¶èÊ̤¹¤ë¤Ë¤Ï¡¢
514 ²¿¤ò¥­¡¼¤È¤·¤Æ»È¤¨¤Ð¤è¤¤¤«¡©
515 .TP
516 .B A0
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 ¸Æ¤Ð¤ì¡¢¥ª¡¼¥×¥ó¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¤Î¥«¡¼¥Í¥ë¤ÎÆâÉôɽ¸½¤Ç¤¢¤ë)¡£
524 .TP
525 .B Q1
526 .\"O What happens if you register the same file descriptor on an
527 .\"O .B epoll
528 .\"O instance twice?
529 1 ¤Ä¤Î
530 .B epoll
531 ¥¤¥ó¥¹¥¿¥ó¥¹¤ËƱ¤¸¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò 2 ²óÅÐÏ¿¤¹¤ë¤È¤É¤¦¤Ê¤ë¤«¡©
532 .TP
533 .B A1
534 .\"O You will probably get
535 .\"O .BR EEXIST .
536 .\"O However, it is possible to add a duplicate
537 .\"O .RB ( dup (2),
538 .\"O .BR dup2 (2),
539 .\"O .BR fcntl (2)
540 .\"O .BR F_DUPFD )
541 .\"O descriptor to the same
542 .\"O .B epoll
543 .\"O instance.
544 ¤¿¤Ö¤ó
545 .B EEXIST
546 ¤ò¼õ¤±¼è¤ë¤À¤í¤¦¡£
547 ¤·¤«¤·¤Ê¤¬¤é¡¢Æ±¤¸
548 .B epoll
549 ¥¤¥ó¥¹¥¿¥ó¥¹¤ËÂФ·¤ÆÊ£À½¤µ¤ì¤¿¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÄɲ乤뤳¤È¤Ï²Äǽ¤Ç¤¢¤ë
550 .RB ( dup (2),
551 .BR dup2 (2),
552 .BR fcntl (2)
553 .B F_DUPFD
554 ¤Ê¤É)¡£
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.
565 .\"
566 .\" See http://thread.gmane.org/gmane.linux.kernel/596462/
567 .\" "epoll design problems with common fork/exec patterns"
568 .\"
569 .\" mtk, Feb 2008
570 .\"O This can be a useful technique for filtering events,
571 .\"O if the duplicate file descriptors are registered with different
572 .\"O .I events
573 .\"O masks.
574 Ê£À½¤·¤¿¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò°Û¤Ê¤ë
575 .I events
576 ¥Þ¥¹¥¯¤ÇÅÐÏ¿¤¹¤ì¤Ð¡¢¥¤¥Ù¥ó¥È¤ò¥Õ¥£¥ë¥¿¥ê¥ó¥°¤¹¤ë¤Î¤Ë
577 ¤³¤Îµ¡Ç½¤ÏÍ­ÍѤʼêË¡¤Ç¤¢¤ë¡£
578 .TP
579 .B Q2
580 .\"O Can two
581 .\"O .B epoll
582 .\"O instances wait for the same file descriptor?
583 .\"O If so, are events reported to both
584 .\"O .B epoll
585 .\"O file descriptors?
586 2 ¤Ä¤Î
587 .B epoll
588 ¥¤¥ó¥¹¥¿¥ó¥¹¤¬Æ±¤¸¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÂÔ¤Á¼õ¤±¤ë¤³¤È¤Ï²Äǽ¤«¡©
589 ¤â¤·²Äǽ¤Ç¤¢¤ì¤Ð¡¢¥¤¥Ù¥ó¥È¤ÏξÊý¤Î
590 .B epoll
591 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ËÊó¹ð¤µ¤ì¤ë¤«¡©
592 .TP
593 .B A2
594 .\"O Yes, and events would be reported to both.
595 .\"O However, careful programming may be needed to do this correctly.
596 ¥¤¥Ù¥ó¥È¤ÏξÊý¤ËÊó¹ð¤µ¤ì¤ë¡£
597 ¤·¤«¤·¤Ê¤¬¤é¡¢¤³¤ì¤òÀµ¤·¤¯°·¤¦¤Ë¤ÏÃí°Õ¿¼¤¯¥×¥í¥°¥é¥ß¥ó¥°¤¹¤ëɬÍפ¬
598 ¤¢¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
599 .TP
600 .B Q3
601 .\"O Is the
602 .\"O .B epoll
603 .\"O file descriptor itself poll/epoll/selectable?
604 .B epoll
605 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¼«¿È¤Ï poll/epoll/select ¤¬²Äǽ¤«¡©
606 .TP
607 .B A3
608 .\"O Yes.
609 .\"O If an
610 .\"O .B epoll
611 .\"O file descriptor has events waiting then it will
612 .\"O indicate as being readable.
613 ²Äǽ¤Ç¤¢¤ë¡£
614 .B epoll
615 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ë½èÍýÂÔ¤Á¤Î¥¤¥Ù¥ó¥È¤¬¤¢¤ë¾ì¹ç¤Ï¡¢
616 Æɤ߽Ф·²Äǽ¤À¤ÈÄÌÃΤµ¤ì¤ë¤³¤È¤À¤í¤¦¡£
617 .TP
618 .B Q4
619 .\"O What happens if one attempts to put an
620 .\"O .B epoll
621 .\"O file descriptor into its own file descriptor set?
622 .B epoll
623 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò¼«¿È¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤Ë
624 Æþ¤ì¤è¤¦¤È¤¹¤ë¤È¤É¤¦¤Ê¤ë¤«¡©
625 .TP
626 .B A4
627 .\"O The
628 .\"O .BR epoll_ctl (2)
629 .\"O call will fail
630 .\"O .RB ( EINVAL ).
631 .\"O However, you can add an
632 .\"O .B epoll
633 .\"O file descriptor inside another
634 .\"O .B epoll
635 .\"O file descriptor set.
636 .BR epoll_ctl (2)
637 ¤Î¸Æ¤Ó½Ð¤·¤Ï
638 .RB ( EINVAL
639 ¤Ç) ¼ºÇÔ¤¹¤ë¤À¤í¤¦¡£
640 ¤¿¤À¤·
641 .B epoll
642 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò¾¤Î
643 .B epoll
644 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿½¸¹ç¤ÎÆâÉô¤ËÄɲ乤뤳¤È¤Ï²Äǽ¤Ç¤¢¤ë¡£
645 .TP
646 .B Q5
647 .\"O Can I send an
648 .\"O .B epoll
649 .\"O file descriptor over a UNIX domain socket to another process?
650 .B epoll
651 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò UNIX ¥É¥á¥¤¥ó¥½¥±¥Ã¥È¤Ç¾¤Î¥×¥í¥»¥¹¤ËÁ÷¤ë¤³¤È¤Ï²Äǽ¤«¡©
652 .TP
653 .B A5
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
656 .\"O .B epoll
657 .\"O set.
658 ²Äǽ¤À¤¬¡¢¤³¤ì¤ò¤¹¤ë¤³¤È¤Ë°ÕÌ£¤Ï¤Ê¤¤¡£
659 ¤Ê¤¼¤Ê¤é¡¢¼õ¿®Â¦¤Î¥×¥í¥»¥¹¤¬
660 .B epoll
661 ½¸¹çÆâ¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¥³¥Ô¡¼¤ò»ý¤Ã¤Æ¤¤¤Ê¤¤¤«¤é¤Ç¤¢¤ë¡£
662 .TP
663 .B Q6
664 .\"O Will closing a file descriptor cause it to be removed from all
665 .\"O .B epoll
666 .\"O sets automatically?
667 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò¥¯¥í¡¼¥º¤¹¤ë¤È¡¢¤½¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÏÁ´¤Æ¤Î
668 .B epoll
669 ½¸¹ç¤«¤é¼«Æ°Åª¤Ëºï½ü¤µ¤ì¤ë¤«¡©
670 .TP
671 .B A6
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
675 .\"O .BR open (2)).
676 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ï¥ª¡¼¥×¥ó¥Õ¥¡¥¤¥ëµ­½Ò
677 .RB ( open (2)
678 »²¾È) ¤Ø¤Î»²¾È¤Ç¤¢¤ë¡£
679 .\"O Whenever a descriptor is duplicated via
680 .\"O .BR dup (2),
681 .\"O .BR dup2 (2),
682 .\"O .BR fcntl (2)
683 .\"O .BR F_DUPFD ,
684 .\"O or
685 .\"O .BR fork (2),
686 .\"O a new file descriptor referring to the same open file description is
687 .\"O created.
688 ¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÎÊ£À½¤ò
689 .BR dup (2),
690 .BR dup2 (2),
691 .BR fcntl (2)
692 ¤Î
693 .B F_DUPFD
694 ¤ä
695 .BR fork (2)
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
703 .\"O .B epoll
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 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬
710 .B epoll
711 ½¸¹ç¤«¤éºï½ü¤µ¤ì¤ë¤Î¤Ï¡¢Âбþ¤¹¤ë¥ª¡¼¥×¥ó¥Õ¥¡¥¤¥ëµ­½Ò¤ò»²¾È¤·¤Æ¤¤¤ë
712 Á´¤Æ¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬¥¯¥í¡¼¥º¤µ¤ì¤¿¸å¤Ç¤¢¤ë
713 .RB ( epoll_ctl ()
714 .B EPOLL_CTL_DEL
715 ¤ò»È¤Ã¤Æ¤½¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÌÀ¼¨Åª¤Ëºï½ü¤·¤¿¾ì¹ç¤Ë¤âºï½ü¤µ¤ì¤ë)¡£
716 .\"O This means that even after a file descriptor that is part of an
717 .\"O .B epoll
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.
721 ¤³¤Î¤³¤È¤Ï¡¢
722 .B epoll
723 ½¸¹ç¤Ë°¤·¤Æ¤¤¤ë¤¢¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò¥¯¥í¡¼¥º¤·¤¿¸å¤Ç¤¢¤Ã¤Æ¤â¡¢
724 Ʊ¤¸¥Õ¥¡¥¤¥ëµ­½Ò¤ò»²¾È¤¹¤ë¾¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬¥ª¡¼¥×¥ó¤µ¤ì¤Æ¤¤¤ë´Ö¤Ï¡¢
725 ¥¯¥í¡¼¥º¤·¤¿¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿°¸¤Ë¥¤¥Ù¥ó¥È¤¬Êó¹ð¤µ¤ì¤ë²ÄǽÀ­¤¬¤¢¤ë¤È
726 ¤¤¤¦¤³¤È¤ò°ÕÌ£¤¹¤ë¡£
727 .TP
728 .B Q7
729 .\"O If more than one event occurs between
730 .\"O .BR epoll_wait (2)
731 .\"O calls, are they combined or reported separately?
732 2 ¤Ä°Ê¾å¤Î¥¤¥Ù¥ó¥È¤¬
733 .BR epoll_wait (2)
734 ¥³¡¼¥ë¤Î´Ö¤ËȯÀ¸¤·¤¿¾ì¹ç¡¢¤½¤ì¤é¤Ï¤Þ¤È¤á¤ÆÊó¹ð¤µ¤ì¤ë¤«¡¢
735 ¤½¤ì¤È¤âÊÌ¡¹¤ËÊó¹ð¤µ¤ì¤ë¤«¡©
736 .TP
737 .B A7
738 .\"O They will be combined.
739 ¤Þ¤È¤á¤ÆÊó¹ð¤µ¤ì¤ë¤À¤í¤¦¡£
740 .TP
741 .B Q8
742 .\"O Does an operation on a file descriptor affect the
743 .\"O already collected but not yet reported events?
744 ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ËÂФ¹¤ëÁàºî¤Ï¡¢
745 ´û¤Ë½¸¤á¤é¤ì¤Æ¤¤¤ë¤¬¤Þ¤ÀÊó¹ð¤µ¤ì¤Æ¤¤¤Ê¤¤¥¤¥Ù¥ó¥È¤Ë±Æ¶Á¤¹¤ë¤«¡©
746 .TP
747 .B A8
748 .\"O You can do two operations on an existing file descriptor.
749 .\"O Remove would be meaningless for
750 .\"O this case.
751 .\"O Modify will reread available I/O.
752 ´û¸¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ËÂФ·¤Æ 2 ¤Ä¤ÎÁàºî¤ò¹Ô¤¦¤³¤È¤¬¤Ç¤­¤ë¡£
753 ¤³¤Î¾ì¹ç¡¢ºï½ü¤Ë¤Ï°ÕÌ£¤¬¤Ê¤¤¡£
754 Êѹ¹¤¹¤ë¤È¡¢»ÈÍѲÄǽ¤Ê I/O ¤¬ºÆ¤ÓÆɤ߹þ¤Þ¤ì¤ë¡£
755 .TP
756 .B Q9
757 .\"O Do I need to continuously read/write a file descriptor
758 .\"O until
759 .\"O .B EAGAIN
760 .\"O when using the
761 .\"O .B EPOLLET
762 .\"O flag (edge-triggered behavior) ?
763 .B EPOLLET
764 ¥Õ¥é¥° (¥¨¥Ã¥¸¥È¥ê¥¬Æ°ºî) ¤ò»È¤Ã¤Æ¤¤¤ë¾ì¹ç¡¢
765 .B EAGAIN
766 ¤ò¼õ¤±¼è¤ë¤Þ¤Ç¡¢
767 ·Ñ³¤·¤Æ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÆɤ߽ñ¤­¤¹¤ëɬÍפ¬¤¢¤ë¤«¡©
768 .TP
769 .B A9
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.
774 .BR epoll_wait (2)
775 ¤«¤é¥¤¥Ù¥ó¥È¤ò¼õ¤±¼è¤ë¤³¤È¤Ï¡¢
776 ¤½¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬Í׵ᤵ¤ì¤¿ I/O Áàºî¤ËÂФ·¤Æ½àÈ÷ºÑ¤ß¤Ç¤¢¤ë¡¢
777 ¤È¤¤¤¦¤³¤È¤ò¥æ¡¼¥¶¤Ë¼¨¤¹¤â¤Î¤Ç¤¢¤ë¡£
778 .\"O You must consider it ready until the next (nonblocking)
779 .\"O read/write yields
780 .\"O .BR EAGAIN .
781 ¼¡¤Î (¥Ö¥í¥Ã¥¯¤·¤Ê¤¤) read/write ¤Ç
782 .B EAGAIN
783 ¤ò¼õ¤±¼è¤ë¤Þ¤Ç¤Ï¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ï½àÈ÷ºÑ¤ß¤Ç¤¢¤ë¤È
784 ¹Í¤¨¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
785 .\"O When and how you will use the file descriptor is entirely up to you.
786 ¤½¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò¤¤¤Ä¤É¤Î¤è¤¦¤Ë»È¤¦¤«¤Ï¡¢
787 Á´¤¯¥æ¡¼¥¶¤ËǤ¤µ¤ì¤Æ¤ë¡£
788 .sp
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
793 .\"O .BR EAGAIN .
794 ¥Ñ¥±¥Ã¥È»Ø¸þ¤ä¥È¡¼¥¯¥ó»Ø¸þ¤Î¥Õ¥¡¥¤¥ë (Î㤨¤Ð¡¢¥Ç¡¼¥¿¥°¥é¥à¥½¥±¥Ã¥È¡¢
795 canonical ¥â¡¼¥É¤ÎüËö) ¤Ç¤Ï¡¢
796 Æɤ߹þ¤ßÍÑ / ½ñ¤­¹þ¤ßÍѤΠI/O ¶õ´Ö¤ÎËöÈø¤ò¸¡ÃΤ¹¤ëÍ£°ì¤ÎÊýË¡¤Ï
797 .B EAGAIN
798 ¤Ë¤Ê¤ë¤Þ¤Ç read/write ¤ò¹Ô¤¦¤³¤È¤Ç¤¢¤ë¡£
799 .sp
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
803 .\"O descriptor.
804 ¥¹¥È¥ê¡¼¥à»Ø¸þ¤Î¥Õ¥¡¥¤¥ë (Î㤨¤Ð¡¢¥Ñ¥¤¥×¡¢FIFO¡¢¥¹¥È¥ê¡¼¥à¥½¥±¥Ã¥È) ¤Ç¤Ï¡¢
805 Æɤ߹þ¤ßÍÑ / ½ñ¤­¹þ¤ßÍѤΠI/O ¶õ´Ö¤¬»È¤¤¿Ô¤¯¤µ¤ì¤¿¾õÂ֤ϡ¢
806 ÂоݤȤʤë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤«¤éÆɤ߹þ¤ó¤À¥Ç¡¼¥¿Î̤ޤ¿¤Ï
807 ½ñ¤­¹þ¤ó¤À¥Ç¡¼¥¿Î̤ò¥Á¥§¥Ã¥¯¤¹¤ë¤³¤È¤Ç¤â¸¡ÃΤǤ­¤ë¡£
808 .\"O For example, if you call
809 .\"O .BR read (2)
810 .\"O by asking to read a certain amount of data and
811 .\"O .BR read (2)
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
814 .\"O descriptor.
815 .\"O The same is true when writing using
816 .\"O .BR write (2).
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 Î㤨¤Ð¡¢¤¢¤ëÆÃÄê¤ÎÎ̤Υǡ¼¥¿¤òÆɤ߹þ¤à¤¿¤á¤Ë
820 .BR read (2)
821 ¤ò¸Æ¤ó¤À¤È¤­¤Ë¡¢
822 .BR read (2)
823 ¤¬ÊÖ¤·¤¿¥Ð¥¤¥È¿ô¤¬¤½¤ì¤è¤ê¾¯¤Ê¤«¤Ã¤¿¾ì¹ç¡¢
824 ¤½¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÎÆɤ߹þ¤ßÍÑ I/O ¶õ´Ö¤¬
825 »È¤¤¿Ô¤¯¤µ¤ì¤¿¤³¤È¤¬Ê¬¤«¤ë¡£
826 .BR write (2)
827 ¤ò»È¤Ã¤Æ½ñ¤­¹þ¤ß¤ò¤¹¤ë¤È¤­¤â¡¢Æ±¤¸¤³¤È¤¬¸À¤¨¤ë
828 (´Æ»ë¤·¤Æ¤¤¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬¾ï¤Ë¥¹¥È¥ê¡¼¥à»Ø¸þ¤Î¥Õ¥¡¥¤¥ë¤ò
829 »²¾È¤·¤Æ¤¤¤ë¤³¤È¤òÊݾڤǤ­¤Ê¤¤¾ì¹ç¤Ë¤Ï¡¢¸å¼Ô¤Î¼êË¡¤Î»ÈÍѤòÈò¤±¤ë¤³¤È)¡£
830 .\"O .SS Possible Pitfalls and Ways to Avoid Them
831 .SS ¤¢¤ê¤¬¤Á¤ÊÍî¤È¤··ê¤È²óÈòÊýË¡
832 .TP
833 .\"O .B o Starvation (edge-triggered)
834 .B o µ²²î (starvation) (¥¨¥Ã¥¸¥È¥ê¥¬)
835 .PP
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
840 .\"O .BR epoll .)
841 Â礭¤Ê I/O ¶õ´Ö¤¬¤¢¤ë¾ì¹ç¡¢
842 ¤½¤Î I/O ¶õ´Ö¤Î¥Ç¡¼¥¿¤òÁ´¤Æ½èÍý (drain) ¤·¤è¤¦¤È¤¹¤ë¤È¡¢
843 ¾¤Î¥Õ¥¡¥¤¥ë¤¬½èÍý¤µ¤ì¤º¡¢µ²²î¤òȯÀ¸¤µ¤»¤ë¤³¤È¤¬¤¢¤ë
844 (¤³¤ÎÌäÂê¤Ï
845 .B epoll
846 ¤Ë¸ÇÍ­¤Î¤â¤Î¤Ç¤Ï¤Ê¤¤)¡£
847 .PP
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 ¤½¤ì°Ê¸å¤Ë¼õ¤±¼è¤ë¥¤¥Ù¥ó¥È¤ò̵»ë¤¹¤ë¤³¤È¤â¤Ç¤­¤ë¡£
863 .TP
864 .\"O .B o If using an event cache...
865 .B o ¥¤¥Ù¥ó¥È¥­¥ã¥Ã¥·¥å¤ò»È¤Ã¤Æ¤¤¤ë¾ì¹ç
866 .PP
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 ¥¤¥Ù¥ó¥È¥­¥ã¥Ã¥·¥å¤ò»È¤Ã¤Æ¤¤¤ë¾ì¹ç¡¢
876 ¤Þ¤¿¤Ï
877 .BR epoll_wait (2)
878 ¤«¤éÊÖ¤µ¤ì¤¿Á´¤Æ¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò³ÊǼ¤·¤Æ¤¤¤ë¾ì¹ç¡¢
879 ¥¯¥í¡¼¥º¤µ¤ì¤¿¤³¤È¤òưŪ¤Ë¥Þ¡¼¥¯¤¹¤ë
880 (¤Ä¤Þ¤êÁ°¤Î¥¤¥Ù¥ó¥È¤Î½èÍý¤Ë¤è¤Ã¤Æ¥Þ¡¼¥¯¤µ¤ì¤ë) ÊýË¡¤òÄ󶡤¹¤Ù¤­¤Ç¤¢¤ë¡£
881 .BR epoll_wait (2)
882 ¤«¤é 100 ¸Ä¤Î¥¤¥Ù¥ó¥È¤ò¼õ¤±¼è¤ê¡¢
883 ¥¤¥Ù¥ó¥È #47 ¤Ç¤Ï¤¢¤ë¾ò·ï¤Ç¥¤¥Ù¥ó¥È #13 ¤¬ÊĤ¸¤é¤ì¤ë¤È²¾Äꤹ¤ë¡£
884 .\"O If you remove the structure and
885 .\"O .BR close (2)
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 ¤Î¹½Â¤ÂΤòºï½ü¤·¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò
890 .BR close (2)
891 ¤¹¤ë¤È¡¢¥¤¥Ù¥ó¥È¥­¥ã¥Ã¥·¥å¤Ï¤½¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÂԤĥ¤¥Ù¥ó¥È¤¬
892 ¸ºß¤¹¤ë¤È¤¤¤Ã¤Æ¡¢º®Í𤬵¯¤­¤ë¡£
893 .PP
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
897 .\"O .BR close (2),
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 ¤òºï½ü¤·¤Æ
906 .BR close (2)
907 ¤¹¤ë¤¿¤á¤Ë
908 .BR epoll_ctl ( EPOLL_CTL_DEL )
909 ¤ò¸Æ¤Ó½Ð¤·¡¢´ØÏ¢ÉÕ¤±¤é¤ì¤¿ data ¹½Â¤ÂΤòºï½üºÑ¤ß¤È¥Þ¡¼¥¯¤·¤Æ¡¢
910 ¥¯¥ê¡¼¥ó¥¢¥Ã¥×¥ê¥¹¥È¤Ë¥ê¥ó¥¯¤¹¤ë¤³¤È¤Ç¤¢¤ë¡£
911 ¥Ð¥Ã¥Á½èÍý¤ÎÃæ¤Ç¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿ 13 ¤Ë¤Ä¤¤¤Æ¤Î
912 ¾¤Î¥¤¥Ù¥ó¥È¤ò¸«¤Ä¤±¤¿¾ì¹ç¡¢
913 ¤½¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬°ÊÁ°¤Ëºï½ü¤µ¤ì¤¿¤â¤Î¤Ç¤¢¤ë¤Èʬ¤«¤ë¤Î¤Ç¡¢
914 º®Íð¤Ïµ¯¤­¤Ê¤¤¡£
915 .\"O .SH VERSIONS
916 .SH ¥Ð¡¼¥¸¥ç¥ó
917 .\"O The
918 .\"O .B epoll
919 .\"O API was introduced in Linux kernel 2.5.44.
920 .B epoll
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
927 .SH ½àµò
928 .\"O The
929 .\"O .B epoll
930 .\"O API is Linux-specific.
931 .\"O Some other systems provide similar
932 .\"O mechanisms, for example, FreeBSD has
933 .\"O .IR kqueue ,
934 .\"O and Solaris has
935 .\"O .IR /dev/poll .
936 .B epoll
937 API ¤Ï Linux ¸ÇÍ­¤Ç¤¢¤ë¡£
938 ¾¤Î¥·¥¹¥Æ¥à¤Ç¤âƱÍͤε¡¹½¤¬Ä󶡤µ¤ì¤Æ¤¤¤ë¾ì¹ç¤¬¤¢¤ë¡£
939 Î㤨¤Ð¡¢FreeBSD ¤Î
940 .I kqueue
941 ¤ä Solaris ¤Î
942 .I /dev/poll
943 ¤Ê¤É¤Ç¤¢¤ë¡£
944 .\"O .SH "SEE ALSO"
945 .SH ´ØÏ¢¹àÌÜ
946 .BR epoll_create (2),
947 .BR epoll_create1 (2),
948 .BR epoll_ctl (2),
949 .BR epoll_wait (2)