CSC264 ยท 2079 ยท 5 marks
For the following dataset, compute average waiting time for SRTN and SJF. | Process | Arrival Time | Burst Time | |---------|--------------|------------| | P0 | 0 | 7 | | P1 | 2 | 4 | | P2 | 4 | 1 | | P3 | 5 | 4 |
For the following dataset, compute average waiting time for SRTN and SJF.
| Process | Arrival Time | Burst Time |
|---|---|---|
| P0 | 0 | 7 |
| P1 | 2 | 4 |
| P2 | 4 | 1 |
| P3 | 5 | 4 |
[5]
SRTN and SJF Scheduling - Average Waiting Time
Given
| Process | Arrival Time | Burst Time |
|---|---|---|
| P0 | 0 | 7 |
| P1 | 2 | 4 |
| P2 | 4 | 1 |
| P3 | 5 | 4 |
All required data present.
Solution
Part 1: SRTN (Shortest Remaining Time Next) - Preemptive
At each new arrival, compare remaining times and run the smallest.
Execution Trace
- t=0: Only P0. Run P0.
- t=2: P1 arrives (4). P0 remaining = 5. Since 4 < 5, run P1. (P0 rem = 5)
- t=4: P2 arrives (1). P1 remaining = 2. Since 1 < 2, run P2. (P0=5, P1=2)
- t=5: P2 finishes. P3 arrives (4). Ready: P1=2, P3=4, P0=5. Run P1.
- t=7: P1 finishes. Ready: P3=4, P0=5. Run P3.
- t=11: P3 finishes. Run P0 (5 remaining).
- t=16: P0 finishes.
Note: at $t=4$ the remaining time for P1 is $4 - 2 = \mathbf{2}$ units, since P1 ran from $t=2$ to $t=4$.
Gantt Chart
| P0 | P1 | P2 | P1 | P3 | P0 |
0 2 4 5 7 11 16
Waiting Time (WT = Completion - Arrival - Burst)
| Process | Completion | Arrival | Burst | Waiting Time |
|---|---|---|---|---|
| P0 | 16 | 0 | 7 | 16 - 0 - 7 = 9 |
| P1 | 7 | 2 | 4 | 7 - 2 - 4 = 1 |
| P2 | 5 | 4 | 1 | 5 - 4 - 1 = 0 |
| P3 | 11 | 5 | 4 | 11 - 5 - 4 = 2 |
$$\text{Average WT (SRTN)} = \frac{9 + 1 + 0 + 2}{4} = \frac{12}{4} = \boxed{3.0 \text{ ms}}$$
Part 2: SJF (Shortest Job First) - Non-Preemptive
- t=0: Only P0 arrived. Run P0 to completion โ finishes at 7.
- t=7: P1(4), P2(1), P3(4) all arrived. Shortest = P2. Run P2 โ finishes at 8.
- t=8: P1(4), P3(4). Tie broken by arrival โ P1. Finishes at 12.
- t=12: P3 runs โ finishes at 16.
Gantt Chart
| P0 | P2 | P1 | P3 |
0 7 8 12 16
Waiting Time
| Process | Completion | Arrival | Burst | Waiting Time |
|---|---|---|---|---|
| P0 | 7 | 0 | 7 | 0 |
| P1 | 12 | 2 | 4 | 6 |
| P2 | 8 | 4 | 1 | 3 |
| P3 | 16 | 5 | 4 | 7 |
$$\text{Average WT (SJF)} = \frac{0 + 6 + 3 + 7}{4} = \frac{16}{4} = \boxed{4.0 \text{ ms}}$$
Summary
| Algorithm | Average Waiting Time |
|---|---|
| SRTN (Preemptive) | 3.0 ms |
| SJF (Non-Preemptive) | 4.0 ms |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.