Pipe – Advantages of Using Named Pipe Over Unnamed Pipe

pipe

I was reviewing a set of interview questions that are asked from a unix admin; I found a topic called "named pipe".

I googled the topic; to some extent I have been able to understand it :- named pipes || FIFO

But still I feel that I am lacking the knowledge of when to use this particular type of pipe. Are there any special situations where unnamed pipes would fail to work ?

Best Answer

Named pipes (fifo) have four three advantages I can think of:

  • you don't have to start the reading/writing processes at the same time
  • you can have multiple readers/writers which do not need common ancestry
  • as a file you can control ownership and permissions
  • they are bi-directional, unnamed pipes may be unidirectional *

    *) Think of a standard shell | pipeline which is unidirectional, several shells (ksh, zsh, and bash) also offer coprocesses which allow bi-directional communication. POSIX treats pipes as half-duplex (i.e. each side can only read or write), the pipe() system call returns two file handles and you may be required to treat one as read-only and the other as write-only. Some (BSD) systems support read and write simultaneously (not forbidden by POSIX), on others you would need two pipes, one for each direction. Check your pipe(), popen() and possibly popen2() man pages. The undirectionality may not be dependent on whether the pipe is named or not, though on Linux 2.6 it is dependent.

(Updated, thanks to feedback from Stephane Chazelas)

So one immediately obvious task you cannot achieve with an unnamed pipe is a conventional client/server application.

The last (stricken) point above about unidirectional pipes is relevant on Linux, POSIX (see popen()) says that a pipe need only be readable or writeable, on Linux they are unidirectional. See Understanding The Linux Kernel (3rd Ed. O'Reilly) for Linux-specific details (p787). Other OS's offer bidirectional (unnamed) pipes.

As an example, Nagios uses a fifo for its command file. Various external processes (CGI scripts, external checks, NRPE etc) write commands/updates to this fifo and these are processed by the persistent Nagios process.

Named pipes have features not unlike TCP connections, but there are important differences. Because a fifo has a persistent filesystem name you can write to it even when there is no reader, admittedly the writes will block (without async or non-blocking I/O), though you won't loose data if the receiver isn't started (or is being restarted).

For reference, see also Unix domain sockets, and the answer to this Stackoverflow question which summarises the main IPC methods, and this one which talks about popen()