Introduction to Named Pipes

Introduction to Named Pipes

One of the most fundamental features that makes Linux useful is "Pipe". A Pipe allows two separate processes to work together even if they haven't been explicitly coded to do so. 
An example of the pipe is the following command:

When this line is executed, both the commands work together. While ls produces a list of files in the current directory, grep prints only those lines which have the letter 'x' in them.

The above command is an example of an "Unnamed Pipe". This kind of pipe exists only inside the kernel and it cannot be accessed by processes that created it.

The other kind of pipe is called "Named Pipe". It is also called a FIFO pipe i.e. First In Firest Out pipe. This name is given because of its property that the bytes come out in the same order they went in. The name of a named pipe is actually a file within the file system. Pipes are shown by ls like any other file but with a couple of changes:

The p in the leftmost column tells that fifo1 is a pipe. The rest of the permission bits work in the same way they work for a normal file in the file system. The | at the end of the filename is another indicator that the file is a named pipe.

To create a named pipe in a Linux system, the mkfifo utility is used. The mkfifo tool takes one or more filenames as arguments and creates named pipes with those names.

To create a pipe using the file pipe, the mkdir command is used in the following way:

To see how named pipes work, see the following two commands:

When you execute this command, it appears to hang since the pipe has not yet been connected on the other end.
Once you enter this command, the whole pipe is executed:

Here, the order in which the commands are executed doesn't matter.