- publishing free software manuals
Perl Language Reference Manual
by Larry Wall and others
Paperback (6"x9"), 724 pages
ISBN 9781906966027
RRP £29.95 ($39.95)

Sales of this book support The Perl Foundation! Get a printed copy>>>

20.3.5 Avoiding Pipe Deadlocks

In general, if you have more than one sub-process, you need to be very careful that any process which does not need the writer half of any pipe you create for inter-process communication does not have it open.

The reason for this is that any child process which is reading from the pipe and expecting an EOF will never receive it, and therefore never exit. A single process closing a pipe is not enough to close it; the last process with the pipe open must close it for it to read EOF.

Certain built-in Unix features help prevent this most of the time. For instance, filehandles have a 'close on exec' flag (set en masse with Perl using the $^F 10), so that any filehandles which you didn't explicitly route to the STDIN, STDOUT or STDERR of a child program will automatically be closed for you.

So, always explicitly and immediately call close() on the writable end of any pipe, unless that process is actually writing to it. If you don't explicitly call close() then be warned Perl will still close() all the filehandles during global destruction. As warned above, if those filehandles were opened with Safe Pipe Open, they will also call waitpid() and you might again deadlock.

ISBN 9781906966027Perl Language Reference ManualSee the print edition