CSE Roadmap

Core tier · about 3 hours · 6 parts

Part 4 of 6 · 2 min read

Capacity

In plain words: The kernel buffer of a pipe has a fixed size. A writer fills it and, when it is full, the next write blocks (the process goes to the waiting state of page 2) until a reader removes data. This makes a fast producer and a slow consumer work together without losing data. The same happens in the other direction: a reader of an empty pipe waits.

The size can be found with a test. The program below puts the write end in non-blocking mode, so that a full pipe returns an error instead of waiting, and writes 1,024 bytes at a time until that happens:

pipe filled after 65536 bytes

The capacity on the test machine is 65,536 bytes, which is 64 kilobytes, or 16 memory pages of 4,096 bytes. This is the usual default on Linux. A program may ask for a larger buffer, up to a system limit that was 1,048,576 bytes here.

A third case is a pipe with no reader. If a process writes to a pipe whose read ends are all closed, nobody could ever receive the data, so the kernel sends the writer the signal SIGPIPE (page 6), which by default ends it. This is why yes | head -1 stops: head ends after one line, the next write by yes meets a broken pipe, and yes is killed by SIGPIPE. If the program ignores SIGPIPE, write returns -1 with the error EPIPE. A test that ignores the signal printed:

write returned -1, errno: Broken pipe

A pipe has two more limits worth knowing. It is one-way, so two-way talk needs two pipes (the ping-pong in page 5 used two). And an ordinary pipe can only connect processes that share ancestors, because the descriptors are passed by fork.

Named pipe

A named pipe, or FIFO, is a pipe that has a name in the file system, created with mkfifo, so that processes that are not related by fork can open it by name and use it like an ordinary pipe.

Example: one program opens /tmp/jobs for writing and an unrelated program opens it for reading; the data passes through the kernel and is never stored on disk.

Check yourself: 1. What happens when a writer fills the pipe buffer? 2. What happens when a process writes to a pipe nobody reads? 3. When would you choose a named pipe over an ordinary one?