What are Named Pipes in Linux?

Learn via video courses
Topics Covered

To understand named pipes in Linux, we first need to understand what pipes are in Linux. Pipes or anonymous pipes are a feature which allows separate processes to communicate together. The output produced by one command can be used as the output for another Linux command with the help of pipes.

For Example: Running ls -la | grep "something" shows us only the part from the ls command that matches our search string in the grep command. Thus, the input of ls -la was used as an input by grep with the help of pipes.

usage of pipes in linux

Linux named pipes, also called FIFO (First In First Out) are special kind of files which are used to establish two-way communications between two unrelated programs. A named pipe is called so because it is a file in the filesystem, which can be listed with ls unlike the normal pipes in Linux exist only inside the kernel.

For Example: If we create a named pipe called pipe1, it can be listed as a file using ls. But this file will always appear empty and cannot be used to save data.

displaying a named pipe

If you notice, the pipe's permission matrix has a p. This p denotes that it is indeed a named pipe. The file size of the named pipe's file is also 0 and stays 0, even if we pass data into the named pipe.

Why Use Named Pipes?

While Linux named pipes are used very infrequently, they provide an interesting way for two completely unrelated processes to communicate together.

The contents of the named pipe stay in memory and are not drained until another connection is established with the named pipe in Linux. We can also write to the same pipe multiple times before it is drained using another process or command.

Also, as Linux named pipes accept data from one end and the other consumes the data, we can set up two processes, one which pushes the data to the named pipe and another which consumes the data continuously. We do not need to develop a complex algorithm or mechanism for this.

Named pipes also consume negligible resources while running. Writing to a named pipe and reading from it is very light on memory usage. We can run multiple push and consume operations with named pipes simultaneously to consume and process data.

An example of when to use named pipes is if a program services requests of some sort, like accessing a database or printing a file. In such a situation, we can open a named pipe for reading the data. To consume requests, we can start a process which acts as a server and have another process which writes its requests in the named pipe. These requests are consumed by the server from the named pipe.

How to Create and Set Up a Named Pipe in Linux?

As we discussed earlier, named pipes in Linux are called FIFO which stands for First In, First Out. This means that anything which is pushed first into a named pipe is consumed or read first.

And as named pipes are FIFO, we use a command-line tool called mkfifo to create Linux named pipes.

To create a named pipe, run the following command:

This creates a named pipe with the name pipe1. This pipe will appear as a normal file in the filesystem but with some special properties like its identifier starts with a p and its size always remains at 0 bytes, even if we write any data into it.

Example of the working of a named pipe:

creating and working of a named pipe

In the image above, we first create a pipe with the name pipe1. When this pipe is created, we can list that it exists with the help of the ls command. We can see a named pipe has a distinct p in its permissions and its size is 0 when we create it.

Now, the echo command writes a sentence into the Linux named pipe. The & in the command sends the process to the background. This process continues to runs in the background since there is no one to consume data from the pipe.

When we run the cat command to read data from the pipe, the first echo process exits since data was read from the pipe. We can again list the pipe and see that even though data was written into it, its size still says 0.

As named pipes in Linux show up as normal files, we can delete them using the rm command. To delete a named pipe, we just need to run rm <name-of-the-pipe.

Two-way Communication Using Named Pipes

We can write programs that utilize the mknod() function in the GNU C library to create a named pipe and demonstrate the two-way communication between two processes.

In the above code, we are implementing the server side of the code. The code creates a Linux named pipe by the name FIFO1 and an infinite while loop keeps the node open till the user exits.

We're also printing the string as well as the length of the string that we receive from the client using the printf() function.

In the above code, we're building the client-side logic of the named pipe. This code is used to send data into the named pipe in Linux.

Here we enter the data we need to send into the pipe. This data is consumed by the server which is shown as the output on the server side.

two-way communication channel

When to Use Named Pipes and Anonymous Pipes

The decision to use named and anonymous pipes depends entirely on the requirements of the current situation. The situations can have characteristics like two-way communication, creating a filter etc.

For example, if we want to filter multiple commands together in succession, anonymous or normal pipes are the best and easiest way to do this.

But, say we encounter a client-server-like situation where one user needs to create data and the other needs to process it, then Linux named pipes are the perfect solution for it.

Learn More

Conclusion

  • Named piped in Linux, also called FIFO (First In, First Out) are a special type of files which are used to facilitate two-way communication.

  • In Linux named pipes, one instance of the pipe is used to push data into the pipe while the other end is used to read or consume the data from it.

  • We use the command mkfifo <pipe-name> to create a named pipe. Since named pipes appear as normal files in the filesystem, we use rm to delete the named pipes.