Sub topic 18
Worked GATE-Style Problems: Fork Counting and Gantt Charts
This chapter assumes you know what fork, exec and waitpid do, the scheduling criteria (turnaround, waiting and response time), and how FCFS, SJF, SRTF, Round Robin and priority scheduling pick the next process. Here you will turn that knowledge into a repeatable method for the two question types GATE asks most.
Problem type 1: counting processes and output
The rules
fork() returns twice. The parent gets the child's PID (a nonzero value). The child gets 0. A negative value means failure, and GATE questions ignore it. The child starts as a copy of the parent at the point of the call, so it runs only the code after the fork.
| Code pattern | Total processes | Reason | Column | Column |
|---|---|---|---|---|
| n unconditional `fork();` calls in sequence | 2^n | Every call doubles the count | ||
| `for (i=0; i<n; i++) fork();` | 2^n | Children continue the loop with their own `i` | ||
| `for (...) if (fork()==0) break;` | n+1 | Children leave the loop, so only the original parent forks again | ||
| `fork() && fork()` | 3 | The child gets 0 and short-circuits | ||
| `fork() \ | \ | fork()` | 3 | The parent gets nonzero and short-circuits |
New processes are always the total minus one, because the original process already existed. Read the question closely to see which number it wants.
Method
- Number the original process P0.
- Walk down the code. At each
fork, record which process executes it and which new process appears. - For every
ifor&&, decide separately for parent and child whether the branch runs. The child's return value is 0. - Count the processes alive at the line the question asks about.
Worked example
#include <stdio.h>
#include <unistd.h>
int main(void) {
if (fork() && fork())
fork();
printf("x\n");
return 0;
}- P0 runs the first
forkand creates C1. P0 sees a nonzero value, so it evaluates the secondfork. - C1 sees 0 for the first
fork. The&&short-circuits, so C1 never reaches the secondforkor the body. - P0 runs the second
forkand creates C2. P0 sees nonzero, so the condition is true and P0 runs the bodyfork, creating C3. - C2 sees 0 from the second
fork, so the condition is false and C2 skips the body.
Four processes reach printf, so x is printed 4 times.
Problem type 2: Gantt charts
Method
- List the processes with arrival time (AT) and burst time (BT).
- Decide at which instants the scheduler may choose. For non-preemptive algorithms that is when the CPU becomes free. For SRTF and preemptive priority it is every arrival. For Round Robin it is every quantum expiry.
- At each decision, list the ready processes and apply the rule, with a stated tie-break (usually earlier arrival, then lower PID).
- Draw the Gantt chart with segment boundaries.
- Fill a table with completion time (CT), then compute TAT = CT − AT, WT = TAT − BT, and RT = first CPU time − AT.
- Sanity check: the sum of the burst times must equal the total busy time, and the chart must have no overlaps.
Worked example: SRTF
| Process | AT | BT |
|---|---|---|
| P1 | 0 | 8 |
| P2 | 1 | 4 |
| P3 | 2 | 9 |
| P4 | 3 | 5 |
- At t=0 only P1 is ready, so it runs.
- At t=1, P2 arrives with burst 4, and P1 has 7 left. P2 preempts P1.
- P2 has 3 left at t=2 and 2 left at t=3. P3 (9) and P4 (5) do not beat it, so P2 runs until t=5.
- At t=5 the remaining times are P1=7, P3=9, P4=5. P4 runs from 5 to 10.
- P1 runs from 10 to 17, then P3 from 17 to 26.
Gantt chart: | P1 0-1 | P2 1-5 | P4 5-10 | P1 10-17 | P3 17-26 |
| Process | CT | TAT | WT | RT |
|---|---|---|---|---|
| P1 | 17 | 17 | 9 | 0 |
| P2 | 5 | 4 | 0 | 0 |
| P3 | 26 | 24 | 15 | 15 |
| P4 | 10 | 7 | 2 | 2 |
Average waiting time is (9+0+15+2)/4 = 6.5. There are 4 context switches between processes: P1→P2, P2→P4, P4→P1 and P1→P3. Count the one at time 0 or at the end only if the question says so.
You can check a hand solution with a short simulation that advances one time unit at a time.
def srtf(procs):
"""procs: list of (arrival, burst). Returns finish, turnaround, waiting, response."""
n = len(procs)
remaining = [b for _, b in procs]
first = [None] * n
finish = [0] * n
t = done = 0
while done < n:
ready = [i for i in range(n) if procs[i][0] <= t and remaining[i] > 0]
if not ready:
t += 1
continue
i = min(ready, key=lambda k: (remaining[k], procs[k][0], k))
if first[i] is None:
first[i] = t
remaining[i] -= 1
t += 1
if remaining[i] == 0:
finish[i] = t
done += 1
tat = [finish[i] - procs[i][0] for i in range(n)]
wait = [tat[i] - procs[i][1] for i in range(n)]
resp = [first[i] - procs[i][0] for i in range(n)]
return finish, tat, wait, resp
print(srtf([(0, 8), (1, 4), (2, 9), (3, 5)]))
# ([17, 5, 26, 10], [17, 4, 24, 7], [9, 0, 15, 2], [0, 0, 15, 2])Worked example: Round Robin
Take bursts 5, 3 and 8 for P0, P1 and P2, all arriving at 0, with quantum 2. The queue is P0, P1, P2.
Gantt chart: | P0 0-2 | P1 2-4 | P2 4-6 | P0 6-8 | P1 8-9 | P2 9-11 | P0 11-12 | P2 12-16 |
Wait: P0 runs 0-2, P1 2-4, P2 4-6, P0 6-8, P1 8-9, P2 9-11, P0 11-12, P2 12-14, P2 14-16. Merging the last two P2 segments gives the chart above, so the finish times are P0=12, P1=9 and P2=16. Waiting times are 12−5=7, 9−3=6 and 16−8=8. Response times are 0, 2 and 4.
Common errors
- Subtracting the burst time from the completion time without subtracting the arrival time.
- Treating response time as waiting time. They are equal only for non-preemptive algorithms, and in Round Robin only for a process's first stretch.
- Letting the CPU idle when a process is ready, or forgetting to idle when nothing has arrived yet.
- In SRTF, comparing the arriving burst with the running process's original burst instead of its remaining time.
- Mixing the priority convention. Check whether a lower number means higher priority.
Exercises
Exercise
How many times does "hello" print? for (int i = 0; i < 3; i++) { if (fork() == 0) break; } printf("hello\n");
Show answer
4 times. In each iteration the child breaks out of the loop and the parent continues. The parent creates 3 children across the 3 iterations. All 4 processes then reach the print: the original parent and the three children.
Exercise
Non-preemptive SJF. Processes (AT, BT): P1 (0, 7), P2 (2, 4), P3 (4, 1), P4 (5, 4). Break ties by earlier arrival. Give the Gantt chart and the average waiting time.
Show answer
P1 runs 0-7 because nothing else has arrived at t=0. At t=7 the ready processes are P2 (4), P3 (1) and P4 (4). P3 runs 7-8, then P2 runs 8-12 (it arrived before P4), then P4 runs 12-16. The waiting times are P1=0, P2=8−2=6, P3=7−4=3 and P4=12−5=7. The average is 16/4 = 4.
Exercise
Round Robin with quantum 2. P1 (AT 0, BT 4), P2 (AT 1, BT 3). Newly arrived processes join the queue before a preempted process. What is the average turnaround time?
Show answer
P1 runs 0-2. P2 arrived at 1, so the queue is P2, P1. P2 runs 2-4, then P1 runs 4-6 and finishes at 6. P2 runs 6-7 and finishes at 7. TAT is P1 = 6−0 = 6 and P2 = 7−1 = 6. The average is 6.