Professional Documents
Culture Documents
Software Engineering
Software Engineering
Software Engineering
CPU scheduling
CPU Scheduler
• A CPU scheduler, running in the dispatcher, is
responsible for selecting of the next running process.
– Based on a particular strategy
• When does CPU scheduling happen?
– Four cases:
• A process switches from the running state to waiting state
(e.g. I/O request)
• A process switches from the running state to the ready state.
• A process switches from waiting state to ready state
(completion of an I/O operation)
• A process terminates
Scheduling Assumptions
• Many implicit assumptions for CPU scheduling:
– One program per user
– One thread per program
– Programs are independent
• Clearly, these are unrealistic but they simplify the problem
so it can be solved
– For instance: is “fair” about fairness among users or programs?
• If I run one compilation job and you run five, you get five times as much
CPU on many operating systems
• The high-level goal: Dole out CPU time to optimize some
desired parameters of system
Time
Scheduling Policy Goals/Criteria
• Minimize Response Time
– Minimize elapsed time to do an operation (or job)
– Response time is what the user sees:
• Time to echo a keystroke in editor
• Time to compile a program
• Real-time Tasks: Must meet deadlines imposed by World
• Maximize Throughput
– Maximize operations (or jobs) per second
– Throughput related to response time, but not identical:
• Minimizing response time will lead to more context switching than if you
only maximized throughput
– Two parts to maximizing throughput
• Minimize overhead (for example, context-switching)
• Efficient use of resources (CPU, disk, memory, etc)
• Fairness
– Share CPU among users in some equitable way
– Fairness is not minimizing average response time:
• Better average response time by making system less fair
Performance metrics for CPU
scheduling
• CPU utilization: percentage of the time that CPU
is busy.
• +Throughput: the number of processes
completed per unit time
• Turnaround time: the interval from the time of
submission of a process to the time of completion.
• Wait time: the sum of the periods spent waiting in
the ready queue
• Response time: the time of submission to the
time the first response is produced
Assumption: CPU Bursts
Weighted toward small bursts
P1 P2 P3
0 3 6 30
– Waiting time for P1 = 6; P2 = 0; P3 = 3
– Average waiting time: (6 + 0 + 3)/3 = 3
– Average Completion time: (3 + 6 + 30)/3 = 13
• In second case:
– average waiting time is much better (before it was 17)
– Average completion time is better (before it was 27)
• FIFO Pros and Cons:
– Simple (+)
– Short jobs get stuck behind long ones (-)
Round Robin
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
turnaround time of C
turnaround time of B
turnaround time of A
A B C Time
FIFO
FIFO vs. Round Robin
• Round Robin
+ Shorter response time
+ Fair sharing of CPU
- Not all jobs are preemptable
- Not good for jobs of the same length
- More precisely, not good in terms of the turnaround
time.
What if we Knew the Future?
• Could we always mirror best FCFS?
• Shortest Job First (SJF):
– Run whatever job has the least amount of
computation to do
– Sometimes called “Shortest Time to
Completion First” (STCF)
• Shortest Remaining Time First (SRTF):
– Preemptive version of SJF: if job arrives and has a shorter time to
completion than the remaining time on the current job, immediately
preempt CPU
– Sometimes called “Shortest Remaining Time to Completion First”
(SRTCF)
• These can be applied either to a whole program or the
current CPU burst of each program
– Idea is to get short jobs out of the system
– Big effect on short jobs, only small effect on long ones
– Result is better average response time
Discussion
• SJF/SRTF are the best you can do at minimizing
average response time
– Provably optimal (SJF among non-preemptive, SRTF
among preemptive)
– Since SRTF is always at least as good as SJF, focus on
SRTF
• Comparison of SRTF with FCFS and RR
– What if all jobs the same length?
• SRTF becomes the same as FCFS (i.e. FCFS is best can do if
all jobs the same length)
– What if jobs have varying length?
• SRTF (and RR): short jobs not stuck behind long ones
Example to illustrate benefits of
A or B
SRTF C
SRTF
C’s C’s
I/O I/O
Predicting the Length of the Next
CPU Burst
• Adaptive: Changing policy based on past behavior
– CPU scheduling, in virtual memory, in file systems, etc
– Works because programs have predictable behavior
• If program was I/O bound in past, likely in future
• If computer behavior were random, wouldn’t help
• Example: SRTF with estimated burst length
– Use an estimator function on previous bursts:
Let tn-1, tn-2, tn-3, etc. be previous CPU burst lengths. Estimate
next burst n = f(tn-1, tn-2, tn-3, …)
– Function f could be one of many different time series
estimation schemes (Kalman filters, etc)
– For instance,
exponential averaging
n = tn-1+(1-)n-1
with (0<1)
SRTF Further discussion
• Starvation
– SRTF can lead to starvation if many small jobs!
– Large jobs never get to run
• Somehow need to predict future
– How can we do this?
– Some systems ask the user
• When you submit a job, have to say how long it will take
• To stop cheating, system kills job if takes too long
– But: Even non-malicious users have trouble predicting runtime of
their jobs
• Bottom line, can’t really know how long job will take
– However, can use SRTF as a yardstick
for measuring other policies
– Optimal, so can’t do any better
• SRTF Pros & Cons
– Optimal (average response time) (+)
– Hard to predict future (-)
– Unfair (-)
Shortest Job First (SJF)
• SJF runs whatever job puts the least demand on
the CPU, also known as STCF (shortest time to
completion first)
+ Provably optimal in terms of turn-around time (anyone
can give an informal proof?).
+ Great for short jobs
+ Small degradation for long jobs
• Real life example: supermarket express
checkouts
SJF Illustrated
turnaround time of C
turnaround time of B
turnaround time of A
wait time of C
wait time of B
wait time of A = 0
response time of C
response time of B
response time of A = 0
A B C Time
Shortest Job First
SJF and SRTF vs. FIFO and
Round Robin
• If all jobs are the same length, SJF
FIFO
– FIFO is the best you can do
• If jobs have varying length
– Short jobs do not get stuck behind long jobs
under SRTF
Drawbacks of Shortest Job First
• Priority 1: A
• Priority 2: B
• Assume that low numbers represent high
priority C A B Time
Priority Scheduling
Priority Scheduling
+ Generalization of SJF
– With SJF, priority = 1/requested_CPU_time
- Starvation
Multilevel Feedback Queues
Time
Multilevel Feedback Queues
time = 1
A Time
Multilevel Feedback Queues
time = 2
A B Time
Multilevel Feedback Queues
time = 3
A B C Time
Multilevel Feedback Queues
time = 3
A B C Time
Multilevel Feedback Queues
time = 3
A B C Time
Multilevel Feedback Queues
time = 5
A B C B Time
Multilevel Feedback Queues
time = 6
A B C B A Time
Multilevel Feedback Queues
time = 8
A B C B A C Time
Multilevel Feedback Queues
time = 9
A B C B A C C Time
Multilevel Feedback Queues
• Approximates SRTF
– A CPU-bound job drops down
– I/O-bound jobs stay near the top
– Still unfair for long running jobs
– Counter-measure: Aging
• Increase the priority of long running jobs if they are
not serviced for a period of time
• Tricky to tune aging
Case study: Windows
• A priority-based, preemptive scheduling
• Highest priority thread will always run
• Also have multiple classes and priorities within classes
• Similar idea for user processes – Multilevel feedback
queue
• Lower priority when quantum runs out
• Increase priority after a wait event
• Some twists to improve “user perceived” performance:
• Boost priority and quantum for foreground process (the window that
is currently selected).
• Boost priority more for a wait on keyboard I/O (as compared to disk
I/O)
Linux scheduling
• A priority-based, preemptive with global
round-robin scheduling
– Each process have a priority
– Processes with a larger priority also have a larger time slices
– Before the time slices is used up, processes are scheduled
based on priority.
– After the time slice of a process is used up, the process must
wait until all ready processes to use up their time slice (or be
blocked) – a round-robin approach.
• No starvation problem.
– For a user process, its priority may + or – 5 depending whether
the process is I/O- bound or CPU-bound.
• Giving I/O bound process higher priority.
Summary for the case study
• Basic idea for schedule user processes is the
same for all systems:
– Lower priority for CPU bound process
– Increase priority for I/O bound process
• The scheduling in Solaris / Linux is more
concerned about fairness.
– More popular as the OSes for servers.
• The scheduling in Window is more concerned
about user perceived performance.
– More popular as the OS for personal computers.