OSDN Git Service

(split) LDP man-pages の original/ を v3.29 に更新。
[linuxjm/LDP_man-pages.git] / original / 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 .TH EPOLL 7 2009-02-01 "Linux" "Linux Programmer's Manual"
22 .SH NAME
23 epoll \- I/O event notification facility
24 .SH SYNOPSIS
25 .B #include <sys/epoll.h>
26 .SH DESCRIPTION
27 .B epoll
28 is a variant of
29 .BR poll (2)
30 that can be used either as an edge-triggered or a level-triggered
31 interface and scales well to large numbers of watched file descriptors.
32 The following system calls are provided to
33 create and manage an
34 .B epoll
35 instance:
36 .IP * 3
37 An
38 .B epoll
39 instance created by
40 .BR epoll_create (2),
41 which returns a file descriptor referring to the epoll instance.
42 (The more recent
43 .BR epoll_create1 (2)
44 extends the functionality of
45 .BR epoll_create (2).)
46 .IP *
47 Interest in particular file descriptors is then registered via
48 .BR epoll_ctl (2).
49 The set of file descriptors currently registered on an
50 .B epoll
51 instance is sometimes called an
52 .I epoll
53 set.
54 .IP *
55 Finally, the actual wait is started by
56 .BR epoll_wait (2).
57 .SS Level-Triggered and Edge-Triggered
58 The
59 .B epoll
60 event distribution interface is able to behave both as edge-triggered
61 (ET) and as level-triggered (LT).
62 The difference between the two mechanisms
63 can be described as follows.
64 Suppose that
65 this scenario happens:
66 .IP 1. 3
67 The file descriptor that represents the read side of a pipe
68 .RI ( rfd )
69 is registered on the
70 .B epoll
71 instance.
72 .IP 2.
73 A pipe writer writes 2 kB of data on the write side of the pipe.
74 .IP 3.
75 A call to
76 .BR epoll_wait (2)
77 is done that will return
78 .I rfd
79 as a ready file descriptor.
80 .IP 4.
81 The pipe reader reads 1 kB of data from
82 .IR rfd .
83 .IP 5.
84 A call to
85 .BR epoll_wait (2)
86 is done.
87 .PP
88 If the
89 .I rfd
90 file descriptor has been added to the
91 .B epoll
92 interface using the
93 .B EPOLLET
94 (edge-triggered)
95 flag, the call to
96 .BR epoll_wait (2)
97 done in step
98 .B 5
99 will probably hang despite the available data still present in the file
100 input buffer;
101 meanwhile the remote peer might be expecting a response based on the
102 data it already sent.
103 The reason for this is that edge-triggered mode only
104 delivers events when changes occur on the monitored file descriptor.
105 So, in step
106 .B 5
107 the caller might end up waiting for some data that is already present inside
108 the input buffer.
109 In the above example, an event on
110 .I rfd
111 will be generated because of the write done in
112 .B 2
113 and the event is consumed in
114 .BR 3 .
115 Since the read operation done in
116 .B 4
117 does not consume the whole buffer data, the call to
118 .BR epoll_wait (2)
119 done in step
120 .B 5
121 might block indefinitely.
122
123 An application that employs the
124 .B EPOLLET
125 flag should use nonblocking file descriptors to avoid having a blocking
126 read or write starve a task that is handling multiple file descriptors.
127 The suggested way to use
128 .B epoll
129 as an edge-triggered
130 .RB ( EPOLLET )
131 interface is as follows:
132 .RS
133 .TP 4
134 .B i
135 with nonblocking file descriptors; and
136 .TP
137 .B ii
138 by waiting for an event only after
139 .BR read (2)
140 or
141 .BR write (2)
142 return
143 .BR EAGAIN .
144 .RE
145 .PP
146 By contrast, when used as a level-triggered interface
147 (the default, when
148 .B EPOLLET
149 is not specified),
150 .B epoll
151 is simply a faster
152 .BR poll (2),
153 and can be used wherever the latter is used since it shares the
154 same semantics.
155
156 Since even with edge-triggered
157 .BR epoll ,
158 multiple events can be generated upon receipt of multiple chunks of data,
159 the caller has the option to specify the
160 .B EPOLLONESHOT
161 flag, to tell
162 .B epoll
163 to disable the associated file descriptor after the receipt of an event with
164 .BR epoll_wait (2).
165 When the
166 .B EPOLLONESHOT
167 flag is specified,
168 it is the caller's responsibility to rearm the file descriptor using
169 .BR epoll_ctl (2)
170 with
171 .BR EPOLL_CTL_MOD .
172 .SS /proc interfaces
173 The following interfaces can be used to limit the amount of
174 kernel memory consumed by epoll:
175 .\" Following was added in 2.6.28, but them removed in 2.6.29
176 .\" .TP
177 .\" .IR /proc/sys/fs/epoll/max_user_instances " (since Linux 2.6.28)"
178 .\" This specifies an upper limit on the number of epoll instances
179 .\" that can be created per real user ID.
180 .TP
181 .IR /proc/sys/fs/epoll/max_user_watches " (since Linux 2.6.28)"
182 This specifies a limit on the total number of
183 file descriptors that a user can register across
184 all epoll instances on the system.
185 The limit is per real user ID.
186 Each registered file descriptor costs roughly 90 bytes on a 32-bit kernel,
187 and roughly 160 bytes on a 64-bit kernel.
188 Currently,
189 .\" 2.6.29 (in 2.6.28, the default was 1/32 of lowmem)
190 the default value for
191 .I max_user_watches
192 is 1/25 (4%) of the available low memory,
193 divided by the registration cost in bytes.
194 .SS Example for Suggested Usage
195 While the usage of
196 .B epoll
197 when employed as a level-triggered interface does have the same
198 semantics as
199 .BR poll (2),
200 the edge-triggered usage requires more clarification to avoid stalls
201 in the application event loop.
202 In this example, listener is a
203 nonblocking socket on which
204 .BR listen (2)
205 has been called.
206 The function
207 .I do_use_fd()
208 uses the new ready file descriptor until
209 .B EAGAIN
210 is returned by either
211 .BR read (2)
212 or
213 .BR write (2).
214 An event-driven state machine application should, after having received
215 .BR EAGAIN ,
216 record its current state so that at the next call to
217 .I do_use_fd()
218 it will continue to
219 .BR read (2)
220 or
221 .BR write (2)
222 from where it stopped before.
223
224 .in +4n
225 .nf
226 #define MAX_EVENTS 10
227 struct epoll_event ev, events[MAX_EVENTS];
228 int listen_sock, conn_sock, nfds, epollfd;
229
230 /* Set up listening socket, \(aqlisten_sock\(aq (socket(),
231    bind(), listen()) */
232
233 epollfd = epoll_create(10);
234 if (epollfd == \-1) {
235     perror("epoll_create");
236     exit(EXIT_FAILURE);
237 }
238
239 ev.events = EPOLLIN;
240 ev.data.fd = listen_sock;
241 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_sock, &ev) == \-1) {
242     perror("epoll_ctl: listen_sock");
243     exit(EXIT_FAILURE);
244 }
245
246 for (;;) {
247     nfds = epoll_wait(epollfd, events, MAX_EVENTS, \-1);
248     if (nfds == \-1) {
249         perror("epoll_pwait");
250         exit(EXIT_FAILURE);
251     }
252
253     for (n = 0; n < nfds; ++n) {
254         if (events[n].data.fd == listen_sock) {
255             conn_sock = accept(listen_sock,
256                             (struct sockaddr *) &local, &addrlen);
257             if (conn_sock == \-1) {
258                 perror("accept");
259                 exit(EXIT_FAILURE);
260             }
261             setnonblocking(conn_sock);
262             ev.events = EPOLLIN | EPOLLET;
263             ev.data.fd = conn_sock;
264             if (epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock,
265                         &ev) == \-1) {
266                 perror("epoll_ctl: conn_sock");
267                 exit(EXIT_FAILURE);
268             }
269         } else {
270             do_use_fd(events[n].data.fd);
271         }
272     }
273 }
274 .fi
275 .in
276
277 When used as an edge-triggered interface, for performance reasons, it is
278 possible to add the file descriptor inside the
279 .B epoll
280 interface
281 .RB ( EPOLL_CTL_ADD )
282 once by specifying
283 .RB ( EPOLLIN | EPOLLOUT ).
284 This allows you to avoid
285 continuously switching between
286 .B EPOLLIN
287 and
288 .B EPOLLOUT
289 calling
290 .BR epoll_ctl (2)
291 with
292 .BR EPOLL_CTL_MOD .
293 .SS Questions and Answers
294 .TP 4
295 .B Q0
296 What is the key used to distinguish the file descriptors registered in an
297 .B epoll
298 set?
299 .TP
300 .B A0
301 The key is the combination of the file descriptor number and
302 the open file description
303 (also known as an "open file handle",
304 the kernel's internal representation of an open file).
305 .TP
306 .B Q1
307 What happens if you register the same file descriptor on an
308 .B epoll
309 instance twice?
310 .TP
311 .B A1
312 You will probably get
313 .BR EEXIST .
314 However, it is possible to add a duplicate
315 .RB ( dup (2),
316 .BR dup2 (2),
317 .BR fcntl (2)
318 .BR F_DUPFD )
319 descriptor to the same
320 .B epoll
321 instance.
322 .\" But a descriptor duplicated by fork(2) can't be added to the
323 .\" set, because the [file *, fd] pair is already in the epoll set.
324 .\" That is a somewhat ugly inconsistency.  On the one hand, a child process
325 .\" cannot add the duplicate file descriptor to the epoll set.  (In every
326 .\" other case that I can think of, descriptors duplicated by fork have
327 .\" similar semantics to descriptors duplicated by dup() and friends.)  On
328 .\" the other hand, the very fact that the child has a duplicate of the
329 .\" descriptor means that even if the parent closes its descriptor, then
330 .\" epoll_wait() in the parent will continue to receive notifications for
331 .\" that descriptor because of the duplicated descriptor in the child.
332 .\"
333 .\" See http://thread.gmane.org/gmane.linux.kernel/596462/
334 .\" "epoll design problems with common fork/exec patterns"
335 .\"
336 .\" mtk, Feb 2008
337 This can be a useful technique for filtering events,
338 if the duplicate file descriptors are registered with different
339 .I events
340 masks.
341 .TP
342 .B Q2
343 Can two
344 .B epoll
345 instances wait for the same file descriptor?
346 If so, are events reported to both
347 .B epoll
348 file descriptors?
349 .TP
350 .B A2
351 Yes, and events would be reported to both.
352 However, careful programming may be needed to do this correctly.
353 .TP
354 .B Q3
355 Is the
356 .B epoll
357 file descriptor itself poll/epoll/selectable?
358 .TP
359 .B A3
360 Yes.
361 If an
362 .B epoll
363 file descriptor has events waiting then it will
364 indicate as being readable.
365 .TP
366 .B Q4
367 What happens if one attempts to put an
368 .B epoll
369 file descriptor into its own file descriptor set?
370 .TP
371 .B A4
372 The
373 .BR epoll_ctl (2)
374 call will fail
375 .RB ( EINVAL ).
376 However, you can add an
377 .B epoll
378 file descriptor inside another
379 .B epoll
380 file descriptor set.
381 .TP
382 .B Q5
383 Can I send an
384 .B epoll
385 file descriptor over a UNIX domain socket to another process?
386 .TP
387 .B A5
388 Yes, but it does not make sense to do this, since the receiving process
389 would not have copies of the file descriptors in the
390 .B epoll
391 set.
392 .TP
393 .B Q6
394 Will closing a file descriptor cause it to be removed from all
395 .B epoll
396 sets automatically?
397 .TP
398 .B A6
399 Yes, but be aware of the following point.
400 A file descriptor is a reference to an open file description (see
401 .BR open (2)).
402 Whenever a descriptor is duplicated via
403 .BR dup (2),
404 .BR dup2 (2),
405 .BR fcntl (2)
406 .BR F_DUPFD ,
407 or
408 .BR fork (2),
409 a new file descriptor referring to the same open file description is
410 created.
411 An open file description continues to exist until all
412 file descriptors referring to it have been closed.
413 A file descriptor is removed from an
414 .B epoll
415 set only after all the file descriptors referring to the underlying
416 open file description have been closed
417 (or before if the descriptor is explicitly removed using
418 .BR epoll_ctl ()
419 .BR EPOLL_CTL_DEL ).
420 This means that even after a file descriptor that is part of an
421 .B epoll
422 set has been closed,
423 events may be reported for that file descriptor if other file
424 descriptors referring to the same underlying file description remain open.
425 .TP
426 .B Q7
427 If more than one event occurs between
428 .BR epoll_wait (2)
429 calls, are they combined or reported separately?
430 .TP
431 .B A7
432 They will be combined.
433 .TP
434 .B Q8
435 Does an operation on a file descriptor affect the
436 already collected but not yet reported events?
437 .TP
438 .B A8
439 You can do two operations on an existing file descriptor.
440 Remove would be meaningless for
441 this case.
442 Modify will reread available I/O.
443 .TP
444 .B Q9
445 Do I need to continuously read/write a file descriptor
446 until
447 .B EAGAIN
448 when using the
449 .B EPOLLET
450 flag (edge-triggered behavior) ?
451 .TP
452 .B A9
453 Receiving an event from
454 .BR epoll_wait (2)
455 should suggest to you that such
456 file descriptor is ready for the requested I/O operation.
457 You must consider it ready until the next (nonblocking)
458 read/write yields
459 .BR EAGAIN .
460 When and how you will use the file descriptor is entirely up to you.
461 .sp
462 For packet/token-oriented files (e.g., datagram socket,
463 terminal in canonical mode),
464 the only way to detect the end of the read/write I/O space
465 is to continue to read/write until
466 .BR EAGAIN .
467 .sp
468 For stream-oriented files (e.g., pipe, FIFO, stream socket), the
469 condition that the read/write I/O space is exhausted can also be detected by
470 checking the amount of data read from / written to the target file
471 descriptor.
472 For example, if you call
473 .BR read (2)
474 by asking to read a certain amount of data and
475 .BR read (2)
476 returns a lower number of bytes, you
477 can be sure of having exhausted the read I/O space for the file
478 descriptor.
479 The same is true when writing using
480 .BR write (2).
481 (Avoid this latter technique if you cannot guarantee that
482 the monitored file descriptor always refers to a stream-oriented file.)
483 .SS Possible Pitfalls and Ways to Avoid Them
484 .TP
485 .B o Starvation (edge-triggered)
486 .PP
487 If there is a large amount of I/O space,
488 it is possible that by trying to drain
489 it the other files will not get processed causing starvation.
490 (This problem is not specific to
491 .BR epoll .)
492 .PP
493 The solution is to maintain a ready list
494 and mark the file descriptor as ready
495 in its associated data structure, thereby allowing the application to
496 remember which files need to be processed but still round robin amongst
497 all the ready files.
498 This also supports ignoring subsequent events you
499 receive for file descriptors that are already ready.
500 .TP
501 .B o If using an event cache...
502 .PP
503 If you use an event cache or store all the file descriptors returned from
504 .BR epoll_wait (2),
505 then make sure to provide a way to mark
506 its closure dynamically (i.e., caused by
507 a previous event's processing).
508 Suppose you receive 100 events from
509 .BR epoll_wait (2),
510 and in event #47 a condition causes event #13 to be closed.
511 If you remove the structure and
512 .BR close (2)
513 the file descriptor for event #13, then your
514 event cache might still say there are events waiting for that
515 file descriptor causing confusion.
516 .PP
517 One solution for this is to call, during the processing of event 47,
518 .BR epoll_ctl ( EPOLL_CTL_DEL )
519 to delete file descriptor 13 and
520 .BR close (2),
521 then mark its associated
522 data structure as removed and link it to a cleanup list.
523 If you find another
524 event for file descriptor 13 in your batch processing,
525 you will discover the file descriptor had been
526 previously removed and there will be no confusion.
527 .SH VERSIONS
528 The
529 .B epoll
530 API was introduced in Linux kernel 2.5.44.
531 .\" Its interface should be finalized in Linux kernel 2.5.66.
532 Support was added to glibc in version 2.3.2.
533 .SH CONFORMING TO
534 The
535 .B epoll
536 API is Linux-specific.
537 Some other systems provide similar
538 mechanisms, for example, FreeBSD has
539 .IR kqueue ,
540 and Solaris has
541 .IR /dev/poll .
542 .SH "SEE ALSO"
543 .BR epoll_create (2),
544 .BR epoll_create1 (2),
545 .BR epoll_ctl (2),
546 .BR epoll_wait (2)