Under what circumstances does the read() syscall return 0? Under what circumstances does the read() syscall return 0? unix unix

Under what circumstances does the read() syscall return 0?


  • If the Physical File System does not support simple reads from directories, read() will return 0 if it is used for a directory.
  • If no process has the pipe open for writing, read() returns 0 to indicate the end of the file.
  • If the connection is broken on a stream socket, but no data is available, then the read() function returns 0 bytes as EOF.


Normally a return value of 0 always means end-of-file. However, if you specify 0 as the number of bytes to read, it will always return 0 unless there's an error detected.

Terminal devices are a special case. If the terminal is in cooked mode, typing Control-d tells the device driver to return from any pending read() immediately with whatever is in the input editing buffer, rather than waiting for the user to enter a newline. If the buffer is empty, this results in a zero-length read. This is how typing the EOF character at the beginning of a line is automatically treated as EOF by applications.