File Handling in Linux 4

2.10 What can I do with named pipes (FIFOs)?

2.10.1 What is a named pipe?

A named pipe is a special file that is used to transfer data between unrelated processes. One (or more) processes write to it, while another process reads from it. Named pipes are visible in the file system and may be viewed with `ls' like any other file. (Named pipes are also called fifos; this term stands for `First In, First Out'.)
Named pipes may be used to pass data between unrelated processes, while normal (unnamed) pipes can only connect parent/child processes (unless you try very hard).
Named pipes are strictly unidirectional, even on systems where anonymous pipes are bidirectional (full-duplex).

2.10.2 How do I create a named pipe?

To create a named pipe interactively, you'll use either mknod or mkfifo. On some systems, mknod will be found in /etc. In other words, it might not be on your path. See your man pages for details.
To make a named pipe within a C program use mkfifo():
/* set the umask explicitly, you don't know where it's been */
if (mkfifo("test_fifo", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP))
If you don't have mkfifo(), you'll have to use mknod():
/* set the umask explicitly, you don't know where it's been */
if (mknod("test_fifo",
            S_IFIFO | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,

2.10.3 How do I use a named pipe?

To use the pipe, you open it like a normal file, and use read() and write() just as though it was a plain pipe.
However, the open() of the pipe may block. The following rules apply:
  • If you open for both reading and writing (O_RDWR), then the open will not block.
  • If you open for reading (O_RDONLY), the open will block until another process opens the FIFO for writing, unless O_NONBLOCK is specified, in which case the open succeeds.
  • If you open for writing O_WRONLY, the open will block until another process opens the FIFO for reading, unless O_NONBLOCK is specified, in which case the open fails.
When reading and writing the FIFO, the same considerations apply as for regular pipes and sockets, i.e. read() will return EOF when all writers have closed, and write() will raise SIGPIPE when there are no readers. If SIGPIPE is blocked or ignored, the call fails with EPIPE.

2.10.4 Can I use a named pipe across NFS?

No, you can't. There is no facility in the NFS protocol to do this. (You may be able to use a named pipe on an NFS-mounted filesystem to communicate between processes on the same client, though.)

2.10.5 Can multiple processes write to the pipe simultaneously?

If each piece of data written to the pipe is less than PIPE_BUF in size, then they will not be interleaved. However, the boundaries of writes are not preserved; when you read from the pipe, the read call will return as much data as possible, even if it originated from multiple writes.
The value of PIPE_BUF is guaranteed (by Posix) to be at least 512. It may or may not be defined in `<limits.h>', but it can be queried for individual pipes using pathconf() or fpathconf().

2.10.6 Using named pipes in applications

How can I implement two way communication between one server and several clients?
It is possible that more than one client is communicating with your server at once. As long as each command they send to the server is smaller than PIPE_BUF (see above), they can all use the same named pipe to send data to the server. All clients can easily know the name of the server's incoming fifo.
However, the server can not use a single pipe to communicate with the clients. If more than one client is reading the same pipe, there is no way to ensure that the appropriate client receives a given response.
A solution is to have the client create its own incoming pipe before sending data to the server, or to have the server create its outgoing pipes after receiving data from the client.
Using the client's process ID in the pipe's name is a common way to identify them. Using fifos named in this manner, each time the client sends a command to the server, it can include its PID as part of the command. Any returned data can be sent through the appropriately named pipe.

To Look for similar posts on File handling in Linux explore the following links from the same blog as well.


Post a Comment