0

This question is from https://mywiki.wooledge.org/ProcessSubstitution

mkfifo /var/tmp/fifo1
mkfifo /var/tmp/fifo2
sort list1 >/var/tmp/fifo1 &
sort list2 >/var/tmp/fifo2 &
diff /var/tmp/fifo1 /var/tmp/fifo2
rm /var/tmp/fifo1 /var/tmp/fifo2

As I understand about named pipes, I'm thinking the following is in effect:

On lines 1-2, we create two named pipes: fifo1 and fifo2.

sort list1 > fifo1 & and sort list2 > fifo2 & are trying to write to the named pipes so they are blocked until something reads from them.

Then diff command that comes afterwards, reads from fifo1 and fifo2. So lines 3-4 are unblocked. Next diff is executed and the output sent to terminal.

Lastly, we delete named pipes fifo1 and fifo2.

The source says all this is the same as diff <(sort list1) <(sort list2).

Is my explanation correct?

Thanks!

1
  • Yes, it looks correct to me. diff expects files, and process substitution allows you to turn the output of the two sort commands into files. Feb 12, 2021 at 2:40

1 Answer 1

0

Is my explanation correct?

No. The sorts are blocked before even being started (the subshells which would execute the sorts are blocked when trying to open the fifos). And they're only blocked until the other side of the fifo is opened too, no need to read anything from it.

If you try mkfifo /tmp/fifo; true > /tmp/fifo, the true will be blocked, even if it isn't ever trying to write anything to the fifo. A true < /tmp/fifo from another shell will unblock it, without trying to read anything at all.

The source says all this is the same as diff <(sort list1) <(sort list2).

It's not. If for some reason diff fifo1 fifo2 terminates before opening the fifos passed as arguments, the sort ... & will stay there forever, until someone has mercy and manually kills them.

And I don't know if process substitution can really be implemented with named pipes, but FWIW bash is NOT getting there by a stretch; on platforms without /dev/fds, bash's process substitutions are buggy and only usable with very much care.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .