0Pricing
C Academy · Lesson

Pipes

Parent-child communication.

Pipes is a free C Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a Pipe

A pipe is a one-way communication channel between related processes. One process writes bytes into it and another reads them out, like a conveyor belt of data.

Two File Descriptors

The pipe() call fills a two-element array: fd[0] is the read end and fd[1] is the write end. Data written to fd[1] can be read from fd[0].

#include <unistd.h>
#include <stdio.h>

int make_pipe(int fd[2]) {
    if (pipe(fd) < 0) { perror("pipe"); return -1; }
    /* fd[0] read end, fd[1] write end */
    return 0;
}

Combining With fork

After fork(), both parent and child share the pipe's descriptors. This is how a parent and child communicate: one writes, the other reads.

Close Unused Ends

Each process should close the end it does not use. A reader closes the write end; a writer closes the read end. Otherwise the reader never sees end-of-file.

#include <unistd.h>

void parent_as_writer(int fd[2]) {
    close(fd[0]);              /* parent will not read */
    write(fd[1], "hello", 5);  /* parent writes */
    close(fd[1]);              /* signal EOF to reader */
}

A Parent-Child Example

The parent writes a message; the child reads it. Each closes the end it does not need.

#include <unistd.h>
#include <stdio.h>

void demo(int fd[2]) {
    if (fork() == 0) {           /* child: reader */
        close(fd[1]);
        char buf[64];
        ssize_t n = read(fd[0], buf, sizeof buf - 1);
        buf[n] = 0;
        printf("child got: %s\n", buf);
        close(fd[0]);
    } else {                     /* parent: writer */
        close(fd[0]);
        write(fd[1], "hi child", 8);
        close(fd[1]);
    }
}

Reading Returns EOF

When all write ends are closed, read returns 0, signaling end-of-file. If a write end stays open, the reader blocks waiting for more data forever, a common bug.

Pipe Capacity and Blocking

A pipe has a finite buffer (often 64 KB). If it fills, write blocks until a reader drains it. If empty, read blocks until data arrives. This flow control synchronizes the two processes.

Redirecting With dup2

dup2 can replace a process's stdin or stdout with a pipe end. This is how shells implement cmd1 | cmd2, wiring one command's output to the next's input.

#include <unistd.h>

void stdout_to_pipe(int fd[2]) {
    close(fd[0]);
    dup2(fd[1], STDOUT_FILENO); /* writes to stdout go into pipe */
    close(fd[1]);
}

Named Pipes (FIFOs)

An ordinary pipe only links related processes. A named pipe (FIFO), created with mkfifo, appears in the filesystem so unrelated processes can open it by path and communicate.

#include <sys/stat.h>

int make_fifo(const char *path) {
    return mkfifo(path, 0666); /* creates a FIFO file */
}

SIGPIPE Hazard

Writing to a pipe whose read end is closed raises SIGPIPE, which by default kills the process. Either ignore the signal or check for the EPIPE error from write.

When to Use Pipes

Pipes are ideal for simple, one-directional streaming between a parent and child, or for chaining programs. For two-way or unrelated-process communication, consider FIFOs, sockets, or shared memory.

Quick Check

Test your understanding of pipes.

Recap

You learned pipes for parent-child communication.

  • pipe() gives fd[0] (read) and fd[1] (write)
  • Combine with fork; close unused ends
  • read returns 0 at EOF when all write ends close
  • FIFOs link unrelated processes; beware SIGPIPE

Frequently asked questions

Is the “Pipes” lesson free?

Yes — the full text of “Pipes” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.

What will I learn in “Pipes”?

Parent-child communication. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Pipes” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Pipes
  2. Shared Memory
  3. Message Queues
  4. Signals
← Back to C Academy