Processes โ€” Easy Exam Guide Unit u04 (Processes, exec, signals, pipes). Every idea in simple words, linked to how the exam asks it. This is the biggest topic โ€” it gives 4 question types. ๐Ÿ”ด = comes a lot.

Written in simple (B1) English. Read it from top to bottom before the exam.

0How "processes" come in the exam

Processes is the biggest topic on the exam. It gives you four kinds of questions, and together they are worth a lot of points (about 9โ€“11 points in every paper). The good news: each kind follows clear rules. Learn the rules here, then practise.

The question you will seeHow oftenSection
๐Ÿ”ด A C program with fork/exec/system โ†’ "what is the output?" or "how many times does X print?"almost every exam (the headline skill)ยง2 + ยง3
๐Ÿ”ด True/false about the PCB, zombie, orphan, process states, the exec familyalmost every examยง1, ยง3, ยง4
๐Ÿ”ด True/false about signals (kill, handlers, SIGCHLD, race conditions)most examsยง5
๐Ÿ”ด True/false about pipes (and sometimes a write-code pipe task)most examsยง6
โš ๏ธ Always remember
The true/false questions take points away for wrong answers. Tick only what you are sure of. The "output" questions need no guessing โ€” they have one correct trace if you follow the method in ยง2.

1What a process is, and the PCB ๐Ÿ”ด comes a lot

Program vs process

Each process has its own memory: code (the instructions), data (global variables), heap (memory you ask for at run time, grows up), and stack (function calls and local variables, grows down). It also has CPU registers (like the program counter).

The Process Control Block (PCB)

The kernel keeps one PCB for each process. The PCB holds everything the system needs to know about that process:

On a context switch (when the CPU changes from one process to another), the kernel saves the old process into its PCB and loads the new process from its PCB. A context switch is only overhead โ€” no useful work happens during it.

Process states

StateMeaning
Newjust created
Readycan run, but waiting for a free CPU
Runningusing the CPU now
Waitingblocked, waiting for an event or I/O
Terminatedfinished, resources released

A running process does not go straight back to Running. It goes Running โ†’ Ready (the scheduler stops it), and then Ready โ†’ Running later. Only one process per CPU is Running.

๐ŸŽฏ In the exam โ€” PCB & states (2024-02-07 Ex4, 2022-01-28 Ex9, etc.)
TRUE facts to tick: the PCB stores the open files, all CPU registers, and the signal table; a zombie still has its PCB (it is not deleted right away โ€” see ยง4).
โœ— Trap sentences (FALSE)
  • "When a process dies, its PCB is freed immediately." No โ€” a zombie keeps its PCB until the parent waits.
  • "Context switching means deleting the PCB of the old process." No โ€” it only saves and loads.

2fork() and how to read these programs ๐Ÿ”ด the headline skill โ€” almost every exam

The most common process question gives you a C program with fork, exec, and system, and asks for the output, or "how many times does X print?". You do not guess โ€” you follow a method. This section gives you that method.

What fork() does

The method to trace the output

  1. Find what each process prints at the start. If a printf is before the fork, every fresh start prints it once.
  2. Follow the branches. After each fork(), draw the parent path and the child path. Count how many processes you have.
  3. Watch setbuf(stdout,0). This makes output unbuffered. Then output comes out at once and is not duplicated by fork. (If the output is buffered, fork copies the buffer, so text can print twice โ€” a classic trap.)
  4. exec replaces the program (see ยง3). After a successful execlp(...), the lines after it do not run. The process starts the program again from the beginning (a "fresh start").
  5. system("...") runs a new program and waits for it to finish. So a system("./pgrm n-1") is also a "fresh start".
  6. For "how many times" questions, use a count rule. Write S(n) = how many fresh starts happen for input n. Each start makes more starts, so you build a small formula.
โ–ถ Worked count example (2024-06-20 Ex2 โ€” "how many PIDs are printed?")
printf("%d ", getpid());        // every start prints its PID once
n = atoi(argv[1]);
if (n <= 0) return 1;            // n=0 prints and stops
if (fork() == 0) {              // the CHILD...
    execlp(argv[0], argv[0], "n-1", NULL);   // re-starts ./pgrm n-1
}
system("./pgrm n-1");          // the PARENT also starts ./pgrm n-1
So each start makes two new starts (one by the child's exec, one by the parent's system). Count the starts by level for ./pgrm 3:
S(3) = 1
S(2) = 2     (two starts at level 2)
S(1) = 4
S(0) = 8     (these just print and stop)
Total = 1 + 2 + 4 + 8 = 15 PIDs
Answer: 15. You did not guess โ€” you counted.
๐ŸŽฏ In the exam โ€” output tracing (2025-06-12 Ex1, 2025-02-17 Ex2, 2024-09-18 Ex3, 2024-02-07 Ex2, 2023-06-15 Ex1, 2024-06-20 Ex2โ€ฆ)
Two shapes: "choose all possible outputs" (think about which order is allowed), and "count how many times X / [3] / a PID prints" (use the count rule). Always check for setbuf(stdout,0) and remember exec does not come back.
โœ— Common mistakes
  • Thinking the lines after a successful exec still run. They do not.
  • Forgetting that system() waits for its command (it does not run in parallel with the parent's next line).
  • Forgetting buffering: with no setbuf, fork can duplicate buffered text.

3The exec family and system() ๐Ÿ”ด comes a lot

What exec does

The six versions โ€” the letters l / v / p / e

LetterMeaning
l (list)you give the arguments one by one in the call, and end with NULL
v (vector)you give the arguments as an array argv[] that ends with NULL (use this when you do not know the number of args at compile time)
p (path)the system searches the program name in the PATH folders, so you can pass just the name
e (environment)you give your own environment array envp[]

The first argument is the program name (it becomes argv[0]). The real arguments come after it.

execlp("cp", "cp", "f1", "f2", NULL);   // OK: 'p' searches PATH, arg0 = "cp"
execl ("/bin/cp", "cp", "f1", "f2", NULL); // OK: full path given
execl ("cp", "f1", "f2", NULL);         // WRONG: 'l' with no 'p' needs a full path
execlp("ls", "-l", NULL);               // WRONG: missing arg0 (should be "ls","ls","-l",NULL)

system()

system("command") is the easy way: inside, it does fork + exec (of a shell) + wait. It runs the command string and comes back when the command is finished.

๐ŸŽฏ In the exam โ€” exec true/false (2025-01-28 Ex13, 2020-09-14 Ex4)
TRUE: exec replaces the code; if you do not know the number of arguments you must use the v-forms (execv/execvp/execve); execlp can search PATH.
โœ— Trap sentences (FALSE)
  • "You should check exec's return value to see if it worked, by the code after it." No โ€” after success there is no code; exec returns only on error.
  • "execl(\"cp\", ...) works." No โ€” without p you need a full path.
  • "exec is built using system()." No โ€” it is the other way around (system uses fork + exec).

4wait, exit, zombie & orphan ๐Ÿ”ด comes a lot

Zombie ๐ŸงŸOrphan ๐Ÿ‘ถ
What happenedthe child finished, but the parent did not call wait yetthe parent died first, while the child is still running
PCBthe child's PCB still exists (it keeps the exit status)the child is alive, so it has a normal PCB
Can it run code?No โ€” a zombie is already deadYes โ€” it keeps running normally
Who fixes itthe parent calls wait โ†’ the PCB is freedinit (PID 1) adopts it; its PPID becomes 1
Endstays a zombie until the parent waitswhen it dies, init reaps it โ†’ it never stays a zombie
๐ŸŽฏ In the exam โ€” zombie/orphan (2024-02-07 Ex5, 2024-06-20 Ex4, 2025-06-12 Ex10, 2025-09-17 Ex3)
TRUE: a zombie has terminated and keeps its PCB until the parent waits; an orphan keeps running and is adopted by init (so it gets a new parent and never becomes a lasting zombie).
โœ— Trap sentences (FALSE)
  • "A zombie can still run code." No โ€” it is dead.
  • "The zombie itself must call wait." No โ€” the parent calls wait.
  • "When adopted, the orphan gets a new PID." No โ€” only its parent (PPID) changes, not its PID.

5Signals ๐Ÿ”ด most exams

A signal is a "software interrupt" โ€” a short, async message sent to a process to tell it that something happened. A signal carries no data, only its type.

Common signals

SignalMeaning
SIGKILL, SIGSTOPcannot be caught, ignored, or blocked โ€” so the system always keeps control
SIGCHLDsent to the parent when a child stops or ends (default: ignore)
SIGINT / SIGTSTPCtrl-C / Ctrl-Z
SIGUSR1 / SIGUSR2free for your own use
SIGALRMsent by alarm() after some seconds
SIGPIPEsent when you write to a pipe with no reader

What a process can do with a signal

Three choices: default action, ignore it (SIG_IGN), or catch it with a handler function.

Sending signals and waiting

Problems with signals (the exam likes these)

๐ŸŽฏ In the exam โ€” signals true/false (2024-09-18 Ex4, 2024-02-27 Ex7, 2024-06-20 Ex9, 2025-01-28 Ex6, 2025-02-17 Ex3)
TRUE: a handler runs when a signal comes; signal(SIGCHLD,SIG_IGN) auto-reaps children (so no zombies, and wait then returns โˆ’1); sleep can be interrupted; handlers must be reentrant; handlers can cause race conditions; both shell kill and the syscall kill() can end a process.
โœ— Trap sentences (FALSE)
  • "With signal() you can ignore any signal." No โ€” SIGKILL and SIGSTOP cannot be ignored.
  • "raise() sends a signal to another process." No โ€” it sends to itself.
  • "Getting a signal always ends the process." No โ€” it depends on the signal and the handler.

6Pipes and IPC ๐Ÿ”ด most exams

Processes can talk in two ways: shared memory (good for big data) or messages (good for small data). The pipe is the oldest message channel. It lives in memory, so it is faster than a file.

How a pipe works

Blocking, EOF, and SIGPIPE (the key rules)

ActionWhat happens
read on an empty pipeblocks (waits) until data comes
read when all write ends are closedreturns 0 = EOF (not an error, not SIGPIPE)
write on a full pipeblocks until there is space (it does not return an error)
write when all read ends are closedthe kernel sends SIGPIPE
write of โ‰ค PIPE_BUF bytesis atomic (PIPE_BUF is 4096 on Linux)
Important difference
PIPE_BUF is the atomic-write limit, not the pipe size. The pipe can hold more bytes than PIPE_BUF; PIPE_BUF only says how many bytes are written in one piece without mixing.

Common bug and tips

๐ŸŽฏ In the exam โ€” pipes true/false (2024-02-07 Ex12, 2024-09-18 Ex5, 2023-09-15 Ex7, 2023-02-03 Ex7, 2025-09-17 Ex4)
TRUE: write on a full pipe blocks; write is atomic up to PIPE_BUF; read on an empty pipe blocks; write with no reader gives SIGPIPE; read with no writer gives EOF (0); a pipe needs a common parent and is made before fork.
โœ— Trap sentences (FALSE)
  • "A read on a pipe is always blocking." No โ€” if all writers are closed it returns EOF.
  • "A write on a full pipe returns an error." No โ€” it blocks.
  • "PIPE_BUF is the maximum size of the pipe." No โ€” it is the atomic-write limit.
  • "A read with no writers causes SIGPIPE." No โ€” that gives EOF; SIGPIPE is for write with no readers.
  • "You can write on both ends at the same time." No โ€” a pipe is one direction.

7One-page trap sheet

PCB / states

  • PCB holds: state, PID, program counter, all registers, open files, scheduling info, signal table.
  • Context switch = save old PCB + load new PCB (not delete). A zombie still has a PCB.

fork / exec / system (output tracing)

  • fork: called once, returns twice โ€” parent gets child PID (>0), child gets 0, error โˆ’1.
  • setbuf(stdout,0) = unbuffered (no duplicated text). Without it, fork can double buffered output.
  • exec replaces the program, same PID, returns only on error; code after a good exec never runs.
  • system() = fork + exec + wait; it waits for the command. Count "fresh starts" with a small formula.
  • exec letters: l = list of args + NULL ยท v = argv array + NULL ยท p = search PATH ยท e = own environment.
  • Without p you must give a full path. arg0 is the program name (argv[0]).

zombie / orphan

  • Zombie = child finished, parent did not wait. Keeps PCB, cannot run code; the parent reaps it.
  • Orphan = parent died first. Still runs; init (PID 1) adopts it; PPID changes (not PID); init reaps it.

signals

  • SIGKILL and SIGSTOP cannot be caught/ignored/blocked. signal() only chooses, it does not send.
  • signal(SIGCHLD,SIG_IGN) = no zombies; then wait returns โˆ’1. raise = signal to self.
  • Signals are not queued (can be lost). Handlers must be reentrant (no printf/malloc). Beware alarm+pause races.

pipes

  • fd[0] = read, fd[1] = write. Make the pipe before fork; close the unused end (or the reader hangs).
  • read empty โ†’ blocks; read with no writers โ†’ EOF (0). write full โ†’ blocks; write with no readers โ†’ SIGPIPE.
  • Atomic up to PIPE_BUF (4096). PIPE_BUF โ‰  pipe size. One writer + one reader is the safe case.