ch6 Cpu Scheduling
ch6 Cpu Scheduling
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 6: CPU Scheduling
Basic Concepts
Scheduling Criteria
Scheduling Algorithms
Operating System Concepts – 9th Edition 6.2 Silberschatz, Galvin and Gagne ©2013
Objectives
To introduce CPU scheduling, which is the basis for multiprogrammed
operating systems
To describe various CPU-scheduling algorithms
To discuss evaluation criteria for selecting a CPU-scheduling algorithm
for a particular system
To examine the scheduling algorithms of several operating systems
Operating System Concepts – 9th Edition 6.3 Silberschatz, Galvin and Gagne ©2013
Basic Concepts
The objective of multiprogramming is to
have some process running all time to
maximize CPU utilization.
Several processes are kept in memory at
one time . When one process has to wait
the OS takes the CPU away from that
process and gives the CPU to another
process.
Scheduling of this kind is the fundamental
function of OS.
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 and so
on. Eventually final CPU burst ends with
terminate system call.
CPU burst distribution is of main
concern
Operating System Concepts – 9th Edition 6.4 Silberschatz, Galvin and Gagne ©2013
CPU Scheduler
Short-term scheduler selects from among the processes in ready queue,
and allocates the CPU to one of them
CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
Scheduling under 1 and 4 is non preemptive or cooperative
All other scheduling is preemptive ( Preemption means that a process may
be forcibly removed from the CPU even if it does not want to release the CPU.
Once a process gets CPU, the simplest approach is to allow the process to
continue using CPU until it voluntarily yields CPU. This is called a non-
preemptive approach.
Operating System Concepts – 9th Edition 6.5 Silberschatz, Galvin and Gagne ©2013
Dispatcher
Dispatcher is the module gives control of the CPU to the process selected by
the short-term scheduler; this involves:
switching context
switching to user mode
jumping to the proper location in the user program to restart that program
The dispatcher should be as fast as possible, since it invokes during every
process switch.
Dispatch latency –The time it takes for the dispatcher to stop one process
and start another running
Operating System Concepts – 9th Edition 6.6 Silberschatz, Galvin and Gagne ©2013
Scheduling Criteria
Many criteria have been suggested for comparing CPU scheduling
algorithms.
CPU utilization – keep the CPU as busy as possible (maximum)
Throughput – # of processes that complete their execution per time unit, for
long process may be one process/hour, ten process /second. (maximum)
Arrival time: time at which process enters in the ready queue or the ready
state.
Burst time: Time required by a process to get execute on CPU.
Completion/finish time: point of time at which a process get completed.
Turnaround time – Interval from the time of submission of a process to the
time of completion. (job time+ waiting time) OR Finish time – Arrival time
Waiting time – amount of time a process has been waiting in the ready
queue (minimum)
W. T = Finish time - burst time – arrival time
Response time – amount of time it takes from when a request was submitted
until the first response is produced, not output (for time-sharing environment)
Operating System Concepts – 9th Edition 6.7 Silberschatz, Galvin and Gagne ©2013
Scheduling Algorithm Optimization Criteria
Operating System Concepts – 9th Edition 6.8 Silberschatz, Galvin and Gagne ©2013
First- Come, First-Served (FCFS) Scheduling
P1 P2 P3
0 24 27 30
Operating System Concepts – 9th Edition 6.9 Silberschatz, Galvin and Gagne ©2013
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order:
P2 , P3 , P1
The Gantt chart for the schedule is:
P2 P3 P1
0 3 6 30
Operating System Concepts – 9th Edition 6.10 Silberschatz, Galvin and Gagne ©2013
FCFS Example 2.
Assume the following processes :
Process A.T Burst Time Finish Wait. Time T.A.T
time
A 0.00 3 3 0 3-0=3
B 1.001 6 9 9-6-1.001=1.999 9-1.001=7.999
C 4.001 4 13 13-4- 13-4.001=8.99
4.001=4.999
D 6.001 2 15 15-2- 15-
6.001=6.999 6.001=8.999
A B C D
0 3 9 13 15
A.W.T= (0+1.999+4.999+6.999)/4=3.499
Throughput= 15/4=3.75time units /job
A.T.A.T= (3+7.999+8.99+8.99)/4=7.249
Operating System Concepts – 9th Edition 6.11 Silberschatz, Galvin and Gagne ©2013
Shortest-Job-First (SJF) Scheduling
Associate with each process the length of its next CPU burst
Use these lengths to schedule the process with the shortest time
SJF is optimal – gives minimum average waiting time for a given set of
processes
Non preemptive scheduling algorithm
If two processes have the same CPU burst then FCFS is used to break tie.
The difficulty is knowing the length of the next CPU request
SJF favors short jobs over long ones. In extreme cases, the constant
arrival of small jobs can lead to starvation of a long job.
Operating System Concepts – 9th Edition 6.12 Silberschatz, Galvin and Gagne ©2013
Example of SJF
ProcessA rBurst Time W.T T.,A.R
P10. 6 3 9 9
P2 2.0 8 16 24 24
P34.0 7 9 16 16
P4 5.0 3 0 3 3
P4 P1 P3 P2
0 3 9 16 24
Average waiting time = (3 + 16 + 9 + 0) / 4 = 7
A.T.A.T = (9+24+16+3)/4=
Throuput=24/4=6
Operating System Concepts – 9th Edition 6.13 Silberschatz, Galvin and Gagne ©2013
SJF Example 2
Process A.T Burst Finish Wait. Time T.A.T
Time time
P1 0 7 7 0 7
P2 2 4 13 7 11
P3 4 2 9 3 5
P4 5 4 17 8 12
P1 P3 P2 P4
0 7 9 13 17
A.W.T=(0+7+3+8)/4=4.5
A.T.A.T=(7+11+5+12)/4=8.75
Operating System Concepts – 9th Edition 6.14 Silberschatz, Galvin and Gagne ©2013
Shortest Remaining Time First
If a new process arrives with a shorter CPU burst than remaining CPU burst
of the currently executing process, it replaces currently executing process.
It is preemptive version of SJF.
Operating System Concepts – 9th Edition 6.15 Silberschatz, Galvin and Gagne ©2013
Example of Shortest-remaining-time-first
Now we add the concepts of varying arrival times and preemption to the analysis
ProcessAarri Arrival TimeT Burst Time W.T T.A.T
P1 0 8-1=7 17-8=9 17-
0=17
P2 1 4-1=3-1=2 5-4-1=0 4
P3 2 9 26-9-2=15 24
P4 3 5 10-5-3=2 7
Preemptive SJF Gantt Chart
P1 P2 P4 P1 P3
0 1 5 10 17 26
Operating System Concepts – 9th Edition 6.16 Silberschatz, Galvin and Gagne ©2013
Example of Shortest-remaining-time-first
Process A.T Burst Finish Wait. Time T.A.T
Time time
P1 0 7-2=5 16 16-7=9 16
P2 2 4-2=2 7 1 5
P3 4 1 5 0 1
P4 5 4 11 2 6
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
Operating System Concepts – 9th Edition 6.17 Silberschatz, Galvin and Gagne ©2013
H.W of Shortest-remaining-time-first
Process A.T Burst Finish Wait. Time T.A.T
Time time
A 0.0 3
B 1.001 6
C 4.001 4
D 6.001 2
Operating System Concepts – 9th Edition 6.18 Silberschatz, Galvin and Gagne ©2013
Priority Scheduling
A priority number (integer) is associated with each process
The CPU is allocated to the process with the highest priority (smallest integer
highest priority)
Preemptive
Non preemptive
SJF is priority scheduling where priority is the inverse of predicted next CPU
burst time, larger the CPU burst the lower the priority and vice versa.
Equal priority processes are scheduled in FCFS order.
Operating System Concepts – 9th Edition 6.19 Silberschatz, Galvin and Gagne ©2013
Example of Priority Scheduling (non Preemptive)
ProcessAarri Burst TimeTPriority W.T
P1 10 3 6
P2 1 1 0
P3 2 4 16
P4 1 5 18
P5 5 2 1
Operating System Concepts – 9th Edition 6.20 Silberschatz, Galvin and Gagne ©2013
Example of Priority Scheduling (Preemptive)
ProcessAarri Arrival Time Burst Time Priority W.T
P1 0 10 3
P2 2 1 1
P3 4 2 2
P4 6 1 4
P5 8 5 5
Operating System Concepts – 9th Edition 6.21 Silberschatz, Galvin and Gagne ©2013
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
Operating System Concepts – 9th Edition 6.22 Silberschatz, Galvin and Gagne ©2013
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
The Gantt chart is:
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Operating System Concepts – 9th Edition 6.23 Silberschatz, Galvin and Gagne ©2013
Time Quantum and Context Switch Time
Operating System Concepts – 9th Edition 6.24 Silberschatz, Galvin and Gagne ©2013
Turnaround Time Varies With The Time Quantum
Operating System Concepts – 9th Edition 6.25 Silberschatz, Galvin and Gagne ©2013
Multilevel Queue
Ready queue is partitioned into separate queues, eg:
foreground (interactive)
background (batch)
Process permanently in a given queue
Each queue has its own scheduling algorithm:
foreground – RR
background – FCFS
Scheduling must be done between the queues:
Fixed priority scheduling; (i.e., serve all from foreground then
from background). Possibility of starvation.
Time slice – each queue gets a certain amount of CPU time
which it can schedule amongst its processes; i.e., 80% to
foreground in RR
20% to background in FCFS
Operating System Concepts – 9th Edition 6.26 Silberschatz, Galvin and Gagne ©2013
Multilevel Queue Scheduling
Operating System Concepts – 9th Edition 6.27 Silberschatz, Galvin and Gagne ©2013
Multilevel Feedback Queue
Operating System Concepts – 9th Edition 6.28 Silberschatz, Galvin and Gagne ©2013
Example of Multilevel Feedback Queue
Three queues:
Q0 – RR with time quantum 8 milliseconds
Q1 – RR time quantum 16 milliseconds
Q2 – FCFS
Scheduling
A new job enters queue Q0 which is
served FCFS
When it gains CPU, job receives 8
milliseconds
If it does not finish in 8 milliseconds,
job is moved to queue Q1
At Q1 job is again served FCFS and
receives 16 additional milliseconds
If it still does not complete, it is
preempted and moved to queue Q2
Operating System Concepts – 9th Edition 6.29 Silberschatz, Galvin and Gagne ©2013