The process table
A running program can start another with three system calls. fork() copies the calling process. exec() swaps the program inside a process for a new one. wait() lets the first process collect the result. Each is work the kernel does.
Our program runs as process 100. For each process the kernel keeps a record called a PCB (process control block) in its process table: the ID, the state (now running), the saved registers and where its memory is.
fork: copy
Line 1 calls fork(). Like every system call it traps into the kernel, which now works on behalf of process 100.
The kernel makes a new PCB with a new ID, 101, and copies the parent into it: its memory, its open files and its saved registers. The child is a near twin, ready to run the same code from the same spot.
fork() returns twice, once in each process. The parent gets the child's ID, 101. The child gets 0. That is the only difference, and line 2, if (pid == 0), uses it to choose a path.
wait: block
In the parent pid is 101, not 0, so it skips line 3 and calls waitpid(). The child has not finished, so the kernel marks the parent blocked: it uses no CPU. The kernel runs the child instead.
exec: replace
In the child pid is 0, so it runs line 3, execvp(). The kernel throws away the child's memory, loads /bin/echo from disk in its place and starts it. Same process, same ID 101: only the program changed.
echo prints hi, then calls exit(0). Notice that execvp() never returned: the code that called it no longer exists. On success exec has nowhere to return to.
wait: collect
The kernel frees the child's memory and files, but keeps its PCB holding the exit status, 0. A finished process whose status nobody has collected yet is a zombie. The kernel also wakes the parent: blocked becomes ready.
When the parent runs again, waitpid() returns 101 and puts the exit status in status. Now the kernel deletes the zombie's PCB: the child is gone for good. The parent prints exit 0.
Between fork() and exec() the child still runs the parent's code, so it can change its own setup first, such as sending its output to a file. A shell runs every command you type this way.
What happens
;; step 1/11 · three-calls
fork() copy me: a new process, the child
exec() replace my program with another one
wait() sleep until my child ends, then get its result
the whole program (C99, POSIX):
#define _POSIX_C_SOURCE 200809L /* fork, execvp, waitpid under -std=c99 */
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void) {
char *argv[] = {"echo", "hi", NULL};
pid_t pid;
int status;
pid = fork();
if (pid == 0)
execvp("echo", argv);
waitpid(pid, &status, 0);
if (pid == 0) return 127; /* only if exec failed */
printf("exit %d\n", WEXITSTATUS(status));
return 0;
}
Copying a whole memory would be slow, so Linux uses copy-on-write: after fork both processes share the same pages, marked read-only, and a page is copied only when one of them writes to it. The lesson draws the copy as if it were made at once. The PIDs 100 and 101 are made up for the example. Linux hands out the next free number, counting up from the last one used and wrapping around at pid_max, so real PIDs depend on what else has run.