Home arrow BrainDump arrow Page 3 - Linux Files and the Event Poll Interface

Waiting for Events with Epoll - BrainDump

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.

  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



The system call epoll_wait() waits for events on the file descriptors associated with the given epoll instance:

  #include <sys/epoll.h>

  int epoll_wait (int epfd,
struct epoll_event *events,
int maxevents,
int timeout);

A call to epoll_wait() waits up to timeoutmilliseconds for events on the files associated with the epoll instanceepfd. Upon success,eventspoints to memory containingepoll_eventstructures describing each event, up to a maximum ofmaxeventsevents. The return value is the number of events, or-1on error, in which caseerrnois set to one of the following:

   epfdis not a valid file descriptor.

   The process does not have write access to the
   memory pointed at byevents.

   The system call was interrupted by a signal before it
   could complete.

 epfd is not a valid epoll instance, ormaxeventsis 
  equal to or less than0.

Iftimeoutis0, the call returns immediately, even if no events are available, in which case the call will return0. If thetimeoutis-1, the call will not return until an event is available.

When the call returns, theeventsfield of theepoll_eventstructure describes the events that occurred. Thedata field contains whatever the user set it to before invocation ofepoll_ctl().

A fullepoll_wait()example looks like this:

  #define MAX_EVENTS   64

  struct epoll_event *events;
  int nr_events, i, epfd;

  events = malloc (sizeof (struct epoll_event) * MAX_EVENTS);
  if (!events) {
perror ("malloc");
return 1;

  nr_events = epoll_wait (epfd, events, MAX_EVENTS, -1);
  if (nr_events < 0) {
perror ("epoll_wait");
          free (events);
return 1;

  for (i = 0; i < nr_events; i++) {
printf ("event=%ld on fd=%d\n",

* We now can, per events[i].events, operate on
* events[i].data.fd without blocking.

  free (events);

We will cover the functionsmalloc()andfree()in Chapter 8.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: