Introduction
SJF stands for Shortest Job First. It is a CPU scheduling algorithm that tries to reduce the delay caused when short jobs wait behind long jobs.
Unlike FCFS, SJF does not simply follow arrival order. It checks the burst time of the processes that are currently available and chooses the shortest one first.
Core Rule of SJF Scheduling
In SJF scheduling, the operating system selects the available process with the smallest CPU burst time. CPU burst time means the amount of CPU time a process needs before it finishes or blocks.
The version discussed here is non-preemptive SJF. Once a process starts running, it continues until completion or blocking, even if a shorter process arrives later.
SJF improves over FCFS by giving shorter available jobs a chance to finish earlier. This often reduces average waiting time and average turnaround time.
It can also reduce the convoy effect because short jobs are less likely to remain stuck behind long CPU-bound jobs. However, this benefit depends on knowing or predicting burst times accurately.
SJF Scheduling Example
Consider five processes with arrival time and burst time:
Process | Arrival Time | Burst Time |
|---|---|---|
P1 | 0 | 8 |
P2 | 1 | 4 |
P3 | 2 | 9 |
P4 | 3 | 5 |
P5 | 7 | 2 |
At time 0, only P1 has arrived, so P1 runs first. Since this is non-preemptive SJF, P1 continues until it finishes at time 8.
At time 8, P2, P3, P4, and P5 are available. The scheduler chooses the shortest burst time among them, so the order becomes P5, P2, P4, P3.
SJF Scheduling Example
Process | CPU Time Interval |
|---|---|
P1 | 0 to 8 |
P5 | 8 to 10 |
P2 | 10 to 14 |
P4 | 14 to 19 |
P3 | 19 to 28 |
Metric Calculation
The main formulas are:
CT: finish time of the process
TAT:
CT - ATWT:
TAT - BTRT:
first start time - AT
For example, P2 starts at time 10 and finishes at time 14. Its arrival time is 1 and burst time is 4, so TAT = 14 - 1 = 13, WT = 13 - 4 = 9, and RT = 10 - 1 = 9.
Process | CT | TAT | WT | RT |
|---|---|---|---|---|
P1 | 8 | 8 | 0 | 0 |
P2 | 14 | 13 | 9 | 9 |
P3 | 28 | 26 | 17 | 17 |
P4 | 19 | 16 | 11 | 11 |
P5 | 10 | 3 | 1 | 1 |
Average TAT = 13.2, average WT = 7.6, and average RT = 7.6.
Strengths and Weaknesses of SJF
Strengths | Weaknesses |
|---|---|
Reduces average waiting time | Requires known or predicted burst time |
Reduces average turnaround time | Long processes may starve |
Useful for batch scheduling | Not ideal for interactive systems |
Reduces convoy effect compared to FCFS | Non-preemptive SJF cannot stop a running job |
Summary
SJF scheduling chooses the available process with the smallest CPU burst time. In the non-preemptive version, a selected process keeps the CPU until it finishes or blocks.
It often gives better average waiting time and turnaround time than FCFS, but it depends on burst-time prediction. Its main limitations are possible starvation of long jobs and weaker responsiveness for newly arrived interactive processes.
Be the first to add a comment.