Unix Named Pipes - An example of Interprocess communication
So, I have been selected as one of the teaching assistants for Operating systems course
and this time we had to give some examples of Named Pipes, demonstrating one form of Inter Process Communication aka IPC to the students. I had never used them before, but its fun knowing and working with them.
I hope all of us have used pipes on Linux systems. Here is a simple pipe example I often use to check if a process is running or not.
[shredder12]$ ps -e | grep firefox
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.
[shredder12]$ mkfifo mypipe
This command creates a pipe. Now, if you do ls -l, you will see something like this
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.
[shredder12]$ rm mypipe
Lets begin with the usage part. Try running this command in a terminal
[shredder12]$ 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
[shredder12]$ 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.
If you have understood the basic working of pipes then you might wonder if we can create an infinite loop using two of them. In a 1997 article of LJ, the writer mentioned this little trick. Using two commands, he hooked up the ouptut of one pipe to other's input and vice versa, thus generating a loop.
However, I couldn't make it work on my system. After running the command, I didn't see any cat processes consuming huge resources. May be I did something wrong or may be the article is too old to work on today's systems, probably some defensive technique in later kernels to escape such accidents.
Anyway, I hope this small introduction to named pipes has given you a new insight into the working of Unix and Linux sytems.





























3 Comments
ping google.com -c1 | grep time= | cut -d= -f4
This would give me something like 93ms which if you are making a test page in php i would be a cool to have this sort of info.
and the one i like more is in this case:
Lets say you have several huge files .log in which a repeated information about bubblegum x appears several times in some of this files. You would like to grab ONLY this information and put it in a file to later read it or process it. Just do
cat *.log | grep "BubbleGum X" > BurnMeAfterReading.txt
This would read all log files, send through the pipe to grep who would search for the bubblegum x string, every time grep finds it, it will send the line to the file BurnMeAfterReading.txt which is the one you would process of something.
But the amount of things you can do with pipes depends on the amount of commands you know, how to use them, the attributes or parameters of this commands and most of all, YOUR IMAGINATION!!.
Post new comment