Lect10 424 002
Lect10 424 002
Lect10 424 002
Scheduling (con’t)
February 25th, 202
Prof. John Kubiatowic
http://cs162.eecs.Berkeley.edu
Recall: Scheduling
if ( readyThreads(TCBs) )
nextTCB = selectThread(TCBs)
run( nextTCB )
} else
run_idle_thread()
}
• Discussion of Scheduling:
– Which thread should run on the CPU next
• Scheduling goals, policie
• Look at a number of different schedulers
:
2
Comparisons between FCFS and Round Robin
• Assuming zero-cost context-switching time, is RR always better than
FCFS
• Simple example: 10 jobs, each take 100s of CPU time
RR scheduler quantum of 1s
All jobs start at the same tim
• Completion Times Job # FIFO RR
1 100 991
2 200 992
… … …
9 900 999
– Both RR and FCFS finish at10the same1000
time 1000
– Average response time is much worse under RR!
» Bad when all jobs same lengt
• Also: Cache state must be shared between all jobs with RR but can be
devoted to each job with FIF
– Total time for RR longer even for zero-cost switch!
2/25/2020 Kubiatowicz CS162 ©UCB Fall 2020 5
?
e
Earlier Example with Different Time Quantum
P2 P4 P1 P3
Best FCFS: [8] [24] [53] [68]
0 8 32 85 153
Quantum P1 P2 P3 P4 Average
Best FCFS 32 0 85 8 31¼
Q=1 84 22 85 57 62
Q=5 82 20 85 58 61¼
Wai
Q=8 80 8 85 56 57¼
Time
Q = 10 82 10 85 68 61¼
Q = 20 72 20 85 88 66¼
Worst FCFS 68 145 0 121 83½
Best FCFS 85 8 153 32 69½
Q=1 137 30 153 81 100½
Q=5 135 28 153 82 99½
Completion
Q=8 133 16 153 80 95½
Time
Q = 10 135 18 153 92 99½
Q = 20 125 28 153 112 104½
Worst FCFS 121 153 68 145 121¾
2/25/2020 Kubiatowicz CS162 ©UCB Fall 2020 6
t
• Execution Pla
– Always execute highest-priority runable jobs to completio
– Each queue can be processed in RR with some time-quantu
• Problems
– Starvation:
» Lower priority jobs don’t get to run because higher priority job
– Deadlock: Priority Inversio
» Not strictly a problem with priority scheduling, but happens when low priority task has
lock needed by high-priority tas
» Usually involves third, intermediate priority task that keeps running even though high-
priority task should be runnin
• How to fix problems
– Dynamic priorities – adjust base-level priority up or down based on heuristics
about interactivity, locking, burst behavior, etc…
2/25/2020 Kubiatowicz CS162 ©UCB Fall 2020 7
:
Scheduling Fairness
• What about fairness
– Strict fixed-priority scheduling between queues is unfair (run
highest, then next, etc):
» long running jobs may never get CPU
» Urban legend: In Multics, shut down machine, found 10-year-
old job ⇒ Ok, probably not
– Must give long-running jobs a fraction of the CPU even when
there are shorter jobs to run
– Tradeoff: fairness gained by hurting avg response time!
Scheduling Fairness
• How to implement fairness
– Could give each queue some fraction of the CPU
» What if one long-running job and 100 short-running ones?
» Like express lanes in a supermarket—sometimes express
lanes get so long, get better service by going into one of the
other lines
– Could increase priority of jobs that don’t get service
» What is done in some variants of UNIX
» This is ad hoc—what rate should you increase priorities?
» And, as system gets overloaded, no job gets CPU time, so
everyone increases in priority⇒Interactive jobs suffer
Lottery Scheduling
• Yet another alternative: Lottery Schedulin
– Give each job some number of lottery tickets
– On each time slice, randomly pick a winning ticket
» NOTE: Not a “real” random number generator; instead
pseudo-random number generators can make sure that every
ticket picked once before repeating
– On average, CPU time is proportional to number of tickets
given to each job
• How to assign tickets
– To help with responsiveness, give short running jobs more
tickets, long running jobs get fewer ticket
– To avoid starvation, every job gets at least one ticket
(everyone makes progress)
• Advantage over strict priority scheduling: behaves gracefully as
load change
– Adding or deleting a job affects all jobs proportionally,
independent of how many tickets each job possesses
2/25/2020 Kubiatowicz CS162 ©UCB Fall 2020 10
s
How to Evaluate a Scheduling algorithm?
• Deterministic modelin
– takes a predetermined workload and compute the performance
of each algorithm for that workload
• Queueing model
– Mathematical approach for handling stochastic workloads
• Implementation/Simulation
– Build system which allows actual algorithms to be run against
actual data – most flexible/general
m
Discussion
• SJF/SRTF are the best you can do at minimizing average response
tim
– Provably optimal (SJF among non-preemptive, SRTF among
preemptive
– Since SRTF is always at least as good as SJF, focus on SRT
O
SRTF
C’s C’s
I/O I/O
2/25/2020 Kubiatowicz CS162 ©UCB Fall 2020 17
g
e
r
Multi-Level Feedback Scheduling
Long-Running Compute
Tasks Demoted to
Low Priority
Scheduling Details
Long-Running Compute
Tasks Demoted to
Low Priority
Scheduling Details
Long-Running Compute
Tasks Demoted to
Low Priority
Case Study: Linux O(1) Scheduler
Kernel/Realtime Tasks User Tasks
0 100 139
• Priority-based scheduler: 140 prioritie
– 40 for “user tasks” (set by “nice”), 100 for “Realtime/Kernel
– Lower priority value ⇒ higher priority (for nice values
– Highest priority value ⇒ Lower priority (for realtime values
– All algorithms O(1
» Timeslices/priorities/interactivity credits all computed when job finishes time slic
» 140-bit bit mask indicates presence or absence of job at given priority leve
• Two separate priority queues: “active” and “expired
– All tasks in the active queue use up their timeslices and get placed on the
expired queue, after which queues swappe
• Timeslice depends on priority – linearly mapped onto timeslice rang
– Like a multi-level queue (one queue per priority) with different timeslice at
each leve
– Execution split into “Timeslice Granularity” chunks – round robin through
priority
:
Time
T2 = (5,2)
T3 = (7,2)
0 5 10 15
∑( )
Schedulable when ≤1
𝑖
•
𝑖
𝑃
=1
𝑖
𝑖
𝑖
𝑖
𝑖
𝑖
𝐶
𝑃
𝐶
𝑖
2/25/2020 Kubiatowicz CS162 ©UCB Fall 2020 29
𝐷
𝐷
𝑃
𝑛
𝑡
𝑡
e
Respons
time
» Perhaps you’re paying for worse response
time in reduced productivity, customer angst,
100%
etc
» Might think that you should buy a faster X
when X is utilized 100%, but usually, response
time goes to infinity as utilization⇒100%
Utilization
• An interesting implication of this curve
– Most scheduling algorithms work fine in the “linear” portion of the load
curve, fail otherwis
– Argues for buying a faster X when hit “knee” of curve
:
?
Summary (1 of 2)
• Scheduling Goals
– Minimize Response Time (e.g. for human interaction
– Maximize Throughput (e.g. for large computations
– Fairness (e.g. Proper Sharing of Resources
– Predictability (e.g. Hard/Soft Realtime)
• Round-Robin Scheduling:
– Give each thread a small amount of CPU time when it executes; cycle
between all ready thread
– Pros: Better for short jobs
• Shortest Job First (SJF)/Shortest Remaining Time First (SRTF)
– Run whatever job has the least amount of computation to do/least
remaining amount of computation to d
– Pros: Optimal (average response time)
– Cons: Hard to predict future, Unfai
• Multi-Level Feedback Scheduling
– Multiple queues of different priorities and scheduling algorithm
– Automatic promotion/demotion of process priority in order to
approximate SJF/SRTF
Summary (2 of 2)
• Lottery Scheduling
– Give each thread a priority-dependent number of tokens (short
tasks⇒more tokens
• Linux CFS Scheduler: Fair fraction of CP
– Approximates a “ideal” multitasking processo
• Realtime Schedulers such as ED
– Guaranteed behavior by meeting deadline
– Realtime tasks defined by tuple of compute time and perio
– Schedulability test: is it possible to meet deadlines with proposed set of
processes?