2023-05-30 - By Robert Elder
I use the 'mkfifo' command to create named pipes:
What Are 'Regular' 'Non-Named' Unix Pipes?
In order to explain what a 'named pipe' is, it's first helpful to make sure you understand what a regular non-named pipe is. A Unix pipe is a feature that allows programs to communicate with each other. The simplest pipe can be created using the pipe symbol:
echo "Hello World!" | xxd
In example above, the '|' symbol causes the output of the echo command to be fed into the input of the xxd command.
You could also redirect the output of the echo command into a temporary file:
echo "Hello World!" > my-temporary-output
and then run the xxd command to read that file:
However, using a file to store the output could be slow and take up space on your hard drive.
What Are 'Named' Unix Pipes?
A 'named pipe' is just a file that serves the same purpose as the '|' symbol in a regular old 'pipe command'. You can identify a named pipe by the 'p' character that appears at the start of a file listing from the 'ls' command:
prw-rw-r-- 1 robert robert 0 May 27 12:18 my-first-named-pipe
What Are Named Pipes Useful For?
An alternative to the '|' symbol is to create a named pipe using the 'mkfifo' command. This will give you a 'pipe' that can keep data in memory, but also provide blocking semantics. You also won't experience the resource waste (IOPS and disk space) that comes from writing the data to a hard drive:
# Run in first terminal
prw-rw-r-- 1 robert robert 0 May 27 12:15 my-hexdump-fifo
If I run this echo command to write text into the named pipe in one terminal:
# Run in first terminal
echo "Hello World1!" > my-hexdump-fifo
The echo statement will block until I run this command to read the data in the second terminal.
# Run in second terminal
If I try the process in the opposite direction, by reading the data first in the second terminal, the xxd command will block until new data is written in the first terminal.
And that's why the 'mkfifo' command is my favourite Linux command.
A Surprisingly Common Mistake Involving Wildcards & The Find Command
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Intro To 'stty' Command In Linux
The Most Confusing Grep Mistakes I've Ever Made
Intro To 'comm' Command In Linux
Use The 'tail' Command To Monitor Everything
How To Force The 'true' Command To Return 'false'
Why Bother Subscribing?