| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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).
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 */
umask(0);
if (mkfifo("test_fifo", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP))
{
perror("mkfifo");
exit(1);
}
|
If you don't have mkfifo(), you'll have to use mknod():
/* set the umask explicitly, you don't know where it's been */
umask(0);
if (mknod("test_fifo",
S_IFIFO | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
0))
{
perror("mknod");
exit(1);
}
|
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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:
O_RDWR), then the open
will not block.
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.
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.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.)
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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().
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] |
This document was generated on September, 10 2007 using texi2html 1.77.