0% found this document useful (0 votes)
222 views16 pages

CPU Scheduling Algorithms: Shortest Job First (SJF)

CPU scheduling algorithms determine the order processes are executed. Shortest Job First (SJF) selects the process with the shortest execution time. In non-preemptive SJF, once a process starts running it continues until completion. Preemptive SJF may interrupt a running process if a shorter one arrives. Both aim to minimize average waiting times by prioritizing shorter jobs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
222 views16 pages

CPU Scheduling Algorithms: Shortest Job First (SJF)

CPU scheduling algorithms determine the order processes are executed. Shortest Job First (SJF) selects the process with the shortest execution time. In non-preemptive SJF, once a process starts running it continues until completion. Preemptive SJF may interrupt a running process if a shorter one arrives. Both aim to minimize average waiting times by prioritizing shorter jobs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

CPU scheduling Algorithms

Shortest Job First (SJF)


What are CPU scheduling algorithms?
● CPU scheduling is the task performed by the CPU that decides the way and
order in which processes should be executed.
● There are various CPU scheduling algorithms, such as First Come First
Serve, Shortest Job First, Round Robin, Priority Scheduling, etc.
● Each process resides in the ready queue and then it is made available to the
CPU for execution.
● Then after, CPU decides which process to execute based on these CPU
scheduling algorithms.
Shortest Job First (SJF)
● Shortest Job First is a scheduling algorithm that selects the process with the
smallest execution time to execute next.
● In case of two processes with the same execution time, FCFS is used.
● This algorithm method is helpful for batch-type processing.
● It can improve process throughput by making sure that shorter jobs are
executed first, hence possibly have a short turnaround time.
● This scheduling method can be preemptive or non-preemptive.
Non-preemptive SJF
● In non-preemptive scheduling, once the CPU cycle is allocated to process,
the process holds it till it reaches a waiting state or terminated.
● Non-preemptive SJF will allow the currently running process to finish its CPU
burst.
● Let's take an example.
Example of Non-preemptive SJF
● Consider the following set of processes, with the length of the CPU burst
given in milliseconds.
Example of Non-preemptive SJF
● The Gantt chart will be:
Example of Non-preemptive SJF
● Waiting time for process P4 is 0 milliseconds.
● Waiting time for process P1 is 3 milliseconds.
● Waiting time for process P3 is 9 milliseconds.
● Waiting time for process P2 is 16 milliseconds.
Average waiting time = waiting time of all the process
Number of processes
Here the average waiting time for the given processes will be:
= 0 + 3 + 9 + 16 = 28 = 7 milliseconds.
4 4
Preemptive SJF
● A preemptive scheduling algorithm will execute different process in the middle
of the execution of the current process.
● In Preemptive SJF Scheduling, jobs are put into the ready queue as they
come.
● A process with shortest burst time begins execution.
● If a process with even a shorter burst time or execution time arrives, the
current process is preempted (halted) from execution, and the new shorter job
is allocated the CPU.
● Preemptive SJF scheduling is also called as shortest-remaining-time-first
scheduling.
● Let's take an example.
Example of Preemptive SJF
● Consider the following four processes, with the length of the CPU burst given
in milliseconds.
Example of Preemptive SJF
● The Gantt chart will be:
Example of Preemptive SJF
● Process P1 is started at time 0, since it is the only process in the queue.
● Process P2 arrives at time 1. The remaining time for process P1 (7
milliseconds) is larger than the time required by process P2 (4 milliseconds),
so process P1 is preempted (halted), and process P2 is scheduled.
● After the execution of process P2, now process P4 is executed as it has the
execution time of 5 milliseconds which is less than the remaining time for
process P1 (7 milliseconds).
● Now the process P1 will be executed as it’s execution time is less than the
process P3(9 milliseconds).
● And lastly, the only remaining process P3 is executed.
Example of Preemptive SJF
● To calculate the waiting time, we need to subtract the arrival time of the
process from the time when it begins the execution.
● Waiting time for Process P1 is (10 - 0 - 1) i.e. 9 milliseconds.
● Waiting time for Process P2 is (1 - 1) i.e. 0 milliseconds.
● Waiting time for Process P3 is (17 - 2) i.e. 15 milliseconds.
● Waiting time for Process P4 is (5 - 3) i.e. 2 milliseconds.
● Average waiting time is = 9 + 0 + 15 + 2 = 26 = 6.5 milliseconds.

4 4
Advantages of SJF
● It provides maximum throughput time.
● It is optimal as compared to FCFS.
● It provides minimum average waiting and turnaround time as compared to
FCFS.
● It is appropriate for jobs running in batch, where run times are known in
advance.
Disadvantages of SJF
● Complex to implement as we need to know the next process with shortest
execution time.
● This algorithm may cause very long turnaround times or starvation for long
processes.
● Requires knowledge of how long a process or job will run.
Study Resources & References for SJF
● https://www.guru99.com/shortest-job-first-sjf-scheduling.html#1
● https://www.gatevidyalay.com/sjf-scheduling-srtf-cpu-scheduling/
● https://www.youtube.com/watch?v=VCIVXPoiLpU
● https://www.youtube.com/watch?v=t0g9b3SJECg
Thank You

You might also like