Questions tagged [epoll]

epoll is a Linux 2.6 readiness notification API for sockets, pipes, and special event-, signal-, and timer descriptors which can operate both in level- and edge-triggered mode, although presently only level-triggered behaviour is in accordance with the documentation. As opposed to poll or select, epoll scales O(1) in respect to the number of descriptors and O(N) in respect realized events.

The epoll API is built around 3 functions:

  • epoll_create creates a new epoll instance and returns a file descriptor that refers to it. This descriptor can be operated on with the other epoll functions and can be added to a different epoll instance
  • epoll_ctl allows file descriptors (sockets, pipes, eventfd, timerfd, signalfd, and epoll) being added and removed to an epoll's set of monitored descriptors, as well as flags of existing descriptors being modified
  • epoll_wait will return up to maxevents queued events. If no events are available, it will return zero. If a timeout is provided and no events are available, epoll_wait will block for the duration of the timeout (a value of -1 means forever).

The conceptual idea behind the API is that applications usually have a certain set of descriptors that changes rarely if ever, but which needs to be observed for readiness many times. Also, typically a lot fewer descriptors are ready than open. epoll therefore separates copying the list of descriptors to watch from the actual watching and notifies registered listeners instead of iterating a list of descriptors.

The operation of level-triggered mode (default) is easy, since it is identical of how poll/select works. As long as the resource is ready (e.g. as long as there remains data to be read), every call to epoll_wait will return an event.

The operation of edge-triggered mode (EPOLLET flag) is more complicated, more error-prone, inconsistenly documented, and inconsistently implemented. In epoll(7), it is explained in terms of reading partial data causing the next call to epoll_wait to block until new data arrives, but not while some data remains in the buffers. It is therefore recommended to use non-blocking descriptors and reading until EAGAIN is received.
According to The Linux Programming Interface, edge-triggered mode only reports events that happened since the last call to epoll_wait.
In reality, it does a mixture of both (i.e. both reads and epoll_wait reset the status to "not ready"), and it does not work as indicated in respect of several epoll instances listening to the same socket or several threads waiting on the same epoll instance (observed under kernel 2.6.38 with timerfd and eventfd). Although epoll is supposed to signal all waiters upon arrival of an event, in edge-triggered mode it only ever signals a single waiter.

792 questions
9
votes
1 answer

Are epoll events being watched when not epoll_waiting

I'm rather new to event based programming. I'm experimenting with epoll's edge-mode which apparently only signals files which have become ready for read/write (as opposed to level-mode which signals all ready files, regardless of whether there were…
Antoine
  • 13,494
  • 6
  • 40
  • 52
9
votes
2 answers

Significance of parameters in epoll_event structure (epoll)

I am using epoll_ctl() and epoll_wait() system calls. int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout); struct epoll_event { uint32_t events; /*…
SPSN
  • 1,411
  • 2
  • 13
  • 14
9
votes
4 answers

How do you use AIO and epoll together in a single event loop?

How can you combine AIO and epoll together in a single event loop? Google finds lots of talk from 2002 and 2003 about unifying them, but its unclear if anything happened, or if it's possible. Has anyone rolled-their-own with an epoll loop using…
Will
  • 73,905
  • 40
  • 169
  • 246
9
votes
2 answers

use different glibc version

Currently I have glibc which does not support epoll, so I installed new glibc at non default lation which supports epoll. I have python program which uses this epoll. I tried to set LD_LIBRARY_PATH export…
big
  • 1,888
  • 7
  • 28
  • 48
8
votes
2 answers

Missing something or do I just not understand epoll?

Full disclosure, I'm a student and this is an assignment. I've been working on it for over a week almost non-stop (in addition to previous time spent) and I can't figure out what I'm doing wrong. My server keeps hanging on epoll_wait after only a…
David Young
  • 441
  • 1
  • 4
  • 13
8
votes
1 answer

Why does select.select() work with disk files but not epoll()?

The following code essentially cats a file with select.select(): f = open('node.py') fd = f.fileno() while True: r, w, e = select.select([fd], [], []) print '>', repr(os.read(fd, 10)) time.sleep(1) When I try a similar thing with epoll…
Andy C
  • 83
  • 1
  • 5
8
votes
2 answers

epoll VS select

I have read a couple of networking books to get some idea of differences between epoll and select but they only covered this concepts slightly. I will be appreciated if you guys can provide me the key differences in details. Thanks in advance
codereviewanskquestions
  • 13,460
  • 29
  • 98
  • 167
8
votes
2 answers

libevent and epoll,which is more efficient?

I think these are the two event-dealing libraries among the best. These two both have many users,but which is better?
wireshark
  • 1,295
  • 2
  • 14
  • 20
8
votes
1 answer

POLLHUP vs. POLLRDHUP?

According to the poll man page, the poll function can return POLLHUP and POLLRDHUP events. From what I understand, only POLLHUP is POSIX compliant, and POLLRDHUP is a Linux non-standard extension. Howerver, both seem to signal that the write end of…
Guett31
  • 258
  • 2
  • 15
8
votes
1 answer

Is it possible to change the io_context of a socket in boost::asio?

I'm currently writing a multi-threaded server where each thread has an io_context and a list of task objects to execute, with each task object having an associated ip::tcp::socket object. For load-balancing I'm sometimes migrating tasks from one…
8
votes
4 answers

Python: Lib to use epoll if available, fallback to select

I would like to use select.epoll() in my Python library. Unfortunately epoll is not available everywhere. I need a way to fallback to select.select(). I tried to find something at pypi, but failed to find a matching package:…
guettli
  • 25,042
  • 81
  • 346
  • 663
8
votes
2 answers

Boost Message Queue not based on POSIX message queue? Impossible to select(2)?

I thought I'd use Boost.Interprocess's Message Queue in place of sockets for communication within one host. But after digging into it, it seems that this library for some reason eschews the POSIX message queue facility (which my Linux system…
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
8
votes
2 answers

Multithreading UDP server with epoll?

I'd like to develop a multithreaded UDP server in C/Linux. The service is running on a single port x, thus there's only the possibility to bind a single UDP socket to it. In order to work under high loads, I have n threads (statically defined), say…
Daniel
  • 83
  • 1
  • 1
  • 3
8
votes
1 answer

Is it OK to share the same epoll file descriptor among threads?

Is it safe to share the same Epoll fd (not socket fd) among several threads? And if so, will each thread have to pass its own events array to epoll_wait(2) or can they share it? For example void *thread_func(void *thread_args) { // extract…
MiJo
  • 569
  • 5
  • 19
8
votes
3 answers

Why is the TUX Web Server Dead? Does Nginx/Lighttpd/Epoll/Kqueue Replace it?

I recall a very fast kernel module for Linux called "TUX" for static files to answer IIS's superior-to-Linux static file web-serving performance and solve the "C10K problem." Now I keep seeing: Nginx Lighttpd CDNs ... for "fast static…
Jaimie Sirovich
  • 1,088
  • 2
  • 10
  • 14