Type ls in a terminal and the files appear. But your shell is itself a program, and ls is another. So how does one running program start a different program? Does the shell turn into ls? If it did, the shell would be gone after one command.
Unix answers with three system calls that each do one small job, and the shell combines them every time you press Enter.
Three calls, three jobs
forkcreates a new process that is a copy of the caller. The caller is the parent, the copy is the child. They have the same program, the same variables and the same place in the code: both carry on from the line afterfork.execreplaces the program inside the current process with a different one. The process keeps its PID and its open files, but its code, data, heap and stack are thrown away and rebuilt from the new program.waitmakes the parent pause until a child has exited, then collects the child’s exit status: the number it passed toexit, where 0 means success.
fork has one strange property: it is called once but returns twice, once in each process. In the parent it returns the child’s PID. In the child it returns 0. That is how the same code can tell which side it is on.
Here is a shell, bash, running ls. Watch the tree of processes. The line child exec ls means “only the child runs this line”, and parent wait means the same for the parent. Under the tree is the terminal.
A shell runs ls
-
The first process is running
bash is the only process of its own so far. Its parent is init (PID 1), the first process on the system. The sim shows one step per line the process runs.
- On the CPU
- PID 4100 (bash)
- Processes
- 1
- Zombies
- 0
- Printed lines
- 0
-
PID 4100 (bash): fork
fork makes a copy of the process: same program, same variables, same place in the code. The copy is PID 4101. In the parent, fork returns 4101 (the child's PID); in the child it returns 0. That is how each one can tell which it is. The parent carries on first.
- On the CPU
- PID 4100 (bash)
- Processes
- 2
- Zombies
- 0
- Printed lines
- 0
-
PID 4100 (bash): wait
No child has ended yet, so wait blocks. The process moves to waiting and uses no CPU until a child exits.
- On the CPU
- PID 4100 (bash)
- Processes
- 2
- Zombies
- 0
- Printed lines
- 0
-
PID 4101 (ls): exec ls
exec throws away this process's program, memory and stack, and loads ls instead. The PID stays the same and so do the open files. Whatever lines were left in the old program will never run.
- On the CPU
- PID 4101 (ls)
- Processes
- 2
- Zombies
- 0
- Printed lines
- 0
-
PID 4101 (ls): print "notes.txt todo.md"
The process writes a line to the terminal (a write system call, Lesson 1.3).
- On the CPU
- PID 4101 (ls)
- Processes
- 2
- Zombies
- 0
- Printed lines
- 1
-
PID 4101 (ls): exit
The process exits with status 0. The kernel frees its memory and open files. The process cannot vanish yet: its parent has not collected the exit status, so it stays as a zombie (state Z). The parent was waiting, so the kernel wakes it.
- On the CPU
- PID 4101 (ls)
- Processes
- 1
- Zombies
- 1
- Printed lines
- 1
-
PID 4100 (bash): wait
wait finds the child PID 4101 has ended, collects its exit status (0) and lets the kernel delete its leftover record. The zombie is gone.
- On the CPU
- PID 4100 (bash)
- Processes
- 1
- Zombies
- 0
- Printed lines
- 1
-
PID 4100 (bash): keeps running
The process goes on running and never exits. Nothing else can change now, so the sim stops here.
- On the CPU
- PID 4100 (bash)
- Processes
- 1
- Zombies
- 0
- Printed lines
- 1
Look at what the shell did:
fork: a secondbashappears. For a moment there are two shells.- The child calls
exec ls. It stops beingbashand becomesls, with the same PID. The parent never changes program. - The parent calls
waitand sleeps. This is why your prompt does not come back until the command finishes. lsprints, exits, and becomes a zombie for an instant. The parent wakes, collects the status and the prompt returns.
Splitting “make a process” from “load a program” looks odd, but it is useful. Between fork and exec the child is still running the shell’s code, so it can change things for the new program. For example, ls > files.txt works because the child opens files.txt and attaches it to standard output before exec. The open files survive exec, so ls writes into the file without knowing.
fork in C
This program forks, lets the child exit with status 7, and has the parent collect it. In C, fork() gives back a pid_t, a PID. The if (pid == 0) is only true in the child.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void) {
printf("before fork\n");
fflush(stdout); /* so the line is not copied into the child's buffer */
pid_t pid = fork(); /* from here on, two processes run this code */
if (pid == 0) {
printf("child: fork returned 0\n");
exit(7); /* end with exit status 7 */
}
printf("parent: fork returned %d\n", (int)pid);
int status;
waitpid(pid, &status, 0); /* wait for that child, and collect its status */
printf("parent: child %d exited with status %d\n", (int)pid, WEXITSTATUS(status));
return 0;
}
Output (one run)
before fork child: fork returned 0 parent: fork returned 1585237 parent: child 1585237 exited with status 7
The PID is different on every run, so the lesson cannot tell you its value. The status, 7, is the one number that is always the same. The build also marks this output as “any order”: the parent and child run at the same time and the kernel decides who prints first. Only the parent’s last line is always last, because it comes after the wait. Never write code that depends on which process prints first.
How many processes?
Here is the classic puzzle. What does this do?
fork();
fork();
printf("hi\n");The second fork runs in the parent and in the first child, because the child was copied at a point before the second fork. So each fork doubles the number of processes. The two wait lines make every process wait for its children before it exits, as the C version below does, so you can see all four processes alive together. Predict first, then step through, then press Try it and build your own program from the blocks: fork, exec ls, wait, exit, print and work. Start a line with child or parent to give a block to one side only. Try three forks, or put the print between the forks, and see if you predicted the count.
fork(); fork(); printf("hi\n");
-
The first process is running
a.out is the only process of its own so far. Its parent is init (PID 1), the first process on the system. The sim shows one step per line the process runs.
- On the CPU
- PID 4100 (a.out)
- Processes
- 1
- Zombies
- 0
- Printed lines
- 0
-
PID 4100 (a.out): fork
fork makes a copy of the process: same program, same variables, same place in the code. The copy is PID 4101. In the parent, fork returns 4101 (the child's PID); in the child it returns 0. That is how each one can tell which it is. The parent carries on first.
- On the CPU
- PID 4100 (a.out)
- Processes
- 2
- Zombies
- 0
- Printed lines
- 0
-
PID 4100 (a.out): fork
fork makes a copy of the process: same program, same variables, same place in the code. The copy is PID 4102. In the parent, fork returns 4102 (the child's PID); in the child it returns 0. That is how each one can tell which it is. The parent carries on first.
- On the CPU
- PID 4100 (a.out)
- Processes
- 3
- Zombies
- 0
- Printed lines
- 0
-
PID 4100 (a.out): print "hi"
The process writes a line to the terminal (a write system call, Lesson 1.3).
- On the CPU
- PID 4100 (a.out)
- Processes
- 3
- Zombies
- 0
- Printed lines
- 1
-
PID 4100 (a.out): wait
No child has ended yet, so wait blocks. The process moves to waiting and uses no CPU until a child exits.
- On the CPU
- PID 4100 (a.out)
- Processes
- 3
- Zombies
- 0
- Printed lines
- 1
-
PID 4101 (a.out): fork
fork makes a copy of the process: same program, same variables, same place in the code. The copy is PID 4103. In the parent, fork returns 4103 (the child's PID); in the child it returns 0. That is how each one can tell which it is. The parent carries on first.
- On the CPU
- PID 4101 (a.out)
- Processes
- 4
- Zombies
- 0
- Printed lines
- 1
-
PID 4101 (a.out): print "hi"
The process writes a line to the terminal (a write system call, Lesson 1.3).
- On the CPU
- PID 4101 (a.out)
- Processes
- 4
- Zombies
- 0
- Printed lines
- 2
-
PID 4101 (a.out): wait
No child has ended yet, so wait blocks. The process moves to waiting and uses no CPU until a child exits.
- On the CPU
- PID 4101 (a.out)
- Processes
- 4
- Zombies
- 0
- Printed lines
- 2
-
PID 4102 (a.out): print "hi"
The process writes a line to the terminal (a write system call, Lesson 1.3).
- On the CPU
- PID 4102 (a.out)
- Processes
- 4
- Zombies
- 0
- Printed lines
- 3
-
PID 4102 (a.out): wait
This process has no children, so wait returns at once with an error (-1). Nothing to collect.
- On the CPU
- PID 4102 (a.out)
- Processes
- 4
- Zombies
- 0
- Printed lines
- 3
-
PID 4102 (a.out): wait
This process has no children, so wait returns at once with an error (-1). Nothing to collect.
- On the CPU
- PID 4102 (a.out)
- Processes
- 4
- Zombies
- 0
- Printed lines
- 3
-
PID 4102 (a.out): exit
The process exits (the program reached its end) with status 0. The kernel frees its memory and open files. The process cannot vanish yet: its parent has not collected the exit status, so it stays as a zombie (state Z). The parent was waiting, so the kernel wakes it.
- On the CPU
- PID 4102 (a.out)
- Processes
- 3
- Zombies
- 1
- Printed lines
- 3
-
PID 4100 (a.out): wait
wait finds the child PID 4102 has ended, collects its exit status (0) and lets the kernel delete its leftover record. The zombie is gone.
- On the CPU
- PID 4100 (a.out)
- Processes
- 3
- Zombies
- 0
- Printed lines
- 3
-
PID 4100 (a.out): wait
No child has ended yet, so wait blocks. The process moves to waiting and uses no CPU until a child exits.
- On the CPU
- PID 4100 (a.out)
- Processes
- 3
- Zombies
- 0
- Printed lines
- 3
-
PID 4103 (a.out): print "hi"
The process writes a line to the terminal (a write system call, Lesson 1.3).
- On the CPU
- PID 4103 (a.out)
- Processes
- 3
- Zombies
- 0
- Printed lines
- 4
-
PID 4103 (a.out): wait
This process has no children, so wait returns at once with an error (-1). Nothing to collect.
- On the CPU
- PID 4103 (a.out)
- Processes
- 3
- Zombies
- 0
- Printed lines
- 4
-
PID 4103 (a.out): wait
This process has no children, so wait returns at once with an error (-1). Nothing to collect.
- On the CPU
- PID 4103 (a.out)
- Processes
- 3
- Zombies
- 0
- Printed lines
- 4
-
PID 4103 (a.out): exit
The process exits (the program reached its end) with status 0. The kernel frees its memory and open files. The process cannot vanish yet: its parent has not collected the exit status, so it stays as a zombie (state Z). The parent was waiting, so the kernel wakes it.
- On the CPU
- PID 4103 (a.out)
- Processes
- 2
- Zombies
- 1
- Printed lines
- 4
-
PID 4101 (a.out): wait
wait finds the child PID 4103 has ended, collects its exit status (0) and lets the kernel delete its leftover record. The zombie is gone.
- On the CPU
- PID 4101 (a.out)
- Processes
- 2
- Zombies
- 0
- Printed lines
- 4
-
PID 4101 (a.out): wait
This process has no children, so wait returns at once with an error (-1). Nothing to collect.
- On the CPU
- PID 4101 (a.out)
- Processes
- 2
- Zombies
- 0
- Printed lines
- 4
-
PID 4101 (a.out): exit
The process exits (the program reached its end) with status 0. The kernel frees its memory and open files. The process cannot vanish yet: its parent has not collected the exit status, so it stays as a zombie (state Z). The parent was waiting, so the kernel wakes it.
- On the CPU
- PID 4101 (a.out)
- Processes
- 1
- Zombies
- 1
- Printed lines
- 4
-
PID 4100 (a.out): wait
wait finds the child PID 4101 has ended, collects its exit status (0) and lets the kernel delete its leftover record. The zombie is gone.
- On the CPU
- PID 4100 (a.out)
- Processes
- 1
- Zombies
- 0
- Printed lines
- 4
-
PID 4100 (a.out): exit
The process exits (the program reached its end) with status 0. The kernel frees its memory and open files. Its parent is init, which always collects its children at once, so no zombie is left.
- On the CPU
- PID 4100 (a.out)
- Processes
- 0
- Zombies
- 0
- Printed lines
- 4
-
Every process has ended
Only init is left. Every other process has exited and been collected.
- On the CPU
- nothing
- Processes
- 0
- Zombies
- 0
- Printed lines
- 4
Real C proves it. Each process prints one line and then waits for any children, so that the output is finished before the program ends:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void) {
fork();
fork();
printf("hi\n");
while (wait(NULL) > 0) { } /* wait for every child before exiting */
return 0;
}
Output (one run)
hi hi hi hi
exec in C
exec has several variants. execlp looks the program up on the path, like a shell does. If it works, it never returns, because the program that called it has been replaced. So the line after it only runs when exec fails.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void) {
pid_t pid = fork();
if (pid == 0) {
execlp("echo", "echo", "hello from the new program", (char *)NULL);
printf("exec failed\n"); /* only reached if exec could not start the program */
exit(1);
}
waitpid(pid, NULL, 0);
printf("parent: echo has finished\n");
return 0;
}
Output (one run)
hello from the new program parent: echo has finished
There is no “exec failed” line, because exec worked and the child’s own code, including that printf, no longer existed. This is a mini shell: fork, exec, wait.
Zombies
What if the parent never calls wait? The child exits, but its record stays in the process table as a zombie. It uses no CPU and no memory except the record itself, but it keeps its PID occupied. A parent that forks again and again without waiting can fill the table.
A parent that never waits
-
The first process is running
a.out is the only process of its own so far. Its parent is init (PID 1), the first process on the system. The sim shows one step per line the process runs.
- On the CPU
- PID 4100 (a.out)
- Processes
- 1
- Zombies
- 0
- Printed lines
- 0
-
PID 4100 (a.out): fork
fork makes a copy of the process: same program, same variables, same place in the code. The copy is PID 4101. In the parent, fork returns 4101 (the child's PID); in the child it returns 0. That is how each one can tell which it is. The parent carries on first.
- On the CPU
- PID 4100 (a.out)
- Processes
- 2
- Zombies
- 0
- Printed lines
- 0
-
PID 4100 (a.out): work
The process computes something. It stays running.
- On the CPU
- PID 4100 (a.out)
- Processes
- 2
- Zombies
- 0
- Printed lines
- 0
-
PID 4100 (a.out): keeps running
The process goes on running and never exits. For the sim it steps aside so the other processes can run.
- On the CPU
- PID 4100 (a.out)
- Processes
- 2
- Zombies
- 0
- Printed lines
- 0
-
PID 4101 (a.out): exit
The process exits with status 3. The kernel frees its memory and open files. The process cannot vanish yet: its parent has not collected the exit status, so it stays as a zombie (state Z).
- On the CPU
- PID 4101 (a.out)
- Processes
- 1
- Zombies
- 1
- Printed lines
- 0
-
PID 4100 (a.out) is the only one left running
This process never ends, so nothing else will change. The zombie above stays in the process table until a parent calls wait or the parent exits.
- On the CPU
- PID 4100 (a.out)
- Processes
- 1
- Zombies
- 1
- Printed lines
- 0
You cannot kill a zombie, because it is already dead. The fix is for the parent to call wait, or for the parent to exit, which hands the zombie to init, who collects it at once.
Orphans
The opposite case: the parent exits while the child is still running. The child is now an orphan. The kernel gives it a new parent, init (PID 1, Lesson 1.4), which does nothing except call wait on every child it inherits.
A parent that dies first
-
The first process is running
a.out is the only process of its own so far. Its parent is init (PID 1), the first process on the system. The sim shows one step per line the process runs.
- On the CPU
- PID 4100 (a.out)
- Processes
- 1
- Zombies
- 0
- Printed lines
- 0
-
PID 4100 (a.out): fork
fork makes a copy of the process: same program, same variables, same place in the code. The copy is PID 4101. In the parent, fork returns 4101 (the child's PID); in the child it returns 0. That is how each one can tell which it is. The parent carries on first.
- On the CPU
- PID 4100 (a.out)
- Processes
- 2
- Zombies
- 0
- Printed lines
- 0
-
PID 4100 (a.out): exit
The process exits with status 0. The kernel frees its memory and open files. Its running child (PID 4101) lost their parent, so init adopts them: they are orphans now. Its parent is init, which always collects its children at once, so no zombie is left.
- On the CPU
- PID 4100 (a.out)
- Processes
- 1
- Zombies
- 0
- Printed lines
- 0
-
PID 4101 (a.out): work
The process computes something. It stays running.
- On the CPU
- PID 4101 (a.out)
- Processes
- 1
- Zombies
- 0
- Printed lines
- 0
-
PID 4101 (a.out): print "late"
The process writes a line to the terminal (a write system call, Lesson 1.3).
- On the CPU
- PID 4101 (a.out)
- Processes
- 1
- Zombies
- 0
- Printed lines
- 1
-
PID 4101 (a.out): exit
The process exits (the program reached its end) with status 0. The kernel frees its memory and open files. Its parent is init, which always collects its children at once, so no zombie is left.
- On the CPU
- PID 4101 (a.out)
- Processes
- 0
- Zombies
- 0
- Printed lines
- 1
-
Every process has ended
Only init is left. Every other process has exited and been collected.
- On the CPU
- nothing
- Processes
- 0
- Zombies
- 0
- Printed lines
- 1
Orphans are not a problem. Servers rely on them on purpose: a program starts a child, exits, and the child carries on in the background (a daemon). On modern Linux, a process can also be set as a subreaper, which adopts orphans instead of PID 1. Container tools use this, and you will meet containers in Module 8.
In the real world
Every program you start on Linux is made this way, by something calling fork and exec. pstree draws the whole family tree, which always has init (or systemd) at the root:
$ pstree
systemd─┬─sshd───sshd───bash───pstree
├─cron
└─...
The tree is trimmed and yours will look different. The pstree at the end is the child of bash, created by the same fork and exec you stepped through. In ps output, a zombie shows as Z and is marked <defunct> in the command column. Windows has no fork: its CreateProcess call creates a process and loads a program in one go, and Windows has no zombies in the Unix sense. :::
Quick check
Quick check
-
That is how the same code can tell which process it is in: if (pid == 0) is the child's side.
-
exec loads a new program into the same process. That is why a shell can open a file before exec and have the new program write into it.
-
The kernel keeps the finished process's record until the parent calls wait. A child whose parent exited is an orphan, and gets adopted by init.
-
Each fork doubles the number of processes, because the existing children also reach the next fork. Two forks give four processes, and each one prints.
Recap
forkcopies the process. It returns the child’s PID in the parent and 0 in the child.execreplaces the program in the same process. The PID and open files stay.waitpauses the parent until a child exits, then collects its exit status. A shell runs every command with fork, exec and wait.- A child that has exited but not been collected is a zombie. A child whose parent exits first is an orphan, adopted by init.
- Parent and child run at the same time, so their output order is not fixed.
Next: a process is heavy: its own address space, its own PCB. For many jobs inside one program, there is something lighter.
Join the discussion