Skip to content

pipe()

Purpose

pipe() creates a unidirectional byte-stream channel with two file descriptors.

It is useful for simple parent-child communication and for building fd-based event examples.

#include <unistd.h>

Prototype

int pipe(int pipefd[2]);

Parameters

Parameter Description
pipefd[0] Read end of the pipe.
pipefd[1] Write end of the pipe.

Return Value

Returns 0 on success. Returns -1 on failure and sets errno.

Minimal Example

int pipefd[2];

if (pipe(pipefd) < 0) {
    perror("pipe");
    return -1;
}

Common Pitfalls

  • Not closing unused ends after fork().
  • Expecting message boundaries; pipes are byte streams.
  • Not handling EPIPE when the read end is closed.
  • Forgetting that pipe capacity is finite.