|
Home > Archive > Unix Programming > August 2006 > questions about pipe
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
questions about pipe
|
|
| pankajtakawale 2006-08-10, 1:33 pm |
| 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?
| |
| Maxim Yegorushkin 2006-08-10, 1:33 pm |
|
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).
| |
| Barry Margolin 2006-08-11, 1:31 am |
| In article <1155219090.067062.315940@i3g2000cwc.googlegroups.com>,
"Maxim Yegorushkin" <maxim.yegorushkin@gmail.com> wrote:
> pankajtakawale wrote:
>
> 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).
And if the parent tries to write more data than the pipe can buffer, the
call to flush the data will block. Unless the parent has put the pipe
into non-blocking mode, in which case the attempt to flush will report
an error and it's up to the parent to decide what to do about it.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| pankajtakawale 2006-08-17, 7:35 am |
| so, any call to write to pipe would block untill reader process reads
that data from pipe?
Barry Margolin wrote:
> In article <1155219090.067062.315940@i3g2000cwc.googlegroups.com>,
> "Maxim Yegorushkin" <maxim.yegorushkin@gmail.com> wrote:
>
>
> And if the parent tries to write more data than the pipe can buffer, the
> call to flush the data will block. Unless the parent has put the pipe
> into non-blocking mode, in which case the attempt to flush will report
> an error and it's up to the parent to decide what to do about it.
>
> --
> Barry Margolin, barmar@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***
> *** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| Bill Pursell 2006-08-17, 7:18 pm |
|
pankajtakawale wrote:
> Barry Margolin wrote:
[vbcol=seagreen]
> so, any call to write to pipe would block untill reader process reads
> that data from pipe?
No. small writes will probably not block. You can write
up to PIPE_BUF bytes without blocking (on Linux at least,
I'm not sure if that's universal.) If you write that many bytes
and no one reads them, then subsequent writes will block
until someone reads the data. Or, you can make the pipe
non-blocking, in which case the writes will never block.
The call to write will return immediately, but if the pipe
is full, it will return 0. (Assuming there is no error.)
| |
|
|
|
|
|