2. CPU scheduling
A process execution consist office cycle of CPU execution and input output
execution
Every process begins with CPU burst that may be followed by input output
bus, then another CPU burst and then input output burst and so on
eventually in the last will end up on CPU burst
CPU burst there are those processes which require most of time on CPU
Input output bus which require most of the time on input output devices
for peripherals
A good CPU scheduling idea should choose the mixture of both so that
both input and output devices and CPU can be utilized efficiently
3. Basic Concepts
Maximum CPU utilization obtained with
multiprogramming
CPU–I/O Burst Cycle – Process execution
consists of a cycle of CPU execution and
I/O wait
CPU burst followed by I/O burst
CPU burst distribution is of main concern
For example
o cricket team
o printing document
o addition of two numbers
4. CPU scheduling terminology
Burst time /execution time /running time:- is the time causes required for
running on CPU
Waiting time:- time spent by a process in ready state waiting for CPU
Arrival time:- When a process enters ready state
Turnaround time:- total time spent by a process in the system and around time
equal to {exit time - arrival time} or {burst time + waiting time}
Response time:- Time between a process enters ready queue and get
scheduled on the CPU for the first time
Exit time:- when process completes execution and exit from system
6. CPU scheduling Algorithm
Two approaches
1. Non-Preemptive Scheduling:
2. Preemptive Scheduling:
Non-preemptive
Once resources(CPU Cycle) are allocated to a process, the process holds it till it completes its burst time or switches
to waiting state
Process can not be interrupted until it terminates itself or its time is up.
Rigid, not cost association
Preemptive
In this resources(CPU Cycle) are allocated to a process for a limited time
Process can be interrupted in between.
Flexible and cost associated
7. Scheduling Algorithm Optimization Criteria
Max CPU utilization
Max throughput
Min turnaround time
Min waiting time
Min response time
8. First Come First serve (FCFS)
It assign CPU to the process which arrives first
Easy to understand and can easily be implemented using queue data structure
Allows non-pre-emptive in nature
9. First- Come, First-Served (FCFS) Scheduling
Process Burst Time
P1 24
P2 3
P3 3
Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:
Waiting time for P1 = 0; P2 = 24; P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 17
P P P1 2 3
0 24 3027
A Gantt chart is a horizontal bar chart
developed as a production control tool in
1917 by Henry L. Gantt,
an American engineer and social scientist
10. Solve this
Pid AT BT TAT WT
A 3 4
B 5 3
C 0 2
D 5 1
E 4 3
Create Gantt Chart
Calculate Waiting Time of each and
average waiting time
11. Convoy Effect
Pid AT BT WT AWT
P1 0 100 0
P2 1 1 99
Create Gantt Chart
Pid AT BT WT AWT
P1 1 100 1
P2 0 1 0
Create Gantt Chart
Smaller process have to wait for long time for bigger process to release CPU
12. Advantages
Easy to learn
Easy to understand
Easy to implement
We used for background process where execution is not urgent
13. Disadvantages
Suffer from Convoy effect
Normally long average waiting time
No consideration to burst time or arrival time/Priority
Should not be used for interactive system
14. Shortest job first (non-preemptive) shortest
remaining time first(SRTF) (preemptive)
Out of all available process CPU is assigned to the process having smallest burst time
requirement no priority, no seniority
If there is tie first Come First serve is used to break tie
Can we used both with non primitive and primitive approach
Primitive version STRF is also called as optimal as it currently minimum average waiting
time
15. Example of SJF
Process Arrivall Time Burst Time
P1 1 6
P2 2 8
P3 4 7
P4 0 3
SJF scheduling chart
Average waiting time = (3 + 16 + 9 + 0) / 4 = 7
P3
0 3 24
P4
P1
169
P2
16. SJF (non-preemptive)
Pid AT BT TAT WT
A 3 1
B 1 4
C 4 2
D 0 6
E 2 3
Create Gantt Chart
Calculate Waiting Time of Processes
17. Example of Shortest-remaining-time-first
Now we add the concepts of varying arrival times and preemption to
the analysis
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Preemptive SJF Gantt Chart
Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5
P4
0 1 26
P1
P2
10
P3
P1
5 17
18. SRTF(Pre-emptive)
Pid AT BT TAT WT
A 3 4
B 5 3
C 0 2
D 5 1
E 4 3
Create Gantt Chart
Calculate Waiting Time of Processes
19. Advantages and disadvantages of shortest job
first scheduling algorithm
Advantages
SRTF guarantees minimal average waiting
time
Standard for other algorithm in terms of
average waiting time
Better Average response time compare to
First Come First serve
Disadvantages
Algorithm cannot be implemented as there
is no way to know the burst time of a
process
Process with the longest CPU burst time
requirement will go into starvation
No idea of priority, process with large burst
time have work response time
20. Priority Algorithm
Here a priority is associated with each process
At any instance of time out of all available process, CPU is allocated to the process
which posses the highest Priority
Tie is broken with FCFS order
No importance to arrival time or burst time
Supports both non pre-emptive and pre-emptive version
21. Continue..
A priority number (integer) is associated with each process
The CPU is allocated to the process with the highest priority
Preemptive
Non-preemptive
SJF is priority scheduling where priority is the inverse of predicted
next CPU burst time
Problem Starvation – low priority processes may never execute
Solution Aging – as time progresses increase the priority of the
process
23. Non Pre-emptive
Pid AT BT Priority TAT WT
P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
Create Gantt Chart
Calculate Waiting Time of Processes
24. Pre-emptive
Pid AT BT Priority TAT WT
P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
Create Gantt Chart
Calculate Waiting Time of Processes
25. Advantages and disadvantages of priority
scheduling algorithm
Advantages
Provide a facility of priority specially for
system process
Allows to run important process first even
if it is a user process
Disadvantages
Here process with smaller priority may
starve for CPU
No idea of response time or waiting time
Note aging is a technique of gradually
increasing the priority of process that wait
in the system for long time
26. Round Robin (RR)
Each process gets a small unit of CPU time (time quantum q),
usually 10-100 milliseconds. After this time has elapsed, the
process is preempted and added to the end of the ready queue.
If there are n processes in the ready queue and the time quantum
is q, then each process gets 1/n of the CPU time in chunks of at
most q time units at once. No process waits more than (n-1)q
time units.
Timer interrupts every quantum to schedule next process
Performance
q large FIFO
q small q must be large with respect to context switch, otherwise
overhead is too high
27. Continue.
It is design for time sharing system where it is not necessary to complete one process
and then start another, but to be responsive and divide time of CPU among the
process in the ready state
Here ready q to be treated as circular queue
We will fix the time quantum up to which process can hold the CPU in one go within
which either a process terminates or process must release the CPU and re-enter in the
circular queue and wait for the next chance
Round Robin is called preemptive in nature
28. Example of RR with Time Quantum =
4
Process Burst Time
P1 24
P2 3
P3 3
The Gantt chart is:
Typically, higher average turnaround than SJF, but better response
q should be large compared to context switch time
q usually 10ms to 100ms, context switch < 10 usec
P P P1 1 1
0 18 3026144 7 10 22
P2
P3
P1
P1
P1
29. Pre-emptive
Pid AT BT TAT WT
A 0 5
B 1 3
C 2 1
D 3 2
E 4 3
TQ=2
Create Queue
Create Gantt Chart
Calculate Waiting Time of Processes
30. Advantages and disadvantages of Round Robin
scheduling algorithm
Advantages
Perform best in terms of average response
time
Works well in call to time sharing system,
client server architecture and interactive
system
Kind of shortest job first implementation
Disadvantages
Longer process may starve
Performance depends heavily on time
Quantum
No idea of reality
31. 1. The following is the set of processes whose arrival time, burst time and the priorities are given
below:
S.No. Process Arrival time (in ms) Bunt Time(in ms) Priority
1 P1 0 10 5
2 P2 0 5 2
3 P3 2 3 1
4 P4 5 20 4
5 P5 10 2 3
If Shortest job first ( vith pre-emption) scheduling policy and Priority scheduling policy(with pre-
emption) is used then, what will be the average waiting time in both the cases?
Ops:
A. 5.6 ms, 8 ms
B. None of the mentioned options
C. 6.8 ms, 7.6 ms
D. 0 5.6 ms, 7.6 ms
Question