Intro To 'mkfifo' Command In Linux
2023-05-30 - By Robert Elder
I use the 'mkfifo' command to create named pipes:
mkfifo my-first-named-pipe
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:
xxd my-temporary-output
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:
ls -l
total 0
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
mkfifo my-hexdump-fifo
ls -l
total 0
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
xxd my-hexdump-fifo
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.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|