🧠 What is a Pipe?
A Pipe is a unidirectional communication channel that allows one process to send data to another by writing into a buffer, which the other process reads from.
Think of it as a one-way tunnel: data goes in one end and comes out the other.
📦 Key Characteristics
| Feature | Description |
|---|---|
| 🔁 Direction | Unidirectional (one writes, one reads) |
| 🧬 Relationship | Typically used between related processes (like parent ↔ child) |
| 💾 Backed By | Kernel buffer (in-memory) |
| 🚦 Synchronization | Automatically blocks reader/writer if the buffer is empty/full |
| 🧠 OS Support | Managed by the kernel (usually uses file descriptors) |
🔍 How It Works
Process A (Writer) Process B (Reader)
| |
write() ------------> read()
[ Kernel Pipe Buffer ]
-
pipe(fd)creates two file descriptors:-
fd[0]→ read end -
fd[1]→ write end
-
🛠️ Code Example in C (Parent → Child)
int fd[2];
pipe(fd);
if (fork() == 0) {
// Child
close(fd[1]); // Close write end
read(fd[0], buffer, sizeof(buffer));
} else {
// Parent
close(fd[0]); // Close read end
write(fd[1], "Hello", 5);
}📚 Types of Pipes
| Type | Description |
|---|---|
| 🔒 Anonymous Pipe | Used between related processes (created with pipe()) |
| 🧾 Named Pipe (FIFO) | Works between unrelated processes; exists in file system (mkfifo()) |
⚠️ Limitations of Pipes
| Limitation | Reason |
|---|---|
| ⛔ Unidirectional | You need two pipes for two-way communication |
| 🧬 Related processes only | Anonymous pipes can’t be used between unrelated processes |
| 💽 Buffer size limited | Usually around 64 KB |
| 📥 No message boundaries | It’s a byte stream, not message-structured |
🧠 Real-World Analogy
Imagine two people using a plastic tube — one speaks into it, the other listens.
But they can’t speak at the same time through the same tube — that’s a pipe.
🧠 Interview-Ready Definition:
A Pipe is a unidirectional IPC mechanism provided by the OS that allows one process to write data into a buffer and another to read from it. It’s commonly used between related processes and operates through file descriptors and kernel-managed buffers.