Subject: Re: bug#6379: process substitution with a give suffix

From: Eric Blake <[email-masked]>
Date: Tue, 08 Jun 2010 13:13:54 -0600

On 06/08/2010 12:33 PM, Peng Yu wrote:
>>  mkfifo myfifo.suffix
>>  something >myfifo.suffix &
>>  ./program <myfifo.suffix
>>  wait
>>  rm myfifo.suffix
> 
> The above question was sent to bug-bash. But since it is related to
> mkfilo. I redirect it to bug-coreutils.

Your question is about how to use various Unix tools together; rather
than directing to bug-bash or bug-coreutils, you may be better off
directing to a generic shell-programming forum.

As it is, you didn't raise any bug report about the mkfifo program, so
redirecting to coreutils didn't really buy you anything.

> 
> I have more than one arguments. I tried the following code. It doesn't
> seem to work for more than one arguments. Would you please let me know
> what is wrong?
> 
> BTW, using fifo is going to be much faster than using a temp file as
> it avoid the disk usage, right?

A fifo will have the same speed as a pipe (another name for a fifo is
named pipe; unlike 'foo | bar', where the pipe is anonymous and exists
between exactly two processes, a fifo can be accessed from the file
system by multiple processes, but but under the hood, it uses the same
kernel pipe handling code).  It has the drawback of being non-seekable
in comparison to temporary regular files.  It has the advantage of
atomic operations not guaranteed by disk files, provided you stick to
transactions below the size guaranteed by your kernel.  And if you use a
ramdisk backing store for /tmp, there is little difference in speed
(either way, a ramdisk or a fifo does not have to do disk I/O); but
since you can't guarantee that /tmp is a ramdisk, yes, a fifo can be
faster for interprocess communication.  And if used incorrectly, it has
the potential to deadlock your shell script (something that won't happen
with regular files).

> $ cat a.txt
> In a.txt
> $ cat b.txt
> In b.txt
> $ cat main.sh
> #!/usr/bin/env bash
> mkfifo a.suffix
> cat a.txt >a.suffix &
> mkfifo b.suffix
> cat b.txt >b.suffix &
> cat <a.suffix <b.suffix

This is redirecting stdin twice, with the net effect that cat only sees
the contents of the fifo b.suffix.  You probably meant to do:

cat a.suffix b.suffix

> wait
> rm a.suffix b.suffix
> $ ./main.sh
> In b.txt

-- 
Eric Blake   [email-masked]    +1-801-349-2682
Libvirt virtualization library http://libvirt.org