Wednesday, July 13, 2005

Named pipes in Linux

In computing, a named pipe (also FIFO for its behaviour) is an extension to the classical pipe concept on UNIX and UNIX-like systems, and is one of the methods of interprocess communication. The concept is also found in Windows, albeit the semantics are largely different.
A good article is available at http://www2.linuxjournal.com/article/2156
Snippet from the article:

We often used "un-named" pipes on the command prompt. For e.g. ls grep x
The other sort of pipe is a "named'' pipe, which is sometimes called a FIFO. FIFO stands for "First In, First Out'' and refers to the property that the order of bytes going in is the same coming out. The "name'' of a named pipe is actually a file name within the file system. Pipes are shown by ls as any other file with a couple of differences:
% ls -l fifo1
prw-r--r-- 1 andy users 0 Jan 22 23:11 fifo1

The p in the leftmost column indicates that fifo1 is a pipe. The rest of the permission bits control who can read or write to the pipe just like a regular file. On systems with a modern ls, the character at the end of the file name is another clue, and on Linux systems with the color option enabled, fifo is printed in red by default.

To make a pipe named pipe1

% mkfifo pipe
Then open 2 console windows:
% ls -l > pipe1 { in one window }
% cat <>

Voila! The output of the command run on the first console shows up on the second console. Note that the order in which you run the commands doesn't matter.

No comments:

Post a Comment