blog home

Named pipes

Posted by William in backend on January 24th, 2009

Pipes are very useful feature on Linux/Unix. It allow separate process to communicate easily.

A simple example:

grep xyz log | wc -l

first process finds the lines that has “xyz” in file “log, the output feeds to the second process which count the number of lines. So this simple pipe counts the number of lines in log that has xyz.

This kind of pipe is called “unnamed pipe”.

There are also “named” pipes, or FIFO. A named pipe is a file within the filesystem. A named pipe can be created by mkfifo (or mknod on older systems).

mkdir my_fifo

ls -l my_fifo

prw-r–r–  1   dexin users 0 2009-03-04 22:04 my_fifo

A simple use of named pipe: in two separate terminals,

ls -l > my_fifo

cat < pipe

you will see the output from the first command gets displayed on the second terminal. The order in which you run the command does not matter.

A more interesting example of using named pipe could be you have a log file that’s being updated periodically, and you want to parse the new lines in the log. You can have one process that tail -F on the log and redirect to a pipe, and another process takes the output from the pipe:

tail -F log > my_pipe

parser < my_pipe

Leave a Reply