Process Scheduling Algorithm

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Turnaround Time (TT) = Completion time (CT) - Arrival Time (AT)

Or
TT = CT – AT

Waiting Time (WT) = Turnaround Time (TT) - Execute Time (ET)


Or
WT = TAT – ET

1. First Come First Serve (FCFS)

 Jobs are executed on first come, first serve basis.


 Easy to understand and implement.
 Its implementation is based on FIFO queue.
 Poor in performance as average wait time is high.
Example: - Given: Table of processes, and their Arrival time, Execution time
Process Arrival Time Execute Time
P0 0 5
P1 1 3
P2 2 8
P3 3 6

Gantt chart:-

P0 P1 P2 P3

0 5 8 16 22
Process Arrival Execute Completion Turnaround Time Waiting Time (WT) of
Time Time Time (CT) (TT) Each Process
(AT) (ET)
P0 0 5 5 CT – AT = 5-0 = 5 TT – ET = 5-5= 0
P1 1 3 8 CT – AT = 8-1 = 7 TT – ET = 7-3= 4
P2 2 8 16 CT – AT = 16-2 = 14 TT – ET = 14-8 = 6
P3 3 6 22 CT – AT = 22-3 = 19 TT – ET = 19-6= 13

Average Wait Time: (P0+P1+P2+P3) / 4 = (0+4+6+13) / 4 = 5.75


Average Turnaround Time: (P0+P1+P2+P3) / 4 (5+7+14+19) / 4= 45/4 = 11.25
2. Shortest Job First (SJF)

 This is also known as shortest job next, or SJN


 This is a non-preemptive, pre-emptive scheduling algorithm.
 Best approach to minimize waiting time.
 Easy to implement in Batch systems where required CPU time is known in advance.
 Impossible to implement in interactive systems where required CPU time is not known.
 The processer should know in advance how much time process will take.
Example: - Given: Table of processes, and their Arrival time, Execution time

Process Arrival Time Execution Time


P0 0 5
P1 1 3
P2 2 8
P3 3 6

Gantt chart:-
Based On Non-Preemptive Scheduling

P0 P1 P3 P2

0 5 8 14 22

Process Arrival Execute Completion Turnaround Time Waiting Time (WT) of


Time Time Time (CT) (TT) Each Process
(AT) (ET)
P0 0 5 5 CT – AT = 5-0 = 5 TT – ET = 5-5 = 0
P1 1 3 8 CT – AT = 8-1 = 7 TT – ET = 7-3 = 4
P2 2 8 22 CT – AT = 22-2 = 20 TT – ET = 20-8 = 12
P3 3 6 14 CT – AT = 14-3 = 11 TT – ET = 11-6 = 5

Average Wait Time: (0+4+12+5) / 4 = 21/4 = 5.25


Average Turnaround Time: (5+7+20+11) / 4 = 43/4 = 10.75

3. Priority Based Scheduling


 Priority scheduling is a preemptive or non-preemptive algorithm and one of the most
common scheduling algorithms in batch systems.
 Each process is assigned a priority. Process with highest priority is to be executed first and
so on.

 Processes with same priority are executed on first come first served basis.

 Priority can be decided based on memory requirements, time requirements or any other
resource requirement.

Given: Table of processes, and their Arrival time, Execution time, and priority. Here we are
considering Highest number is the highest priority.

Process Arrival Time Execution Time Priority


P0 0 5 1
P1 1 3 2
P2 2 8 1
P3 3 6 3

Gantt chart :- ( Based on preemptive algorithm)

P0 P1 P3 P1 P0 P2

0 1 3 9 10 14 22

Process Arrival Execute Completion Turnaround Time Waiting Time (WT) of


Time Time time (CT) (TT) Each Process
(AT) (ET)
P0 0 5 14 CT – AT = 14 - 0 = 14 TT – ET = 14 - 5 = 9
P1 1 3 10 CT – AT= 10 - 1= 9 TT – ET= 9 - 3 = 6
P2 2 8 22 CT – AT= 22 - 2 = 20 TT – ET= 20 - 8 = 12
P3 3 6 9 CT – AT = 9 - 3 = 6 TT – ET = 6 - 6 = 0

Average Wait Time: (P0+P1+P2+P3) / 4 = (9 + 6 + 12 + 0) / 4 = 21/4 = 6.75


Average Turnaround Time: (P0+P1+P2+P3) / 4 = (14 + 9 + 20 + 6) / 4 = 49/4 = 12.25

4. Shortest Remaining Time (SRT)


 Shortest remaining time (SRT) is the preemptive version of the shortest job next SJN
algorithm.
 The processor is allocated to the job closest to completion but it can be preempted by a newer
ready job with shorter time to completion.
 Impossible to implement in interactive systems where required CPU time is not known.
 It is often used in batch environments where short jobs need to give preference.

5. Round Robin Scheduling

 Round Robin is the preemptive process scheduling algorithm.


 Each process is provided a fix time to execute, it is called a quantum.
 Once a process is executed for a given time period, it is preempted and other process executes
for a given time period.
Context switching is used to save states of preempted processes
Process Arrival Time Execution Time
P0 0 5
P1 1 3
P2 2 8
P3 3 6

Quantum = 3
Gant Chart

P0 P1 P2 P3 P0 P2 P3 P2

0 3 6 9 12 14 17 20 22

Process Arrival Execute Completion Turnaround Time Waiting Time (WT) of


Time Time Time (CT) (TT) Each Process
(AT) (ET)
P0 0 5 14 CT – AT = 14 - 0 = 14 TT – ET = 14 - 5 = 9
P1 1 3 6 CT – AT= 6 – 1 = 5 TT – ET = 5 - 3 = 2
P2 2 8 22 CT – AT= 22 - 2 = 20 TT – ET = 20 - 8 = 12
P3 3 6 20 CT – AT = 20 - 3 = 17 TT – ET = 17 - 6 = 11

Average Wait Time: (P0+P1+P2+P3) / 4 = (9 + 2 + 12 + 11) / 4 = 34/4 = 8.5


Average Turnaround Time: (P0+P1+P2+P3) / 4 = (14 + 5 + 20 + 17) / 4 = 56/4 = 14

You might also like