Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

RACHIT MITTAL

B.Sc Hons. Computer Science


Paper - Operating Systems
Roll number: 19/19015
Year: II Year
Topic: Shortest Job First Scheduling
Algorithm
SJFS
(SHORTEST JOB FIRST
SCHEDULING ALGORITHM)

“NON-
PREEMPTIVE”
This algorithm associates with each
process the length of the process’s
next CPU burst. When the CPU is
available, it is assigned to the process
that has the smallest next CPU burst.
If the next CPU bursts of two
processes are the same, FCFS
scheduling is used to break the tie.
a) When the arrival time of all processes is ‘0’
(all processes arrive at same time)

PID Arrival Time Burst Time

P1 0 6

P2 0 25

P3 0 9

P4 0 4

--> Since the arrival time of all processes is same, we’ll sort the processes
according to the burst time; shortest executing first.

Gantt Chart
P4 P1 P3 P2
0 4 10 19 44
Gantt Chart
P4 P1 P3 P2
0 4 10 19 44

 for P1 : waiting time = 4–0=4


Turnaround Time = 4 + 6 = 10

 for P2 : waiting time = 19 – 0 = 19


Turnaround Time = 19+ 25 = 44

 for P3 : waiting time = 10 – 0 = 10


Turnaround Time = 10 + 9 = 19

 for P4 : waiting time = 0–0=0


Turnaround Time = 0+4=4
---------------------------------------------------------------------------------------------
Average Waiting time : (4 + 19 + 10 + 0) / 4 = 33 / 4 = 8.25

Average Turnaround Time : (10 + 44 + 19 + 4) / 4 = 77 / 4 = 19.25


b) When the arrival time of all processes is different.

PID Arrival Time Burst Time

P1 0 6

P2 1 25

P3 2 9

P4 3 4

--> Since all the processes are already sorted w.r.t arrival time, we’ll sort
the processes according to the burst time; shortest executing first. So, ‘P4’
must have been selected, but at time ‘0’, P4 hasn’t entered the system. So
initially ‘P1’ will have to execute till time ‘6’ and by that time all the
processes have arrived, so next ‘P4’ will be selected and so on.
Gantt Chart
P1 P4 P3 P2
0 6 10 19 44

 for P1 : waiting time = 0–0=0


Turnaround Time = 0+6=6

 for P2 : waiting time = 19 – 1 = 18


Turnaround Time = 18+ 25 = 43

 for P3 : waiting time = 10 – 2 = 8


Turnaround Time = 8 + 9 = 17

 for P4 : waiting time = 6–3=3


Turnaround Time = 3+4=7
---------------------------------------------------------------------------------------------

Average Waiting time : (0 + 18 + 8 + 3) / 4 = 29 / 4 = 7.25

Average Turnaround Time : (6 + 43 + 17 + 7) / 4 = 73 / 4 = 18.25


SRTF
(SHORTEST REMAINING
TIME FIRST
SCHEDULING ALGORITHM)
“PREEMPTIVE

This algorithm is a preemptive version
of SJF Algorithm. Check is done after
every time unit and CPU is assigned to
the process that has the smallest
remaining CPU burst. If the remaining
CPU bursts of two processes are the
same, FCFS scheduling is used to break
the tie, same as in SJF Algorithm.
Let’s solve the algorithm with an example.
PID Arrival Time Burst Time

P1 0 8

P2 1 25

P3 2 9

P4 3 4

The processes are already sorted according to arrival time.

 Here since, at time ‘0’ only P1 is available so it starts execution


and after every time unit it checks whether next process that arrived
has less CPU burst then P1’s remaining burst time. If yes, then
next process starts execution preempting P1 else P1 continues and
repeats the same process.

Since P2 & P3 arrived but have more burst time than P1’s
remaining Burst time, P1 continues till time unit ‘3’ when P4 arrives.
PID Arrival Time Burst Time

P1 0 8

P2 1 25

P3 2 9

P4 3 4

 Now, at time unit ‘3’ :


Remaining burst time of P1 : = 8 – 3 = 5 units
Burst time of P4 : = 4 units

So, process P1 will be preempted and P4 will start execution


and follow the same procedure of checking and comparing the
remaining burst times of all processes thus allocating the CPU
accordingly.

So, the Gantt chart is shown next..


Gantt Chart
P1 P4 P1 P3 P2

0 3 7 12 21 46

 for P1 : waiting time = 0+4–0=4

(for P1, it arrived and started execution at time 0 till 3 time units. But,
it also waited for 4 time units when it got preempted (as P4 as
executing during that time), and started again at time 7, So total
waiting time will be:
Starting time – arrival time + Time waited again = 0 – 0 + 4
= 4 units )

Turnaround Time = 4 + 8 = 12
P1 P4 P1 P3 P2

0 3 7 12 21 46

 for P2 : waiting time = 21 – 1 = 20


Turnaround Time = 20 + 25 = 45

 for P3 : waiting time = 12 – 2 = 10


Turnaround Time = 10 + 9 = 19

 for P4 : waiting time = 3–3=0


Turnaround Time = 0+4=4

---------------------------------------------------------------------------------------------
Average Waiting time : (4 + 20 + 10 + 0) / 4 = 34 / 4 = 8.5

Average Turnaround Time : (12 + 45 + 19 + 4) / 4 = 80 / 4 = 20

The End

You might also like