$_ CLI In Depth
Learning progress 0%
lesson processes signals job-control

fork, exec & wait

5 min read Module 10: Processes, Signals & Job Control Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:14

Two system calls, one new process

The fork system call creates a new process that is a copy of the caller. The copy gets a new PID, and its PPID is the PID of the caller. Both processes continue from the same point in the code, and the only difference the call reports is its return value: the parent receives the child PID, and the child receives zero. The child keeps the open file descriptors of the parent, which is how a child inherits its terminal, its pipes, and its redirections.

The execve system call replaces the memory of the calling process with a new program loaded from a file. The PID does not change, the open file descriptors that are not marked close-on-exec stay open, and the parent relationship remains. Only the program image changes. If execve succeeds, it never returns to the old program. If it fails, it returns an error, which is how a shell learns that a file was not found or not executable.

Put the two together and you get the usual sequence for running a command. The shell forks. The child sets up its file descriptors, and then calls execve with the path of the program. The parent calls wait and blocks until the child ends.

Watching the sequence with strace

The strace program prints the system calls a process makes. Filtering the output to the process-creation calls shows the sequence without the noise of the program itself. The lab has strace installed, and it writes its report to standard error.

BASH1 lines
strace -f -e trace=clone,clone3,execve,wait4 bash -c "true" 2>&1 | tail -n 5

The exact list depends on the C library and the kernel, so the names may differ on your machine. Look for an execve call that names the program you asked for, and for a process-creation call that comes before it. The -f option follows the children, which is how the trace shows the new process as well as the parent. On many systems the creation call is clone or clone3, because fork is implemented with clone.

wait and the exit status

A parent that forks should wait for its child. The wait4 and waitpid system calls block until a child exits, and they return the child exit status. The shell does this for every foreground command, and it stores the status in $?. If the parent does not wait, the child stays in the process table as a zombie, which is the subject of a later unit.

The wait builtin does the same job for background jobs. Run a short program in the background, then wait for its PID and read the status.

BASH2 lines
sleep 1 & pid=$!
wait "$pid"; echo "status=$?"

The variable $! holds the PID of the most recent background job, and wait with that PID returns when the job ends. The status printed is zero, because sleep 1 succeeded. If the job had failed, the same pattern would report its failure.

Note

A command substitution such as $(sleep 1) also forks. The shell creates a child that writes its output into a pipe, reads that pipe, and waits for the child, which is why a substitution works like a small subshell.

exec without a fork

The exec builtin runs execve in the shell process itself, with no fork. When you use it with a program name, the shell is replaced by that program and the shell never comes back. This is common at the end of a wrapper script, where the wrapper sets up the environment and then hands control to the real program. The process keeps its PID, so a process manager that watches that PID sees the real program directly.

BASH1 lines
sh -c "exec sleep 1; echo never printed"; echo "after: $?"

The echo inside the quotes never runs, because exec replaced the shell with sleep. The outer shell prints the status of the whole command. The message is missing because the program that would have printed it was never started.

Why the design works

Forking before exec is useful because the child can change its environment between the two steps. It can redirect standard output to a file, close descriptors it does not want the program to see, or change its working directory. None of those changes affect the parent. The parent shell keeps its own directory, its own variables, and its own descriptors, and it is still running when the child finishes. This separation is why you can run a crashing program and still have a shell to type the next command in.

A later lesson in this module shows what a child receives from the parent during this copy, and how signals travel to the children after the fork.

Hands-on: connect the pieces

Run these commands in order. The first starts a child process that prints its parent PID. The second runs a background job and waits for it. The third replaces a shell with a program, so the trailing message never appears.

BASH3 lines
bash -c 'echo child $$ has parent $PPID'
sleep 1 & wait $!; echo "done $?"
sh -c "exec true"; echo "status $?"

In the first command the child reports its own PID and its parent, which is the bash process that started it. The second command shows the pattern of fork followed by wait in a single line. The third shows that exec gives the shell the exit status of the program that replaced it, and that the outer shell is still there to print it.

Parents, children and process states

Visualizer

Every process has a PID and a parent PID. Follow fork, exec and wait, then see how a zombie and an orphan show up in ps.

1 / 5
ps -eo pid,ppid,stat,comm (illustrative values)
PIDPPIDSTATCOMMAND
41204101Sbash

Every process has a PID and a PPID, the PID of its parent. For example, the interactive shell here is PID 4120. It sleeps (state S) while it waits for your next line.

echo "shell $$"; sh -c 'echo "child $$, parent $PPID"'; echo "shell waited, status $?"
Terminal lab · 2 steps · real Linux in your browser

Wait for a child and see exec replace a shell

Record the exit status of a background child, then show that exec replaces the shell.

01
Wait for a background job and record its status
Start sleep in the background, keep its PID in a variable, then wait for that PID. The status of wait is the status of the child. Save the value in wait.txt in the form status=N. ``bash sleep 1 & pid=$! wait "$pid"; echo "status=$?" > wait.txt `` The sleep succeeds, so the file holds status=0.
Show hint for step 1
Store $! in a variable, then call wait with that variable. The line echo "status=$?" must run right after wait.
02
Show that exec replaces the program
Run a shell that replaces itself with sleep. The text after exec never runs, so the output file gets nothing from the inner shell. The outer shell then records the status. ``bash sh -c 'exec sleep 1; echo never' > exec.txt 2>&1 echo "status=$?" >> exec.txt `` exec.txt should contain one line: status=0. The word never must not appear.
Show hint for step 2
Put exec in front of sleep inside the sh command. Redirect both output streams to exec.txt, then append the status line.
Check yourself Question 1 of 4
Concept Check Creating a copy of a process recall

Which system call creates a new process that is a copy of the caller?

Recap & Key Takeaways

fork creates a child that is a copy of the caller with a new PID, and execve replaces the child program without changing its PID.
The child inherits open file descriptors from its parent, which is how redirections and pipes are set up before exec.
A parent calls wait to collect the child exit status, and the shell stores it in $?.
The exec builtin replaces the shell itself with a program, so the rest of the script does not run.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue