From APUE
FIFOs can be used to duplicate an output stream in a series of shell commands. This prevents writing the data to an intermediate disk file (similar to using pipes to avoid intermediate disk files).
But whereas pipes can be used only for linear connections between processes, a FIFO has a name, so it can be used for nonlinear connections.
Consider a procedure that needs to process a filtered input stream twice.
mkfifo fifo1 prog3 < fifo1 & prog1 < infile | tee fifo1 | prog2
We create the FIFO and then start prog3 in the background, reading from the FIFO. We then start prog1 and use tee to send its input to both the FIFO and prog2.
How does a FIFO "duplicate an output stream in a series of shell commands"? Isn't this done by
tee
instead of a FIFO?In the example,
mkfifo fifo1
creates a file in the current directory, andfifo1
seems replaceable with a regular file . So what is the point of a FIFO "prevent writing the data to an intermediate disk file"?What do "linear connections" and "nonlinear connections" between processes mean? What does it mean that a FIFO can be used for nonlinear connections, while a pipe can be only used for linear connections between processes?
Thanks.
tee
, not by a FIFO, for example, usingtee
and a temporary regular file we can achieve the same nonlinear connection too. – Tim Mar 25 '18 at 22:15mkfifo fifo && seq 1 1000000 | tee fifo
. You won’t see any input from this initially, because the FIFO doesn’t start accepting data until both its ends are open. Runhead fifo
, and you’ll see theseq
side of the pipe output a bunch of numbers and then stop with exit code 141, i.e.SIGPIPE
(128 + 13): the output gives an indication the size of the kernel buffer used for the FIFO, at most a few tens of kilobytes. That’s all the memory that’s used by a pipe. – Stephen Kitt Mar 26 '18 at 03:42prog1
needs two exit nodes (one forprog2
and one forprog3
), someone can usetee
under some circumstances like this:echo "hello world" | tee >(sed 's/h/H/') >(sed 's/w/W/g')
– George Vasiliou Apr 11 '18 at 12:22pipe
syscall has no such limitation. – derobert Apr 11 '18 at 22:48