Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

sockets

epoll

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 necessary in the example.

like image 913
Dan Avatar asked Feb 06 '12 15:02

Dan


People also ask

What is edge-triggered and level-triggered in epoll?

epoll provides both edge-triggered and level-triggered modes. In edge-triggered mode, a call to epoll_wait will return only when a new event is enqueued with the epoll object, while in level-triggered mode, epoll_wait will return as long as the condition holds.

Why is epoll used?

epoll stands for event poll and is a Linux specific construct. It allows for a process to monitor multiple file descriptors and get notifications when I/O is possible on them. It allows for both edge-triggered as well as level-triggered notifications.


2 Answers

When an FD becomes read or write ready, you might not necessarily want to read (or write) all the data immediately.

Level-triggered epoll will keep nagging you as long as the FD remains ready, whereas edge-triggered won't bother you again until the next time you get an EAGAIN (so it's more complicated to code around, but can be more efficient depending on what you need to do).

Say you're writing from a resource to an FD. If you register your interest for that FD becoming write ready as level-triggered, you'll get constant notification that the FD is still ready for writing. If the resource isn't yet available, that's a waste of a wake-up, because you can't write any more anyway.

If you were to add it as edge-triggered instead, you'd get notification that the FD was write ready once, then when the other resource becomes ready you write as much as you can. Then if write(2) returns EAGAIN, you stop writing and wait for the next notification.

The same applies for reading, because you might not want to pull all the data into user-space before you're ready to do whatever you want to do with it (thus having to buffer it, etc etc). With edge-triggered epoll you get told when it's ready to read, and then can remember that and do the actual reading "as and when".

like image 94
James McLaughlin Avatar answered Oct 19 '22 22:10

James McLaughlin


In my experiments, ET doesn't guarantee that only one thread wakes up, although it often wakes up only one. The EPOLLONESHOT flag is for this purpose.

like image 36
cpq Avatar answered Oct 19 '22 23:10

cpq