mkfifo

file managementLinux/Unix
The mkfifo command is one of the most frequently used commands in Linux/Unix-like operating systems. mkfifo Make FIFOs (named pipes)

Quick Reference

Command Name:

mkfifo

Category:

file management

Platform:

Linux/Unix

Basic Usage:

mkfifo [options] [arguments]

Common Use Cases

    Syntax

    mkfifo [OPTION]... NAME...

    Options

    Option Description
    -m, --mode=MODE Set file permission bits to MODE, not a=rw - umask
    -Z Set SELinux security context of each created FIFO to default type
    --context[=CTX] Like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX
    -z Set SELinux security context of each created FIFO to default type
    --help Display help information and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the mkfifo command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    Basic Examples:

    mkfifo mypipe
    Create a named pipe called mypipe in the current directory.
    mkfifo /tmp/pipe1
    Create a named pipe called pipe1 in the /tmp directory.

    Advanced Examples:

    mkfifo -m 644 secure_pipe
    Create a named pipe with specific permissions (read/write for owner, read-only for others).
    mkfifo pipe1 pipe2 pipe3 Create multiple named pipes at once. mkfifo --verbose data_pipe Create a named pipe with verbose output. mkfifo -Z pipe_selinux Create a named pipe with default SELinux context. mkfifo -Z system_u:object_r:tmp_t:s0 /tmp/selinux_pipe Create a named pipe with specific SELinux context. mkfifo -m a=rw public_pipe Create a named pipe with read/write permissions for all users. mkfifo "$(mktemp -u pipe_XXXXXX)" Create a named pipe with a unique temporary name. if ! mkfifo pipe_check 2>/dev/null; then echo "Failed to create pipe"; fi Check if pipe creation was successful.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The 'mkfifo' command is a utility for creating special FIFO (First-In-First-Out) files, also known as named pipes. Unlike regular files, FIFOs are a form of IPC (Inter-Process Communication) mechanism that allow separate processes to communicate with each other without using network connections or other more complex methods. Key features of the mkfifo command: 1. Named Pipe Creation: mkfifo creates a special file in the filesystem that behaves like a pipe, allowing data written to the file by one process to be read by another process. 2. Persistent IPC: Unlike anonymous pipes created with the pipe() system call, named pipes persist in the filesystem until explicitly deleted, making them suitable for communication between unrelated processes. 3. Permission Control: The command allows setting specific permissions on the named pipe, controlling which users can read from or write to it. 4. SELinux Integration: On systems with SELinux enabled, mkfifo supports setting appropriate security contexts for the created pipes. 5. Multiple Creation: The command can create multiple named pipes in a single invocation, streamlining shell scripts that need several pipes. 6. First-In-First-Out Behavior: Data written to a named pipe is read in the same order it was written, ensuring sequential processing. 7. Blocking Operations: By default, operations on named pipes block until a complementary operation occurs (e.g., a read blocks until data is written), facilitating synchronization between processes. Named pipes created with mkfifo are particularly useful in shell scripts and system programming for scenarios such as: - Creating a communication channel between two unrelated processes - Setting up a producer-consumer relationship between programs - Implementing simple client-server architectures - Redirecting output from one command to multiple consumers - Creating logging pipelines where multiple processes can write to a common log collector - Building data processing pipelines in shell scripts - Implementing simple one-way IPC without using more complex mechanisms like sockets When a process writes data to a named pipe, the data is not stored in the file itself but is held in memory until another process reads it. If no process has the pipe open for reading, a writing process will typically block until a reader appears, unless the pipe is opened with non-blocking flags. Unlike regular files, the size of a named pipe in a directory listing (ls -l) does not reflect the amount of data currently in the pipe, but rather the size of the pipe's buffer in the kernel, which is typically fixed. Named pipes provide a simple and effective way to implement IPC in Unix-like systems, offering advantages in terms of simplicity and persistence compared to other IPC mechanisms like message queues or shared memory.

    Related Commands

    These commands are frequently used alongside mkfifo or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the mkfifo command works in different scenarios.

    $ mkfifo
    View All Commands