Frequently Asked Question

How does the kernel actually implement a pipe?

A pipe is a circular buffer inside the kernel, a fixed-size region of memory (16 pages, 64 KB, on Linux by default) with separate read and write positions. When the shell calls pipe(2) the kernel allocates this buffer and hands back two file descriptors: one wired to the write end, one to the read end. Writes append bytes; reads consume them. The same read and write syscalls used for files work unchanged, which is why so many programs work in pipelines without ever being designed for them.

When the buffer is full, a write blocks until a reader makes space. When the buffer is empty, a read blocks until a writer supplies data. When the writer closes its end, subsequent reads return EOF; when the reader closes its end, subsequent writes are killed with SIGPIPE. This flow control is what lets you pipe a 50 GB log file through grep | sort | uniq: the producer is automatically throttled to the consumer's speed, and only 64 KB ever lives in memory at once.

Further reading and video