Search⌘ K
AI Features

Pipes and FIFOs

Explore how to use pipes and FIFOs for interprocess communication in C. Learn the differences between anonymous pipes and named pipes, how to create and use them, and their role in data exchange between related and unrelated processes.

In the previous lesson, we learned that separate processes cannot directly share variables because each process runs in its own protected memory space. We also introduced fork(), which creates a parent and a child process that execute independently.

We now ask a practical question: How can a parent and child process exchange data? One of the simplest mechanisms provided by the operating system is the pipe.

What is a pipe?

A pipe is a unidirectional communication channel that allows data to flow from one process to another. Unidirectional means data travels in one direction only; one end is used for writing, and the other end is used for reading.

Internally, a pipe is managed by the kernel and uses file descriptors. When a pipe is created, the operating system returns two file descriptors:

  • A read descriptor

  • A write descriptor

Anything written to the write end can be read from the read end.

Anonymous pipes

An anonymous pipe is created using the pipe() system call:

int pipe(int fd[2]);

If successful, pipe() fills the array so that fd[0] is the read end and fd[1] is the write ...