Cpu Scheduling
Cpu Scheduling
The currently running process may be interrupted and moved to the ready state by
the operating system.
This is known as preemptive scheduling.
The decision to preempt the process may be performed due to
1) When a new process arrives
2) When an interrupt occurs that blocked process in the ready state
3) Periodically base on a clock interrupt
Dispatcher:
A dispatcher is a module, it connects the CPU to the process selected by short term scheduler.
Functions of Dispatcher:
1) Function of dispatcher is switching, it means switching the CPU from one process to another
process.
2) Jumping to the proper location in the user program to restart the program.
The dispatcher should be fast because it is invoked during each and every process switch.
The time takes by dispatcher to stop one process and start another running process is known as
dispatcher latency.
Types of processor scheduling:
Each time a job terminates, the scheduler may take the decision to add one or more
new jobs.
Medium term scheduling:
If a process request I/o in the middle of the execution the process will removed
from main memory and loaded into waiting queue.
When the I/O operation is completed then job is moved from waiting queue to
ready queue.
1) CPU utilisation:
our main aim is to keep the CPU as busy as possible.
The utilisation of CPU may range from 0 to 100 percent.
In real time loaded system range is from 40% to 90%.
2) Throughput:
Number of process completed per time unit is called throughput.
For long process this rate be one process per hour.
For short transaction might be the ten process per sec.
Turnaround Time:
For a process the important criteria is how long it takes to execute process.
The interval from time of submission of a process to the time completion is
turn around time.
It is the sum of period spends waiting to get into memory, waiting in the ready
queue, executing.
Waiting time:
The amount of time that process spends waiting in the ready queue.
Waiting time is the sum of period spent waiting in the ready queue.
FCFS (First Come First Serve)
Consider the following set of processes. All processes arrived at time 0.
Calculate average waiting time and average turn around time.
P1 24
P2 3
P3 3
P1 p2 p3
0 24 27 30
(p1 + p2 + p3) /3
24 + 27 + 30/3
27ms
Consider the following set of processes. All arrived at time o.
Calculate average waiting time and average turn around time
P1 p2 p3 p4 p5
0 5 29 45 55 58
Process Arrival Burst Start Time Waiting Finishing Turn
Time Time time Time around
time
P1 0 5 0 0 5 5
P2 0 24 5 5 29 29
P3 0 16 29 29 45 45
P4 0 10 45 45 55 55
P5 0 3 55 55 58 58
Waiting time: start time – arrival time
P1 = 0 – 0 = 0
P2= 5 – 0 = 5
P3= 29 – 0 =29
P4 = 45 – 0 = 45
P5 = 55 – 0 =55
Average waiting time: (0 +5+29+45+55)/5 = 26.8 milisec
Turn around time = finishing time – arrival time
P1 = 5 – 0 = 5
P2 = 29 – 0 =29
P3 = 45 – 0 =25
P4 = 55 – 0 = 55
P5 = 58 – 0= 58
Consider the following set of processes.
Calculate average waiting time and average turn around time
P1 P2 P3 P4 P5
0 3 9 13 18 20
Process Arrival time Burst time start time waiting time Finishing time turn A ti
P1 0 3 0 0 3 3
P2 2 6 3 1 9 7
P3 4 4 9 5 13 9
P4 6 5 13 7 18 12
P5 8 2 18 10 20 12
Waiting time= start time – Arrival time
P1 = 0 – 0 =0
P2= 3 – 2 = 1
P3 = 9 – 4 = 5
P4 = 13 – 6 = 7
P5 = 18 – 8 =10
Average Waiting time
(p1+p2+p3+p4+p5)/5
4.6.ms
Turn around time = finishing time – arrival time
P1 = 3 – 0 = 3
P2 = 9 -2 =7
P3 = 13 – 4 = 9
P4 = 18 – 6 =12
P5 = 20 – 8 = 12
SJF algorithm allocates CPU to a process having smallest next cpu burst time.
When CPU is available it is assigned to the process that has the smallest next CPU burst time.
If 2 process having same CPU burst time then processes will be scheduled according to FCFS
P2 P3 P1
0 3 6 30
Process Arrival time Burst Time Start time Waiting time FT TRT
P1 0 24 6 6 30 30
P2 0 3 0 0 3 3
P3 0 3 0 3 6 6
Waiting time = start time – arrival time
P1= 6 – 0 = 6
P2 = 0 – 0 = 0
P3 = 3- 0 = 3
Average waiting time = (p1+p2+p3) /3
= 3ms
Turn around time = F.T – A.T
P1= 30 – 0 =30
P2 = 3 – 0 =3
P3 = 6 – 0 =6
Average turn around time = (p1+p2+p3)/3 = 13ms
Non Pre-emptive algorithm:
P1= 0 – 0 = 0
P2 = 8 – 2 = 6
P3 = 7 – 4 = 3
P4 = 12 – 5 = 7
Average waiting time = (p1+p2+p3+p4)/4
4ms
Turn Around time = finishing time – arrival time
Average turn around time = 8ms
Non Pre-emptive algorithm:
Gantt Chart
P1 P2 P5 P3 P4
0 3 9 11 15 20
Process Arrival time Burst Time Start FT Waiting Turn around
P1 0 3 0 3 0 3
P2 2 6 3 9 1 7
P3 4 11 15 7 11
4
P4 6 20 9 14
5 15
P5 8 2 9 11 1 3
SJF (Pre-emptive)
Consider the following set of processes. Calculate average waiting time and average turn around time.
P1 P2 P3 P3 P4 P1
0 1 2 3 4 5 8
Process Arrival time Burst Time Start FT Waiting Turn around
P1 0 4 0 8 4 8
P2 1 1 1 2 0 1
P3 2 2 4 0 2
2
P4 3 5 1 2
1 4
Waiting Time = Arrival time – start time
P1 = (0-0) + (5-1) =4
P2=1-1 =0
P3= 2 – 2 =0
P4 = 4 - 3 = 1
Average waiting time : (p1+p2+p3+p4)/4
1.25ms
Turn around time : Ft – AT
P1= 8 – 0 = 8
P2 = 2 – 1= 1
P3 = 4 – 2 = 2
P4 = 5 – 3 = 2
SJF ( Pre-emptive)
Consider the following set of processes.
Calculate average waiting time and average turn around time.
Process Arrival Burst Time
time
P1 0 7
P2 2 4
P3 4 1
p4 5 4
Gantt Chart:
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
Process Arrival time Burst Time Start FT Waiting Turn around
P1 0 7 0 16 9 16
P2 2 4 2 7 1 5
P3 4 4 5 0 1
1
P4 5 11 2 6
4 7
Waiting time = start time – arrival time
P1 = (0-0) + (11-2) = 9
P2 = (2-2) + (5-4)= 1
P3 = 4 – 4 =0
P4= 7 – 5 = 2
Avarage Waiting time = (p1+p2+p3+p4)/4
Turn Around time = FT – At
P1 = 16 – 0 = 16
P2 = 7 – 2 = 5
P3 = 5 – 4 = 1
P4 = 11- 5 =6
Priority scheduling:
Priority is assigned to each process and CPU is allocated to the process with highest priority.
Equal priority process are scheduled with FCFS algorithm.
Priorities are the numbers ranging from 0 to 7 given to each process.
2) Aging: A solution to this problem is to gradually increase the priority of a process that wait in the
system for ling time is called Aging.
Priority scheduling are of two types:
1) Pre-emptive scheduling
2) Non Pre-emptive scheduling
Priority ( Non Pre-emptive)
Consider the following set of processes.
Calculate average waiting time and average turn around time.
Process Burst Time Arrival Time Priority
P1 3 1 3(H)
P2 2 2 2
P3 5 0 1
Gantt Chart
P3 P1 P2
0 5 8 10
Process Burst Time Arrival Start time Waiting Finishing Turn
time timr around time
P1 3 1 5 4 8 7
P2 2 2 8 6 10 8
P3 5 0 0 0 5 5
Waiting time = start time – arrival time
P1= 5 – 1 = 4
P2= 8 -2 = 6
P3 = 0 – 0 =0
Average Waiting time = (p1+p2+p3)/3
Gantt Chart:
P2 P5 p1 p3 p4
0 1 6 16 18 19
Process Burst Time Arrival Start time Waiting Finishing Turn
time timr around time
P1 10 0 6 6 16 16
P2 1 0 0 0 1 1
P3 2 0 16 16 18 18
P4 1 0 18 18 19 19
P5 5 0 1 1 6 6
Priority (Pre-emptive)
Consider the following set of processes.
Calculate average waiting time and average turn around time.
Gantt Chart
P3 P1 P2 P3
0 1 4 6 10
Process Burst Time Arrival Start time Waiting Finishing Turn
time time around time
P1 3 1 1 0 4 3
P2 2 2 4 2 6 4
P3 5 0 0 5 10 10
Waiting time = start time – arrival time
P1= 1 – 1 = 0
P2= 4 -2 = 2
P3 = (0 – 0) + (6-1) =5
Average Waiting time = (p1+p2+p3)/3
2.3 ms
Gantt Chart:
P2 P1 p4 p1 p3 P2 P5 P2
0 1 3 7 10 12 13 15 16
Process Arrival time Burst Time Start FT Waiting Turn around
P1 1 5 1 10 4 9
P2 0 3 0 16 2 16
P3 2 10 12 8 10
2
P4 3 7 0 4
4 3
P5 13 2 13 15 0 2
Waiting time = start time – arrival time
P1= (1 – 1) + ( 7 – 3) = 4
P2= (0 -0) + (15-12)-1 = 3-1 =2
P3 = 10-2 = 8
P4= 3 – 3= 0
P5 = 13 – 13= 0
Average Waiting time = (p1+p2+p3)/3
Turn around time = finishing time – arrival time
P1= 10 – 1= 9
P2= 16 – 0 = 16
P3= 12 – 2 = 10
P4= 7 – 3 = 4
P5 = 15 – 13 = 2
Average turn around time = (p1+p2+p3)/3
5.6ms
Priority (Pre-emptive)
Consider the following set of processes.
Calculate average waiting time and average turn around time.
Gantt Chart:
P2 P1 P3 P2 P4
0 1 6 8 13 17
Process Arrival time Burst Time Start FT Waiting Turn around
P1 1 5 1 6 0 5
P2 0 6 0 13 7 13
P3 1 6 8 5 7
2
P4 0 17 13 17
4 13
Waiting time = start time – arrival time
P1= (1 – 1) = 0
P2= (0 -0) + (8-1)= 7
P3 = 6-1 = 5
P4= 13 – 3= 13
Average Waiting time = (p1+p2+p3)/3 = 6.25ms
Turn around time = finishing time – arrival time
P1= 6– 1= 5
P2= 13 – 0 = 13
P3= 8 – 1 = 7
P4= 17 – 0 = 17
Average turn around time = (p1+p2+p3)/3= 10.5ms
Round Robin Method:
Consider the following set of processes. (Time Slice 4)
Calculate average waiting time and average turn around time.
Gantt Chart:
P1 P2 p3 p1 p1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Process Burst Time Arrival Start time Waiting Finishing Turn
time timr around time
P1 24 0 0 6 30 30
P2 3 0 4 4 7 7
P3 3 0 7 7 10 10
Waiting time = start time – arrival time
P1= (0-0) + (10-4) = 6
P2= (4 – 0)
P3 = (7 – 0) = 7
Average Waiting time = (p1+p2+p3)/3 = 5.6 ms
Turn around time = finishing time – arrival time
P1= 30 – 0 = 30
P2= 7 – 0 = 7
P3= 10 – 0 = 10
Average turn around time = (p1+p2+p3)/3 = 15.67ms
Round Robin Method:
Consider the following set of processes. (Time Slice= 3 )
Calculate average waiting time and average turn around time.
P2 P4 p1 p3 p2 P4 P1
0 3 6 9 11 14 15 17
Process BT AT
P1 5 1
P2 3 0
P3 2 2
P4 4 3
P5 2 13
P2 P1 p3 p4 p2 P1 P4 P5 P1
0 2 4 6 8 9 11 13 15 16