9.9. Pipes
Ever since pipes were introduced in Third Edition UNIX (1973), they have been an integral feature of Unix systems. The Unix program-stream redirection facility uses pipes. Therefore, Unix
Pipes are also called unnamed pipes , since there also exist named pipes (see Section 9.10). The kernel's internal file descriptor type for a pipe descriptor is DTYPE_PIPE . Descriptors for other IPC mechanisms, such as sockets, POSIX semaphores, and POSIX shared memory, have their own descriptor types. Table 98 shows the various descriptor types used in the kernel. Table 98. File Descriptor Types Used in the Kernel
|
9.10. Named Pipes (Fifos)
A named pipealso called a
fifo
is an abstraction that provides the functionality of an unnamed pipe but uses the file system namespace to represent the pipe, allowing readers and writers to
If a fifo is opened for reading, the open() call will block if there are no writersthat is, if somebody else does not have the fifo open for writing. Conversely, if a fifo is opened for writing, the open() will block if there are no readers. It is possible to open a fifo in nonblocking mode by specifying the O_NONBLOCK flag in the open() call. A nonblocking open for reading will return immediately, with a valid file descriptor, even if there are no writers. A nonblocking open for writing, however, will return immediately with an ENXIO error if there are no readers. The Mac OS X implementation of fifos internally uses local (Unix Domain) stream socketsthat is, sockets of type SOCK_STREAM in the AF_LOCAL domain.
Although a fifo has physical existence on a file system, it must be different from a regular file for the kernel to treat it as a communication channel with properties that regular files do not have. This is indeed the case: Fifos are conceptually similar to block or character special files in how the file system treats them. Consider a fifo on an HFS Plus volume. The
mkfifo()
system call simply calls the create operation exported by the HFS Plus file system, additionally setting the type of the vnode as
VFIFO
. The file type is stored as part of the BSD information structure (
struct HFSPlusBSDInfo
), which in
Block and character devices on HFS Plus are handled similarly, except the special file system (specfs) is used instead of fifofs. We will see more of fifofs and specfs in Chapter 11. Chapter 12 is entirely dedicated to HFS Plus. This way, opening a fifo file results in fifo_open() [ bsd/miscfs/fifofs/fifo_vnops.c ] being called. On the first open of a fifo, fifo_open() creates two AF_LOCAL stream sockets: one for reading and the other for writing. Similarly, several other system calls, in particular read() and write() , eventually resolve to fifofs functions. |