🧠 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

FeatureDescription
🔁 DirectionUnidirectional (one writes, one reads)
🧬 RelationshipTypically used between related processes (like parent ↔ child)
💾 Backed ByKernel buffer (in-memory)
🚦 SynchronizationAutomatically blocks reader/writer if the buffer is empty/full
🧠 OS SupportManaged 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

TypeDescription
🔒 Anonymous PipeUsed between related processes (created with pipe())
🧾 Named Pipe (FIFO)Works between unrelated processes; exists in file system (mkfifo())

⚠️ Limitations of Pipes

LimitationReason
⛔ UnidirectionalYou need two pipes for two-way communication
🧬 Related processes onlyAnonymous pipes can’t be used between unrelated processes
💽 Buffer size limitedUsually around 64 KB
📥 No message boundariesIt’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.