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>
22 .TH EPOLL 7 "2002-10-23" Linux "Linux Programmer's Manual"
24 epoll \- I/O event notification facility
26 .B #include <sys/epoll.h>
31 that can be used either as Edge or Level Triggered interface and scales
32 well to large numbers of watched fds.
33 Three system calls are provided to
43 set is connected to a file descriptor created by
45 Interest for certain file descriptors is then registered via
47 Finally, the actual wait is started by
52 event distribution interface is able to behave both as Edge Triggered
53 ( ET ) and Level Triggered ( LT ).
54 The difference between ET and LT
55 event distribution mechanism can be described as follows.
57 this scenario happens :
60 The file descriptor that represents the read side of a pipe (
67 Pipe writer writes 2Kb of data on the write side of the pipe.
72 is done that will return
74 as ready file descriptor.
77 The pipe reader reads 1Kb of data from
87 file descriptor has been added to the
95 will probably hang because of the available data still present in the file
96 input buffers and the remote peer might be expecting a response based on the
98 The reason for this is that Edge Triggered event
99 distribution delivers events only when events happens on the monitored file.
102 the caller might end up waiting for some data that is already present inside
104 In the above example, an event on
106 will be generated because of the write done in
108 and the event is consumed in
110 Since the read operation done in
112 does not consume the whole buffer data, the call to
116 might lock indefinitely.
119 interface, when used with the
121 flag ( Edge Triggered )
122 should use non-blocking file descriptors to avoid having a blocking
123 read or write starve the task that is handling multiple file descriptors.
124 The suggested way to use
128 interface is below, and possible pitfalls to avoid follow.
132 with non-blocking file descriptors
135 by going to wait for an event only after
142 On the contrary, when used as a Level Triggered interface,
144 is by all means a faster
146 and can be used wherever the latter is used since it shares the
148 Since even with the Edge Triggered
150 multiple events can be generated up on receipt of multiple chunks of data,
151 the caller has the option to specify the
155 to disable the associated file descriptor after the receipt of an event with
160 it is caller responsibility to rearm the file descriptor using
164 .SH EXAMPLE FOR SUGGESTED USAGE
167 when employed like a Level Triggered interface does have the same
170 an Edge Triggered usage requires more clarification to avoid stalls
171 in the application event loop.
172 In this example, listener is a
173 non-blocking socket on which
176 The function do_use_fd() uses the new ready
177 file descriptor until EAGAIN is returned by either
181 An event driven state machine application should, after having received
182 EAGAIN, record its current state so that at the next call to do_use_fd()
187 from where it stopped before.
190 struct epoll_event ev, *events;
193 nfds = epoll_wait(kdpfd, events, maxevents, \-1);
195 for (n = 0; n < nfds; ++n) {
196 if (events[n].data.fd == listener) {
197 client = accept(listener, (struct sockaddr *) &local,
203 setnonblocking(client);
204 ev.events = EPOLLIN | EPOLLET;
206 if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, client, &ev) < 0) {
207 fprintf(stderr, "epoll set insertion error: fd=%d\\n",
212 do_use_fd(events[n].data.fd);
218 When used as an Edge triggered interface, for performance reasons, it is
219 possible to add the file descriptor inside the epoll interface (
221 ) once by specifying (
222 .BR EPOLLIN | EPOLLOUT
224 This allows you to avoid
225 continuously switching between
233 .SH QUESTIONS AND ANSWERS
236 What happens if you add the same fd to an epoll_set twice?
239 You will probably get EEXIST.
240 However, it is possible that two
241 threads may add the same fd twice.
242 This is a harmless condition.
247 sets wait for the same fd? If so, are events reported
254 However, it is not recommended.
255 Yes it would be reported to both.
260 fd itself poll/epoll/selectable?
268 fd is put into its own fd set?
272 However, you can add an
274 fd inside another epoll fd set.
279 fd over a unix-socket to another process?
285 Will the close of an fd cause it to be removed from all
293 If more than one event comes in between
295 calls, are they combined or reported separately?
298 They will be combined.
301 Does an operation on an fd affect the already collected but not yet reported
305 You can do two operations on an existing fd.
306 Remove would be meaningless for
308 Modify will re-read available I/O.
311 Do I need to continuously read/write an fd until EAGAIN when using the
313 flag ( Edge Triggered behaviour ) ?
317 Receiving an event from
319 should suggest to you that such file descriptor is ready
320 for the requested I/O operation.
321 You have simply to consider it ready until you will receive the
323 When and how you will use such file descriptor is entirely up
325 Also, the condition that the read/write I/O space is exhausted can
326 be detected by checking the amount of data read/write from/to the target
328 For example, if you call
330 by asking to read a certain amount of data and
332 returns a lower number of bytes, you can be sure to have exhausted the read
333 I/O space for such file descriptor.
334 Same is valid when writing using the
337 .SH POSSIBLE PITFALLS AND WAYS TO AVOID THEM
339 .B o Starvation ( Edge Triggered )
341 If there is a large amount of I/O space,
342 it is possible that by trying to drain
343 it the other files will not get processed causing starvation.
344 This is not specific to
347 The solution is to maintain a ready list
348 and mark the file descriptor as ready
349 in its associated data structure, thereby allowing the application to
350 remember which files need to be processed but still round robin amongst
352 This also supports ignoring subsequent events you
353 receive for fd's that are already ready.
355 .B o If using an event cache...
357 If you use an event cache or store all the fd's returned from
359 then make sure to provide a way to mark
360 its closure dynamically (ie- caused by
361 a previous event's processing).
362 Suppose you receive 100 events from
364 and in event #47 a condition causes event #13 to be closed.
365 If you remove the structure and
367 the fd for event #13, then your
368 event cache might still say there are events waiting for that fd causing
371 One solution for this is to call, during the processing of event 47,
372 .BR epoll_ctl ( EPOLL_CTL_DEL )
375 then mark its associated
376 data structure as removed and link it to a cleanup list.
378 event for fd 13 in your batch processing, you will discover the fd had been
379 previously removed and there will be no confusion.
381 The epoll API is Linux specific.
382 Some other systems provide similar
383 mechanisms, e.g., FreeBSD has
389 is a new API introduced in Linux kernel 2.5.44.
390 Its interface should be finalized in Linux kernel 2.5.66.
392 .BR epoll_create (2),