mkfifo Command in Linux



The mkfifo command in Linux is a powerful utility used to create named pipes, also known as FIFOs (First In, First Out). Named pipes are a type of inter-process communication (IPC) mechanism that allows data to be exchanged between processes in a sequential manner.

Unlike regular pipes, which are unnamed and exist only as long as the processes are running, named pipes have a name in the file system and can be accessed by multiple processes at different times.

Table of Contents

Here is a comprehensive guide to the options available with the mkfifo command −

Understanding mkfifo Command

The mkfifo command in Linux is used to create named pipes, also known as FIFOs (First In, First Out). Named pipes are a type of inter-process communication (IPC) mechanism that allows data to be exchanged between processes in a FIFO manner.

The mkfifo command is used to create a named pipe with a specified name and permissions. The basic syntax of the mkfifo command is as follows −

mkfifo [options] name

Here, name refers to the name of the named pipe to be created. The options parameter allows users to specify various settings and configurations for the named pipe.

Examples of mkfifo Command in Linux

The mkfifo command is a powerful tool for creating named pipes in Linux. Named pipes provide a simple and efficient way to facilitate inter-process communication and can be used in a variety of applications, from logging data to creating data processing pipelines. By understanding the various options and features available with mkfifo, you can effectively use named pipes to enhance your Linux workflows.

Here are some examples of how to use the mkfifo command to create and use named pipes in a Linux environment −

Creating a Named Pipe

To create a named pipe, you can use the following command −

mkfifo mypipe
mkfifo Command in Linux1

This command creates a named pipe called mypipe.

Creating a Named Pipe with Specific Permissions

You can specify the permissions for the named pipe using the -m option. For example, to create a named pipe with read and write permissions for the owner and read permissions for others −

mkfifo -m 644 mypipee
mkfifo Command in Linux2

Creating Multiple Named Pipes

You can create multiple named pipes in a single command by specifying multiple names −

mkfifo pipe1 pipe2 pipe3
mkfifo Command in Linux3

Writing to a Named Pipe

To write data to a named pipe, you can use the echo command or any other command that writes to standard output. For example −

echo "Hello, World!" > mypipe
mkfifo Command in Linux4

Reading from a Named Pipe

To read data from a named pipe, you can use the cat command or any other command that reads from standard input. For example −

cat < mypipe
mkfifo Command in Linux5

Using Named Pipes for Inter-Process Communication

Named pipes can be used for inter-process communication between two or more processes. For example, you can have one process write data to a named pipe and another process read data from the same named pipe −

In one terminal, write data to the named pipe −

echo "Hello from process 1" > mypipe
mkfifo Command in Linux6

In another terminal, read data from the named pipe −

cat < mypipe
mkfifo Command in Linux7

Using Named Pipes with Shell Scripts

Named pipes can be used in shell scripts to facilitate communication between different parts of the script. For example −

!/bin/bash
# Create a named pipe
mkfifo mypipe
mkfifo Command in Linux8

Write data to the named pipe in the background −

echo "Data from script" > mypipe &
mkfifo Command in Linux9

Read data from the named pipe −

cat < mypipe
mkfifo Command in Linux10

Remove the named pipe −

rm mypipe
mkfifo Command in Linux11

Using Named Pipes with Network Applications

Named pipes can be used in network applications to facilitate communication between different components. For example, you can use a named pipe to pass data between a server and a client −

mkfifo mypipe
mkfifo Command in Linux12

Start a server that writes data to the named pipe

nc -l 12345 > mypipe &
mkfifo Command in Linux13

Start a client that reads data from the named pipe

nc localhost 12345 < mypipe
mkfifo Command in Linux14

Logging Data

Named pipes can be used to log data from different processes to a single file. For example, you can have multiple processes write log data to a named pipe, and a single process read data from the named pipe and write it to a log file −

Create a named pipe

mkfifo logpipe
mkfifo Command in Linux15

Start a process that reads data from the named pipe and writes it to a log file

cat < logpipe > logfile &

Start multiple processes that write log data to the named pipe

echo "Log entry 1" > logpipe
echo "Log entry 2" > logpipe
mkfifo Command in Linux16

Data Processing Pipelines

Named pipes can be used to create data processing pipelines where data is passed between different stages of processing. For example −

Create a named pipe

mkfifo datapipeline
mkfifo Command in Linux17

Start a process that writes data to the named pipe

echo "Raw data" > datapipeline &
mkfifo Command in Linux18

Start a process that reads data from the named pipe, processes it, and writes it to another named pipe

cat < datapipeline | tr 'a-z' 'A-Z' > processeddata &
mkfifo Command in Linux19

Start a process that reads processed data from the second named pipe

cat < processeddata
mkfifo Command in Linux20

Creating a Named Pipe

In this example, the mkfifo command is used to create a named pipe called mypipe −

mkfifo mypipee1
mkfifo Command in Linux21

The named pipe is created with the default file permissions.

Creating a Named Pipe with Custom Permissions

The -m 0666 option sets the file permissions to 0666, which allows read and write access for the owner, group, and others −

mkfifo -m 0666 mypipee
mkfifo Command in Linux22

In this example, the mkfifo command is used to create a named pipe called mypipe with read and write permissions for all users.

Using a Named Pipe for Inter-Process Communication

In this example, two terminals are used to demonstrate inter-process communication using a named pipe. In Terminal 1, the cat command is used to write data to the named pipe mypipe

cat < mypipe
mkfifo Command in Linux23

In Terminal 2, the cat command is used to read data from the named pipe mypipe. Any data entered in Terminal 1 will be immediately displayed in Terminal 2.

Conclusion

The mkfifo command is a powerful tool for creating named pipes in Linux. Named pipes provide a simple and effective mechanism for inter-process communication, allowing data to be exchanged between processes in a sequential manner. By understanding the various options and parameters available with the mkfifo command, users can create and use named pipes for a wide range of applications.

Whether you are logging data, streaming information, synchronizing processes, or facilitating communication in scripts, the mkfifo command provides the flexibility and functionality needed to get the job done.

Advertisements