Oktaheta
Oktaheta

Reputation: 626

What happened if I don't set EPOLLOUT event and direct call write() function?

I have EPOLLIN event for read data only. Is it ok to direct write the data without setting EPOLLOUT event?

Upvotes: 4

Views: 1589

Answers (1)

jxh
jxh

Reputation: 70502

When using epoll with level triggered notifications, you will get constant EPOLLOUT notifications on a socket except for those brief periods of time that the output buffer for the socket is actually full. The impact of this is that your event loop will wake up even if you have nothing to send, and there was no data to receive either.

To deal with that, you have two practical choices:

  1. Disable EPOLLOUT until those times you actually get EWOULDBLOCK / EAGAIN. At that point, you enable EPOLLOUT, and disable it again once your send is complete.

  2. Enable edge triggered notifications, and leave EPOLLOUT enabled all the time. Now, you will only get EPOLLOUT notifications when the system changes from a output buffer full state to non-full state. However, EPOLLIN is now also also edge triggered, which means you only get one EPOLLIN notification until you drain the input buffer.

In comments, a third option was presented to you to implement your own polling loop to deal with the sockets with full output buffers. This can be made to work, but should not be necessary, since epoll can tell you when it is okay to write.

Upvotes: 6

Related Questions