Running stdin/out through a pipe to a child process

chris at mimsy.UUCP chris at mimsy.UUCP
Mon Jan 19 02:45:09 AEST 1987


In article <423 at bobkat.UUCP> m5d at bobkat.UUCP (Mike McNally) warns:
>It is important for the parent to close the unused ends of the pipes for
>a reason other than tidiness.  If the child dies, the parent may want
>to know.  The system (BSD, at least) will send a SIGPIPE to the parent
>if the child dies but the parent is still writing to the pipe AND the
>child's copy of the descriptor is the only reader.  If the parent's copy
>of the read end is still open, the system will not deliver SIGPIPE.

It is yet more important than that.  As long as the parent still
has open its chain to the read end of the pipe, the kernel will
(having duly noted that there is *some* process out there that can
read the pipe) eventually block the parent in a write() system call
on the pipe until someone reads some of the data from that pipe.
If the child dies or otherwise lets go of its own chain to the read
end, only the (blocked) writer will be able to read enough data to
unclog the pipe---but that writer can do nothing until someone
unclogs the pipe!

There *are* situations in which everything will work (the parent
may write just a little bit, not blocking, then exit), but this
is a bug just waiting to bite.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP:	seismo!mimsy!chris	ARPA/CSNet:	chris at mimsy.umd.edu



More information about the Comp.unix.questions mailing list