Shortest Job
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
Arrival of a new process: The system checks if the new
process has a shorter burst time than the currently
running process.
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
The preempted process is added back to the ready
queue and will be executed when it becomes the
shortest job available.
Example of Preemptive SJF
Process Arrival Time Burst Time Execution Order
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
average waiting time = ((16-7-0)+(7-4-2)+(5-1-4)+(11-4-5))/4 = (9+1+0+2)/4 = 3
Advantages of Preemptive SJF
Fairness Improved Reduced
Efficiency Starvation
Shorter jobs are
prioritized, Preemptive SJF Preemption allows
ensuring a fair optimizes the use shorter jobs to be
of CPU resources executed
distribution of
by executing the
CPU time and immediately,
shortest job first,
preventing longer reducing the reducing the risk
jobs from average waiting of longer jobs
time and starving and
monopolizing the
turnaround time.
system waiting
indefinitely.
Non-Preemptive SJF Algorithm
Step 1 Step 3
Arrival of a new process: The The currently running
system checks the burst time process will complete its
of the new process and the execution before the new,
currently running process. shorter process is scheduled.
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
average waiting time = ((9-9-0)+(17-7-3)+(10-1-5)+(24-7-6))/4 = (0+7+4+11)/4 = 11/2 = 5.5
Advantages and
Disadvantages of Non-
Preemptive SJF
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