Mac OS X Internals: A Systems Approach
9.10. Named Pipes (Fifos)
A named pipealso called a fifois 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 open the fifo file like a regular file. A fifo can be created using the mkfifo() system call or through the mkfifo command-line program. 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 turn is part of the on-disk file metadata. Thereafter, whenever the fifo file is being looked up (typically for opening), the corresponding vnode's file system operations table pointer is switched by HFS Plus to point to another table, some (but not all) of whose operations are that of the fifo file system (fifofs).
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. |
Категории