OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man7 / epoll.7
1 .\"  Copyright (C) 2003  Davide Libenzi
2 .\"
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.
8 .\"
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.
13 .\"
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/>.
17 .\" %%%LICENSE_END
18 .\"
19 .\"  Davide Libenzi <davidel@xmailserver.org>
20 .\"
21 .TH EPOLL 7 2015-01-10 "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 The
28 .B epoll
29 API performs a similar task to
30 .BR poll (2):
31 monitoring multiple file descriptors to see if I/O is possible on any of them.
32 The
33 .B epoll
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
37 create and manage an
38 .B epoll
39 instance:
40 .IP * 3
41 .BR epoll_create (2)
42 creates an
43 .B epoll
44 instance and returns a file descriptor referring to that instance.
45 (The more recent
46 .BR epoll_create1 (2)
47 extends the functionality of
48 .BR epoll_create (2).)
49 .IP *
50 Interest in particular file descriptors is then registered via
51 .BR epoll_ctl (2).
52 The set of file descriptors currently registered on an
53 .B epoll
54 instance is sometimes called an
55 .I epoll
56 set.
57 .IP *
58 .BR epoll_wait (2)
59 waits for I/O events,
60 blocking the calling thread if no events are currently available.
61 .SS Level-triggered and edge-triggered
62 The
63 .B epoll
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.
68 Suppose that
69 this scenario happens:
70 .IP 1. 3
71 The file descriptor that represents the read side of a pipe
72 .RI ( rfd )
73 is registered on the
74 .B epoll
75 instance.
76 .IP 2.
77 A pipe writer writes 2 kB of data on the write side of the pipe.
78 .IP 3.
79 A call to
80 .BR epoll_wait (2)
81 is done that will return
82 .I rfd
83 as a ready file descriptor.
84 .IP 4.
85 The pipe reader reads 1 kB of data from
86 .IR rfd .
87 .IP 5.
88 A call to
89 .BR epoll_wait (2)
90 is done.
91 .PP
92 If the
93 .I rfd
94 file descriptor has been added to the
95 .B epoll
96 interface using the
97 .B EPOLLET
98 (edge-triggered)
99 flag, the call to
100 .BR epoll_wait (2)
101 done in step
102 .B 5
103 will probably hang despite the available data still present in the file
104 input buffer;
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.
109 So, in step
110 .B 5
111 the caller might end up waiting for some data that is already present inside
112 the input buffer.
113 In the above example, an event on
114 .I rfd
115 will be generated because of the write done in
116 .B 2
117 and the event is consumed in
118 .BR 3 .
119 Since the read operation done in
120 .B 4
121 does not consume the whole buffer data, the call to
122 .BR epoll_wait (2)
123 done in step
124 .B 5
125 might block indefinitely.
126
127 An application that employs the
128 .B EPOLLET
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
132 .B epoll
133 as an edge-triggered
134 .RB ( EPOLLET )
135 interface is as follows:
136 .RS
137 .TP 4
138 .B i
139 with nonblocking file descriptors; and
140 .TP
141 .B ii
142 by waiting for an event only after
143 .BR read (2)
144 or
145 .BR write (2)
146 return
147 .BR EAGAIN .
148 .RE
149 .PP
150 By contrast, when used as a level-triggered interface
151 (the default, when
152 .B EPOLLET
153 is not specified),
154 .B epoll
155 is simply a faster
156 .BR poll (2),
157 and can be used wherever the latter is used since it shares the
158 same semantics.
159
160 Since even with edge-triggered
161 .BR epoll ,
162 multiple events can be generated upon receipt of multiple chunks of data,
163 the caller has the option to specify the
164 .B EPOLLONESHOT
165 flag, to tell
166 .B epoll
167 to disable the associated file descriptor after the receipt of an event with
168 .BR epoll_wait (2).
169 When the
170 .B EPOLLONESHOT
171 flag is specified,
172 it is the caller's responsibility to rearm the file descriptor using
173 .BR epoll_ctl (2)
174 with
175 .BR EPOLL_CTL_MOD .
176 .SS Interaction with autosleep
177 If the system is in
178 .B autosleep
179 mode via
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
185 .BR epoll (7)
186 .B EPOLLWAKEUP
187 flag.
188
189 When the
190 .B EPOLLWAKEUP
191 flag is set in the
192 .B events
193 field for a
194 .IR "struct epoll_event" ,
195 the system will be kept awake from the moment the event is queued,
196 through the
197 .BR epoll_wait (2)
198 call which returns the event until the subsequent
199 .BR epoll_wait (2)
200 call.
201 If the event should keep the system awake beyond that time,
202 then a separate
203 .I wake_lock
204 should be taken before the second
205 .BR epoll_wait (2)
206 call.
207 .SS /proc interfaces
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
211 .\" .TP
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.
215 .TP
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.
223 Currently,
224 .\" 2.6.29 (in 2.6.28, the default was 1/32 of lowmem)
225 the default value for
226 .I max_user_watches
227 is 1/25 (4%) of the available low memory,
228 divided by the registration cost in bytes.
229 .SS Example for suggested usage
230 While the usage of
231 .B epoll
232 when employed as a level-triggered interface does have the same
233 semantics as
234 .BR poll (2),
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
239 .BR listen (2)
240 has been called.
241 The function
242 .I do_use_fd()
243 uses the new ready file descriptor until
244 .B EAGAIN
245 is returned by either
246 .BR read (2)
247 or
248 .BR write (2).
249 An event-driven state machine application should, after having received
250 .BR EAGAIN ,
251 record its current state so that at the next call to
252 .I do_use_fd()
253 it will continue to
254 .BR read (2)
255 or
256 .BR write (2)
257 from where it stopped before.
258
259 .in +4n
260 .nf
261 #define MAX_EVENTS 10
262 struct epoll_event ev, events[MAX_EVENTS];
263 int listen_sock, conn_sock, nfds, epollfd;
264
265 /* Code to set up listening socket, \(aqlisten_sock\(aq,
266    (socket(), bind(), listen()) omitted */
267
268 epollfd = epoll_create1(0);
269 if (epollfd == \-1) {
270     perror("epoll_create1");
271     exit(EXIT_FAILURE);
272 }
273
274 ev.events = EPOLLIN;
275 ev.data.fd = listen_sock;
276 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_sock, &ev) == \-1) {
277     perror("epoll_ctl: listen_sock");
278     exit(EXIT_FAILURE);
279 }
280
281 for (;;) {
282     nfds = epoll_wait(epollfd, events, MAX_EVENTS, \-1);
283     if (nfds == \-1) {
284         perror("epoll_pwait");
285         exit(EXIT_FAILURE);
286     }
287
288     for (n = 0; n < nfds; ++n) {
289         if (events[n].data.fd == listen_sock) {
290             conn_sock = accept(listen_sock,
291                             (struct sockaddr *) &local, &addrlen);
292             if (conn_sock == \-1) {
293                 perror("accept");
294                 exit(EXIT_FAILURE);
295             }
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,
300                         &ev) == \-1) {
301                 perror("epoll_ctl: conn_sock");
302                 exit(EXIT_FAILURE);
303             }
304         } else {
305             do_use_fd(events[n].data.fd);
306         }
307     }
308 }
309 .fi
310 .in
311
312 When used as an edge-triggered interface, for performance reasons, it is
313 possible to add the file descriptor inside the
314 .B epoll
315 interface
316 .RB ( EPOLL_CTL_ADD )
317 once by specifying
318 .RB ( EPOLLIN | EPOLLOUT ).
319 This allows you to avoid
320 continuously switching between
321 .B EPOLLIN
322 and
323 .B EPOLLOUT
324 calling
325 .BR epoll_ctl (2)
326 with
327 .BR EPOLL_CTL_MOD .
328 .SS Questions and answers
329 .TP 4
330 .B Q0
331 What is the key used to distinguish the file descriptors registered in an
332 .B epoll
333 set?
334 .TP
335 .B A0
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).
340 .TP
341 .B Q1
342 What happens if you register the same file descriptor on an
343 .B epoll
344 instance twice?
345 .TP
346 .B A1
347 You will probably get
348 .BR EEXIST .
349 However, it is possible to add a duplicate
350 .RB ( dup (2),
351 .BR dup2 (2),
352 .BR fcntl (2)
353 .BR F_DUPFD )
354 descriptor to the same
355 .B epoll
356 instance.
357 .\" But a 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, descriptors duplicated by fork have
362 .\" similar semantics to descriptors duplicated by dup() and friends.)  On
363 .\" the other hand, the very fact that the child has a duplicate of the
364 .\" descriptor means that even if the parent closes its descriptor, then
365 .\" epoll_wait() in the parent will continue to receive notifications for
366 .\" that descriptor because of the duplicated descriptor in the child.
367 .\"
368 .\" See http://thread.gmane.org/gmane.linux.kernel/596462/
369 .\" "epoll design problems with common fork/exec patterns"
370 .\"
371 .\" mtk, Feb 2008
372 This can be a useful technique for filtering events,
373 if the duplicate file descriptors are registered with different
374 .I events
375 masks.
376 .TP
377 .B Q2
378 Can two
379 .B epoll
380 instances wait for the same file descriptor?
381 If so, are events reported to both
382 .B epoll
383 file descriptors?
384 .TP
385 .B A2
386 Yes, and events would be reported to both.
387 However, careful programming may be needed to do this correctly.
388 .TP
389 .B Q3
390 Is the
391 .B epoll
392 file descriptor itself poll/epoll/selectable?
393 .TP
394 .B A3
395 Yes.
396 If an
397 .B epoll
398 file descriptor has events waiting, then it will
399 indicate as being readable.
400 .TP
401 .B Q4
402 What happens if one attempts to put an
403 .B epoll
404 file descriptor into its own file descriptor set?
405 .TP
406 .B A4
407 The
408 .BR epoll_ctl (2)
409 call will fail
410 .RB ( EINVAL ).
411 However, you can add an
412 .B epoll
413 file descriptor inside another
414 .B epoll
415 file descriptor set.
416 .TP
417 .B Q5
418 Can I send an
419 .B epoll
420 file descriptor over a UNIX domain socket to another process?
421 .TP
422 .B A5
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
425 .B epoll
426 set.
427 .TP
428 .B Q6
429 Will closing a file descriptor cause it to be removed from all
430 .B epoll
431 sets automatically?
432 .TP
433 .B A6
434 Yes, but be aware of the following point.
435 A file descriptor is a reference to an open file description (see
436 .BR open (2)).
437 Whenever a descriptor is duplicated via
438 .BR dup (2),
439 .BR dup2 (2),
440 .BR fcntl (2)
441 .BR F_DUPFD ,
442 or
443 .BR fork (2),
444 a new file descriptor referring to the same open file description is
445 created.
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
449 .B epoll
450 set only after all the file descriptors referring to the underlying
451 open file description have been closed
452 (or before if the descriptor is explicitly removed using
453 .BR epoll_ctl (2)
454 .BR EPOLL_CTL_DEL ).
455 This means that even after a file descriptor that is part of an
456 .B epoll
457 set has been closed,
458 events may be reported for that file descriptor if other file
459 descriptors referring to the same underlying file description remain open.
460 .TP
461 .B Q7
462 If more than one event occurs between
463 .BR epoll_wait (2)
464 calls, are they combined or reported separately?
465 .TP
466 .B A7
467 They will be combined.
468 .TP
469 .B Q8
470 Does an operation on a file descriptor affect the
471 already collected but not yet reported events?
472 .TP
473 .B A8
474 You can do two operations on an existing file descriptor.
475 Remove would be meaningless for
476 this case.
477 Modify will reread available I/O.
478 .TP
479 .B Q9
480 Do I need to continuously read/write a file descriptor
481 until
482 .B EAGAIN
483 when using the
484 .B EPOLLET
485 flag (edge-triggered behavior) ?
486 .TP
487 .B A9
488 Receiving an event from
489 .BR epoll_wait (2)
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)
493 read/write yields
494 .BR EAGAIN .
495 When and how you will use the file descriptor is entirely up to you.
496 .sp
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
501 .BR EAGAIN .
502 .sp
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
506 descriptor.
507 For example, if you call
508 .BR read (2)
509 by asking to read a certain amount of data and
510 .BR read (2)
511 returns a lower number of bytes, you
512 can be sure of having exhausted the read I/O space for the file
513 descriptor.
514 The same is true when writing using
515 .BR write (2).
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
519 .TP
520 .B o Starvation (edge-triggered)
521 .PP
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
526 .BR epoll .)
527 .PP
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
532 all the ready files.
533 This also supports ignoring subsequent events you
534 receive for file descriptors that are already ready.
535 .TP
536 .B o If using an event cache...
537 .PP
538 If you use an event cache or store all the file descriptors returned from
539 .BR epoll_wait (2),
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
544 .BR epoll_wait (2),
545 and in event #47 a condition causes event #13 to be closed.
546 If you remove the structure and
547 .BR close (2)
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.
551 .PP
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
555 .BR close (2),
556 then mark its associated
557 data structure as removed and link it to a cleanup list.
558 If you find another
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.
562 .SH VERSIONS
563 The
564 .B epoll
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.
568 .SH CONFORMING TO
569 The
570 .B epoll
571 API is Linux-specific.
572 Some other systems provide similar
573 mechanisms, for example, FreeBSD has
574 .IR kqueue ,
575 and Solaris has
576 .IR /dev/poll .
577 .SH SEE ALSO
578 .BR epoll_create (2),
579 .BR epoll_create1 (2),
580 .BR epoll_ctl (2),
581 .BR epoll_wait (2)
582 .SH COLOPHON
583 This page is part of release 3.79 of the Linux
584 .I man-pages
585 project.
586 A description of the project,
587 information about reporting bugs,
588 and the latest version of this page,
589 can be found at
590 \%http://www.kernel.org/doc/man\-pages/.