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.

Tim
  • 101,790

1 Answers1

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.

Stephen Kitt
  • 434,908
  • Thanks. Why "You can use FIFOs in (some) GUIs, unlike process substitution"? – Tim Mar 26 '18 at 04: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 04:22
  • Doesn't process substitution provide some file in /dev/fd/, which we can look up in GUIs? – Tim Mar 26 '18 at 04: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 04: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 05:05
  • I’m not sure exactly what you mean by “same shell”. Process substitution is always set up by the shell which parses the command line; however you can use another shell to run the command. See for example bash -c "cat <(zsh -c 'echo Hello')". – Stephen Kitt Mar 26 '18 at 06:04
  • Thanks. (1) Sorry that I wasn't clear about different or same shell. when using mkfifo fifo, we can run cat fifo in a shell process running in one terminal emulator window, and run echo hello > fifo in a shell process running in another terminal emulator window. I was wondering if process substitution can work like that or not? (2) As we have seen my previous post that a FIFO created by mkfifo can be used for nonlinear connection between processes, while a pipe just for linear connection. Can process substitution be used for nonlinear or linear connection? – Tim Mar 26 '18 at 15:13
  • From multiple windows, no, you can’t use process substitution. Process substitution can be used non-linearly, e.g. you can use it to feed multiple commands’ output into a single process. – Stephen Kitt Mar 26 '18 at 17:26