Shortest Job First SJF Algorithm
Shortest Job First SJF Algorithm
First (SJF)
Algorithm
Introduction
to Shortest
Job First
(SJF)
Algorithm
The Shortest Job First (SJF) algorithm is a CPU
scheduling technique that prioritizes jobs or
processes based on their estimated or actual burst
time. It aims to minimize the average waiting time
and turnaround time for processes in a computer
system.
Preemptive SJF Algorithm
1 Step 1
2 Step 2
If the new process has a shorter burst time, the currently
running process is preempted (interrupted) and the new
process is executed.
3 Step 3
P1 0 ms 7 ms P1 (2 ms)
P2 2 ms 4 ms P2 (4 ms)
P3 4 ms 1 ms P3 (5 ms)
P4 5 ms 4 ms P2 (7 ms)
P4 (11 ms)
P1 (16 ms)
now, average waiting time = (completion time- burst time- arrival time)/total no. of processes
Step 1 Step 3
1 2 3
Step 2
If the new process has a
shorter burst time, it is added
to the ready queue but will not
interrupt the currently running
Example of Non-Preemptive SJF
Process Arrival Time Burst Time Execution Order
P1 0 ms 9 ms P1 (9 ms)
P2 3 ms 7 ms P3 (10 ms)
P3 5 ms 1 ms P2 (17 ms)
P4 6 ms 7 ms P4 (24 ms)
now, average waiting time = (completion time- burst time- arrival time)/total no. of processes
Advantages Disadvantages
Simpler to implement, less Increased average waiting time,
overhead, and better for CPU- potential for starvation of longer
bound processes. jobs, and less responsive to
changing system conditions.
Conclusion and Comparison of
Preemptive and Non-Preemptive
SJF
Preemptive SJF Non-Preemptive SJF
Optimizes CPU utilization, reduces average Simpler to implement and better for CPU-
waiting time, and prevents starvation. bound processes.
More complex to implement and may Increased average waiting time and
introduce additional overhead. potential for starvation of longer jobs.
PROJECT
OUTPUT
THANK YOU