ch5_CPU Scheduling
ch5_CPU Scheduling
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Outline
Basic Concepts
Scheduling Criteria
Scheduling Algorithms
Operating System Concepts – 10th Edition 5.2 Silberschatz, Galvin and Gagne ©2018
Objectives
To introduce CPU scheduling, which is the basis for multi-
programmed operating systems
Operating System Concepts – 10th Edition 5.3 Silberschatz, Galvin and Gagne ©2018
Basic Concepts
Operating System Concepts – 10th Edition 5.4 Silberschatz, Galvin and Gagne ©2018
Basic Concepts
Operating System Concepts – 10th Edition 5.5 Silberschatz, Galvin and Gagne ©2018
Histogram of CPU-burst Times
Operating System Concepts – 10th Edition 5.6 Silberschatz, Galvin and Gagne ©2018
CPU Scheduler
The CPU scheduler selects from among the processes in ready queue, and
allocates a CPU core to one of them
• Queue may be ordered in various ways
• For situations 1 and 4, there is no choice in terms of scheduling. A new process (if
one exists in the ready queue) must be selected for execution.
• For situations 2 and 3, however, there is a choice (because there are some other
processes waiting).
Operating System Concepts – 10th Edition 5.7 Silberschatz, Galvin and Gagne ©2018
Preemptive and Nonpreemptive Scheduling
o Preemptive scheduling:
● OS can force (preempt) a process from CPU at anytime,
e.g., an interrupt to allocate CPU to another higher priority process
● However, it can result in race conditions (Ch6) when data are shared among several
processes
● Virtually all modern operating systems including Windows, MacOS, Linux, and UNIX
use preemptive scheduling algorithms.
Operating System Concepts – 10th Edition 5.8 Silberschatz, Galvin and Gagne ©2018
Preemptive Scheduling and Race Conditions
Operating System Concepts – 10th Edition 5.9 Silberschatz, Galvin and Gagne ©2018
Dispatcher
Dispatcher module gives control of
the CPU to the process selected by
the CPU scheduler; this involves:
• Switching context
• Switching to user mode
• Jumping to the proper location in
the user program to restart that
program
Operating System Concepts – 10th Edition 5.10 Silberschatz, Galvin and Gagne ©2018
Scheduling Criteria
Operating System Concepts – 10th Edition 5.12 Silberschatz, Galvin and Gagne ©2018
First- Come, First-Served (FCFS) Scheduling
• The simplest algorithm to implement using a FIFO queue.
• With this scheme, the process that requests the CPU first is allocated the CPU first.
• On the negative side, the average waiting time under the FCFS policy is often quite
long.
P1 P2 P3
0 24 27 30
Operating System Concepts – 10th Edition 5.13 Silberschatz, Galvin and Gagne ©2018
1. Example of FCFS Scheduling (Cont.)
P2 P3 P1
0 3 6 30
Convoy effect - short process behind/after long process may lower device
utilization. Consider one CPU-bound and many I/O-bound processes
■ FCFS is non-preemptive
Operating System Concepts – 10th Edition 5.14 Silberschatz, Galvin and Gagne ©2018
2. Example of FCFS
Operating System Concepts – 10th Edition 5.15 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 5.16 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 5.17 Silberschatz, Galvin and Gagne ©2018
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
Two schemes:
Non-Preemptive – once CPU given to the process it cannot be preempted
until completes its CPU burst
Preemptive – if a new process arrives with CPU burst length less than
remaining time of current executing process, preempt. This scheme is
known as the Shortest-Remaining-Time-First (SRTF)
Operating System Concepts – 10th Edition 5.18 Silberschatz, Galvin and Gagne ©2018
1. Example of SJF (Non-Preemptive)
P4 P1 P3 P2
0 3 9 16 24
Operating System Concepts – 10th Edition 5.19 Silberschatz, Galvin and Gagne ©2018
2. Example of Shortest-remaining-time-first
P1 P2 P4 P1 P3
0 1 5 10 17 26
Operating System Concepts – 10th Edition 5.20 Silberschatz, Galvin and Gagne ©2018
3. Example of Non-
Preemptive
Process
SJFArrival Time
P Burst Time
0.
0 7
1
P 2. 4
0 1
2
4. 4
P 0
■ SJF (non- 3 5.
preemptive) P P1 0 P3 P2 P4
40 3 7 8 1
12 6
0.0
P2 4
2.0
P3 1
P1 P2 P4 P1
4.0
P3
0 2 P44P2 7 1 4 1
1 6
5.0
5
■ Average waiting time = ((11-2) + (5-2-2) + (4-4)
■ SJF
+(7-5))/4 = 3
(preemptive)
Shortest-Job-First (SJF) Scheduling
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
Operating System Concepts – 10th Edition 5.23 Silberschatz, Galvin and Gagne ©2018
Round Robin (RR) Algorithm
Especially designed 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.
The ready queue is treated as a circular queue. The CPU scheduler goes
around the ready queue, allocating the CPU to each process for a time interval of
up to 1 time quantum.
Operating System Concepts – 10th Edition 5.24 Silberschatz, Galvin and Gagne ©2018
-Reading
Operating System Concepts – 10th Edition 5.25 Silberschatz, Galvin and Gagne ©2018
1. Example of RR with Time Quantum = 4
Operating System Concepts – 10th Edition 5.26 Silberschatz, Galvin and Gagne ©2018
Time Quantum and Context Switch Time
Operating System Concepts – 10th Edition 5.27 Silberschatz, Galvin and Gagne ©2018
Round Robin (RR) Algorithm
Operating System Concepts – 10th Edition 5.28 Silberschatz, Galvin and Gagne ©2018
Priority Scheduling
A priority number (integer) is associated with each process and the
CPU is allocated to the process with the highest priority (smallest
integer highest priority)
Types:
• Preemptive: it will preempt the CPU if the priority of the newly arrived
process is higher than the priority of the currently running process
• Non-preemptive: it will simply put the new process at the head of the
ready queue.
Operating System Concepts – 10th Edition 5.29 Silberschatz, Galvin and Gagne ©2018
Starvation & Aging
Problem Starvation (indefinite blocking) – low priority processes may
never execute
Operating System Concepts – 10th Edition 5.30 Silberschatz, Galvin and Gagne ©2018
1. Example of Priority Scheduling (Non-preemptive)
Operating System Concepts – 10th Edition 5.31 Silberschatz, Galvin and Gagne ©2018
2. Priority Scheduling w/ Round-Robin
ProcessA arri Burst TimeT Priority
P1 4 3
P2 5 2
P3 8 2
P4 7 1
P5 3 3
Run the process with the highest priority. Processes with the same
priority run round-robin
Operating System Concepts – 10th Edition 5.32 Silberschatz, Galvin and Gagne ©2018
3. Example (Preemptive)
Operating System Concepts – 10th Edition 5.33 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 5.34 Silberschatz, Galvin and Gagne ©2018
Part 2 Outline
Multilevel Queue Scheduling
Multilevel feedback queue Scheduling
Thread Scheduling
Algorithm Evaluation
Operating System Concepts – 10th Edition 5.35 Silberschatz, Galvin and Gagne ©2018
>> Multilevel Queue Scheduling
Another class of scheduling algorithms has been created for situations
in which processes are easily classified into different groups.
Operating System Concepts – 10th Edition 5.36 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue
Prioritization based upon process type
Operating System Concepts – 10th Edition 5.37 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue
With priority scheduling, we have separate queues for each priority.
Every queue has full priority over lower level queues (guaranteed with
preemptive scheduling)
Operating System Concepts – 10th Edition 5.38 Silberschatz, Galvin and Gagne ©2018
>> Multilevel Feedback Queue
A process can move between the various queues.
Operating System Concepts – 10th Edition 5.39 Silberschatz, Galvin and Gagne ©2018
Multilevel Feedback Queue
Multilevel-feedback-queue scheduler is defined by the following
parameters:
• Number of queues
• Scheduling algorithms for each queue
• Method used to determine when to upgrade a process
• Method used to determine when to demote (downgrade) a process
• Method used to determine which queue a process will enter when that
process needs service
Operating System Concepts – 10th Edition 5.40 Silberschatz, Galvin and Gagne ©2018
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 process enters queue Q0 which is
served in RR
When it gains CPU, the process receives 8
milliseconds
If it does not finish in 8 milliseconds, the
process is moved to the tail of queue Q1
• At Q1 job is again served in RR and
receives 16 additional milliseconds
If it still does not complete, it is preempted
and moved to queue Q2
Q1 Gets CPU time only
if Q0 is empty, and so
on
Operating System Concepts – 10th Edition 5.41 Silberschatz, Galvin and Gagne ©2018
Thread Scheduling
When threads supported, threads scheduled, not processes
Distinction between user-level and kernel-level threads:
1. For systems using many-to-one and many-to-many models,
thread library schedules user-level threads to run on lightweight
process (LWP)
This scheme is known as process-contention scope (PCS) since
competition for the CPU takes place among threads belonging to
the same process.
Typically done via priority set by the programmer
Operating System Concepts – 10th Edition 5.42 Silberschatz, Galvin and Gagne ©2018
Thread Scheduling
Operating System Concepts – 10th Edition 5.43 Silberschatz, Galvin and Gagne ©2018
Algorithm Evaluation
How to select CPU-scheduling algorithm for an OS?
Determine criteria, then evaluate algorithms
Deterministic modeling
• Type of analytic evaluation
• Takes a particular predetermined workload and defines
the performance of each algorithm for that workload
Consider 5 processes arriving at time 0:
Operating System Concepts – 10th Edition 5.44 Silberschatz, Galvin and Gagne ©2018
Deterministic Evaluation
For each algorithm, calculate minimum average waiting time
Simple and fast, but requires exact numbers for input, applies
only to those inputs
• FCS is 28ms:
• RR is 23ms:
Operating System Concepts – 10th Edition 5.45 Silberschatz, Galvin and Gagne ©2018
Thanks
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018