Unit 03 - CPU Scheduling
Unit 03 - CPU Scheduling
Operating System Concepts – 9th Edition 5.2 Silberschatz, Galvin and Gagne
Basic Concepts
Operating System Concepts – 9th Edition 5.3 Silberschatz, Galvin and Gagne
Histogram of CPU-burst Times
Operating System Concepts – 9th Edition 5.4 Silberschatz, Galvin and Gagne
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
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 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 5.5 Silberschatz, Galvin and Gagne
Dispatcher
Operating System Concepts – 9th Edition 5.6 Silberschatz, Galvin and Gagne
Scheduling Criteria
Operating System Concepts – 9th Edition 5.7 Silberschatz, Galvin and Gagne
Scheduling Algorithm Optimization Criteria
Operating System Concepts – 9th Edition 5.8 Silberschatz, Galvin and Gagne
First- Come, First-Served (FCFS) Scheduling
P1 P2 P3
0 24 27 30
Operating System Concepts – 9th Edition 5.9 Silberschatz, Galvin and Gagne
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 5.10 Silberschatz, Galvin and Gagne
Shortest-Job-First (SJF) Scheduling
Operating System Concepts – 9th Edition 5.11 Silberschatz, Galvin and Gagne
Example of SJF
P4 P1 P3 P2
0 3 9 16 24
Operating System Concepts – 9th Edition 5.12 Silberschatz, Galvin and Gagne
Example of Shortest-remaining-time-first (SRTF)
P1 P2 P4 P1 P3
0 1 5 10 17 26
Operating System Concepts – 9th Edition 5.13 Silberschatz, Galvin and Gagne
Priority Scheduling
Operating System Concepts – 9th Edition 5.14 Silberschatz, Galvin and Gagne
Example of Priority Scheduling
Operating System Concepts – 9th Edition 5.15 Silberschatz, Galvin and Gagne
Round Robin (RR)
Operating System Concepts – 9th Edition 5.16 Silberschatz, Galvin and Gagne
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 5.17 Silberschatz, Galvin and Gagne
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 5.18 Silberschatz, Galvin and Gagne
Multilevel Queue Scheduling
Operating System Concepts – 9th Edition 5.19 Silberschatz, Galvin and Gagne
Multilevel Feedback Queue (MFQ)
Operating System Concepts – 9th Edition 5.20 Silberschatz, Galvin and Gagne
Example of MFQ
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 5.21 Silberschatz, Galvin and Gagne
Thread Scheduling
Operating System Concepts – 9th Edition 5.22 Silberschatz, Galvin and Gagne
Multicore Processors
Operating System Concepts – 9th Edition 5.23 Silberschatz, Galvin and Gagne
Multithreaded Multicore System
Operating System Concepts – 9th Edition 5.24 Silberschatz, Galvin and Gagne
Real-Time Operating System (RTOS)
Is a software that manages the computing
resources with guaranteeing all timing
constraints are satisfied.
Operating System Concepts – 9th Edition 5.25 Silberschatz, Galvin and Gagne
RTOS
The RTOS must manage resources like memory,
processor and I/O.
The RTOS will guarantee strict timing constraints and
provide reliable operation.
The RTOS will support synchronization and
communication between tasks.
As complex systems are built the RTOS manages the
integration of components.
Evolution is the notion of a system changing to
improve performance, features and reliability.
Operating System Concepts – 9th Edition 5.26 Silberschatz, Galvin and Gagne
Difference between
Regular OS and Real-time OS
Operating System Concepts – 9th Edition 5.27 Silberschatz, Galvin and Gagne
What does real-time mean?
Means that the embedded system must respond
to critical events within a strictly defined time,
called the deadline.
A guarantee to meet all deadlines can only be
made, if the behaviour of the operating system
can be predicted.
In other words, the timing must be
deterministic.
Operating System Concepts – 9th Edition 5.28 Silberschatz, Galvin and Gagne
Embedded System
A smart device with a processor having a special
and dedicated purpose.
There are five types of software functions the
processor can perform in an embedded system:
1. Mathematical and/or data processing operations.
It can analyse data and make decisions based on the data.
3. Real-time input/output
for the purpose of measurement or control.
Operating System Concepts – 9th Edition 5.29 Silberschatz, Galvin and Gagne
Embedded Systems
Conventional Systems are Transformative Systems taking an
input and generating an output
However, Embedded Systems are Reactive, taking
continuous input and generating continuous outputs
1. Parallel (interaction with physical world)
2. Real-time (guarantees on reaction time)
Operating System Concepts – 9th Edition 5.30 Silberschatz, Galvin and Gagne
Six constraints typify an embedded system
First, they are small size.
For example, many systems must be handheld.
Second, they must have low weight.
If the device is deployed in a system that moves, e.g., attached to a
human, aircraft or vehicle, then weight incurs an energy cost.
Third, they often must be low power.
For example, they might need to operate for a long time on battery power.
Low power also impacts the amount of heat they are allowed to generate.
Fourth, embedded systems often must operate in harsh
environments
such as heat, pressure, vibrations, and shock. They may be subject to
noisy power, RF interference, water, and chemicals.
Fifth, embedded systems are often used in safety critical
systems
Real-time behaviour is essential. For these systems they must function
properly at extremely high levels of reliability.
Lastly, embedded systems are extremely sensitive to cost.
Operating System Concepts – 9th Edition 5.31 Silberschatz, Galvin and Gagne
Real-Time Embedded Systems
Embedded computers are the most prevalent form of
computers in existence.
These devices are found everywhere, from car engines
and manufacturing robots to DVDs and microwave ovens.
They tend to have very specific tasks.
The systems they run on are usually primitive, and so the
operating systems provide limited features.
Usually, they have little or no user interface, preferring to
spend their time monitoring and managing hardware
devices, such as automobile engines and robotic arms.
Operating System Concepts – 9th Edition 5.32 Silberschatz, Galvin and Gagne
Real-Time Embedded Systems
The real-time embedded systems vary considerably:
Some are general-purpose computers, running
standard operating systems—such as Linux—with
special-purpose applications to implement the
functionality.
Others are hardware devices with a special-purpose
embedded operating system providing just the
functionality desired.
Yet others are hardware devices with application
specific integrated circuits (ASICs) that perform their
tasks without an operating system.
Operating System Concepts – 9th Edition 5.33 Silberschatz, Galvin and Gagne
Real-Time Embedded Systems
These embedded systems almost always run real-time
operating systems.
A real-time system is used when rigid time requirements
have been placed on the operation of a processor or the
flow of data; thus, it is often used as a control device in a
dedicated application.
Sensors bring data to the computer.
The computer must analyze the data and possibly adjust
controls to modify the sensor inputs.
Systems that control scientific experiments, medical
imaging systems, industrial control systems, and certain
display systems are real-time systems.
Some automobile-engine fuel-injection systems, home-
appliance controllers, and weapon systems are also real-
time systems.
Operating System Concepts – 9th Edition 5.34 Silberschatz, Galvin and Gagne
Real-Time Embedded Systems
A real-time system has well-defined, fixed time
constraints.
Processing must be done within the defined constraints,
or the system will fail.
For instance, it would not do for a robot arm to be
instructed to halt after it had smashed into the car it was
building.
A real-time system functions correctly only if it returns
the correct result within its time constraints.
Contrast this system with a time-sharing system, where it
is desirable (but not mandatory) to respond quickly, or a
batch system, which may have no time constraints at all.
Operating System Concepts – 9th Edition 5.35 Silberschatz, Galvin and Gagne
Real-Time CPU Scheduling
CPU scheduling for real-time operating systems involves
special issues.
Two major types:
1. Soft real-time systems
Provide no guarantee as to when a critical real-time
process will be scheduled.
They guarantee only that the process will be given
preference over noncritical processes.
2. Hard real-time systems
Have stricter requirements.
A task must be serviced by its deadline; service after
the deadline has expired is the same as no service at
all.
Operating System Concepts – 9th Edition 5.36 Silberschatz, Galvin and Gagne
Real-Time CPU Scheduling
Issues related to process scheduling in both soft and hard
real-time operating systems:
Consider the event-driven nature of a real-time system.
The system is typically waiting for an event in real time
to occur.
Events may arise either in software —as when a timer
expires—or in hardware—as when a remote-controlled
vehicle detects that it is approaching an obstruction.
When an event occurs, the system must respond to and
service it as quickly as possible.
We refer to event latency as the amount of time that
elapses from when an event occurs to when it is
serviced.
Operating System Concepts – 9th Edition 5.37 Silberschatz, Galvin and Gagne
Real-Time CPU Scheduling
Can present obvious
challenges
Two types of latencies
affect performance
1. Interrupt latency – time
from arrival of interrupt
to start of routine that
services interrupt
2. Dispatch latency – time
for schedule to take
current process off CPU
and switch to another
Operating System Concepts – 9th Edition 5.38 Silberschatz, Galvin and Gagne
Real-Time CPU Scheduling
Conflict phase of
dispatch latency:
1. Preemption of
any process
running in
kernel mode
2. Release by
low-priority
process of
resources
needed by
high-priority
processes
Operating System Concepts – 9th Edition 5.39 Silberschatz, Galvin and Gagne
Example 1: Linux Scheduling in Version 2.6.23 +
Completely Fair Scheduler (CFS)
Scheduling classes
Each has specific priority
Scheduler picks highest priority task in highest scheduling class
Rather than quantum based on fixed time allotments, based on
proportion of CPU time
2 scheduling classes included, others can be added: Default and real-
time
Quantum calculated based on nice value from -20 to +19
Lower value is higher priority
Calculates target latency – interval of time during which task should
run at least once
Target latency can increase if say number of active tasks increases
CFS scheduler maintains per task virtual run time in variable
vruntime
Associated with decay factor based on priority of task
lower priority is higher decay rate
Normal default priority yields virtual run time = actual run time
To decide next task to run, scheduler picks task with lowest
virtual run time
Operating System Concepts – 9th Edition 5.40 Silberschatz, Galvin and Gagne
Example 2: Windows Scheduling
Operating System Concepts – 9th Edition 5.41 Silberschatz, Galvin and Gagne