The Linux Programming Interface

(nextflipdebug5) #1

1356 Chapter 63


z The epoll_ctl() system call manipulates the interest list associated with an epoll
instance. Using epoll_ctl(), we can add a new file descriptor to the list, remove
an existing descriptor from the list, and modify the mask that determines
which events are to be monitored for a descriptor.
z The epoll_wait() system call returns items from the ready list associated with an
epoll instance.

63.4.1 Creating an epoll Instance: epoll_create()


The epoll_create() system call creates a new epoll instance whose interest list is ini-
tially empty.

The size argument specifies the number of file descriptors that we expect to monitor
via the epoll instance. This argument is not an upper limit, but rather a hint to the
kernel about how to initially dimension internal data structures. (Since Linux 2.6.8,
the size argument is ignored, because changes in the implementation meant
that the information it provided is no longer required.)
As its function result, epoll_create() returns a file descriptor referring to the new
epoll instance. This file descriptor is used to refer to the epoll instance in other epoll
system calls. When the file descriptor is no longer required, it should be closed in
the usual way, using close(). When all file descriptors referring to an epoll instance
are closed, the instance is destroyed and its associated resources are released back
to the system. (Multiple file descriptors may refer to the same epoll instance as a
consequence of calls to fork() or descriptor duplication using dup() or similar.)

Starting with kernel 2.6.27, Linux supports a new system call, epoll_create1().
This system call performs the same task as epoll_create(), but drops the obsolete
size argument and adds a flags argument that can be used to modify the behavior
of the system call. One flag is currently supported: EPOLL_CLOEXEC, which causes
the kernel to enable the close-on-exec flag (FD_CLOEXEC) for the new file descriptor.
This flag is useful for the same reasons as the open() O_CLOEXEC flag described in
Section 4.3.1.

63.4.2 Modifying the epoll Interest List: epoll_ctl()


The epoll_ctl() system call modifies the interest list of the epoll instance referred to
by the file descriptor epfd.

#include <sys/epoll.h>

int epoll_create(int size);
Returns file descriptor on success, or –1 on error

#include <sys/epoll.h>

int epoll_ctl(int epfd, int op, int fd, struct epoll_event *ev);
Returns 0 on success, or –1 on error
Free download pdf