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

How to solve process.nextTick error?

I am using node.js to build a TCP server and I got the following errors. How to solve this? node.js:134 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: ETIMEDOUT, Connection timed out at…
Mickey Shine
  • 12,187
  • 25
  • 96
  • 148
3
votes
1 answer

Is there a system call or some way to know the type of file descriptor in Linux (e.g. regular file fd, socket fd, signal fd, timer fd)?

As I keep discovering, there are a variety of File Descriptors - Almost every thing is abstracted around a file descriptor: regular files, sockets, signals and timers (for example). All file descriptors are merely integers. Given a file…
asinix
  • 966
  • 1
  • 9
  • 22
3
votes
0 answers

Epoll - EPOLLONESHOT multithreaded

I'm currently building a websocket server using epoll with non-blocking sockets. The server supports SSL using OpenSSL. My setup is the following: I have 1 main thread, that runs epoll_wait() on all client sockets. When something new happens…
3
votes
2 answers

epoll_wait return EPOLLOUT even with EPOLLET flag

I am using linux epoll in edge trigger mode. Each time a new connection is incoming, I add the file descriptor to epoll with EPOLLIN|EPOLLOUT|EPOLLET flag. My first question is: What's the right way to check which kind of event(s) occur for each…
demonatic
  • 67
  • 7
3
votes
2 answers

struct epoll_event memset or no memset?

When browsing through code on the Internet, I often see snippets like these: struct epoll_event event; memset(&event, 0, sizeof(event)); This pattern seems needless to me, if event is filled out in full, but it is widespread. Perhaps to take into…
user1095108
  • 14,119
  • 9
  • 58
  • 116
3
votes
2 answers

Why does epoll_ctl need the filedescriptor twice?

In the example: event.events = EPOLLIN; event.data.fd = fd; int ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, event.data.fd, &event); I pass the file descriptor in as both a member of event.data and as an argument in its own right. What does epoll_ctl…
fadedbee
  • 42,671
  • 44
  • 178
  • 308
3
votes
2 answers

linux - Can't get eventfd to work with epoll together

I'm writing a simple server class based on epoll. In order to wake up epoll_wait(), I decided to use an eventfd. It is said that it is better suited for simple event communication and I agree with that. So I created my event and put a watch on…
user666412
  • 528
  • 8
  • 18
3
votes
1 answer

How do "special" epoll flags correspond to kqueue ones?

I'm struggling to draw a parallel between epoll and kqueue flags, specifically EPOLLONESHOT EPOLLET EPOLLEXCLUSIVE and EV_CLEAR/EV_DISPATCH/EV_ONESHOT. I'm investigating the kqueue for the first time; I only had an experience with…
ghostmansd
  • 3,285
  • 5
  • 30
  • 44
3
votes
1 answer

.net core async/await tcp with linux

I try to write simple echo server with .net and linux with code static async Task soc() { var listener = new TcpListener(IPAddress.Loopback, 8888); listener.Start(); for (;;) { logger.LogInformation("Wait new…
Sem Grozovski
  • 404
  • 3
  • 11
3
votes
1 answer

Can read() from socket return EWOULDBLOCK?

My code uses epoll for multiplexing. After EPOLLIN is encountered it reads one byte from ready socket. read(fd, &onebyte, 1); Can the above line return EWOULDBLOCK?
user14063792468
  • 839
  • 12
  • 28
3
votes
1 answer

Nested EPOLL FD

I find very limited information online about the behaviour of nested FDs. Lets say EPOLL FD1 is shared between EPOLL FD2 and FD3. So what would happen if epoll FD1 get an event Is it going to raise an event to both FD2 and Fd3 if it is added…
3
votes
1 answer

EBADF while recv after epoll_wait

i've got a following problem: i have a epoll code which receives connections: while (1) { int nfds = epoll_wait(epollfd, events, 4096, -1); if (nfds == -1) { if (errno == EINTR) continue; perror("epoll_wait"); …
milo
  • 1,220
  • 3
  • 17
  • 33
3
votes
1 answer

Does `epoll_wait` signify which event was triggered when both EPOLLIN and EPOLLOUT were added?

Suppose I am specifying both EPOLLIN and EPOLLOUT flags when adding descriptors to monitor with epoll_wait. From the 'epoll' manpages it is unclear what exactly each of the epoll_event structures returned as part of the array carries in its events…
Armen Michaeli
  • 8,625
  • 8
  • 58
  • 95
3
votes
3 answers

Python: retrieve several URLs via select.epoll()

I have an event oriented server which already uses select.epoll(). Now a new requirement should be solved: URLs should get fetched (async). Up to now I always used the requests library, and I always used it synchronous, never asynchronous. How can I…
guettli
  • 25,042
  • 81
  • 346
  • 663
3
votes
3 answers

epoll: difference between level triggered and edge triggered when EPOLLONESHOT specified

What's the difference between level triggered and edge triggered mode, when EPOLLONESHOT specified? There's a similar question already here. The answer by "Crouching Kitten" doesn't seem to be right (and as I understand, the other answer doesn't…
geza
  • 28,403
  • 6
  • 61
  • 135