cunixpipe

Why does the read end of a pipe read EOF only if the write end is closed?


I don't really understand the difference between "closing the write end of the pipe" and "not writing anything to the pipe". If I don't write anything to the pipe and the pipe is empty, why is the read end simply blocked rather than reading an EOF? How is that different from closing the write end?


Solution

  • Reading an EOF from a pipe (or from anything) indicates that there's no more input, and that there won't be any more input in the future.

    If there's no input available at the moment, but the pipe hasn't been closed, then the reader will (by default) block waiting for input; if the writer then writes to the pipe, that data will become available to the reader. An EOF would tell the reader to stop trying to read any more data.