Re: questions about pipe

pankajtakawale wrote:
I have a parent process, which pulls stdout of its child process using
pipe (child process's stdout is redirected to write end of pipe)

I have few doubts about the pipe mechanism:

parent code:

//other parent code;

while(read from read-end of pipe)
process read data;

1. what if child process flushes some data to stdout & breaks pipe
(either by exiting or explicitly closing stdout), even before parent
calls 'read on read-end of pipe'?

In this case, read would fail as other end of pipe is broken,
right? or does it able to read data flushed by child process and then
it would see broken pipe and then come out of read?

2. If Parent is in the middle of processing 'read data', and child
flushed data to stdout & exited (i.e. broke wite-end of pipe)

In this case, next read after processing would be able to read data
from pipe and process it. Then next read would see broken pipe and
returns error, right?

In both the cases, read() will return all the data written to the pipe
before its write end has been closed. When no data is available and the
write end is closed read() will return 0 (EOF).