007 Scheduling
007 Scheduling
Note: Some slides and/or pictures in the following are adapted from slides ©2005
Silberschatz, Galvin, and Gagne. Slides courtesy of Anthony D. Joseph, John
Kubiatowicz, AJ Shankar, George Necula, Alex Aiken, Eric Brewer, Ras Bodik,
Ion Stoica, Doug Tygar, and David Wagner.
CPU Scheduling
• Maximize 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
First-Come, First-Served (FCFS) Scheduling
• First-Come, First-Served (FCFS)
– Also “First In, First Out” (FIFO) or “Run until done”
» In early systems, FCFS meant one program
scheduled until done (including I/O)
» Now, means keep CPU until thread blocks
• Example: Process Burst Time
P1 24
P2 3
P3 3
– Suppose 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
– Average completion time: (24 + 27 + 30)/3 = 27
• Convoy effect: short process behind long process
FCFS Scheduling (Cont.)
• Example continued:
– Suppose that processes arrive in order: P2 , P3 , P1
Now, 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
– 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)
• FCFS Pros and Cons:
– Simple (+)
– Short jobs get stuck behind long ones (-)
» Safeway: Getting milk, always stuck behind cart full of small items
Round Robin (RR)
• FCFS Scheme: Potentially bad for short jobs!
– Depends on submit order
– If you are first in line at supermarket with milk, you don’t care who
is behind you, on the other hand…
• Round Robin Scheme
– Each process gets a small unit of CPU time
(time quantum), usually 10-100 milliseconds
– After quantum expires, the process is preempted
and added to the end of the ready queue
– n processes in ready queue and time quantum is q ⇒
» Each process gets 1/n of the CPU time
» In chunks of at most q time units
» No process waits more than (n-1)q time units
• Performance
– q large ⇒ FCFS
– q small ⇒ Interleaved
– q must be large with respect to context switch, otherwise overhead is
too high (all overhead)
Example of RR with Time Quantum = 20
• Example: Process Burst Time Remaining Time
P1 53 53
P2 8 8
P3 68 68
P4 24 24
– The Gantt chart is:
Example of RR with Time Quantum = 20
• Example: Process Burst Time Remaining Time
P1 53 33
P2 8 8
P3 68 68
P4 24 24
– The Gantt chart is:
P1
0 20
Example of RR with Time Quantum = 20
• Example: Process Burst Time Remaining Time
P1 53 33
P2 8 0
P3 68 68
P4 24 24
– The Gantt chart is:
P1 P2
0 20 28
Example of RR with Time Quantum = 20
• Example: Process Burst Time Remaining Time
P1 53 33
P2 8 0
P3 68 48
P4 24 24
– The Gantt chart is:
P1 P2 P3
0 20 28 48
Example of RR with Time Quantum = 20
• Example: Process Burst Time Remaining Time
P1 53 33
P2 8 0
P3 68 48
P4 24 4
– The Gantt chart is:
P1 P2 P3 P4
0 20 28 48 68
Example of RR with Time Quantum = 20
• Example: Process Burst Time Remaining Time
P1 53 13
P2 8 0
P3 68 48
P4 24 4
– The Gantt chart is:
P1 P2 P3 P4 P1
0 20 28 48 68 88
Example of RR with Time Quantum = 20
• Example: Process Burst Time Remaining Time
P1 53 13
P2 8 0
P3 68 28
P4 24 4
– The Gantt chart is:
P1 P2 P3 P4 P1 P3
0 20 28 48 68 88 108
Example of RR with Time Quantum = 20
• Example: Process Burst Time Remaining Time
P1 53 0
P2 8 0
P3 68 0
P4 24 0
– The Gantt chart is:
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 28 48 68 88 108 112 125 145 153
Wait
Time
Completion
Time
Earlier Example with Different Time Quantum
P3 P1 P4 P2
Worst FCFS:
[68] [53] [24] [8]
0 68 121 145 153
Quantum P1 P2 P3 P4 Average
Best FCFS 32 0 85 8 31¼
Wait
Time
Completion
Time
A or B C
SRTF
C’s C’s
I/O I/O
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 (-)
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.)
– Example:
Exponential averaging
τn = αtn-1+(1-α)τn-1
with (0<α≤1)
Multi-Level Feedback Scheduling
Long-Running
Compute tasks
demoted to
low priority
Response time
» Assuming you’re paying for worse
response time in reduced productivity,
customer angst, etc…
100%
» 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 otherwise
– Argues for buying a faster X when hit “knee” of curve
Summary
• Scheduling: selecting a process from the ready queue and
allocating the CPU to it
• FCFS Scheduling:
– Run threads to completion in order of submission
– Pros: Simple (+)
– Cons: Short jobs get stuck behind long ones (-)
• Round-Robin Scheduling:
– Give each thread a small amount of CPU time when it executes; cycle
between all ready threads
– Pros: Better for short jobs (+)
– Cons: Poor when jobs are same length (-)
Summary (cont’d)
• 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 do
– Pros: Optimal (average response time)
– Cons: Hard to predict future, Unfair
• Lottery Scheduling:
– Give each thread a number of tokens (short tasks ⇒ more tokens)
– Reserve a minimum number of tokens for every thread to ensure
forward progress/fairness