What is named pipe in Unix?

What is Pipe?

[Sachi]$ ps -ef | grep postgres

The symbol "|" used here is called pipe(its mostly above the "Enter" key). Just like conventional pipes, UNIX pipes are used to transfer data between processes, aka Inter Process communication. They transfer one program's/process's output to another process as input, ps and grep in our case. The output of 'ps -e' is transfered through pipe as an input to grep. They are called "pipes" because of their FIFO(first in first out) nature. They can be seen as files but with the FIFO property. We will see the reason in a little while.

Difference between Pipes and Named Pipes

In above example, as soon as the message is passed and grep executes, the pipe will no longer exist, i.e. it is a non-persistent thing, gets removed as soon as the task is over. Named pipes, however are persistent, have a name and they exist even after the processes are terminated. I think I might have lost you people here, so lets see a simple named pipe example.

[[Sachi]$ ]$ mkfifo mypipe

This command creates a pipe. Now, if you do ls -l, you will see something like this

prw-r--r-- 1 sachi sachi 0 Jan 31 15:27 mypipe ======è see the first letter is p meaning this is a named pipe.

So, as you can see, it has everything similar to a file, but the FIFO property. It can be deleted just like a simple file, using rm.

[[Sachi]$ ]$ rm mypipe

Lets begin with the usage part. Try running this command in a terminal

[[Sachi]$ ]$ps -e > mypipe

This command should have returned to a prompt(thats what you would have expected) but apparently it seems to get hanged in the middle of nowhere, why? The answer is these pipes work exactly like a conventional water-pipe does, i.e. it needs both the end to be open, one serving as an input and the other as output. So, the reason why the above command hasn't returned a prompt is because only one end of the pipe is opened, for the input. Until there is a process at the other end of the pipe to receive the data, the transfer or the communication isn't complete and so the command doesn't terminate.

Now, with the above command running in a terminal, try to run the following in another

[[Sachi]$ ]$ cat mypipe

Now you will actually see the output of 'ps -e'. And note that this means the communication is done and as a result, both the commands will terminate and return to prompt.