Skip to content
Tutorials Center
My learning

Topics

Lesson 100 of 135 ยท Advanced C

Processes: fork, exec and Pipes

Create processes in C with fork, replace them with exec, collect exit codes with waitpid and connect them with pipe and dup2 - the building blocks of every Unix shell.

A process is a running program: it has its own memory, its own file descriptors and a process ID. When you type ls | sort in a shell, the shell creates two new processes, connects the output of the first to the input of the second, and waits for them to finish. All of that is done with a handful of POSIX system calls that you can use from C: fork, exec, waitpid, pipe and dup2.

This lesson builds those pieces one at a time and ends with a tiny pipeline of your own. The functions here come from POSIX (Linux, macOS, the BSDs), not from the C standard, so they are not available in a plain Windows build. If you haven't yet seen file descriptors, read System Calls and File Descriptors first.

fork: one process becomes two

fork() clones the calling process. After the call there are two almost identical processes: the parent (the original) and the child (the copy). Both continue running at the line after fork(), and the only difference is the return value:

  • In the parent, fork returns the child's process ID (a positive number).
  • In the child, fork returns 0.
  • If it fails (for example, too many processes), it returns -1 and no child exists.
flowchart TD
    A["parent calls fork()"] --> B["parent: fork returns child PID"]
    A --> C["child: fork returns 0"]
    B --> D["parent continues<br/>waitpid(child)"]
    C --> E["child runs its own code<br/>then exits"]
    E --> D

After fork, one code path becomes two. The return value tells each process which one it is.

The child gets a copy of the parent's memory. The copy is independent: changing a variable in the child does not change it in the parent. (Modern systems use copy-on-write, so the pages are only duplicated when one side writes to them.)

C fork: the child gets its own copy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The child prints 15, the parent still sees 10. The parent called waitpid before printing, so the order of output is predictable. Without a wait, two processes printing at the same time can interleave in any order.

Warning: stdio buffers are copied by fork too. If the parent has unflushed text in stdout's buffer when it forks, both processes will eventually print it, so you see the text twice. Call fflush(stdout) (or fflush(NULL)) before fork, as the example does. This is especially likely when output goes to a pipe or file, where stdout is fully buffered (see Debugging C Programs).

waitpid and exit codes

A child that has finished but hasn't been waited for becomes a zombie: it uses almost no memory, but its entry stays in the process table until the parent collects its exit status. A parent that creates children forever without waiting eventually runs out of process slots. Always wait for children you create.

waitpid(pid, &status, 0) blocks until that child ends and stores a packed status value. Don't read the number directly; use the macros from <sys/wait.h>:

Macro Meaning
WIFEXITED(status) The child ended by returning from main or calling exit
WEXITSTATUS(status) Its exit code (0 to 255), valid only if WIFEXITED is true
WIFSIGNALED(status) The child was killed by a signal
WTERMSIG(status) Which signal killed it (see Signals)

In a child created by fork, prefer _exit(code) over exit(code) when the child fails right after fork and must not run the parent's atexit handlers or flush the copied stdio buffers a second time. Returning from main is fine in simple cases like the one above, because it only runs the normal exit path once for that process.

exec: replace the program

fork alone only gives you copies of your own program. To run a different program you use the exec family. A successful exec throws away the current program's code and memory and loads a new executable into the same process (same PID, same open descriptors). It never returns on success; it returns -1 only if it fails.

The variants differ in how you pass arguments:

Function Arguments Searches PATH
execl(path, arg0, ..., NULL) listed one by one no
execlp(file, arg0, ..., NULL) listed one by one yes
execv(path, argv) an array ending in NULL no
execvp(file, argv) an array ending in NULL yes

By convention arg0 repeats the program name, and the argument list ends with a NULL pointer. Together fork plus exec is the classic "spawn a program" recipe:

sequenceDiagram
    participant P as Parent
    participant C as Child
    P->>C: fork()
    Note over C: copy of the parent
    C->>C: execvp("echo", ...)
    Note over C: now running echo
    C-->>P: exit status
    P->>P: waitpid() returns

The child starts as a copy, becomes another program through exec, and the parent collects its exit status.

C fork + exec + waitpid

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The first call prints the echo output and then echo returned 0. The second prints the perror message (no-such-program: No such file or directory) from the child, and the parent sees exit code 127. Our editor shows stderr after stdout, so in a real terminal the two lines appear interleaved in time order.

Everything between fork and exec in the child is your chance to rearrange the child's environment: change directories, close descriptors, or redirect input and output. That is exactly how shells implement > and |.

pipe: a one-way channel

pipe(fds) creates a kernel buffer with two ends and fills a two-element int array:

  • fds[0] is the read end.
  • fds[1] is the write end.

Bytes written into fds[1] come out of fds[0] in the same order. Because fork copies the descriptor table, a pipe created before the fork is available to both processes, which is how parent and child talk to each other.

