1360 Chapter 63
The EPOLLONESHOT flag
By default, once a file descriptor is added to an epoll interest list using the epoll_ctl()
EPOLL_CTL_ADD operation, it remains active (i.e., subsequent calls to epoll_wait() will
inform us whenever the file descriptor is ready) until we explicitly remove it from
the list using the epoll_ctl() EPOLL_CTL_DEL operation. If we want to be notified only
once about a particular file descriptor, then we can specify the EPOLLONESHOT flag
(available since Linux 2.6.2) in the ev.events value passed in epoll_ctl(). If this flag is
specified, then, after the next epoll_wait() call that informs us that the corresponding
file descriptor is ready, the file descriptor is marked inactive in the interest list, and
we won’t be informed about its state by future epoll_wait() calls. If desired, we can
subsequently reenable monitoring of this file descriptor using the epoll_ctl()
EPOLL_CTL_MOD operation. (We can’t use the EPOLL_CTL_ADD operation for this purpose,
because the inactive file descriptor is still part of the epoll interest list.)
Example program
Listing 63-5 demonstrates the use of the epoll API. As command-line arguments, this
program expects the pathnames of one or more terminals or FIFOs. The program
performs the following steps:
z Create an epoll instance q.
z Open each of the files named on the command line for input w and add the
resulting file descriptor to the interest list of the epoll instance e, specifying the
set of events to be monitored as EPOLLIN.
z Execute a loop r that calls epoll_wait() t to monitor the interest list of the epoll
instance and handles the returned events from each call. Note the following
points about this loop:
–After the epoll_wait() call, the program checks for an EINTR return y, which
may occur if the program was stopped by a signal in the middle of the
epoll_wait() call and then resumed by SIGCONT. (Refer to Section 21.5.) If this
occurs, the program restarts the epoll_wait() call.
- It the epoll_wait() call was successful, the program uses a further loop to
check each of the ready items in evlist u. For each item in evlist, the pro-
gram checks the events field for the presence of not just EPOLLIN i, but also
EPOLLHUP and EPOLLERR o. These latter events can occur if the other end of a
FIFO was closed or a terminal hangup occurred. If EPOLLIN was returned,
then the program reads some input from the corresponding file descriptor
and displays it on standard output. Otherwise, if either EPOLLHUP or EPOLLERR
occurred, the program closes the corresponding file descriptor a and decre-
ments the counter of open files (numOpenFds). - The loop terminates when all open file descriptors have been closed (i.e.,
when numOpenFds equals 0).
The following shell session logs demonstrate the use of the program in Listing 63-5.
We use two terminal windows. In one window, we use the program in Listing 63-5 to
monitor two FIFOs for input. (Each open of a FIFO for reading by this program
will complete only after another process has opened the FIFO for writing, as