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
8
votes
2 answers

Cancelling thread that is stuck on epoll_wait

I'm doing some event handling with C++ and pthreads. I have a main thread that reads from event queue I defined, and a worker thread that fills the event queue. The queue is of course thread safe. The worker thread have a list of file descriptors…
Dmitry Kudryavtsev
  • 16,354
  • 4
  • 25
  • 32
8
votes
1 answer

Given any epoll TCP socket event, if EPOLLRDHUP=0 and EPOLLIN=1; is a subsequent call to read()/recv() guaranteed to return a read size unequal to 0?

From the manual of epoll_ctl: EPOLLRDHUP (since Linux 2.6.17) Stream socket peer closed connection, or shut down writing half of connection. (This flag is especially useful for writing simple code to detect peer shutdown when using Edge Triggered…
Will
  • 2,014
  • 2
  • 19
  • 42
8
votes
2 answers

select(), poll() or epoll() ? for sysfs attribute

I am working with gpio interrupts. I have a file in "/sys/class/gpio/gpio38/value". I want a notification whenever there is a change in attribute value. So how can I achieve this in user-space. As I have already collected information, I can use…
duslabo
  • 1,487
  • 4
  • 20
  • 33
8
votes
4 answers

Async connect and disconnect with epoll (Linux)

I need async connect and disconnect for tcp client using epoll for Linux. There are ext. functions in Windows, such as ConnectEx, DisconnectEx, AcceptEx, etc... In tcp server standard accept function is working, but in tcp client doesn't working…
Alexander
  • 275
  • 2
  • 4
  • 13
7
votes
3 answers

Socket server with epoll and threads

I am trying to create a socket server in C for a Collaborative real-time editor http://en.wikipedia.org/wiki/Collaborative_real-time_editor but I don't know what is the best server architecture for it. At the first, I was trying to use select for…
cemycc
  • 117
  • 1
  • 8
7
votes
2 answers

Linux Evdev Poll Lag

I'm on a laptop with 2 connected keyboards (built-in and USB). I'm obtaining these connected keyboards with libudev and using epoll to poll them for input via the evdev interface: // Compile with $(gcc udev.c -ludev) #include #include…
7
votes
0 answers

Nodejs profiling, is epoll_pwait affecting the performance?

I am developing a node-js application that expects midi input and sends midi output. In order to measure and improve the performance of the application, following this guide, I have extracted the CPU usage profile while using the application. This…
7
votes
2 answers

Using edge triggered epoll, should I loop over send?

I'm using epoll to write a media server. The fds are all set to non-blocking and I'm using edge-triggered events. I know for EPOLLIN I need to loop over reading the fd until EAGAIN is returned. But what about writing? When I want to write I queue…
Martin Redmond
  • 13,366
  • 6
  • 36
  • 32
7
votes
2 answers

How to use an eventfd with level triggered behaviour on epoll?

Registering a level triggered eventfd on epoll_ctl only fires once, when not decrementing the eventfd counter. To summarize the problem, I have observed that the epoll flags (EPOLLET, EPOLLONESHOT or None for level triggered behaviour) behave…
linkjumper
  • 169
  • 1
  • 8
7
votes
1 answer

A problem of multithread epoll in linux

I have a multithread linux program which uses epoll(7). The epoll(7) man page says when one of its fds gets closed, this fd will be automatically removed from the epoll set. My question is what if a fd of the epoll set gets closed in one thread…
Utoah
  • 1,052
  • 1
  • 12
  • 18
7
votes
2 answers

epoll_ctl : Operation not permitted error - c program

1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 9 int main() { 10 struct epoll_event event ; 11 int ret,fd,…
webnoon
  • 945
  • 3
  • 9
  • 18
7
votes
3 answers

Handle signals with epoll_wait and signalfd

I'm writing my own echo server using sockets and syscalls. I am using epoll to work with many different clients at the same time and all the operations done with clients are nonblocking. When the server is on and doing nothing, it is in epoll_wait.…
Aleksandr Tukallo
  • 1,299
  • 17
  • 22
7
votes
3 answers

Do I get a notification from epoll when a fd is closed?

I am currently building something that uses epoll. It works pretty nice, but it would be good to have a notification when a file descriptor gets removed from epoll when the underlying fd is closed. Is there a way to get a notification from epoll as…
Nidhoegger
  • 4,973
  • 4
  • 36
  • 81
7
votes
1 answer

epoll_ctl() failed: No such file or directory [errno = 2]

Recently updated the Linux kernel from 2.6.18 to 2.6.32, and an existing application starts error out with following error message: epoll_ctl() failed: No such file or directory [errno = 2]. I did read through the linux man page on epoll_ctl but…
Feng Deng
  • 153
  • 1
  • 7
7
votes
2 answers

Performance tuning for Netty 4.1 on linux machine

I am building a messaging application using Netty 4.1 Beta3 for designing my server and the server understands MQTT protocol. This is my MqttServer.java class that sets up the Netty server and binds it to a specific port. EventLoopGroup…
Sachin Malhotra
  • 1,211
  • 2
  • 11
  • 24