0

As far as I know, process substitution in bash and FIFO (created by mkfifo) are both named pipes. They both seem viable solutions for communication between processes. I was wondering how to decide when to use which? Thanks.

2

Process substitution doesn’t necessarily involved named pipes; it can be implemented using /dev/fd on platforms which support that.

At least with Bash on Linux, process substitution is significantly faster than FIFOs. Based on Performance differences between pipelines and process substitution, I used the following script to test FIFOs:

mkfifo fifo
for i in {1..10000}; do
    echo foo bar > fifo &
    while read; do
    echo $REPLY > /dev/null
    done < fifo
done
rm fifo

and ended up with similar timings to those given for pipes in the linked question.

With Zsh, the performance difference isn’t as dramatic, but it is still present (approximately 10% in favour of process substitution).

Apart from that I don’t think there’s much practical difference. You can use FIFOs in (some) GUIs, unlike process substitution (except in cases where the GUI ignores its command-line). You can also create a FIFO and use one of its ends without immediately connecting the other end, again unlike process substitution.

  • Thanks. Why "You can use FIFOs in (some) GUIs, unlike process substitution"? – Tim Mar 26 '18 at 4:17
  • You can find a FIFO in a “File / Open” dialog; you can’t find a substituted process. (That doesn’t mean the GUI will actually work with a FIFO, but it’s still an advantage on the FIFO side of the balance sheet.) – Stephen Kitt Mar 26 '18 at 4:22
  • Doesn't process substitution provide some file in /dev/fd/, which we can look up in GUIs? – Tim Mar 26 '18 at 4:24
  • It can use a file in /dev/fd, which could be used in a GUI, except that it’s impossible to separate the definition from the use of this form of pipe, so in practice there’s not much scope for actually putting this to any use. The only exception is with GUI programs which ignore their command line, and then you’re right, /dev/fd would work. – Stephen Kitt Mar 26 '18 at 4:41
  • Thanks. I have heard that we can read and write with a FIFO (created by mkfifo) from different shells. Is it the same case for process substitution? In process substitution, the command inside it and the command which uses it are specified in the same command line, so should they be run in the same shell? – Tim Mar 26 '18 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.