is a variant of
that can be used either as an edge-triggered or a level-triggered
interface and scales well to large numbers of watched file descriptors.
The following system calls are provided to
create and manage an
instance created by
which returns a file descriptor referring to the epoll instance.
(The more recent
extends the functionality of
Interest in particular file descriptors is then registered via
The set of file descriptors currently registered on an
instance is sometimes called an
Finally, the actual wait is started by
Level-Triggered and Edge-Triggered
event distribution interface is able to behave both as edge-triggered
(ET) and as level-triggered (LT).
The difference between the two mechanisms
can be described as follows.
this scenario happens:
The file descriptor that represents the read side of a pipe
is registered on the
A pipe writer writes 2 kB of data on the write side of the pipe.
A call to
is done that will return
as a ready file descriptor.
file descriptor has been added to the
interface using the
flag, the call to
done in step
will probably hang despite the available data still present in the file
meanwhile the remote peer might be expecting a response based on the
data it already sent.
The reason for this is that edge-triggered mode only
delivers events when changes occur on the monitored file descriptor.
So, in step
the caller might end up waiting for some data that is already present inside
the input buffer.
In the above example, an event on
will be generated because of the write done in
and the event is consumed in
Since the read operation done in
does not consume the whole buffer data, the call to
done in step
might block indefinitely.
An application that employs the
flag should use nonblocking file descriptors to avoid having a blocking
read or write starve a task that is handling multiple file descriptors.
The suggested way to use
as an edge-triggered
interface is as follows:
with nonblocking file descriptors; and
by waiting for an event only after
By contrast, when used as a level-triggered interface
(the default, when
is not specified),
is simply a faster
and can be used wherever the latter is used since it shares the
Since even with edge-triggered
multiple events can be generated upon receipt of multiple chunks of data,
the caller has the option to specify the
flag, to tell
to disable the associated file descriptor after the receipt of an event with
flag is specified,
it is the caller's responsibility to rearm the file descriptor using
The following interfaces can be used to limit the amount of
kernel memory consumed by epoll:
/proc/sys/fs/epoll/max_user_watches (since Linux 2.6.28)
This specifies a limit on the total number of
file descriptors that a user can register across
all epoll instances on the system.
The limit is per real user ID.
Each registered file descriptor costs roughly 90 bytes on a 32-bit kernel,
and roughly 160 bytes on a 64-bit kernel.
the default value for
is 1/25 (4%) of the available low memory,
divided by the registration cost in bytes.
Example for Suggested Usage
While the usage of
when employed as a level-triggered interface does have the same
the edge-triggered usage requires more clarification to avoid stalls
in the application event loop.
In this example, listener is a
nonblocking socket on which
has been called.
uses the new ready file descriptor until
is returned by either
An event-driven state machine application should, after having received
record its current state so that at the next call to
it will continue to
from where it stopped before.
When used as an edge-triggered interface, for performance reasons, it is
possible to add the file descriptor inside the
once by specifying
This allows you to avoid
continuously switching between
Questions and Answers
What is the key used to distinguish the file descriptors registered in an
The key is the combination of the file descriptor number and
the open file description
(also known as an "open file handle",
the kernel's internal representation of an open file).
What happens if you register the same file descriptor on an
You will probably get
However, it is possible to add a duplicate
descriptor to the same
This can be a useful technique for filtering events,
if the duplicate file descriptors are registered with different
instances wait for the same file descriptor?
If so, are events reported to both
Yes, and events would be reported to both.
However, careful programming may be needed to do this correctly.
file descriptor itself poll/epoll/selectable?
file descriptor has events waiting then it will
indicate as being readable.
What happens if one attempts to put an
file descriptor into its own file descriptor set?
call will fail
However, you can add an
file descriptor inside another
file descriptor set.
Can I send an
file descriptor over a Unix domain socket to another process?
Yes, but it does not make sense to do this, since the receiving process
would not have copies of the file descriptors in the
Will closing a file descriptor cause it to be removed from all
Yes, but be aware of the following point.
A file descriptor is a reference to an open file description (see
Whenever a descriptor is duplicated via
a new file descriptor referring to the same open file description is
An open file description continues to exist until all
file descriptors referring to it have been closed.
A file descriptor is removed from an
set only after all the file descriptors referring to the underlying
open file description have been closed
(or before if the descriptor is explicitly removed using
This means that even after a file descriptor that is part of an
set has been closed,
events may be reported for that file descriptor if other file
descriptors referring to the same underlying file description remain open.
If more than one event occurs between
calls, are they combined or reported separately?
They will be combined.
Does an operation on a file descriptor affect the
already collected but not yet reported events?
You can do two operations on an existing file descriptor.
Remove would be meaningless for
Modify will reread available I/O.
Do I need to continuously read/write a file descriptor
when using the
flag (edge-triggered behavior) ?
Receiving an event from
should suggest to you that such
file descriptor is ready for the requested I/O operation.
You must consider it ready until the next (nonblocking)
When and how you will use the file descriptor is entirely up to you.
For packet/token-oriented files (e.g., datagram socket,
terminal in canonical mode),
the only way to detect the end of the read/write I/O space
is to continue to read/write until
For stream-oriented files (e.g., pipe, FIFO, stream socket), the
condition that the read/write I/O space is exhausted can also be detected by
checking the amount of data read from / written to the target file
For example, if you call
by asking to read a certain amount of data and
returns a lower number of bytes, you
can be sure of having exhausted the read I/O space for the file
The same is true when writing using
(Avoid this latter technique if you cannot guarantee that
the monitored file descriptor always refers to a stream-oriented file.)
Possible Pitfalls and Ways to Avoid Them
o Starvation (edge-triggered)
If there is a large amount of I/O space,
it is possible that by trying to drain
it the other files will not get processed causing starvation.
(This problem is not specific to
The solution is to maintain a ready list
and mark the file descriptor as ready
in its associated data structure, thereby allowing the application to
remember which files need to be processed but still round robin amongst
all the ready files.
This also supports ignoring subsequent events you
receive for file descriptors that are already ready.
o If using an event cache...
If you use an event cache or store all the file descriptors returned from
then make sure to provide a way to mark
its closure dynamically (i.e., caused by
a previous event's processing).
Suppose you receive 100 events from
and in event #47 a condition causes event #13 to be closed.
If you remove the structure and
the file descriptor for event #13, then your
event cache might still say there are events waiting for that
file descriptor causing confusion.
One solution for this is to call, during the processing of event 47,
to delete file descriptor 13 and
then mark its associated
data structure as removed and link it to a cleanup list.
If you find another
event for file descriptor 13 in your batch processing,
you will discover the file descriptor had been
previously removed and there will be no confusion.
API was introduced in Linux kernel 2.5.44.
Support was added to glibc in version 2.3.2.
API is Linux-specific.
Some other systems provide similar
mechanisms, for example, FreeBSD has
and Solaris has