Inter-Process Communication

the named pipe (FIFO)

/ FY-foh /

An anonymous pipe is a private hose between relatives — you can only hand it to your own children. But what if two completely unrelated programs, started at different times by different people, need to pass data? A named pipe solves this by giving the pipe a name in the filesystem. It is a meeting point with a fixed address: anyone who knows the path can show up and connect.

You create one with mkfifo mypipe (or the mkfifo() call). This makes a special file in a directory — ls shows it, and ls -l marks it with a leading p — but it is not a normal file: it stores no data on disk. It is just a rendezvous name. One program opens it for reading, another opens it for writing, and from then on it behaves exactly like an anonymous pipe: a one-way, ordered byte stream with no message boundaries, buffered in the kernel. The crucial twist is how open() blocks: opening a FIFO for reading waits until some writer also opens it (and vice versa), so the two ends synchronize at the meeting point. That is the whole point — unrelated processes find each other by name.

FIFOs are handy for simple local plumbing: a long-running data producer writes to /tmp/feed and any consumer reads from it, with no parent-child relationship required. The caveats are the same family as anonymous pipes plus a few extras. It is still one-way (open two FIFOs for two-way), still has no message framing, and still raises SIGPIPE if you write with no reader. And because the name persists on disk after the processes exit, you must remove it yourself (rm or unlink()), unlike an anonymous pipe which vanishes when its descriptors close.

$ mkfifo /tmp/feed # terminal 1 $ cat < /tmp/feed # waits here for a writer $ echo hello > /tmp/feed # terminal 2 — 'hello' now appears in terminal 1

A name in the filesystem lets two strangers meet. Reader blocks on open() until a writer arrives.

A FIFO has a name but stores no data — the file is just a rendezvous. Do not confuse it with a regular file: reading a FIFO twice does not re-read old data; once bytes are read they are gone, exactly like a pipe.

Also called
FIFOfifo special file具名管線FIFO 特殊檔案