0 · Recall Warm-up
Answer from memory first. Answers are in the Answer Vault at the bottom.
- Pre-emption moves a process from Running to? from Lesson 1.1.1
- Is fork a system call? from Lesson 0.1.3
- 2⁴? from Lesson 0.1.1
1 · The Hook
In UNIX, every process (except the very first) is created by copying an existing one with fork(). The copy starts life at the same line as its parent, so a few forks inside a loop multiply processes surprisingly fast. "How many times is hello printed?" is one of the most common GATE OS questions, and it has a simple, reliable method.
2 · Predict First
Try the questions first. Open when you are ready to check.
This program runs: fork(); fork(); printf("hi\n");. How many times is "hi" printed?
Make a guess in your head. The answer opens Section 3.
3 · Core Concept
Predict First — the answer
The first fork makes 2 processes; each of them runs the second fork, making 4. All 4 print: 4 times.
In plain words
fork() makes an almost exact copy of the calling process. Both copies continue from the line after the fork. The only difference is the value fork returns: 0 in the child, and the child's ID (a positive number) in the parent. Each fork doubles the number of processes that execute it.
1. fork(), exec(), wait(), exit()
- fork(): creates a child that is a copy of the parent (same code, a copy of data, heap and stack, same open files). Returns 0 to the child, the child's PID (> 0) to the parent, −1 on failure.
- exec(): replaces the current process's program with a new one (same PID; the old code is gone).
- wait(): the parent pauses until a child exits.
- exit(): the process ends.
- After fork, parent and child have separate memory: a change to a variable in one is invisible to the other.
2. Counting processes and prints
- n forks executed one after another (or in a loop of n iterations): 2ⁿ processes in all, 2ⁿ − 1 children created.
- A print after the forks runs once per process.
- A print inside the loop runs once per process alive at that iteration:
for (i = 0; i < 3; i++) { fork(); printf("x"); }prints 2 + 4 + 8 = 14. - Conditions split the processes.
if (fork() == 0) fork(); printf("y");→ only the child forks again → 3 processes, 3 prints. - Short-circuit operators: in
fork() && fork() || fork(), a process where the first fork returned 0 skips the second fork. Draw the tree: 5 processes.
3. Zombies and orphans
- Zombie: the child has exited but the parent has not yet called wait(), so its PCB entry remains.
- Orphan: the parent exited first; the child is adopted by the init process.
4. Context switch
Switching the CPU from one process to another: save the old process's state into its PCB and load the new one's. It is pure overhead (no useful work), typically microseconds.
5. The three schedulers
| Scheduler | Decides | How often |
|---|---|---|
| Long-term (job) | which new jobs are admitted to memory; controls the degree of multiprogramming | rarely |
| Short-term (CPU) | which ready process gets the CPU next | very often (milliseconds) |
| Medium-term | which processes to swap out and back in | in between |
A good mix of I/O-bound processes (short CPU bursts, lots of waiting) and CPU-bound processes (long CPU bursts) keeps both the CPU and the devices busy.
4 · Visual
fork(); fork(); — after two forks there are 4 processes (the original and 3 children); a printf after them runs 4 times.
5 · Worked Examples
Example 1 Easy
for (i = 0; i < 4; i++) fork(); How many processes exist at the end (including the original)?
Solution
2⁴ = 16 (15 children).
Example 2 Medium
int x = 5; if (fork() == 0) x = x + 5; else x = x − 5; printf("%d", x); What is printed?
Solution
Two lines: 10 (child) and 0 (parent), in either order. Each has its own copy of x.
Example 3 GATE
printf("start"); fork(); printf("A"); fork(); printf("B"); How many lines are printed in total?
Solution
"start" once, "A" twice, "B" four times: 7.
6 · GATE Traps
- fork returns 0 in the child, not in the parent.
- Variables are copied, not shared, after fork.
- n forks → 2ⁿ processes but 2ⁿ − 1 children; read which one is asked.
- Prints inside a loop are counted per iteration (2 + 4 + 8 …), not once at the end.
7 · Memory Hook
"Each fork doubles whoever reaches it; child gets 0."
8 · Connect the Dots
Chapter 1.1 is complete. Copying a whole process is expensive. Threads let one process run several tasks that share its memory. They come next, together with the ways separate processes talk to each other (IPC).
9 · Self-Test
GATE format. Try every question before opening the vault.
Q1 · NAT · 1 mark(s) — Loop
for (i = 0; i < 5; i++) fork(); How many child processes are created?
Numerical answer: ________
Q2 · NAT · 2 mark(s) — Prints in a loop
for (i = 0; i < 3; i++) { fork(); printf("x"); } How many x's are printed in total?
Numerical answer: ________
Q3 · NAT · 2 mark(s) — Conditions
if (fork() && fork()) fork(); How many processes exist in total (including the original)?
Numerical answer: ________
Q4 · NAT · 2 mark(s) — Short circuit
fork() && fork() || fork(); How many processes exist in total (including the original)?
Numerical answer: ________
Q5 · MCQ · 1 mark(s) — Schedulers
Which scheduler controls the degree of multiprogramming?
- (A) Long-term
- (B) Short-term
- (C) Dispatcher
- (D) Medium-term
10 · Rule Card
- fork: child is a copy; returns 0 in child, child PID in parent; memory is copied, not shared.
- n sequential forks → 2ⁿ processes, 2ⁿ − 1 children; prints after forks happen once per process; inside a loop, add per iteration.
- exec replaces the program; wait waits for a child; zombie = exited, not waited for; orphan = parent gone.
- Context switch = save + load PCB (overhead).
- Long-term (admission, degree of multiprogramming), short-term (CPU), medium-term (swapping).
11 · Concept Ledger
Unlocked in this lesson. From here on, these terms may be used.
forkreturn values of forkexecwaitexitcopied address spacecounting processes and printsfork treesshort-circuit evaluation with forkzombie processorphan processcontext switchlong-term, short-term and medium-term schedulersdispatcherI/O-bound vs CPU-bound
12 · Answer Vault
Try the questions first. Open when you are ready to check.
🔐 Open the Answer Vault
Recall Warm-up
- Ready.
- Yes.
-
Self-Test
Q1 Answer: 31 · 1 mark(s)
2⁵ − 1.
Re-read: Lesson 1.1.2 — Creating processes with fork(), exec() and wait(); context switches and schedulers
Q2 Answer: 14 · 2 mark(s)
2 + 4 + 8.
Re-read: Lesson 1.1.2 — Creating processes with fork(), exec() and wait(); context switches and schedulers
Q3 Answer: 4 · 2 mark(s)
Only the original passes both conditions and forks a third time.
Re-read: Lesson 1.1.2 — Creating processes with fork(), exec() and wait(); context switches and schedulers
Q4 Answer: 5 · 2 mark(s)
Draw the tree: 2 from the first fork, the parent's && adds 1, the two zeros each run the third fork.
Re-read: Lesson 1.1.2 — Creating processes with fork(), exec() and wait(); context switches and schedulers
Q5 Answer: (A) · 1 mark(s)
It admits new jobs into memory.
- (B) is wrong: Picks from those already ready.
- (C) is wrong: Performs the switch the short-term scheduler chose.
- (D) is wrong: Swaps, but admission is long-term.
Re-read: Lesson 1.1.2 — Creating processes with fork(), exec() and wait(); context switches and schedulers