]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/epoll.7
Moved BUGS to inotify.7.
[thirdparty/man-pages.git] / man7 / epoll.7
CommitLineData
fea681da
MK
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.\"
6d86c355 22.TH EPOLL 7 "2002-10-23" Linux "Linux Programmer's Manual"
fea681da
MK
23.SH NAME
24epoll \- I/O event notification facility
25.SH SYNOPSIS
26.B #include <sys/epoll.h>
27.SH DESCRIPTION
28.B epoll
29is a variant of
30.BR poll (2)
31that can be used either as Edge or Level Triggered interface and scales
32well to large numbers of watched fds. Three system calls are provided to
33set up and control an
34.B epoll
35set:
36.BR epoll_create (2),
37.BR epoll_ctl (2),
38.BR epoll_wait (2).
39
40An
41.B epoll
42set is connected to a file descriptor created by
43.BR epoll_create (2).
44Interest for certain file descriptors is then registered via
45.BR epoll_ctl (2).
46Finally, the actual wait is started by
47.BR epoll_wait (2).
fea681da
MK
48.SH NOTES
49The
50.B epoll
51event distribution interface is able to behave both as Edge Triggered
52( ET ) and Level Triggered ( LT ). The difference between ET and LT
53event distribution mechanism can be described as follows. Suppose that
54this scenario happens :
55.TP
56.B 1
57The file descriptor that represents the read side of a pipe (
58.B RFD
59) is added inside the
60.B epoll
61device.
62.TP
63.B 2
64Pipe writer writes 2Kb of data on the write side of the pipe.
65.TP
66.B 3
67A call to
68.BR epoll_wait (2)
69is done that will return
70.B RFD
71as ready file descriptor.
72.TP
73.B 4
74The pipe reader reads 1Kb of data from
75.BR RFD .
76.TP
77.B 5
78A call to
79.BR epoll_wait (2)
80is done.
81.PP
82
83If the
84.B RFD
85file descriptor has been added to the
86.B epoll
87interface using the
88.B EPOLLET
89flag, the call to
90.BR epoll_wait (2)
91done in step
92.B 5
93will probably hang because of the available data still present in the file
94input buffers and the remote peer might be expecting a response based on the
95data it already sent. The reason for this is that Edge Triggered event
96distribution delivers events only when events happens on the monitored file.
97So, in step
98.B 5
99the caller might end up waiting for some data that is already present inside
100the input buffer. In the above example, an event on
101.B RFD
102will be generated because of the write done in
66eca51e
MK
103.BR 2
104and the event is consumed in
fea681da
MK
105.BR 3 .
106Since the read operation done in
107.B 4
108does not consume the whole buffer data, the call to
109.BR epoll_wait (2)
110done in step
111.B 5
112might lock indefinitely. The
113.B epoll
114interface, when used with the
115.B EPOLLET
116flag ( Edge Triggered )
117should use non-blocking file descriptors to avoid having a blocking
118read or write starve the task that is handling multiple file descriptors.
119The suggested way to use
120.B epoll
66eca51e
MK
121as an Edge Triggered
122.RB ( EPOLLET )
123interface is below, and possible pitfalls to avoid follow.
fea681da
MK
124.RS
125.TP
126.B i
127with non-blocking file descriptors
128.TP
129.B ii
130by going to wait for an event only after
131.BR read (2)
132or
133.BR write (2)
134return EAGAIN
135.RE
136.PP
137On the contrary, when used as a Level Triggered interface,
138.B epoll
139is by all means a faster
140.BR poll (2),
141and can be used wherever the latter is used since it shares the
142same semantics. Since even with the Edge Triggered
143.B epoll
3f1c1b0a 144multiple events can be generated up on receipt of multiple chunks of data,
fea681da
MK
145the caller has the option to specify the
146.B EPOLLONESHOT
147flag, to tell
148.B epoll
3f1c1b0a 149to disable the associated file descriptor after the receipt of an event with
fea681da
MK
150.BR epoll_wait (2).
151When the
152.B EPOLLONESHOT
153flag is specified, it is caller responsibility to rearm the file descriptor using
154.BR epoll_ctl (2)
155with
156.BR EPOLL_CTL_MOD .
fea681da 157.SH EXAMPLE FOR SUGGESTED USAGE
fea681da
MK
158While the usage of
159.B epoll
160when employed like a Level Triggered interface does have the same
161semantics of
162.BR poll (2),
9fdfa163 163an Edge Triggered usage requires more clarification to avoid stalls
fea681da
MK
164in the application event loop. In this example, listener is a
165non-blocking socket on which
166.BR listen (2)
167has been called. The function do_use_fd() uses the new ready
168file descriptor until EAGAIN is returned by either
169.BR read (2)
170or
171.BR write (2).
172An event driven state machine application should, after having received
173EAGAIN, record its current state so that at the next call to do_use_fd()
174it will continue to
175.BR read (2)
176or
177.BR write (2)
178from where it stopped before.
179
180.nf
181struct epoll_event ev, *events;
182
183for(;;) {
2bc2f479 184 nfds = epoll_wait(kdpfd, events, maxevents, \-1);
fea681da
MK
185
186 for(n = 0; n < nfds; ++n) {
187 if(events[n].data.fd == listener) {
188 client = accept(listener, (struct sockaddr *) &local,
189 &addrlen);
190 if(client < 0){
191 perror("accept");
192 continue;
193 }
194 setnonblocking(client);
195 ev.events = EPOLLIN | EPOLLET;
196 ev.data.fd = client;
197 if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, client, &ev) < 0) {
7dfefab8 198 fprintf(stderr, "epoll set insertion error: fd=%d\\n",
fea681da 199 client);
2bc2f479 200 return \-1;
fea681da
MK
201 }
202 }
203 else
204 do_use_fd(events[n].data.fd);
205 }
206}
207.fi
208
209When used as an Edge triggered interface, for performance reasons, it is
210possible to add the file descriptor inside the epoll interface (
211.B EPOLL_CTL_ADD
212) once by specifying (
213.BR EPOLLIN | EPOLLOUT
214). This allows you to avoid
215continuously switching between
216.B EPOLLIN
217and
218.B EPOLLOUT
219calling
220.BR epoll_ctl (2)
221with
222.BR EPOLL_CTL_MOD .
223
1de2d7cd 224.SH QUESTIONS AND ANSWERS
fea681da 225
fea681da
MK
226.TP
227.B Q1
228What happens if you add the same fd to an epoll_set twice?
229.TP
230.B A1
231You will probably get EEXIST. However, it is possible that two
232threads may add the same fd twice. This is a harmless condition.
233.TP
234.B Q2
235Can two
236.B epoll
237sets wait for the same fd? If so, are events reported
238to both
239.B epoll
240sets fds?
241.TP
242.B A2
243Yes. However, it is not recommended. Yes it would be reported to both.
244.TP
245.B Q3
246Is the
247.B epoll
248fd itself poll/epoll/selectable?
249.TP
250.B A3
251Yes.
252.TP
253.B Q4
254What happens if the
255.B epoll
256fd is put into its own fd set?
257.TP
258.B A4
259It will fail. However, you can add an
260.B epoll
261fd inside another epoll fd set.
262.TP
263.B Q5
264Can I send the
265.B epoll
266fd over a unix-socket to another process?
267.TP
268.B A5
269No.
270.TP
271.B Q6
272Will the close of an fd cause it to be removed from all
273.B epoll
274sets automatically?
275.TP
276.B A6
277Yes.
278.TP
279.B Q7
280If more than one event comes in between
281.BR epoll_wait (2)
282calls, are they combined or reported separately?
283.TP
284.B A7
285They will be combined.
286.TP
287.B Q8
288Does an operation on an fd affect the already collected but not yet reported
289events?
290.TP
291.B A8
292You can do two operations on an existing fd. Remove would be meaningless for
293this case. Modify will re-read available I/O.
294.TP
295.B Q9
296Do I need to continuously read/write an fd until EAGAIN when using the
297.B EPOLLET
298flag ( Edge Triggered behaviour ) ?
299.TP
300.B A9
301No you don't. Receiving an event from
302.BR epoll_wait (2)
303should suggest to you that such file descriptor is ready for the requested I/O
304operation. You have simply to consider it ready until you will receive the
305next EAGAIN. When and how you will use such file descriptor is entirely up
306to you. Also, the condition that the read/write I/O space is exhausted can
307be detected by checking the amount of data read/write from/to the target
308file descriptor. For example, if you call
309.BR read (2)
310by asking to read a certain amount of data and
311.BR read (2)
312returns a lower number of bytes, you can be sure to have exhausted the read
313I/O space for such file descriptor. Same is valid when writing using the
314.BR write (2)
315function.
fea681da 316.SH POSSIBLE PITFALLS AND WAYS TO AVOID THEM
fea681da
MK
317.TP
318.B o Starvation ( Edge Triggered )
319.PP
320If there is a large amount of I/O space, it is possible that by trying to drain
321it the other files will not get processed causing starvation. This
322is not specific to
323.BR epoll .
324.PP
325.PP
326The solution is to maintain a ready list and mark the file descriptor as ready
327in its associated data structure, thereby allowing the application to
328remember which files need to be processed but still round robin amongst
329all the ready files. This also supports ignoring subsequent events you
330receive for fd's that are already ready.
fea681da
MK
331.TP
332.B o If using an event cache...
333.PP
334If you use an event cache or store all the fd's returned from
335.BR epoll_wait (2),
336then make sure to provide a way to mark its closure dynamically (ie- caused by
337a previous event's processing). Suppose you receive 100 events from
338.BR epoll_wait (2),
9fdfa163 339and in event #47 a condition causes event #13 to be closed.
b5cc2ffb 340If you remove the structure and
4d52e8f8 341.BR close ()
b5cc2ffb 342the fd for event #13, then your
fea681da
MK
343event cache might still say there are events waiting for that fd causing
344confusion.
fea681da
MK
345.PP
346One solution for this is to call, during the processing of event 47,
347.BR epoll_ctl ( EPOLL_CTL_DEL )
b5cc2ffb 348to delete fd 13 and
f87925c6
MK
349.BR close (),
350then mark its associated
fea681da
MK
351data structure as removed and link it to a cleanup list. If you find another
352event for fd 13 in your batch processing, you will discover the fd had been
353previously removed and there will be no confusion.
fea681da 354.SH CONFORMING TO
6d86c355 355.BR epoll (7)
fea681da
MK
356is a new API introduced in Linux kernel 2.5.44.
357Its interface should be finalized in Linux kernel 2.5.66.
358.SH "SEE ALSO"
359.BR epoll_create (2),
360.BR epoll_ctl (2),
361.BR epoll_wait (2)