Linux Files and the Event Poll Interface (
Page 1 of 4 )
In this second part of a seven-part series on Linux I/O file system calls, you will learn about the event poll interface. This article is excerpted from chapter four of the book Linux System Programming: Talking Directly to the Kernel and C Library, written by Robert Love (O'Reilly, 2007; ISBN: 0596009585). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
The Event Poll Interface
Recognizing the limitations of both poll() and
select()
, the 2.6 Linux kernel* intro
duced 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.
Both
poll()
and
select()
(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. The
size
parameter 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 sets
errno
to one of the following:
EINVAL
The
size
parameter 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 from
epoll_create()
should be destroyed via a call to
close()
after polling is finished.