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.
strace -f -e trace=clone,clone3,execve,wait4 bash -c "true" 2>&1 | tail -n 5The 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.
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.
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.
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.
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.