Preemptive shortest remaining time first — Aptitude Question
Preemptive shortest remaining time first: Four processes arrive at different times: | Process | Arrival time |… Answer and worked solution on CodeKairo.
- Topic: OS, DBMS & Networks
- Section: Programming MCQs
- Difficulty: Hard
- Time target: 180 seconds
Question
Four processes arrive at different times:
| Process | Arrival time | Burst time |
|---|---|---|
| P1 | 0 | 8 |
| P2 | 1 | 4 |
| P3 | 2 | 9 |
| P4 | 3 | 5 |
Under shortest-remaining-time-first (preemptive SJF) scheduling, what is the average waiting time?
Options
- 4.50
- 5.50
- 6.50
- 7.75
Show the answer and the worked solution
Correct answer: C. 6.50
Worked solution
- P1 starts at 0. At time 1, P2 arrives needing 4 while P1 still needs 7, so P2 preempts and runs to completion at time 5.
- P3 arrives at 2 needing 9 and P4 at 3 needing 5; neither beats P2's remaining time, so neither preempts.
- At time 5 the remaining times are P1 = 7, P3 = 9, P4 = 5, so P4 runs from 5 to 10, then P1 from 10 to 17, then P3 from 17 to 26.
- Waiting time is turnaround minus burst: P1 = 17 − 8 = 9, P2 = 4 − 4 = 0, P3 = 24 − 9 = 15, P4 = 7 − 5 = 2.
- The total is 26, so the average is 26 / 4 = 6.50 units.
Answer: 6.50.
The idea
Compute turnaround as completion minus arrival, then subtract the burst — that route survives any number of preemptions, whereas trying to add up idle intervals by hand rarely does.
Shortcut. Total waiting = sum of completions − sum of arrivals − sum of bursts = (5 + 10 + 17 + 26) − 6 − 26 = 26, so the average is 6.50.
Common trap. 7.75 runs the jobs without preemption: P1 keeps the CPU to 8, then P2, P4 and P3 follow, giving waits 0, 7, 9 and 15, which total 31.