CH 6
CH 6
CH 6
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 6: CPU Scheduling
Basic Concepts
Scheduling Criteria
Scheduling Algorithms
Thread Scheduling
Multiple-Processor Scheduling
Real-Time CPU Scheduling
Operating Systems Examples
Algorithm Evaluation
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 discuss evaluation criteria for selecting a CPU-scheduling algorithm for a particular system
Operating System Concepts – 9th Edition 6.3 Silberschatz, Galvin and Gagne ©2013
Basic Concepts
Operating System Concepts – 9th Edition 6.4 Silberschatz, Galvin and Gagne ©2013
Histogram of CPU-burst Times
Operating System Concepts – 9th Edition 6.5 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
Queue may be ordered in various ways
Operating System Concepts – 9th Edition 6.6 Silberschatz, Galvin and Gagne ©2013
CPU Scheduler
Scheduling under 1 and 4 is nonpreemptive
All other scheduling is preemptive
Consider access to shared data
Consider preemption while in kernel mode
Consider interrupts occurring during crucial OS activities
Operating System Concepts – 9th Edition 6.7 Silberschatz, Galvin and Gagne ©2013
Dispatcher
Operating System Concepts – 9th Edition 6.8 Silberschatz, Galvin and Gagne ©2013
Scheduling Criteria
1. CPU utilization – keep the CPU as busy as possible
4. Waiting time – amount of time a process has been waiting in the ready
queue
Operating System Concepts – 9th Edition 6.9 Silberschatz, Galvin and Gagne ©2013
Scheduling Algorithm Optimization Criteria
Operating System Concepts – 9th Edition 6.10 Silberschatz, Galvin and Gagne ©2013
First-Come, First-Served (FCFS) Scheduling
P1 P2 P3
0 24 27 30
Operating System Concepts – 9th Edition 6.11 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.12 Silberschatz, Galvin and Gagne ©2013
FCFS Scheduling (Cont.)
There is a convoy effect as all the other processes wait for the one big
process to get off the CPU. This effect results in lower CPU and device
utilization than might be possible if the shorter processes were allowed to
go first.
Operating System Concepts – 9th Edition 6.13 Silberschatz, Galvin and Gagne ©2013
Shortest-Job-First (SJF) Scheduling
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.
So this algorithm 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
The difficulty is knowing the length of the next CPU request
Could ask the user
Operating System Concepts – 9th Edition 6.14 Silberschatz, Galvin and Gagne ©2013
Example of SJF
ProcessArrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
SJF scheduling chart
P4 P1 P3 P2
0 3 9 16 24
Operating System Concepts – 9th Edition 6.15 Silberschatz, Galvin and Gagne ©2013
Determining Length of Next CPU Burst
The real difficulty with the SJF algorithm is knowing the length of the next
CPU request
With short-term scheduling, there is no way to know the length of the next
CPU burst but we may be able to predict its value.
We expect that the next CPU burst will be similar in length to the previous
ones. By computing an approximation of the length of the next CPU burst,
we can pick the process with the shortest predicted CPU burst.
Operating System Concepts – 9th Edition 6.16 Silberschatz, Galvin and Gagne ©2013
Shortest-remaining-time-first
Preemptive SJF scheduling is sometimes called shortest-remaining-
time-first scheduling.
Now we add the concepts of varying arrival times and preemption to the
analysis.
ProcessA arri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
P1 P2 P4 P1 P3
0 1 5 10 17 26
Operating System Concepts – 9th Edition 6.17 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
Nonpreemptive
SJF is priority scheduling where priority is the inverse of predicted next CPU
burst time i.e The larger the CPU burst, the lower the priority, and vice versa.
Operating System Concepts – 9th Edition 6.18 Silberschatz, Galvin and Gagne ©2013
Example of Priority Scheduling
ProcessA arri Burst TimeT Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
P2 P5 P1 P3 P4
0 1 6 16 18 19
Operating System Concepts – 9th Edition 6.19 Silberschatz, Galvin and Gagne ©2013
Factors Effecting Priority of a Process
Priorities can be defined either internally or externally.
External priorities: are set by criteria outside the operating system, such as
1. The importance of the process
2. The type and amount of funds being paid for computer use
3. The other, often political, factors.
Operating System Concepts – 9th Edition 6.20 Silberschatz, Galvin and Gagne ©2013
Major Problems Priority scheduling algorithms
Rumor has it that when they shut down the IBM 7094 at MIT in 1973, they
found a low-priority process that had been submitted in 1967 and had not
yet been run.
Needs a solution ?
Operating System Concepts – 9th Edition 6.21 Silberschatz, Galvin and Gagne ©2013
Factors Effecting Priority of a Process
The solution of the indefinite blockage of low-priority processes is aging.
Eventually, even a process with an initial priority of 127 would have the
highest priority in the system and would be executed.
Operating System Concepts – 9th Edition 6.22 Silberschatz, Galvin and Gagne ©2013
Round Robin (RR)
The round-robin (RR) scheduling algorithm is designed especially for
timesharing systems. It is similar to FCFS scheduling, but preemption is
added to enable the system to switch between processes.
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.
Operating System Concepts – 9th Edition 6.23 Silberschatz, Galvin and Gagne ©2013
Round Robin (RR)
One of two things will then happen.
The process may have a CPU burst of less than 1 time quantum. In this case,
the process itself will release the CPU voluntarily.
If the CPU burst of the currently running process is longer than 1 time quantum,
the timer will go off and will cause an interrupt to the operating system. A
context switch will be executed, and the process will be put at the tail of the
ready queue.
Operating System Concepts – 9th Edition 6.24 Silberschatz, Galvin and Gagne ©2013
Example of RR with Time Quantum = 4
Consider the following set of processes that arrive at time 0, with the
length of the CPU burst given in milliseconds:
Process Burst Time
P1 24
P2 3
P3 3
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
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.
Each process must wait no longer than (n − 1) × q time units until its next
time quantum.
Operating System Concepts – 9th Edition 6.27 Silberschatz, Galvin and Gagne ©2013
Turnaround Time Varies With
The Time Quantum
Process Burst Time
P1 10 8 6 4 2 0
P2 10 8 6 4 2 0
P3 10 8 6 4 2 0
P1 P2 P3 P1 P2 P3 P1 P2 P3 P1 P2 P3 P1 P2 P3
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
Operating System Concepts – 9th Edition 6.28 Silberschatz, Galvin and Gagne ©2013
Turnaround Time Varies With
The Time Quantum
Process Burst Time
P1 10
P2 10
P3 10
P1 P2 P3
0 10 20 30
Operating System Concepts – 9th Edition 6.29 Silberschatz, Galvin and Gagne ©2013
Turnaround Time Varies With
The Time Quantum
Operating System Concepts – 9th Edition 6.30 Silberschatz, Galvin and Gagne ©2013
Multilevel Queue
Ready queue is partitioned into separate queues, eg:
foreground (interactive)
background (batch)
Operating System Concepts – 9th Edition 6.31 Silberschatz, Galvin and Gagne ©2013
Multilevel Queue Scheduling
Multilevel queue scheduling algorithm with five queues, listed below
in order of priority:
Operating System Concepts – 9th Edition 6.32 Silberschatz, Galvin and Gagne ©2013
Multilevel Queue
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.33 Silberschatz, Galvin and Gagne ©2013
Multilevel Feedback Queue
A process can move between the various queues; aging can be
implemented this way
Operating System Concepts – 9th Edition 6.34 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
Operating System Concepts – 9th Edition 6.35 Silberschatz, Galvin and Gagne ©2013