fork(), exec(), wait() and exit()
fork(), exec(), wait() and exit()
Recorded by Dr. Krithika L.B. Slot m2-fork-exec
How processes are born with fork(), transformed with exec(), collected with wait() and ended with exit() — plus zombies, orphans and why processes cooperate.
By the end of this session you should be able to:
A process (the parent) can create new processes (children), which can create more — forming
a process tree rooted at PID 1 (systemd). Each process has a unique integer PID and knows its
parent's PID (PPID). Try pstree -p on Linux to see the tree.
When a parent creates a child, the designer has to decide:
Parent and child share all, some, or none of the resources.
Parent continues concurrently with the child, or waits until the child terminates.
Child is a duplicate of the parent (UNIX fork), or loads a new program (exec / Windows CreateProcess).
fork() creates a child that is an almost exact copy of the parent. It is called once but returns twice:
0 in the child, the child's PID (> 0) in the parent, and -1 on failure.exec() (e.g. execlp) replaces the calling process's memory with a new program. It does not return on success.wait() blocks the parent until a child terminates and collects its exit status.exit(status) terminates the calling process and returns status to the parent.#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void)
{
pid_t pid = fork(); /* one call, two returns */
if (pid < 0) { /* error */
perror("fork");
exit(1);
} else if (pid == 0) { /* child */
printf("Child PID=%d, parent=%d\n", getpid(), getppid());
execlp("ls", "ls", "-l", NULL); /* replace child with ls */
perror("exec"); /* only reached if exec fails */
exit(1);
} else { /* parent */
int status;
wait(&status); /* block until child ends */
printf("Parent PID=%d: child %d finished with status %d\n",
getpid(), pid, WEXITSTATUS(status));
}
return 0;
}One process (the shell, PID 100) is running main().
The kernel creates PID 101: a copy of the address space, open files and registers. Both processes now continue from the line after fork().
The return value is different, so the if sends the child into the pid == 0 branch and the parent into the else branch.
The child calls execlp("ls", …): its code, data, heap and stack are replaced by the ls program. PID stays 101.
The parent is in the Waiting state inside wait() until the child terminates.
ls finishes and exits. The kernel records its status; the parent wakes up, collects the status, and the child's PCB is finally removed.
fork();
fork();
fork();
printf("hello\n");Each fork() doubles the number of processes: 1 → 2 → 4 → 8. So 8 processes print hello (2n for n forks), of which 7 are new.
A process terminates when it executes its last statement or calls exit(). The OS deallocates its memory, open files
and buffers. A parent may also terminate a child (kill()) if the child exceeds its resources, its task is no longer
needed, or the parent itself is exiting. Some systems use cascading termination: if a parent ends, all its
children are terminated too.
A child that has terminated but whose parent has not yet called wait(). Its memory is freed, but its PCB entry (PID + exit status) remains so the parent can collect it. Shown as <defunct>, state Z in ps.
Too many zombies can exhaust the process table.
A child whose parent terminated first, without waiting. The orphan keeps running and is re-parented to init/systemd (PID 1) or a subreaper, which calls wait() on it when it ends.
Daemons are often deliberately orphaned.
You will create both deliberately in Lab 3 and watch them with ps -el.
Processes are independent if they cannot affect or be affected by others, and cooperating if they can. Reasons to cooperate:
Cooperation requires inter-process communication (IPC) — shared memory or message passing — and, because shared data can be corrupted by concurrent access, synchronization. Both are the subject of Module 3.
fork(), exec(), wait() and exit()
Recorded by Dr. Krithika L.B. Slot m2-fork-exec
Zombie and orphan processes
Recorded by Dr. Krithika L.B. Slot m2-zombie-orphan
Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.
Answer each question in your own words first, then reveal the model answer.
Revealed 00 to the child, the child's PID to the parent, −1 to the parent if creation failed.
for (i = 0; i < 3; i++) fork(); printf("hi\n");?23 = 8 times — every process existing after the loop prints once.
It never runs: exec replaces the whole memory image, so control never returns to the old program.
A zombie has finished but its parent hasn't waited for it (only a PCB entry remains). An orphan is still running but its parent has exited; it is adopted by init/systemd.
If two processes update shared data at the same time, the result can depend on the order of their instructions (a race condition) — Module 3.
Creating a whole process for every task is expensive — a full copy of the address space, a new PCB, a context switch to reach it. Can several flows of control share one process? Yes: threads.
Continue to 2.3 — Threads and multithreading models →