Parent / child process communication with pipes

Parent / child process communication with pipes

Post by seb » Fri, 10 Sep 1999 04:00:00



Hi all !

I try to implement a parent <-> child communication
using anonymous pipes.

I want the parent to write into the child's standard
input stream and read from its standard output stream.

+----------------+       +----------------+
| Parent         | pipe1 |          Child |
|       write -> |=======| stdin ->       |
|       read  <- |=======| stdout <-      |
|                | pipe2 |                |
+----------------+       +----------------+

In the parent, I use CreateProcess(), CreatePipe()
and CloseHandle() for unused handles.

My problem is the following :

When the parent process stops (due to a crash or
even a normal termination), the child process is
not killed automatically.

On UNIX systems, the child process gets a SIGPIPE
signal and can manage to stop properly.

Is there any way to handle this on Windows systems ?

Which native communication method would you use to
implement this ?
Remark: We need to find a solution near the UNIX
pipe method because we have to provide a solution
on both platforms !

Thanks.
Seb.

 
 
 

Parent / child process communication with pipes

Post by Thomas W. Brow » Sat, 11 Sep 1999 04:00:00



> Hi all !

> I try to implement a parent <-> child communication
> using anonymous pipes.

> I want the parent to write into the child's standard
> input stream and read from its standard output stream.

> +----------------+       +----------------+
> | Parent         | pipe1 |          Child |
> |       write -> |=======| stdin ->       |
> |       read  <- |=======| stdout <-      |
> |                | pipe2 |                |
> +----------------+       +----------------+

> In the parent, I use CreateProcess(), CreatePipe()
> and CloseHandle() for unused handles.

> My problem is the following :

> When the parent process stops (due to a crash or
> even a normal termination), the child process is
> not killed automatically.

> On UNIX systems, the child process gets a SIGPIPE
> signal and can manage to stop properly.

> Is there any way to handle this on Windows systems ?

> Which native communication method would you use to
> implement this ?
> Remark: We need to find a solution near the UNIX
> pipe method because we have to provide a solution
> on both platforms !

If you do a ReadFile() on a pipe but all write handles have been closed
then you will get an error return with GetLastError() ==
ERROR_BROKEN_PIPE.  The same is true for WriteFile() when all read
handles have been closed.

So, although you don't get a signal, you can detect the apparent loss of
the other end of the pipe.

-- Thomas Brown