0% found this document useful (0 votes)
17 views41 pages

Unit 03 - CPU Scheduling

Uploaded by

Assel Majed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views41 pages

Unit 03 - CPU Scheduling

Uploaded by

Assel Majed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 41

Chapter 5: CPU Scheduling

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne


Chapter 5: CPU Scheduling
 Basic Concepts
 Scheduling Criteria
 Scheduling Algorithms
 Thread Scheduling
 Multiple-Processor Scheduling
 Real-Time CPU Scheduling
 Examples

Operating System Concepts – 9th Edition 5.2 Silberschatz, Galvin and Gagne
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

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

 Dispatcher 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
 Dispatch latency – time it takes for the
dispatcher to stop one process and start another
running

Operating System Concepts – 9th Edition 5.6 Silberschatz, Galvin and Gagne
Scheduling Criteria

 CPU utilization – keep the CPU as busy as possible


 Throughput – # of processes that complete their
execution per time unit
 Turnaround time – amount of time to execute a
particular process
 Waiting time – amount of time a process has been
waiting in the ready queue
 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 5.7 Silberschatz, Galvin and Gagne
Scheduling Algorithm Optimization Criteria

 Max CPU utilization


 Max throughput
 Min turnaround time
 Min waiting time
 Min response time

Operating System Concepts – 9th Edition 5.8 Silberschatz, Galvin and Gagne
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:

P1 P2 P3
0 24 27 30

 Waiting time for P1 = 0; P2 = 24; P3 = 27


 Average waiting time: (0 + 24 + 27)/3 = 17

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

 Waiting time for P1 = 6; P2 = 0; P3 = 3


 Average waiting time: (6 + 0 + 3)/3 = 3
 Much better than previous case
 Convoy effect - short process behind long process
 Consider one CPU-bound and many I/O-bound
processes

Operating System Concepts – 9th Edition 5.10 Silberschatz, Galvin and Gagne
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
 The difficulty is knowing the length of the next
CPU request
 Could ask the user

Operating System Concepts – 9th Edition 5.11 Silberschatz, Galvin and Gagne
Example of SJF

ProcessArriva l 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

 Average waiting time = (3 + 16 + 9 + 0) / 4 = 7


 Preemptive version called shortest-remaining-time-first

Operating System Concepts – 9th Edition 5.12 Silberschatz, Galvin and Gagne
Example of Shortest-remaining-time-first (SRTF)

 Now we add the concepts of varying arrival times and


preemption to the analysis
ProcessAarri Arrival TimeTBurst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
 Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

 Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4


 = 26/4 = 6.5 msec

Operating System Concepts – 9th Edition 5.13 Silberschatz, Galvin and Gagne
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

 Problem  Starvation – low priority processes may


never execute

 Solution  Aging – as time progresses increase the


priority of the process

Operating System Concepts – 9th Edition 5.14 Silberschatz, Galvin and Gagne
Example of Priority Scheduling

ProcessAarri Burst TimeT Priority


P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2

 Priority scheduling Gantt Chart

 Average waiting time = 8.2 msec

Operating System Concepts – 9th Edition 5.15 Silberschatz, Galvin and Gagne
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 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

 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

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)

 A process can move between the various queues;


aging can be implemented this way
 Multilevel-feedback-queue scheduler 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 a
process
 method used to determine which queue a process
will enter when that process needs service

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

 Distinction between user-level and kernel-level threads


 When threads supported, threads scheduled, not
processes
 Many-to-one and many-to-many models, thread library
schedules user-level threads to run on LWP
 Known as process-contention scope (PCS) since
scheduling competition is within the process
 Typically done via priority set by programmer
 Kernel thread scheduled onto available CPU is system-
contention scope (SCS) – competition among all threads
in system

Operating System Concepts – 9th Edition 5.22 Silberschatz, Galvin and Gagne
Multicore Processors

 Recent trend to place multiple processor cores on


same physical chip
 Faster and consumes less power
 Multiple threads per core also growing
 Takes advantage of memory stall to make
progress on another thread while memory
retrieve happens

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.

2. Handling and managing time:


 as an input (e.g., measure period), an output (e.g., output
waveforms), and a means to synchronize tasks (e.g., run 1000
times a second).

3. Real-time input/output
 for the purpose of measurement or control.

4. Digital signal processing (DSP)


 which are mathematical calculations on data streams. Examples
include audio, video, radar, and sonar.

5. Communication and networking

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)

1 Input Transformative 1 Output


System

Continuous Reactive Continuous


Inputs System Outputs

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

Fig. Interrupt Latency

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

Fig. Dispatch Latency

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

 Windows uses priority-based preemptive scheduling


 Highest-priority thread runs next
 Dispatcher is scheduler
 Thread runs until (1) blocks, (2) uses time slice, (3)
preempted by higher-priority thread
 Real-time threads can preempt non-real-time
 32-level priority scheme
 Variable class is 1-15, real-time class is 16-31
 Priority 0 is memory-management thread
 Queue for each priority
 If no run-able thread, runs idle thread

Operating System Concepts – 9th Edition 5.41 Silberschatz, Galvin and Gagne

You might also like