Why does the read() block in this case?(linux epoll) Why does the read() block in this case?(linux epoll) unix unix

Why does the read() block in this case?(linux epoll)


As I understand the behavior:

If instead of ctrl-D you type 'ENTER', 4 events are reported as with CTRL-D. We see ascii code:10 for the line feed. With CTRL-D read blocks.

CTRL-D isn't signalling EOF but rather flushing out the data so far input. CTRL-D itself being recognized as an event. But actually no data to be pulled out on thatfd. And given that the socket is blocking, we end up in case where read does not return until another set of events happen.

Now if CTRL-D is the first event, it is recognised with read yeilding zero. Signaling a EOF condition. This does not happen if there were something to be flushed.

When you made the socket non-blocking, CTRL-D read returns -1, with errno set to EAGAIN. Which means 'no data to be read now. try later'.


From the manual (epoll_ctl):

EPOLLERR    Error condition happened on the associated file descriptor.    epoll_wait(2) will always wait for this event;    it is not necessary to set it in events.

As you can see, error conditions are always monitored.