Home arrow BrainDump arrow Linux Files and the Event Poll Interface

Linux Files and the Event Poll Interface

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.

TABLE OF CONTENTS:
  1. Linux Files and the Event Poll Interface
  2. Controlling Epoll
  3. Waiting for Events with Epoll
  4. Edge- Versus Level-Triggered Events
By: O'Reilly Media
Rating: starstarstarstarstar / 7
December 04, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The Event Poll Interface

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.



 
 
>>> More BrainDump Articles          >>> More By O'Reilly Media
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

BRAINDUMP ARTICLES

- Apple Founder Steve Jobs Dies
- Steve Jobs` Era at Apple Ends
- Google's Chrome Developer Tool Updated
- Google's Chrome 6 Browser Brings Speed to th...
- New Open Source Update Fedora 13 is Released...
- Install Linux with Knoppix
- iPad Developers Flock To SDK 3.2
- Managing a Linux Wireless Access Point
- Maintaining a Linux Wireless Access Point
- Securing a Linux Wireless Access Point
- Configuring a Linux Wireless Access Point
- Building a Linux Wireless Access Point
- Migrating Oracle to PostgreSQL with Enterpri...
- Demystifying SELinux on Kernel 2.6
- Yahoo and Microsoft Create Ad Partnership

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: