SlideShare a Scribd company logo
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edit9on
Chapter 6: CPU
Scheduling
6.2 Modified By Dr. Khaled Wassif
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
6.3 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
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
6.4 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
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
6.5 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Histogram of CPU-burst Times
6.6 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
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
 Ready 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, while in kernel mode,
and interrupts occurring during critical OS activities
6.7 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
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
6.8 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
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)
6.9 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Scheduling Algorithm Optimization Criteria
 Max CPU utilization
 Max throughput
 Min turnaround time
 Min waiting time
 Min response time
6.10 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
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:
 Waiting time for P1 = 0; P2 = 24; P3 = 27
 Average waiting time: (0 + 24 + 27)/3 = 17
P1 P2 P3
24 27 30
0
6.11 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
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:
 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
P1
P3
P2
6
3 30
0
6.12 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
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
 The difficulty is knowing the length of the next
CPU request
 Could ask the user
6.13 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Example of SJF
Process Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
 SJF scheduling chart
 Average waiting time = (3 + 16 + 9 + 0) / 4 = 7
P4 P3
P1
3 16
0 9
P2
24
6.14 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Determining Length of
Next CPU Burst
 Can only estimate the length – should be
similar to the previous one
 Then pick process with shortest predicted next
CPU burst
 Can be done by using the length of previous
CPU bursts, using exponential averaging
 Commonly, α set to ½
:
define
1,
0
,
for
Then
burst
CPU
next
for the
value
predicted
and
burst
CPU
of
length
actual
Let
1







n
th
n n
t
  .
1
1 n
n
n t 


 



6.15 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Prediction of the Length of the
Next CPU Burst
6.16 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Examples of Exponential Averaging
  =0
 n+1 = n
 Recent history does not count
  =1
 n+1 = tn
 Only the actual last CPU burst counts
 If we expand the formula, we get:
n+1 =  tn+(1 - ) tn -1 + …+(1 -  )j  tn -j + … +(1 -  )n +1 0
 Since both  and (1 - ) are less than or equal
to 1, each successive term has less weight
than its predecessor
6.17 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
 Associate with each process the length of its
next CPU burst and 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.
 Two schemes:
 Nonpreemptive – 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 know as the
Shortest-Remaining-Time-First (SRTF).
Shortest-Job-First (SJF)
Scheduling
6.18 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
 Now we add the concepts of varying arrival times
 Non-preemptive SJF Gantt Chart
 Average waiting time = (0 + 6 + 3 + 7)/4 = 4
Example of Non-Preemptive SJF
P1 P3 P2
7 16
0
P4
8 12
6.19 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Example of Preemptive SJF (SRTF)
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
 Preemptive SJF Gantt Chart
 Average waiting time = (9 + 1 + 0 +2)/4 = 3
P1 P3
P2
4
2 11
0
P4
5 7
P2 P1
16
6.20 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Example of SRTF
ProcessA Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
 SRTF Gantt Chart
 Average waiting time = (9+0+15+2)/4 = 6.5
P1
P1
P2
1 17
0 10
P3
26
5
P4
6.21 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Priority Scheduling
 A priority number (integer) is associated with
each process
 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
 Problem  Starvation – low priority processes
may never execute
 Solution  Aging – as time progresses increase
the priority of the process
6.22 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Example of Priority Scheduling
ProcessAa Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
 Priority scheduling Gantt Chart:
 Average waiting time = (6+0+16+18+1)/5 = 8.2
P2 P3
P5
1 18
0 16
P4
19
6
P1
6.23 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
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 q to schedule next process
 Performance
 q large  FIFO
 q small  q must be large with respect to context
switch, otherwise overhead is too high
6.24 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Example of RR (Time Quantum = 4)
Process Burst Time
P1 24
P2 3
P3 3
 The Gantt chart is:
 Average waiting time = (6+4+7)/3 = 5.66
 Typically, higher average turnaround than SJF,
but better response
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
6.25 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Process Burst Time
P1 53
P2 17
P3 68
P4 24
 The Gantt chart is:
 Average waiting time = (81+20+86+97)/4 = 71
Example of RR (Time Quantum = 20)
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 37 57 77 97 117 121 134 154 162
6.26 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Time Quantum and Context Switch Time
6.27 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Turnaround Time Varies With
The Time Quantum
80% of CPU bursts
should be shorter than q
6.28 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
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
6.29 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Multilevel Queue Scheduling
6.30 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Multilevel Feedback Queue
 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
6.31 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
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
6.32 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Example of
Multilevel Feedback Queue
6.37 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Multiple-Processor Scheduling
 With multiple CPUs, load sharing becomes
possible but CPU scheduling more complex
 Homogeneous processors
 Can use any processor to run any process in the ready queue
 Asymmetric multiprocessing
 Only one master processor accesses system data structures
and other processors execute only user code
 Symmetric multiprocessing (SMP)
 Each processor is self-scheduling, all processes in common
ready queue, or each has its own private ready queue
 Currently, most common
 Processor affinity – process has affinity for processor
on which it is currently running
 soft affinity or hard affinity
 Variations including processor sets
6.38 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
NUMA and CPU Scheduling
Note that memory-placement
algorithms can also consider affinity
6.39 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Multiple-Processor Scheduling –
Load Balancing
 On SMP, need to keep workload balanced among
all CPUs to fully utilize benefits of multiprocessors
 Load balancing attempts to keep workload
evenly distributed across all processors
 Necessary only when each processor has its own
private queue of ready processes
 Push migration – a task periodically checks
load on each processor, and if found imbalance
pushes task from overloaded CPU to other CPUs
 Pull migration – idle processors pulls waiting
task from busy processor
6.65 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Algorithm Evaluation
 How to select CPU-scheduling algorithm for a
particular OS?
 there are many scheduling algorithms, each with its
own parameters.
 As a result, selecting an algorithm can be difficult.
 Determine criteria, then evaluate algorithms
 Various evaluation methods we can use:
 Deterministic modeling
 Queueing models
 Simulations
 Implementation
6.66 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Deterministic Evaluation
 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:
6.67 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Deterministic Evaluation
 For each algorithm, calculate minimum average
waiting time
 FCS is 28ms:
 Non-preemptive SFJ is 13ms:
 RR is 23ms:
 Simple and fast, but requires exact numbers for
input, and its answers apply only to those cases
6.68 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Queueing Models
 Normally, there is no static set of processes to
use for deterministic modeling
 However, the distribution of CPU and I/O bursts
can be measured and then simply estimated
 Describes the arrival of processes, and CPU and I/O
bursts probabilistically
 Commonly exponential, and described by mean
 Computes verage throughput, utilization, waiting time
 Computer system described as network of
servers, each with queue of waiting processes
 Knowing arrival rates and service rates
 Computes utilization, average queue length, average
wait time, etc
6.69 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Queueing Models
 Little’s Formula:
 n = average queue length
 W = average waiting time in queue
 λ = average arrival rate into queue
 Little’s law – in steady state, processes leaving
queue must equal processes arriving, thus:
n = λ x W
 Valid for any scheduling algorithm and arrival
distribution
 For example, if on average 7 processes arrive per
second, and normally 14 processes in queue, then
average wait time per process = 2 seconds
6.70 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Simulations
 Queueing analysis is useful but has limitations
 Mathematics of complex algorithms and distributions
can be difficult to work with
 Thus, arrival and service distributions are defined in
mathematically tractable ways
 Simulations more accurate
 Programmed model of computer system
 Data structures represent components of the system
 Clock is a variable
 Gather statistics indicating algorithm performance
 Data to drive simulation gathered via
 Random number generator according to probabilities
 Distributions defined mathematically or empirically
 Trace tapes record sequences of real events in real systems
6.71 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Evaluation of CPU Schedulers
by Simulation
6.72 Modified By Dr. Khaled Wassif
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Implementation
 Simulation can be expensive and limited accuracy
 A more detailed simulation provides more accurate
results, but it also takes more computer time.
 Only completely accurate way to implement new
scheduler and test in real systems
 Cost of coding the algorithm and risk of users reaction
 Changing the environment in which algorithm is used
 Most flexible scheduling algorithms that can be
altered by system managers and tuned for a
specific set of applications
 Use APIs that can modify priority of a process or thread
 But again environments vary
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edit9on
End of Chapter 6

More Related Content

PPT
ch6.ppt operating System batch Processing
PPT
ch6.ppt
PPT
ch6 (2).ppt operating system by williamm
PPT
ch6.ppt
PPT
ch6.ppt
PPT
Various CPU Scheduling Algorithms in OS.ppt
PPT
scheduling.ppt
PPT
ch6.ppt operating System batch Processing
ch6.ppt
ch6 (2).ppt operating system by williamm
ch6.ppt
ch6.ppt
Various CPU Scheduling Algorithms in OS.ppt
scheduling.ppt

Similar to W-ch06.pdfcentral processing unit scheduling (20)

PDF
cloud computing chapter one in computer science
PDF
operating system in computer science .pdf
PDF
operating system in computer science ch05.pdf
PDF
6 cpu scheduling
PPTX
Operating System - CPU Scheduling Introduction
PPT
Cpu scheduling (1)
PPT
CPU Scheduling
PPTX
CPU SCHEDULINGCPU SCHEDULINGCPU SCHEDULINGCPU SCHEDULING.pptx
PPT
Process scheduling : operating system ( Btech cse )
PPT
cpu scheduling.ppt
PPT
ch6_CPU Scheduling.ppt
PPTX
cpu scheduling
PDF
ch5_EN_CPUSched_2022.pdf
PDF
CPU scheduling ppt file
PPTX
ch5.pptx CUP Scheduling and its details in OS
PPTX
Chapter Five, operating Systems ,Information And Technology
PPTX
OS-CPU-Scheduling-chap5.pptx
PPTX
Operating systems chapter 5 silberschatz
PPT
ch5jnkhhkjhjkhhkhkjhuyuyiuyiyiuyuiyuyu-1.ppt
cloud computing chapter one in computer science
operating system in computer science .pdf
operating system in computer science ch05.pdf
6 cpu scheduling
Operating System - CPU Scheduling Introduction
Cpu scheduling (1)
CPU Scheduling
CPU SCHEDULINGCPU SCHEDULINGCPU SCHEDULINGCPU SCHEDULING.pptx
Process scheduling : operating system ( Btech cse )
cpu scheduling.ppt
ch6_CPU Scheduling.ppt
cpu scheduling
ch5_EN_CPUSched_2022.pdf
CPU scheduling ppt file
ch5.pptx CUP Scheduling and its details in OS
Chapter Five, operating Systems ,Information And Technology
OS-CPU-Scheduling-chap5.pptx
Operating systems chapter 5 silberschatz
ch5jnkhhkjhjkhhkhkjhuyuyiuyiyiuyuiyuyu-1.ppt
Ad

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Big Data Technologies - Introduction.pptx
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPT
Teaching material agriculture food technology
PPTX
Spectroscopy.pptx food analysis technology
Agricultural_Statistics_at_a_Glance_2022_0.pdf
The AUB Centre for AI in Media Proposal.docx
“AI and Expert System Decision Support & Business Intelligence Systems”
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Reach Out and Touch Someone: Haptics and Empathic Computing
sap open course for s4hana steps from ECC to s4
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
The Rise and Fall of 3GPP – Time for a Sabbatical?
20250228 LYD VKU AI Blended-Learning.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Spectral efficient network and resource selection model in 5G networks
Big Data Technologies - Introduction.pptx
Empathic Computing: Creating Shared Understanding
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Teaching material agriculture food technology
Spectroscopy.pptx food analysis technology
Ad

W-ch06.pdfcentral processing unit scheduling

  • 1. Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edit9on Chapter 6: CPU Scheduling
  • 2. 6.2 Modified By Dr. Khaled Wassif 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
  • 3. 6.3 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition 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
  • 4. 6.4 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 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
  • 5. 6.5 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Histogram of CPU-burst Times
  • 6. 6.6 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition 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  Ready 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, while in kernel mode, and interrupts occurring during critical OS activities
  • 7. 6.7 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 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
  • 8. 6.8 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 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)
  • 9. 6.9 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Scheduling Algorithm Optimization Criteria  Max CPU utilization  Max throughput  Min turnaround time  Min waiting time  Min response time
  • 10. 6.10 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 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:  Waiting time for P1 = 0; P2 = 24; P3 = 27  Average waiting time: (0 + 24 + 27)/3 = 17 P1 P2 P3 24 27 30 0
  • 11. 6.11 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition 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:  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 P1 P3 P2 6 3 30 0
  • 12. 6.12 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition 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  The difficulty is knowing the length of the next CPU request  Could ask the user
  • 13. 6.13 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Example of SJF Process Burst Time P1 0.0 6 P2 2.0 8 P3 4.0 7 P4 5.0 3  SJF scheduling chart  Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 P4 P3 P1 3 16 0 9 P2 24
  • 14. 6.14 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Determining Length of Next CPU Burst  Can only estimate the length – should be similar to the previous one  Then pick process with shortest predicted next CPU burst  Can be done by using the length of previous CPU bursts, using exponential averaging  Commonly, α set to ½ : define 1, 0 , for Then burst CPU next for the value predicted and burst CPU of length actual Let 1        n th n n t   . 1 1 n n n t        
  • 15. 6.15 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Prediction of the Length of the Next CPU Burst
  • 16. 6.16 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Examples of Exponential Averaging   =0  n+1 = n  Recent history does not count   =1  n+1 = tn  Only the actual last CPU burst counts  If we expand the formula, we get: n+1 =  tn+(1 - ) tn -1 + …+(1 -  )j  tn -j + … +(1 -  )n +1 0  Since both  and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor
  • 17. 6.17 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013  Associate with each process the length of its next CPU burst and 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.  Two schemes:  Nonpreemptive – 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 know as the Shortest-Remaining-Time-First (SRTF). Shortest-Job-First (SJF) Scheduling
  • 18. 6.18 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Process Arrival Time Burst Time P1 0 7 P2 2 4 P3 4 1 P4 5 4  Now we add the concepts of varying arrival times  Non-preemptive SJF Gantt Chart  Average waiting time = (0 + 6 + 3 + 7)/4 = 4 Example of Non-Preemptive SJF P1 P3 P2 7 16 0 P4 8 12
  • 19. 6.19 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Example of Preemptive SJF (SRTF) Process Arrival Time Burst Time P1 0 7 P2 2 4 P3 4 1 P4 5 4  Preemptive SJF Gantt Chart  Average waiting time = (9 + 1 + 0 +2)/4 = 3 P1 P3 P2 4 2 11 0 P4 5 7 P2 P1 16
  • 20. 6.20 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Example of SRTF ProcessA Arrival TimeT Burst Time P1 0 8 P2 1 4 P3 2 9 P4 3 5  SRTF Gantt Chart  Average waiting time = (9+0+15+2)/4 = 6.5 P1 P1 P2 1 17 0 10 P3 26 5 P4
  • 21. 6.21 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Priority Scheduling  A priority number (integer) is associated with each process  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  Problem  Starvation – low priority processes may never execute  Solution  Aging – as time progresses increase the priority of the process
  • 22. 6.22 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Example of Priority Scheduling ProcessAa Burst Time Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2  Priority scheduling Gantt Chart:  Average waiting time = (6+0+16+18+1)/5 = 8.2 P2 P3 P5 1 18 0 16 P4 19 6 P1
  • 23. 6.23 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition 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 q to schedule next process  Performance  q large  FIFO  q small  q must be large with respect to context switch, otherwise overhead is too high
  • 24. 6.24 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Example of RR (Time Quantum = 4) Process Burst Time P1 24 P2 3 P3 3  The Gantt chart is:  Average waiting time = (6+4+7)/3 = 5.66  Typically, higher average turnaround than SJF, but better response P1 P2 P3 P1 P1 P1 P1 P1 0 4 7 10 14 18 22 26 30
  • 25. 6.25 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Process Burst Time P1 53 P2 17 P3 68 P4 24  The Gantt chart is:  Average waiting time = (81+20+86+97)/4 = 71 Example of RR (Time Quantum = 20) P1 P2 P3 P4 P1 P3 P4 P1 P3 P3 0 20 37 57 77 97 117 121 134 154 162
  • 26. 6.26 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Time Quantum and Context Switch Time
  • 27. 6.27 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Turnaround Time Varies With The Time Quantum 80% of CPU bursts should be shorter than q
  • 28. 6.28 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition 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
  • 29. 6.29 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Multilevel Queue Scheduling
  • 30. 6.30 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Multilevel Feedback Queue  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
  • 31. 6.31 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition 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
  • 32. 6.32 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Example of Multilevel Feedback Queue
  • 33. 6.37 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Multiple-Processor Scheduling  With multiple CPUs, load sharing becomes possible but CPU scheduling more complex  Homogeneous processors  Can use any processor to run any process in the ready queue  Asymmetric multiprocessing  Only one master processor accesses system data structures and other processors execute only user code  Symmetric multiprocessing (SMP)  Each processor is self-scheduling, all processes in common ready queue, or each has its own private ready queue  Currently, most common  Processor affinity – process has affinity for processor on which it is currently running  soft affinity or hard affinity  Variations including processor sets
  • 34. 6.38 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 NUMA and CPU Scheduling Note that memory-placement algorithms can also consider affinity
  • 35. 6.39 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Multiple-Processor Scheduling – Load Balancing  On SMP, need to keep workload balanced among all CPUs to fully utilize benefits of multiprocessors  Load balancing attempts to keep workload evenly distributed across all processors  Necessary only when each processor has its own private queue of ready processes  Push migration – a task periodically checks load on each processor, and if found imbalance pushes task from overloaded CPU to other CPUs  Pull migration – idle processors pulls waiting task from busy processor
  • 36. 6.65 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Algorithm Evaluation  How to select CPU-scheduling algorithm for a particular OS?  there are many scheduling algorithms, each with its own parameters.  As a result, selecting an algorithm can be difficult.  Determine criteria, then evaluate algorithms  Various evaluation methods we can use:  Deterministic modeling  Queueing models  Simulations  Implementation
  • 37. 6.66 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Deterministic Evaluation  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:
  • 38. 6.67 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Deterministic Evaluation  For each algorithm, calculate minimum average waiting time  FCS is 28ms:  Non-preemptive SFJ is 13ms:  RR is 23ms:  Simple and fast, but requires exact numbers for input, and its answers apply only to those cases
  • 39. 6.68 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Queueing Models  Normally, there is no static set of processes to use for deterministic modeling  However, the distribution of CPU and I/O bursts can be measured and then simply estimated  Describes the arrival of processes, and CPU and I/O bursts probabilistically  Commonly exponential, and described by mean  Computes verage throughput, utilization, waiting time  Computer system described as network of servers, each with queue of waiting processes  Knowing arrival rates and service rates  Computes utilization, average queue length, average wait time, etc
  • 40. 6.69 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Queueing Models  Little’s Formula:  n = average queue length  W = average waiting time in queue  λ = average arrival rate into queue  Little’s law – in steady state, processes leaving queue must equal processes arriving, thus: n = λ x W  Valid for any scheduling algorithm and arrival distribution  For example, if on average 7 processes arrive per second, and normally 14 processes in queue, then average wait time per process = 2 seconds
  • 41. 6.70 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Simulations  Queueing analysis is useful but has limitations  Mathematics of complex algorithms and distributions can be difficult to work with  Thus, arrival and service distributions are defined in mathematically tractable ways  Simulations more accurate  Programmed model of computer system  Data structures represent components of the system  Clock is a variable  Gather statistics indicating algorithm performance  Data to drive simulation gathered via  Random number generator according to probabilities  Distributions defined mathematically or empirically  Trace tapes record sequences of real events in real systems
  • 42. 6.71 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Evaluation of CPU Schedulers by Simulation
  • 43. 6.72 Modified By Dr. Khaled Wassif Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Implementation  Simulation can be expensive and limited accuracy  A more detailed simulation provides more accurate results, but it also takes more computer time.  Only completely accurate way to implement new scheduler and test in real systems  Cost of coding the algorithm and risk of users reaction  Changing the environment in which algorithm is used  Most flexible scheduling algorithms that can be altered by system managers and tuned for a specific set of applications  Use APIs that can modify priority of a process or thread  But again environments vary
  • 44. Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edit9on End of Chapter 6