Recognizing the limitations of both poll() andselect(), the 2.6 Linux kernel* introduced the event poll (epoll) facility. While more complex than the two earlier interfaces, epoll solves the fundamental performance problem shared by both of them, and adds several new features.
Bothpoll()andselect() (discussed in Chapter 2) require the full list of file descriptors to watch on each invocation. The kernel must then walk the list of each file descriptor to be monitored. When this list grows large—it may contain hundreds or even thousands of file descriptors—walking the list on each invocation becomes a scalability bottleneck.
Epoll circumvents this problem by decoupling the monitor registration from the actual monitoring. One system call initializes an epoll context, another adds monitored file descriptors to or removes them from the context, and a third performs the actual event wait.
Creating a New Epoll Instance
An epoll context is created via epoll_create() :
#include <sys/epoll.h>
int epoll_create (int size)
A successful call to epoll_create()instantiates a new epoll instance, and returns a file descriptor associated with the instance. This file descriptor has no relationship to a real file; it is just a handle to be used with subsequent calls using the epoll facility. Thesizeparameter is a hint to the kernel about the number of file descriptors that are going to be monitored; it is not the maximum number. Passing in a good approximation will result in better performance, but the exact number is not required. On error, the call returns-1, and setserrnoto one of the following:
EINVAL Thesizeparameter is not a positive number.
ENFILE The system has reached the limit on the total number of open files.
ENOMEM Insufficient memory was available to complete the operation.
A typical call is:
int epfd;
epfd = epoll_create (100); /* plan to watch ~100 fds */ if (epfd < 0) perror ("epoll_create");
The file descriptor returned fromepoll_create()should be destroyed via a call toclose()after polling is finished.