Sub topic 13
Shortest Job First and Shortest Remaining Time First
Assume you know process arrival time, CPU burst time, and the formulas for waiting, turnaround, and response time. Also assume that context-switch time is zero unless a question states otherwise.
Shortest Job First
Shortest Job First (SJF) is a nonpreemptive scheduling algorithm. When the CPU becomes free, it chooses the ready process with the smallest CPU burst time. After that process starts, it runs until completion.
For a process:
- Turnaround time = completion time − arrival time
- Waiting time = turnaround time − burst time
- Response time = first start time − arrival time
Consider these processes:
| Process | Arrival time | Burst time |
|---|---|---|
| P1 | 0 | 7 |
| P2 | 2 | 4 |
| P3 | 3 | 1 |
At time 0, only P1 is ready, so SJF starts P1. Because SJF is nonpreemptive, P2 and P3 must wait.
The Gantt chart is:
0 7 8 12
| P1 | P3 | P2 |The completion times are 7, 8, and 12 for P1, P3, and P2 respectively. P3 is selected before P2 because P3 has the shorter burst, but only after P1 finishes.
SJF gives the minimum average waiting time when all jobs are available at the same time and their burst times are known. In practice, the exact future burst is usually unknown, so operating systems estimate it.
Shortest Remaining Time First
Shortest Remaining Time First (SRTF) is the preemptive version of SJF. At every scheduling decision, the CPU runs the ready process with the least remaining burst time. A newly arriving process can preempt the current process.
Use the same processes. At time 0, P1 starts. At time 2, P2 arrives. P1 has 5 units remaining, while P2 needs 4, so P2 preempts P1. At time 3, P3 arrives with burst 1 and preempts P2.
The schedule becomes:
0 2 3 7 12
|P1 |P2| P3 | P1 | P2 |At time 4, P3 finishes. P2 has 3 units remaining, while P1 has 5, so P2 continues until time 7. P1 then finishes at time 12.
Common Errors
- Using original burst time after a process has run. SRTF uses remaining time.
- Forgetting that SJF does not preempt.
- Selecting a process that has not arrived yet.
- Treating a tie as important when the question does not specify a tie rule. Use the stated rule, or preserve arrival order when appropriate.
- Calculating waiting time directly from the chart without subtracting the time spent running.
Exercise
Processes P1, P2, and P3 arrive at time 0 with burst times 6, 2, and 4. What is the SJF order and the average waiting time?
Show answer
The order is P2, P3, P1. Waiting times are 0, 2, and 6, so the average waiting time is (0 + 2 + 6) / 3 = 8/3 time units.