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 .TH EPOLL 7 2009-02-01 "Linux" "Linux Programmer's Manual"
23 epoll \- I/O event notification facility
25 .B #include <sys/epoll.h>
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
41 which returns a file descriptor referring to the epoll instance.
44 extends the functionality of
45 .BR epoll_create (2).)
47 Interest in particular file descriptors is then registered via
49 The set of file descriptors currently registered on an
51 instance is sometimes called an
55 Finally, the actual wait is started by
57 .SS Level-Triggered and Edge-Triggered
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.
65 this scenario happens:
67 The file descriptor that represents the read side of a pipe
73 A pipe writer writes 2 kB of data on the write side of the pipe.
77 is done that will return
79 as a ready file descriptor.
81 The pipe reader reads 1 kB of data from
90 file descriptor has been added to the
99 will probably hang despite the available data still present in the file
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.
107 the caller might end up waiting for some data that is already present inside
109 In the above example, an event on
111 will be generated because of the write done in
113 and the event is consumed in
115 Since the read operation done in
117 does not consume the whole buffer data, the call to
121 might block indefinitely.
123 An application that employs the
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
131 interface is as follows:
135 with nonblocking file descriptors; and
138 by waiting for an event only after
146 By contrast, when used as a level-triggered interface
153 and can be used wherever the latter is used since it shares the
156 Since even with edge-triggered
158 multiple events can be generated upon receipt of multiple chunks of data,
159 the caller has the option to specify the
163 to disable the associated file descriptor after the receipt of an event with
168 it is the caller's responsibility to rearm the file descriptor using
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
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.
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.
189 .\" 2.6.29 (in 2.6.28, the default was 1/32 of lowmem)
190 the default value for
192 is 1/25 (4%) of the available low memory,
193 divided by the registration cost in bytes.
194 .SS Example for Suggested Usage
197 when employed as a level-triggered interface does have the same
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
208 uses the new ready file descriptor until
210 is returned by either
214 An event-driven state machine application should, after having received
216 record its current state so that at the next call to
222 from where it stopped before.
226 #define MAX_EVENTS 10
227 struct epoll_event ev, events[MAX_EVENTS];
228 int listen_sock, conn_sock, nfds, epollfd;
230 /* Set up listening socket, \(aqlisten_sock\(aq (socket(),
233 epollfd = epoll_create(10);
234 if (epollfd == \-1) {
235 perror("epoll_create");
240 ev.data.fd = listen_sock;
241 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_sock, &ev) == \-1) {
242 perror("epoll_ctl: listen_sock");
247 nfds = epoll_wait(epollfd, events, MAX_EVENTS, \-1);
249 perror("epoll_pwait");
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) {
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,
266 perror("epoll_ctl: conn_sock");
270 do_use_fd(events[n].data.fd);
277 When used as an edge-triggered interface, for performance reasons, it is
278 possible to add the file descriptor inside the
281 .RB ( EPOLL_CTL_ADD )
283 .RB ( EPOLLIN | EPOLLOUT ).
284 This allows you to avoid
285 continuously switching between
293 .SS Questions and Answers
296 What is the key used to distinguish the file descriptors registered in an
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).
307 What happens if you register the same file descriptor on an
312 You will probably get
314 However, it is possible to add a duplicate
319 descriptor to the same
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.
333 .\" See http://thread.gmane.org/gmane.linux.kernel/596462/
334 .\" "epoll design problems with common fork/exec patterns"
337 This can be a useful technique for filtering events,
338 if the duplicate file descriptors are registered with different
345 instances wait for the same file descriptor?
346 If so, are events reported to both
351 Yes, and events would be reported to both.
352 However, careful programming may be needed to do this correctly.
357 file descriptor itself poll/epoll/selectable?
363 file descriptor has events waiting then it will
364 indicate as being readable.
367 What happens if one attempts to put an
369 file descriptor into its own file descriptor set?
376 However, you can add an
378 file descriptor inside another
385 file descriptor over a UNIX domain socket to another process?
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
394 Will closing a file descriptor cause it to be removed from all
399 Yes, but be aware of the following point.
400 A file descriptor is a reference to an open file description (see
402 Whenever a descriptor is duplicated via
409 a new file descriptor referring to the same open file description is
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
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
420 This means that even after a file descriptor that is part of an
423 events may be reported for that file descriptor if other file
424 descriptors referring to the same underlying file description remain open.
427 If more than one event occurs between
429 calls, are they combined or reported separately?
432 They will be combined.
435 Does an operation on a file descriptor affect the
436 already collected but not yet reported events?
439 You can do two operations on an existing file descriptor.
440 Remove would be meaningless for
442 Modify will reread available I/O.
445 Do I need to continuously read/write a file descriptor
450 flag (edge-triggered behavior) ?
453 Receiving an event from
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)
460 When and how you will use the file descriptor is entirely up to you.
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
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
472 For example, if you call
474 by asking to read a certain amount of data and
476 returns a lower number of bytes, you
477 can be sure of having exhausted the read I/O space for the file
479 The same is true when writing using
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
485 .B o Starvation (edge-triggered)
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
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
498 This also supports ignoring subsequent events you
499 receive for file descriptors that are already ready.
501 .B o If using an event cache...
503 If you use an event cache or store all the file descriptors returned from
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
510 and in event #47 a condition causes event #13 to be closed.
511 If you remove the structure and
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.
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
521 then mark its associated
522 data structure as removed and link it to a cleanup list.
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.
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.
536 API is Linux-specific.
537 Some other systems provide similar
538 mechanisms, for example, FreeBSD has
543 .BR epoll_create (2),
544 .BR epoll_create1 (2),