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 bits for the FIFO to MODE (as in chmod), rather than the default of a=rw - umask.

--help
    Display help message and exit.

--version
    Output version information and exit.

NAME
    The name(s) of the FIFO(s) to be created.

DESCRIPTION

The mkfifo command creates FIFO (named pipe) special files. A FIFO is a type of file that allows processes to communicate using standard file I/O operations. One process can write data to the FIFO, and another process can read that data. Unlike regular pipes, FIFOs have a name in the file system and can be accessed by multiple, unrelated processes.

mkfifo is useful for creating communication channels between processes that don't have a parent-child relationship. It offers a persistent communication mechanism beyond the lifespan of the processes that create and use it. The command ensures proper permissions can be set during creation using the mode argument. Errors can occur if the FIFO already exists or if the user lacks the necessary permissions to create the file in the specified directory.

CAVEATS

If the specified FIFO already exists, mkfifo will return an error. Standard file permissions apply, affecting which users can read from and write to the FIFO.

Data written to a FIFO is discarded if no process is reading from it.

USAGE EXAMPLES

Creating a FIFO with default permissions:
mkfifo mypipe

Creating a FIFO with specific permissions:
mkfifo -m 660 myotherpipe (grants read and write permissions to the owner and group only)

HISTORY

The mkfifo command has been a standard part of Unix-like operating systems for many years. It's part of the core utilities and is specified by POSIX. The usage and functionality have remained largely consistent throughout its history, providing a reliable mechanism for inter-process communication.

SEE ALSO

pipe(7), mknod(1)

Copied to clipboard