Linux Files and the Event Poll Interface - Edge- Versus Level-Triggered Events (
Page 4 of 4 )
If the EPOLLET value is set in the events
field of the
event
parameter passed to
epoll_ctl()
, the watch on
fd
is edge-triggered, as opposed to level-triggered.
Consider the following events between a producer and a consumer communicating over a Unix pipe:
-
The producer writes 1 KB of data onto a pipe.
-
The consumer performs an
epoll_wait()
on the pipe, waiting for the pipe to contain data, and thus be readable.
With a level-triggered watch, the call to
epoll_wait()
in step 2 will return immedi
ately, showing that the pipe is ready to read. With an edge-triggered watch, this call will not return until after step 1 occurs. That is, even if the pipe is readable at the invocation of
epoll_wait()
, the call will not return until the data is written onto the pipe.
Level-triggered is the default behavior. It is how
poll()
and
select()
behave, and it is what most developers expect. Edge-triggered behavior requires a different approach to programming, commonly utilizing nonblocking I/O, and careful checking for
EAGAIN
.
The terminology comes from electrical engineering. A level-triggered interrupt is issued whenever a line is asserted. An edge-triggered interrupt is caused only during the rising or falling edge of the change in assertion. Level-triggered interrupts are useful when the state of the event (the asserted line) is of interest. Edge-triggered interrupts are useful when the event itself (the line being asserted) is of interest.
Please check back next week for the continuation of this article