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

Boost Asio On Linux Not Using Epoll

I was under the impression that boost::asio would use an epoll setup by default instead of a select implementation, but after running some tests it looks like my setup is using select. OS: RHEL 4 Kernel:2.6 GCC:3.4.6 I wrote a little test program to…
Scott Lawson
  • 51
  • 1
  • 2
5
votes
1 answer

What is the state of C10K-like event-based server development in TCL?

TCL is a nice simple programming language, but does not seem to get the credit and/or respect it deserves. I learned it back in 1995 in college and promptly forgot about it only to stumble upon it again recently. I am mostly interested TCL for…
z8000
  • 3,715
  • 3
  • 29
  • 37
5
votes
2 answers

How to interrupt epoll_pwait to catch signal

I would write a small test that shows me the difference between epoll_Wait and epoll_pwait() by catching any interrupted signal. can anyone provide me by a small code or any method to find that? int epoll_pwait(int epfd, struct epoll_event *events, …
Hatem Mashaqi
  • 610
  • 1
  • 7
  • 18
5
votes
3 answers

Getting disconnection notification using TCP Keep-Alive on write blocked socket

I use TCP Keep-Alive option to detect dead connection. It works well with connection that use reading sockets: setsockopt(mysock,...) // set various keep alive options epoll_ctl(ep,mysock,{EPOLLIN|EPOLERR|EPOLLHUP},) epoll_wait -> (exits after…
Artyom
  • 31,019
  • 21
  • 127
  • 215
5
votes
1 answer

Number of watched file descriptors inside a epoll

I am looking for a way to check the current number of file descriptors being monitored by an epoll instance. I use the following for creating and populating the epoll instance epoll_create epoll_ctl Platform is Gnu/Linux.
Yordan Pavlov
  • 1,303
  • 2
  • 13
  • 26
5
votes
2 answers

epoll with edge triggered event

The man page of epoll has a sample code for edge triggered like the following : for (;;) { nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1); if (nfds == -1) { perror("epoll_pwait"); exit(EXIT_FAILURE); } for (n…
barfatchen
  • 1,630
  • 2
  • 24
  • 48
5
votes
1 answer

How to use epoll_event data.ptr

I am having an increasingly hard time using the void *ptr in the epoll_event. I can just link this to a struct? For example, can I do something like this? Because I am trying todo something like this but it does not work, the first loop on the…
User
  • 659
  • 2
  • 12
  • 29
5
votes
2 answers

epoll and timeouts

I'm using epoll to manage about 20 to 30 sockets. I figure out that epoll_wait can be used to wait for some data to arrive over one of the socket but I'm missing how do I implement timeouts on socket level. I can use timeout on epoll_wait but it not…
Shivam
  • 2,134
  • 1
  • 18
  • 28
4
votes
1 answer

epoll behavior when writing to a file descriptor

I'm using epoll to write large messages to a server using HTTP protocol. 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. For writing I am unsure…
user1290156
  • 41
  • 1
  • 2
4
votes
1 answer

accept() interrupted on a signal and epoll_wait()

If I am epoll_wait()ing on a listening socket and, when epoll_wait() returns indicating it has activity (in this case, a connection waiting to be accept()ed), then if the accept() call fails with errno=EINTR, will epoll_wait() indicate that the same…
Steely Dan
  • 223
  • 1
  • 8
4
votes
1 answer

epoll_wait fails due to EINTR, how to remedy this?

My epoll_wait fails due to EINTR. My gdb trace shows this: enter code here 221 in ../nptl/sysdeps/pthread/createthread.c (gdb) 224 in ../nptl/sysdeps/pthread/createthread.c (gdb) [New Thread 0x40988490 (LWP 3589)] 227 in…
RajSanpui
  • 11,556
  • 32
  • 79
  • 146
4
votes
3 answers

Executing epoll_ctl in one thread while the other thread is in the middle of epoll_wait

I am new to linux server programming with epoll. I have 2 threads: Thread_Accept and Thread_epoll. The former is block accept loop, if new connection is coming, it will add the new fd with epoll_ctl(). The latter is a big epoll_wait() loop. Now my…
Martin Ng
  • 89
  • 1
  • 3
  • 10
4
votes
2 answers

epoll best way to detect user disconnection

I am working on a network programming using epoll. I was wondering the best way to detect user disconnection. Right now, I am using select with timeout to see if there is a signal in the receive buffer and there is no signal for a certain amount of…
codereviewanskquestions
  • 13,460
  • 29
  • 98
  • 167
4
votes
1 answer

What is the difference between epoll and boost::asio::io_context?

I know that epoll and io_context work asynchronously. So, can you tell me what is the difference between the two? Are you using epoll inside asio::io_context?
Hwan E
  • 566
  • 2
  • 13
4
votes
1 answer

Does one nginx worker process handle two requests concurrently or one by one?

The really cool part about the filter chain is that each filter doesn't wait for the previous filter to finish; it can process the previous filter's output as it's being produced, sort of like the Unix pipeline. (from here) I guess the…
cpuer
  • 7,413
  • 14
  • 35
  • 39