1 .\" Copyright (C) 2003 Davide Libenzi
3 .\" %%%LICENSE_START(GPLv2+_SW_3_PARA)
4 .\" This program is free software; you can redistribute it and/or modify
5 .\" it under the terms of the GNU General Public License as published by
6 .\" the Free Software Foundation; either version 2 of the License, or
7 .\" (at your option) any later version.
9 .\" This program is distributed in the hope that it will be useful,
10 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
11 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 .\" GNU General Public License for more details.
14 .\" You should have received a copy of the GNU General Public
15 .\" License along with this manual; if not, see
16 .\" <http://www.gnu.org/licenses/>.
19 .\" Davide Libenzi <davidel@xmailserver.org>
21 .TH EPOLL 7 2016-10-08 "Linux" "Linux Programmer's Manual"
23 epoll \- I/O event notification facility
25 .B #include <sys/epoll.h>
29 API performs a similar task to
31 monitoring multiple file descriptors to see if I/O is possible on any of them.
34 API can be used either as an edge-triggered or a level-triggered
35 interface and scales well to large numbers of watched file descriptors.
36 The following system calls are provided to
44 instance and returns a file descriptor referring to that instance.
47 extends the functionality of
48 .BR epoll_create (2).)
50 Interest in particular file descriptors is then registered via
52 The set of file descriptors currently registered on an
54 instance is sometimes called an
60 blocking the calling thread if no events are currently available.
61 .SS Level-triggered and edge-triggered
64 event distribution interface is able to behave both as edge-triggered
65 (ET) and as level-triggered (LT).
66 The difference between the two mechanisms
67 can be described as follows.
69 this scenario happens:
71 The file descriptor that represents the read side of a pipe
77 A pipe writer writes 2 kB of data on the write side of the pipe.
81 is done that will return
83 as a ready file descriptor.
85 The pipe reader reads 1 kB of data from
94 file descriptor has been added to the
103 will probably hang despite the available data still present in the file
105 meanwhile the remote peer might be expecting a response based on the
106 data it already sent.
107 The reason for this is that edge-triggered mode
108 delivers events only when changes occur on the monitored file descriptor.
111 the caller might end up waiting for some data that is already present inside
113 In the above example, an event on
115 will be generated because of the write done in
117 and the event is consumed in
119 Since the read operation done in
121 does not consume the whole buffer data, the call to
125 might block indefinitely.
127 An application that employs the
129 flag should use nonblocking file descriptors to avoid having a blocking
130 read or write starve a task that is handling multiple file descriptors.
131 The suggested way to use
135 interface is as follows:
139 with nonblocking file descriptors; and
142 by waiting for an event only after
150 By contrast, when used as a level-triggered interface
157 and can be used wherever the latter is used since it shares the
160 Since even with edge-triggered
162 multiple events can be generated upon receipt of multiple chunks of data,
163 the caller has the option to specify the
167 to disable the associated file descriptor after the receipt of an event with
172 it is the caller's responsibility to rearm the file descriptor using
176 .SS Interaction with autosleep
180 .I /sys/power/autosleep
181 and an event happens which wakes the device from sleep, the device
182 driver will keep the device awake only until that event is queued.
183 To keep the device awake until the event has been processed,
184 it is necessary to use the
194 .IR "struct epoll_event" ,
195 the system will be kept awake from the moment the event is queued,
198 call which returns the event until the subsequent
201 If the event should keep the system awake beyond that time,
204 should be taken before the second
208 The following interfaces can be used to limit the amount of
209 kernel memory consumed by epoll:
210 .\" Following was added in 2.6.28, but them removed in 2.6.29
212 .\" .IR /proc/sys/fs/epoll/max_user_instances " (since Linux 2.6.28)"
213 .\" This specifies an upper limit on the number of epoll instances
214 .\" that can be created per real user ID.
216 .IR /proc/sys/fs/epoll/max_user_watches " (since Linux 2.6.28)"
217 This specifies a limit on the total number of
218 file descriptors that a user can register across
219 all epoll instances on the system.
220 The limit is per real user ID.
221 Each registered file descriptor costs roughly 90 bytes on a 32-bit kernel,
222 and roughly 160 bytes on a 64-bit kernel.
224 .\" 2.6.29 (in 2.6.28, the default was 1/32 of lowmem)
225 the default value for
227 is 1/25 (4%) of the available low memory,
228 divided by the registration cost in bytes.
229 .SS Example for suggested usage
232 when employed as a level-triggered interface does have the same
235 the edge-triggered usage requires more clarification to avoid stalls
236 in the application event loop.
237 In this example, listener is a
238 nonblocking socket on which
243 uses the new ready file descriptor until
245 is returned by either
249 An event-driven state machine application should, after having received
251 record its current state so that at the next call to
257 from where it stopped before.
261 #define MAX_EVENTS 10
262 struct epoll_event ev, events[MAX_EVENTS];
263 int listen_sock, conn_sock, nfds, epollfd;
265 /* Code to set up listening socket, \(aqlisten_sock\(aq,
266 (socket(), bind(), listen()) omitted */
268 epollfd = epoll_create1(0);
269 if (epollfd == \-1) {
270 perror("epoll_create1");
275 ev.data.fd = listen_sock;
276 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_sock, &ev) == \-1) {
277 perror("epoll_ctl: listen_sock");
282 nfds = epoll_wait(epollfd, events, MAX_EVENTS, \-1);
284 perror("epoll_wait");
288 for (n = 0; n < nfds; ++n) {
289 if (events[n].data.fd == listen_sock) {
290 conn_sock = accept(listen_sock,
291 (struct sockaddr *) &addr, &addrlen);
292 if (conn_sock == \-1) {
296 setnonblocking(conn_sock);
297 ev.events = EPOLLIN | EPOLLET;
298 ev.data.fd = conn_sock;
299 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock,
301 perror("epoll_ctl: conn_sock");
305 do_use_fd(events[n].data.fd);
312 When used as an edge-triggered interface, for performance reasons, it is
313 possible to add the file descriptor inside the
316 .RB ( EPOLL_CTL_ADD )
318 .RB ( EPOLLIN | EPOLLOUT ).
319 This allows you to avoid
320 continuously switching between
328 .SS Questions and answers
331 What is the key used to distinguish the file descriptors registered in an
336 The key is the combination of the file descriptor number and
337 the open file description
338 (also known as an "open file handle",
339 the kernel's internal representation of an open file).
342 What happens if you register the same file descriptor on an
347 You will probably get
349 However, it is possible to add a duplicate
354 file descriptor to the same
357 .\" But a file descriptor duplicated by fork(2) can't be added to the
358 .\" set, because the [file *, fd] pair is already in the epoll set.
359 .\" That is a somewhat ugly inconsistency. On the one hand, a child process
360 .\" cannot add the duplicate file descriptor to the epoll set. (In every
361 .\" other case that I can think of, file descriptors duplicated by fork have
362 .\" similar semantics to file descriptors duplicated by dup() and friends.) On
363 .\" the other hand, the very fact that the child has a duplicate of the
364 .\" file descriptor means that even if the parent closes its file descriptor,
365 .\" then epoll_wait() in the parent will continue to receive notifications for
366 .\" that file descriptor because of the duplicated file descriptor in the child.
368 .\" See http://thread.gmane.org/gmane.linux.kernel/596462/
369 .\" "epoll design problems with common fork/exec patterns"
372 This can be a useful technique for filtering events,
373 if the duplicate file descriptors are registered with different
380 instances wait for the same file descriptor?
381 If so, are events reported to both
386 Yes, and events would be reported to both.
387 However, careful programming may be needed to do this correctly.
392 file descriptor itself poll/epoll/selectable?
398 file descriptor has events waiting, then it will
399 indicate as being readable.
402 What happens if one attempts to put an
404 file descriptor into its own file descriptor set?
411 However, you can add an
413 file descriptor inside another
420 file descriptor over a UNIX domain socket to another process?
423 Yes, but it does not make sense to do this, since the receiving process
424 would not have copies of the file descriptors in the
429 Will closing a file descriptor cause it to be removed from all
434 Yes, but be aware of the following point.
435 A file descriptor is a reference to an open file description (see
437 Whenever a file descriptor is duplicated via
444 a new file descriptor referring to the same open file description is
446 An open file description continues to exist until all
447 file descriptors referring to it have been closed.
448 A file descriptor is removed from an
450 set only after all the file descriptors referring to the underlying
451 open file description have been closed
452 (or before if the file descriptor is explicitly removed using
455 This means that even after a file descriptor that is part of an
458 events may be reported for that file descriptor if other file
459 descriptors referring to the same underlying file description remain open.
462 If more than one event occurs between
464 calls, are they combined or reported separately?
467 They will be combined.
470 Does an operation on a file descriptor affect the
471 already collected but not yet reported events?
474 You can do two operations on an existing file descriptor.
475 Remove would be meaningless for
477 Modify will reread available I/O.
480 Do I need to continuously read/write a file descriptor
485 flag (edge-triggered behavior) ?
488 Receiving an event from
490 should suggest to you that such
491 file descriptor is ready for the requested I/O operation.
492 You must consider it ready until the next (nonblocking)
495 When and how you will use the file descriptor is entirely up to you.
497 For packet/token-oriented files (e.g., datagram socket,
498 terminal in canonical mode),
499 the only way to detect the end of the read/write I/O space
500 is to continue to read/write until
503 For stream-oriented files (e.g., pipe, FIFO, stream socket), the
504 condition that the read/write I/O space is exhausted can also be detected by
505 checking the amount of data read from / written to the target file
507 For example, if you call
509 by asking to read a certain amount of data and
511 returns a lower number of bytes, you
512 can be sure of having exhausted the read I/O space for the file
514 The same is true when writing using
516 (Avoid this latter technique if you cannot guarantee that
517 the monitored file descriptor always refers to a stream-oriented file.)
518 .SS Possible pitfalls and ways to avoid them
520 .B o Starvation (edge-triggered)
522 If there is a large amount of I/O space,
523 it is possible that by trying to drain
524 it the other files will not get processed causing starvation.
525 (This problem is not specific to
528 The solution is to maintain a ready list
529 and mark the file descriptor as ready
530 in its associated data structure, thereby allowing the application to
531 remember which files need to be processed but still round robin amongst
533 This also supports ignoring subsequent events you
534 receive for file descriptors that are already ready.
536 .B o If using an event cache...
538 If you use an event cache or store all the file descriptors returned from
540 then make sure to provide a way to mark
541 its closure dynamically (i.e., caused by
542 a previous event's processing).
543 Suppose you receive 100 events from
545 and in event #47 a condition causes event #13 to be closed.
546 If you remove the structure and
548 the file descriptor for event #13, then your
549 event cache might still say there are events waiting for that
550 file descriptor causing confusion.
552 One solution for this is to call, during the processing of event 47,
553 .BR epoll_ctl ( EPOLL_CTL_DEL )
554 to delete file descriptor 13 and
556 then mark its associated
557 data structure as removed and link it to a cleanup list.
559 event for file descriptor 13 in your batch processing,
560 you will discover the file descriptor had been
561 previously removed and there will be no confusion.
565 API was introduced in Linux kernel 2.5.44.
566 .\" Its interface should be finalized in Linux kernel 2.5.66.
567 Support was added to glibc in version 2.3.2.
571 API is Linux-specific.
572 Some other systems provide similar
573 mechanisms, for example, FreeBSD has
578 The set of file descriptors that is being monitored via
579 an epoll file descriptor can be viewed via the entry for
580 the epoll file descriptor in the process's
581 .IR /proc/[pid]/fdinfo
587 .BR epoll_create (2),
588 .BR epoll_create1 (2),