Master the Linux ‘mkfifo’ Command: A Comprehensive Guide

Peter Hou
2 min readMay 23, 2023

--

This guide offers a comprehensive look into the ‘mkfifo’ command in Linux, aiming to provide beginners with an understanding of its purpose, usage, and applications. It also covers various parameters, common use cases, and special techniques, as well as highlighting precautions to take while using this command.

Instructions

This guide aims to provide a detailed exploration of the Linux ‘mkfifo’ command for beginners. It will cover the command’s history, its applications, usage, parameters, common use cases, unusual techniques, and precautionary measures.

History

The ‘mkfifo’ command was introduced in Unix-like operating systems, including Linux, to manage Inter-Process Communication (IPC) using named pipes (FIFO).

When and why to use it

‘mkfifo’ is primarily used when two processes need to communicate with each other but do not have a parent-child relationship. A FIFO special file is an extension of pipes, which offers a pathway for data between two processes. The FIFO special file can be opened by multiple processes for reading and writing. It is especially useful in scenarios where data streaming is necessary.

How to use it

The ‘mkfifo’ command is straightforward to use. The simplest form of the command includes ‘mkfifo’ followed by the name of the FIFO file.

$ mkfifo my_fifo

The commonly used parameters

The ‘mkfifo’ command has a few useful parameters:

  • -m or --mode sets the file permission mode of the created FIFO file. It follows the same pattern as the chmod command.
$ mkfifo -m 666 my_fifo

Other supported parameters

  • -Z, -context Set the SELinux security context of each created directory to CTX
  • --help display this help and exit
  • --version output version information and exit

Most common use cases

One common use case of ‘mkfifo’ is to facilitate inter-process communication. For example, one process can write to the FIFO file while another process reads from it.

$ mkfifo pipe
$ echo "Hello World" > pipe &
$ cat < pipe
Hello World

The tricky skills

One tricky usage of ‘mkfifo’ is to use it for logging purposes. For instance, you can have a dedicated logging process that reads from a FIFO file and writes to a log file.

$ mkfifo log_pipe
$ cat log_pipe >> log.txt &
$ echo "Log this message" > log_pipe

What needs to be noted

It’s important to remember that FIFO files, like any other files, take up file system nodes (inodes). So, cleaning up unused FIFO files is essential to avoid running out of inodes.

Conclusion

The ‘mkfifo’ command is a powerful tool in Linux for creating named pipes, thus allowing processes to communicate more flexibly. Understanding and utilizing this command effectively can greatly aid in managing and controlling data flow between processes in Linux.

--

--

Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.