Linux Command Reference ยท Files and Directories
Linux mkfifo command
Reference for the Linux mkfifo command: create named pipes (FIFOs), connect two processes through a file name, understand blocking, and avoid deadlocks.
mkfifo creates a named pipe, also called a FIFO (first in, first out). It looks like a file in a directory, but it holds no data on disk: bytes written into it by one process come out of it, in order, for another process. It is the same mechanism as the | in a pipeline, except that the two processes need not be started by the same command line, and they find each other by file name.
The important rule: opening a FIFO blocks until the other end is open too. A writer waits for a reader, and a reader waits for a writer. Most surprises come from forgetting this.
mkfifo [OPTION]... NAME...
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
mkfifo [-m MODE] NAME...
| Option | Description |
|---|---|
-m MODE, --mode=MODE |
Set permissions (octal or symbolic) instead of 0666 minus the umask. |
-Z |
Set the SELinux context to the default type (where supported). |
Returns: 0 if every FIFO was created; non-zero otherwise ("File exists", "Permission denied", or "No such file or directory" for a missing directory).
Usage notes
- Blocking open:
echo hi > fifowaits until some process opens the FIFO for reading. Run the writer in the background (&) or in a second terminal, or both ends will wait forever. - Data lives only in memory, in a kernel buffer (64 KiB by default on Linux). A writer that outruns the reader is paused until space frees up. Nothing is stored in the file system, so the size shows as 0.
- End of file: a reader sees EOF when the last writer closes the pipe. A shell loop
while read ... done < fifotherefore ends after the first writer finishes. To keep reading across writers, reopen the FIFO in a loop. - A reader and writer in one shell can deadlock. The classic trick to avoid blocking is to open it read-write with
exec 3<>fifo, which succeeds without a partner. - Permissions work like for files: use mode
600for private channels. Anyone who can write to the FIFO can inject data into your reader. - Remove it when done. A FIFO stays in the directory until you
rmit. A script that creates one should clean up with a trap, see Traps and signals. - Compared with a regular pipe: use
|when one command line starts both sides; use a FIFO when processes start independently, or to build non-linear flows (one producer, several consumers each with its own FIFO). - Related tools: process substitution
<(cmd)creates a temporary FIFO or/dev/fdpath for you;teecan feed several FIFOs from one stream. - No seeking: a FIFO is a stream, so tools that need to jump around in their input (or read it twice) cannot work on it.
- Portability:
mkfifois POSIX. Named pipes work on Linux, BSD and macOS but not on every network file system.