LinuxCommandLibrary

mkfifo

Create named pipes (FIFOs)

TLDR

Create a named pipe at a given path

$ mkfifo [path/to/pipe]
copy

Send data through a named pipe and send the command to the background
$ echo "[Hello World]" > [path/to/pipe] &
copy

Receive data through a named pipe
$ cat [path/to/pipe]
copy

Share your terminal session in real-time
$ mkfifo [path/to/pipe]; script [[-f|--flush]] [path/to/pipe]
copy

SYNOPSIS

mkfifo [OPTION]... NAME...

PARAMETERS

-m, --mode=MODE
    Set the file mode (permissions) of the named pipe. MODE can be an octal number (e.g., 0644) or a symbolic mode string (e.g., u=rw,go=r), similar to chmod.

--help
    Display a help message and exit.

--version
    Output version information and exit.

DESCRIPTION

The mkfifo command creates a special file on the file system called a named pipe, also known as a FIFO (First-In, First-Out). Unlike regular files, a FIFO does not store data on disk; instead, it acts as a one-way communication channel, a conduit, between two or more processes. When data is written to a FIFO by one process, it can be read from the same FIFO by another process. This mechanism facilitates inter-process communication (IPC) in a way that regular files cannot, as it enforces sequential, ordered data flow.

Named pipes are particularly useful in shell scripting for orchestrating complex workflows where different parts of a script or different programs need to exchange data. For instance, one process can write its output to a FIFO, and another process can simultaneously read from it as input, allowing for a producer-consumer pattern without using temporary files or complex redirection. The FIFO appears as a file in the directory listing, but its behavior is distinctly that of a pipe. It persists on the file system until explicitly removed, typically with the rm command.

CAVEATS


mkfifo creates a file system entry, but it's not a regular file that stores data. It's a special file type.
Reading from or writing to a FIFO can cause the process to block until another process opens the other end of the pipe. This blocking behavior is fundamental to how FIFOs work for synchronization.
Permissions set for the FIFO are crucial, as they control which users and processes can read from or write to it.
Named pipes persist on the file system until they are explicitly removed using rm, unlike anonymous pipes which are ephemeral.

USAGE PATTERN

Named pipes are commonly used in a 'producer-consumer' pattern. One process 'produces' data by writing to the FIFO, and another process 'consumes' data by reading from it. For example, in one terminal: ls -l > myfifo. In another terminal: cat < myfifo. The output of ls -l will be streamed through 'myfifo' to cat. This allows for concurrent execution without needing to store intermediate results in a temporary file.

DIFFERENCE FROM ANONYMOUS PIPES

Anonymous pipes (created with the | operator in shells, e.g., command1 | command2) are unnamed and temporary; they exist only for the duration of the connected processes and cannot be accessed by unrelated processes. Named pipes, created by mkfifo, have a name in the file system, persist until deleted, and can be opened by any process with appropriate permissions, allowing communication between entirely unrelated processes or processes started at different times.

HISTORY

The concept of named pipes (FIFOs) originated in Unix-like operating systems to provide a file system-based mechanism for inter-process communication. The mkfifo command, which creates these named pipes, is standardized as part of the POSIX.1 specification. Its functionality is underpinned by the mknod system call, which is a more general interface for creating various special files. mkfifo was introduced to provide a more specific and user-friendly way to create only FIFOs, simplifying the process compared to directly using mknod with specific device types. Its inclusion in POSIX ensures its availability and consistent behavior across different Unix-like systems.

SEE ALSO

pipe(7): Overview of pipes and FIFOs., fifo(7): Manual page specifically for named pipes., mknod(1): General command to create special files, including FIFOs (mkfifo is a simpler interface for FIFOs)., mknod(2): System call to create special files., chmod(1): Change file permissions of a FIFO., rm(1): Remove a named pipe from the file system.

Copied to clipboard