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
6
votes
1 answer

What happen, when epoll file descriptor is closed?

Suppose I create epoll file descriptor (epfd) by call epfd = epoll_create( 10 ); Next I adding some count of file descriptors into this set by calling epoll_ctl(epfd,EPOLL_CTL_ADD,...) and wait for events in event loop by calling epoll_wait in…
k.o.
  • 91
  • 6
6
votes
1 answer

Event notification from kernel space to user space

How to notify the user space application whenever an event occurs in the kernel space? A hardware generates an interrupt when the data arrives at some GPIO. This data is copied to the kernel buffer. At this point, I want the driver to notify the…
Sagar Jain
  • 7,475
  • 12
  • 47
  • 83
6
votes
2 answers

How can I distinguish read and write events on the same socket?

I'm using epoll to get notifications about incoming data. It's not hard because all events returned by epoll_wait() indicates, that I can read data from epoll_event.data.fd (socket descriptor). But now I want get both types of notification:…
Nelson Tatius
  • 7,693
  • 8
  • 47
  • 70
6
votes
1 answer

epoll with timerfd

I want to use the it_interval of newValue to set the interval of the timeout. But in my example, I can only print timeout once. What happened? How can I set the interval? This is my code: int main() { int efd =epoll_create(256); …
Tengchao
  • 157
  • 1
  • 3
  • 7
6
votes
1 answer

EPOLLPRI when does this case happen?

When I set my epoll option, I can see EPOLLPRI. It expalins there is urgent "read" required. When does this actually occur? Is there any way to send in that PRI Mode?
Jae Park
  • 621
  • 4
  • 13
  • 27
5
votes
2 answers

zeromq zmq.Poller & stdin

Is it possible to use zmq.Poller to also poll for data availability on stdin? If not, what would be the most efficient wait to poll, at the some time (ideally), for data availability on zeromq sockets & stdin?
jldupont
  • 93,734
  • 56
  • 203
  • 318
5
votes
3 answers

How do I make epoll switch between multiple connections?

I am using epoll in what I believe to be the typical manner for TCP sockets (based largely on this example, but slightly adapted to C++); one main listening socket bound to the port, and each new connection socket (from accept()) is also added for…
DigitalMan
  • 2,440
  • 5
  • 26
  • 32
5
votes
10 answers

How epoll detect clientside close in Python?

Here is my server """Server using epoll method""" import os import select import socket import time from oodict import OODict addr = ('localhost', 8989) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET,…
Chenz
5
votes
1 answer

Does poll/epoll block? How is it different from async IO?

I was always under the impression that poll/epoll doesn't block. That's why they are used by non-blocking servers such as Nginx. But in this Stackoverflow question it was stated several times that poll blocks. So does poll/epoll block? And how is…
Continuation
  • 12,722
  • 20
  • 82
  • 106
5
votes
2 answers

How do I check client connection is still alive

I am working on a network programming using epoll. I have a connection list and put every client in the list. I can detect user disconnection by reading 0 if the user disconnected normally. However, if the user somehow got disconnected unexpectedly…
codereviewanskquestions
  • 13,460
  • 29
  • 98
  • 167
5
votes
1 answer

Dealing with listening socket by epoll

All below is from man epoll page: The function do_use_fd() uses the new ready file descriptor until EAGAIN is returned by either read(2) or write(2). Code example for ET triggered : for(;;) { nfds = epoll_wait(kdpfd,…
Je Rog
  • 5,675
  • 8
  • 39
  • 47
5
votes
1 answer

How to code an epoll based sockets client in C

All the examples I can find online are servers. I want to build a basic web crawler using epoll. So I need a basic client example to get me started. When I say basic I really mean a complete example that demonstrates multiple connections with…
James Read
  • 419
  • 2
  • 7
  • 13
5
votes
0 answers

Linux FD expose to user for waiting

Imagine having a library that starts a server socket and manages an epoll fd with all clients. Since everything should be non blocking, control is passed to the user which may want to do a select on different fds, including the one that notifies the…
Lars M.
  • 179
  • 10
5
votes
1 answer

Is it efficient to use epoll with devices (/dev/event/...)?

I am working on a monothreaded process applet which creates a proxy virtual device (more precisely a virtual Xbox 360 pad); I do manage to create it with the uinput interface, I set it up properly and it works just fine. In order to feed commands to…
Emanuele
  • 1,408
  • 1
  • 15
  • 39
5
votes
1 answer

Proper handling of EWOULDBLOCK with polling on a non-blocking socket

I've been working on a polling TCP daemon for some time now. Recently, I've read that non-blocking sockets can sometimes throw an EWOULDBLOCK error during a send() or recv(). My understanding is that if recv() throws an EWOULDBLOCK, this (usually)…
lazyconfabulator
  • 477
  • 2
  • 6
  • 13