Home > File & Directory Management > mkfifo

mkfifo: Create a Named Pipe

`mkfifo` is a command used to create named pipes (FIFOs, First-In, First-Out). A named pipe is a special file that exists in the file system and acts as a communication channel for data exchange between different processes. Unlike regular pipes, named pipes can be accessed via a file path, making it easier for independent processes to communicate.

Overview

`mkfifo` creates a named pipe, a special file in the file system. This pipe serves as a communication channel allowing two or more independent processes to send and receive data. When one process writes data to the pipe, another process can read from it, with data being processed in a First-In, First-Out (FIFO) manner.

Key Features

  • Exists in the file system and is accessible via a path.
  • Provides a communication channel between independent processes.
  • Data is processed in a First-In, First-Out (FIFO) manner.
  • Useful for constructing complex pipelines in shell scripts.

Key Options

The `mkfifo` command primarily takes a file path as an argument and supports a few basic options.

Creation Options

Generated command:

Try combining the commands.

Description:

`mkfifo` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Demonstrates how to create a named pipe using `mkfifo` and use it for inter-process communication.

Create a Basic Named Pipe

mkfifo my_pipe

Creates a pipe named `my_pipe` in the current directory.

Create a Pipe with Specific Permissions

mkfifo -m 600 my_private_pipe

Creates `my_private_pipe` where only the owner can read and write.

Data Transfer Using a Named Pipe

mkfifo data_channel
echo "Hello from Terminal 1!" > data_channel &
cat < data_channel

An example of writing data in one terminal and reading it in another. **Terminal 1:** `mkfifo data_channel` `echo "Hello from Terminal 1!" > data_channel` **Terminal 2:** `cat < data_channel`

Using Pipes with Background Processes

mkfifo background_output
(ls -l; sleep 2; echo "Done listing") > background_output &
cat < background_output

Redirects the output of a background process to another command via a pipe.

Tips & Notes

Points to consider and useful tips when using named pipes.

Deleting Pipes

Named pipes can be deleted using the `rm` command, just like regular files. It's good practice to clean up unnecessary pipes after use.

  • `rm my_pipe`

Blocking Behavior

Named pipes typically operate in a blocking mode. This means that an attempt to write (or read) on one end will wait until a read (or write) operation is initiated on the other end. To avoid this behavior, you can run operations in the background (e.g., `cat < my_pipe &`) or open the pipe in non-blocking mode using the `O_NONBLOCK` flag.

Using Temporary File Systems

For temporary pipes, it's recommended to use the `/tmp` directory or the `mktemp` command to create pipes with unique names. This helps prevent name collisions and ensures that pipes can be automatically cleaned up upon system reboot.


Same category commands