flowchart LR
    subgraph Parent
        PW["fds[1] write end"]
    end
    subgraph Kernel
        BUF[("pipe buffer")]
    end
    subgraph Child
        CR["fds[0] read end"]
    end
    PW --> BUF --> CR

A pipe is a one-way byte channel: the parent writes at one end and the child reads at the other.

The rule that trips everyone up: close the ends you don't use. A reader only sees end-of-file (read returns 0) when every write end is closed. If the reading process keeps its own copy of the write end open, it will wait forever for data that can never arrive.

C A pipe between parent and child

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The while loop keeps reading until read returns 0, which happens only because the parent closed its write end and the child closed (or exited with) its own. Delete the parent's close(fds[1]) and the program hangs until the time limit stops it.

A pipe has a finite buffer (64 KiB on Linux). When it is full, write blocks until the reader drains it; when it is empty, read blocks until data arrives or all writers have closed. If you write to a pipe whose read ends are all closed, the process receives SIGPIPE, which kills it by default.

dup2: wiring a pipe to stdin and stdout

A program like sort reads from file descriptor 0 and writes to descriptor 1. It doesn't know about your fds array. dup2(old, new) makes descriptor new refer to the same open file as old, closing new first if needed. Using it in the child before exec connects a pipe to the program's standard streams, even though the program was written without pipes in mind. (You saw dup2 for files in System Calls and File Descriptors.)

Here is a miniature version of the shell command echo ... | sort, built by hand. Two children are created: the first writes lines into the pipe (we use printf through a shell so it needs no extra program), the second reads from the pipe and runs sort.

C A two-process pipeline

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

After dup2, the child no longer needs the original descriptors, so it closes both. The parent also closes both ends: if the parent kept fds[1] open, sort would never see end-of-file and the pipeline would hang. This "close everything you don't need" discipline is the hardest part of writing process code.

The sorted words appear in alphabetical order, followed by pipeline finished.

popen: the shortcut

When you only need to run one command and read its output (or feed it input), the standard popen wraps all of this for you:

C
FILE *p = popen("ls -l", "r");     /* or "w" to write to the command's stdin */
/* read from p with fgets/fread like any file */
int status = pclose(p);            /* waits for the command, returns its status */

popen runs the command through the shell (/bin/sh -c), which makes it convenient but dangerous when part of the command comes from user input: a value such as ; rm -rf ~ becomes part of the command line (a command injection bug). Prefer fork + execvp with an argument array when any argument is not a fixed string. system("...") has the same shell issue.

Common mistakes

  • Forgetting to wait, which leaves zombies and lets the parent race ahead.
  • Not closing unused pipe ends, causing hangs where a reader never sees end-of-file.
  • Using fork in a multithreaded program and then doing more than exec in the child. Only the forking thread survives in the child, so locks held by other threads stay locked forever. Between fork and exec, call only simple system calls. See Multithreading.
  • Forgetting the NULL terminator in exec argument lists, which is undefined behavior (see Undefined Behavior).
  • Ignoring the return value of exec. If it returns at all, it failed, so print an error and _exit.
  • Forking in a loop without a check, so the child also forks (a "fork bomb"). The child must exit or exec.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. After pid_t pid = fork(); succeeds, what does the CHILD process see in pid?

    fork returns 0 in the child and the child's PID in the parent. A return of -1 means the call failed and there is no child.
  2. 2. A parent creates a pipe, forks, and then the parent reads in a loop until read returns 0. The parent never closes its copy of the write end. What happens?

    read returns 0 only when every write end of the pipe is closed. The parent's own open write end keeps the pipe alive, so read blocks forever.
  3. 3. What does a successful call to execvp do?

    exec loads a new program into the existing process. On success the old code is gone, so there is nothing to return to. It returns -1 only on failure.
  4. 4. Why does a shell-like program call dup2 in the child before exec?

    The program being exec'd reads descriptor 0 and writes descriptor 1 without knowing about pipes. dup2 makes those numbers refer to the pipe ends you chose.
  5. 5. What is a zombie process?

    A finished child stays in the process table until the parent calls wait or waitpid to read its exit status.

Summary

  • fork duplicates the process; it returns the child's PID in the parent, 0 in the child and -1 on failure.
  • waitpid collects a child's status; inspect it with WIFEXITED, WEXITSTATUS, WIFSIGNALED and WTERMSIG. Unwaited children become zombies.
  • exec (execvp, execl, ...) replaces the program in the current process and only returns on error. fork + exec runs another program.
  • pipe gives a read end and a write end; close the ends you don't use, or readers never see end-of-file.
  • dup2 rewires descriptors 0, 1 and 2 so an unmodified program can read and write through a pipe or file.
  • Flush stdout before forking, and be careful with popen and system because they pass text to a shell.

Next: setjmp and longjmp