Monday, March 30, 2009

Named Pipes with mkfifo [Unix]

Named pipes are useful for inter-process communication. Unlike anonymous pipes, any number of readers and writers can use a named pipe. They are very useful for letting other processes know that something has happened e.g. a file has been created etc.

Creating a named pipe
You can create a named pipe using the mkfifo command, which creates a special pipe file that remains in place until it is removed. Since it is a type of file, you can use the rm command to remove it when you are done.

sharfah@firefly:~> mkfifo mypipe
sharfah@firefly:~> ls
Writing to a named pipe
Since a named pipe is just a special type of file, you can write to it just as you would normally write to a file. However, if there are no processes reading from the pipe, the write call will block.

The following example script writes numbers into the pipe. If there are no readers, it will block on line 5.

while (true)
 echo Writer$$: $COUNT
 echo $COUNT > mypipe
 COUNT=`expr $COUNT + 1`
 sleep 1
Reading from a named pipe
Reading from a named pipe is the same as reading from a normal file. You can cat a named pipe, tail it or read it as follows:
while (true)
   read line < mypipe
   echo Reader$$: $line
Multiple readers
If you have multiple readers reading from the same pipe, only one of the readers will receive the output. This is illustrated with the following example, in which I have launched one writer and two readers:
Writer10500: 1 
Reader10501: 1 
Writer10500: 2 
Reader10501: 2 
Writer10500: 3 
Reader10501: 3 
Writer10500: 4 
Reader10502: 4 
Writer10500: 5 
Reader10502: 5 
Writer10500: 6 
Reader10501: 6 
Writer10500: 7 
Reader10502: 7 
Writer10500: 8 
Reader10502: 8 
Writer10500: 9 
Reader10501: 9 
Writer10500: 10 
Reader10502: 10 

1 comment:

  1. Anonymous8:50 PM

    Named pipes rock. We use them at work to copy large volumes between databases. Rather than exporting data to a flat file, then importing the data, we set up the export job to write to a named pipe while the import job reads from the same named pipe. This allows allows the import to proceed as the export is running. It's a big time saver.
    -- Dean


Note: Only a member of this blog may post a comment.