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

September 8th, 2010 10:05 am
Nice. Did not know about named pipes. I use pipes A LOT. I mean without them i can not do about 90% of the stuff i like to do. For example. I would like to show the latency between 2 points, so i would do:

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!!.
LazyO> (not verified)
September 8th, 2010 12:45 pm
No need to use pipes to grep in this case.. just do grep 'BubbleGum X' *.log > BurnMeAfterReading.txt
LazyO> (not verified)
September 8th, 2010 12:50 pm
Not sure why the instructions in the linked LJ article didn't work for you; they worked quite well for me. An interesting hands on example, thanks for the article :)

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <p> <br>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.