Subject: bug#6900: pitfalls of recoding named fifo as pipe

From: John Reiser <[email-masked]>
Date: Wed, 25 Aug 2010 07:14:38 -0700

On 08/24/2010 11:32 AM, Paul Eggert wrote:
>   dir=$(mktemp -d dir.XXXXXX)
>   mkfifo $dir/1 $dir/2
>   listener1 < $dir/1
>   listener2 < $dir/2
>   cmd1 2> $dir/1 | cmd2 2> $dir/2

> Better yet, do not use named fifos, since pipes suffice:
> 
>   (cmd1 2>&1 >&3 | listener1) 3>&1 |
>   (cmd2 2>&1 >&3 | listener2) 3>&1

That is not equivalent!  stdout from listener1 gets piped into
cmd2, instead of going to the common stdout of all processes
other than cmd1.  Fixing this using only anonymous pipes
is cumbersome because the additional file descriptor must be
[should be] redirected by all four processes.

Also, file descriptor 3 is left open in all processes, which
hogs system resources and is less safe.

Therefore something like this is required:
4>&1  (
  3>&1       ( 1>&3  2>&1  3>&-  4>&-  cmd1
             | 1>&4        3>&-  4>&-  listener1
             )
|
  3>&1  4>&- ( 1>&3  2>&1  3>&-        cmd2
             |             3>&-        listener2
             )
)
where the formatting emphasizes prefix actions and common elements.

Using the named fifos is much more readable and understandable.
The cost is explicit cleanup, including the transfer of the onus
for recovery at resource exhaustion (fd not available, space
not available in filesystem) from the shell to the user.

--