In this lesson, you will learn how the Linux kernel uses blocking behavior to synchronize communication between two independent processes using a Named Pipe, or FIFO.
The FIFO Concept
A Named Pipe (often called a FIFO, for "First-In, First-Out") is a special file that acts as a conduit between two processes. Unlike a regular file, it doesn't store data on your disk; instead, the kernel maintains a small buffer in memory to pass bytes from a writer to a reader.
The Blocking Lifecycle
The "lifecycle" of a FIFO is defined by the kernel’s blocking mechanism. When you open a FIFO for reading or writing, the kernel forces the process to pause (block) until the other side of the pipe is ready.
Consider this scenario in two separate terminal windows:
Terminal A (The Reader):
cat my_pipe
The command will hang. The kernel sees that cat is waiting for data, but since nothing is writing to the pipe yet, it puts the cat process into a "sleeping" state, consuming zero CPU cycles.
Terminal B (The Writer):
echo "Hello World" > my_pipe
The moment you hit Enter, the kernel detects that a writer is present. It instantly moves the data from the memory buffer to the reader. Once the echo finishes sending its data, it closes the pipe, which sends an "End of File" (EOF) signal to the reader. cat then finishes, and the blocking ends.
Kernel Coordination
The kernel manages the synchronization so you don't have to. You don't need to manually check if the pipe is empty; the kernel handles the "stop-and-wait" logic internally. If you have multiple readers, the kernel ensures that data is delivered to only one process at a time, maintaining the integrity of the stream. This lifecycle—opening, blocking, transferring, and closing—is the fundamental way shell processes synchronize their activities without needing temporary files on the disk.
Key Takeaway: A Named Pipe is a kernel-level synchronization tool that forces a process to block until its counterpart appears, ensuring data is never lost in transit.