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

What's the difference between epoll, poll, threadpool?

Could someone explain what the difference is between epoll, poll and threadpool? What are the pros / cons? Any suggestions for frameworks? Any suggestions for simple/basic tutorials? It seems that epoll and poll are Linux-specific... Is there an…
Filly
  • 1,341
  • 3
  • 11
  • 7
78
votes
2 answers

Why is epoll faster than select?

I have seen a lot of comparisons which says select have to walk through the fd list, and this is slow. But why doesn't epoll have to do this?
amazingjxq
  • 4,487
  • 7
  • 33
  • 35
74
votes
3 answers

What is the purpose of epoll's edge triggered option?

From epoll's man page: epoll is a variant of poll(2) that can be used either as an edge-triggered or a level-triggered interface When would one use the edge triggered option? The man page gives an example that uses it, but I don't see why it is…
Dan
  • 12,409
  • 3
  • 50
  • 87
69
votes
2 answers

select vs poll vs epoll

I am designing a new server which needs to support thousands of UDP connections (somewhere around 100,000 sessions). Any input or suggestions on which one to use?
ravi
  • 745
  • 1
  • 7
  • 4
59
votes
2 answers

Does epoll(), do its job in O(1)?

Wikipedia says unlike the older system calls, which operate at O(n), epoll operates in O(1) [2]). http://en.wikipedia.org/wiki/Epoll However, the source code at fs/eventpoll.c on Linux-2.6.38, seems it is implemented with an RB tree for…
ddoman
  • 1,051
  • 1
  • 10
  • 12
55
votes
4 answers

What's the difference between event-driven and asynchronous? Between epoll and AIO?

Event-driven and asynchronous are often used as synonyms. Are there any differences between the two? Also, what is the difference between epoll and aio? How do they fit together? Lastly, I've read many times that AIO in Linux is horribly broken. How…
Continuation
  • 12,722
  • 20
  • 82
  • 106
44
votes
4 answers

What is the best epoll/kqueue/select equvalient on Windows?

What is Windows' best I/O event notification facility? By best I mean something that ... doesn't have a limit on number of input file descriptors works on all file descriptors (disk files, sockets, ...) provides various notification modes (edge…
blackwing
  • 3,154
  • 3
  • 23
  • 20
38
votes
2 answers

Is epoll thread-safe?

There are two functions in epoll: epoll_ctl epoll_wait Are they thread-safe when I use the same epoll_fd? What will happen if one thread calls epoll_wait and others call epoll_ctl at the same time?
atomd
  • 558
  • 1
  • 5
  • 11
30
votes
2 answers

Epoll on regular files

Can epoll (on Linux) be somehow useful for regular files? I know it's primarily used with sockets but just wonder.
Cartesius00
  • 23,584
  • 43
  • 124
  • 195
30
votes
1 answer

Difference between inotify and epoll

I would like to know what the difference is between both i/o watchers inotify and epoll? inotify inotify_init(void) creates inotify instance to read events from inotify_add_watch(int fd, const char * path, int mask) returns a watch fd around the…
bodokaiser
  • 15,122
  • 22
  • 97
  • 140
29
votes
1 answer

Is it necessary to deregister a socket from epoll before closing it?

Assume the following code where "sock" is a handle to TCP socket that was previously registered with an epoll file descriptor designated by epfd. epoll_ctl(epfd, EPOLL_CTL_DEL, sock, &ev); close(sock); Is it still necessary to call epoll_ctl if the…
selbie
  • 100,020
  • 15
  • 103
  • 173
29
votes
2 answers

Could you recommend some guides about Epoll on Linux

I need to know about Epoll On linux System. Could you recommend manual or guides about epoll library? need more detailed guides. it's better to have some examples. help me. and Thank you for reading.
Simon Kim
  • 377
  • 1
  • 6
  • 9
29
votes
2 answers

Why native epoll support is introduced in Netty?

I believe Java's NIO library will use epoll on Linux machines. What are all the advantages of using Epoll instead of NIO on Linux machines.
Dhanaraj Durairaj
  • 644
  • 1
  • 6
  • 17
26
votes
3 answers

Linux, sockets, non-blocking connect

I want to create a non-blocking connect. Like this: socket.connect(); // returns immediately For this, I use another thread, an infinite loop and Linux epoll. Like this(pseudocode): // in another thread { create_non_block_socket(); connect(); …
herolover
  • 733
  • 3
  • 9
  • 18
26
votes
1 answer

Poorly-balanced socket accepts with Linux 3.2 kernel vs 2.6 kernel

I am running a fairly large-scale Node.js 0.8.8 app using Cluster with 16 worker processes on a 16-processor box with hyperthreading (so 32 logical cores). We are finding that since moving to the Linux 3.2.0 kernel (from 2.6.32), the balancing of…
Brett
  • 3,478
  • 1
  • 22
  • 23
1
2 3
52 53