2

I'm trying to learn about Linux I/O. Recently I was going through the manual page of epoll system call and I notice that a file descriptor can be notified if it is available for writing via EPOLLOUT event. I'm trying to understand when is this possible that a non-blocking file descriptor fd is not available for writing, i.e., the system call,

ssize_t bytes_written = write(fd, buff, SIZE_OF_DATA_TO_WRITE)

returns bytes_written to be -1 and sets errno to be EAGAIN. I want to know when is the above condition possible? Is it like when the server is expecting a read() but client issues a write() call or if either the server/client has closed the socket and the other party is trying to write to the socket? Or is it something else entirely?

Note: I'll be talking about file descriptors in terms of network sockets and not files on disk. I know that writing to a disk will in general not result in the condition mentioned above.

  • If you have set the socket to non-blocking and the write would block (say, because the write buffer is full) you might get EAGAIN. – President James K. Polk Oct 09 '22 at 11:19
  • @PresidentJamesK.Polk by that you mean the kernel buffer that has the data, that has been copied from the user buffer `buff`, is full and it hasn't been sent to the receiver via the network protocol (TCP/UDP) right? – Aritra Sur Roy Oct 09 '22 at 11:32
  • 1
    Basically, yes, although the exact mechanism is not important. The point is that your computer is probably much faster than your network and therefore you can write faster than your network can accept data. – President James K. Polk Oct 09 '22 at 11:34

0 Answers0