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 shells use pipes extensively. Mac OS X provides the pipe() system call, which allocates and returns a pair of file descriptors: The first is the read end, and the second is the write end. The two descriptors can provide an I/O stream between two processes, thus serving as an IPC channel. However, pipes have several limitations, some of which may be rather severe for certain applications. Pipes are possible only between related processesthat is, those with a common ancestor. The kernel buffer corresponding to a pipe consumes kernel memory. Pipes support only untyped byte streams. Historically, pipes have allowed only unidirectional data flow. The Single UNIX Specification allows but does not require full-duplex pipes. Only writes below PIPE_BUF bytes in size are guaranteed to be atomic. PIPE_BUF is 512 bytes on Mac OS X. The fpathconf() system call can be used to retrieve the value of PIPE_BUF given a pipe descriptor. You can use pipes only for local (non-networked) communication. Bidirectional Pipes Not all of the shortcomings with pipes are universal. Certain operating systemsfor example, FreeBSD and Solarisimplement bidirectional pipes, wherein the pair of descriptors returned by the pipe system call is such that data written to one can be read on the other. Although the Mac OS X pipe implementation is based on FreeBSD's, Mac OS X 10.4 does not provide bidirectional pipes. Moreover, it is possible to send a pipe descriptor to another, unrelated process through file descriptor passing, which is supported on Mac OS X. We will see an example of descriptor passing in Section 9.11. |
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 KernelDescriptor Type | Notes |
---|
DTYPE_VNODE | File | DTYPE_SOCKET | Socket-based communication end point | DTYPE_PSXSHM | POSIX shared memory | DTYPE_PSXSEM | POSIX semaphore | DTYPE_KQUEUE | Kqueue | DTYPE_PIPE | Pipe | DTYPE_FSEVENTS | File system event notification descriptor |
|