Académique Documents
Professionnel Documents
Culture Documents
CPSC 457
CPU scheduling
Fall 2017
Contains slides from Mea Wang, Andrew Tanenbaum and Herbert Bos
1
CPSC 457
Outline
● objectives of scheduling
○ cpu-bound vs io-bound processes
○ when to schedule
○ preemptive vs non-preemptive scheduling
○ categories of CPU scheduling algorithms
□ batch, interactive, real-time
○ metrics
● FCFS, RR, SJF, SRTN
2
CPSC 457
CPU scheduling
● the software that decides which process runs next is called a scheduler
○ usually part of a kernel
○ scheduler implements a scheduling algorithm
3
CPSC 457
Process behaviour
● most processes alternate bursts of CPU activity with bursts of I/O activity
● CPU-bound (or compute-bound) processes ― have long CPU bursts and infrequent I/O waits
● I/O-bound processes ― have short CPU burst and frequent I/O waits
CPU-bound
process
I/O-bound
process
● As CPUs get faster, processes tend to get more I/O-bound. It takes quite a few I/O-bound processes
to keep the CPU fully occupied.
4
CPSC 457
When to schedule
5
CPSC 457
Preemptive vs non-preemptive CPU scheduling
● non-preemptive ― context switch happens only voluntarily
○ multitasking is possible, but only through cooperation
○ process runs until it does a blocking syscall (eg. I/O), terminates, or yields CPU
○ example: FCFS
● preemptive ― context switch can happen without cooperation
○ usually as a direct or indirect result of an interrupt, but not limited to clock interrupt
eg. new job is added, existing process is unblocked
○ however, 'preemptive' is often (mis)used to mean preemptive time-sharing
○ example: SRTN
● preemptive time-sharing ― special case of preemptive
○ processes are context switched periodically, usually to enforce time-slice policy
○ implemented through clock interrupts
○ without a clock, only cooperative multitasking (non-preemptive) is possible
○ example: RR 6
CPSC 457
Categories of scheduling algorithms
● Batch systems ― no impatient users
○ usually on mainframes ― eg. processing payroll, bank interests, insurance claims
○ HPC systems ― eg. weather simulations
○ no interactivity needed, generally no time-slice needed
○ preemption is normally limited to adding new jobs, and often no preemption at all
● Interactive systems ― impatient users
○ general systems ― running many tasks, many of them must remain interactive
○ preemption (time-sharing) is essential to keep users happy
● Real time systems
○ applications must be given guaranteed CPU cycles / second
○ often tied closely to some hardware
□ robots, planes, cars, video/audio capture, games, streaming
7
CPSC 457
Scheduling metrics
Statistics about each process/job:
● Arrival time: the time a process arrives (eg. when you double-click Firefox icon)
● Start time: the time process first gets to run on CPU
○ different from arrival for batch systems, nearly identical to arrival on interactive systems
● Finish time: when the process is done (time of the last instruction)
● Response time: how long before you get first feedback, often response = start - arrival
● Turnaround time: time from arrival to finish, turnaround = finish - arrival
● CPU time: how much time the process spent on CPU
● Waiting time: total time spent it waiting queue, waiting = turnaround - CPU - I/O
Overall statistics:
● Average turnaround time, Average wait time
● Throughput - number of jobs finished per unit of time
8
CPSC 457
Scheduling algorithm goals (examples)
● All systems:
○ Fairness: giving each process a fair share of the CPU
○ Policy/priority enforcement: seeing that stated policy or priority is carried out
○ Balance: keeping all parts of the system busy
● Batch systems:
○ Throughput: maximize jobs per hour (or per minute)
○ Turnaround time: minimize time between submission and termination
○ CPU utilization: keep the CPU busy all the time
○ Waiting time: turnaround time - execution time
● Interactive systems:
○ Response time: minimize time between submission and the responses
○ Proportionality: meet users’ expectations
● Real-time systems:
○ Meeting deadlines: avoid losing data
○ Predictability: avoid quality degradation in multimedia systems
9
CPSC 457
First-come-first-served (FCFS) scheduling
10
CPSC 457
FCFS scheduling
● let's construct a Gantt chart to visualize scheduling of 5 processes
● assume no I/O activity
P1 P2 P3 P4 P5
0 6 12 15 23 25
11
CPSC 457
FCFS - Simulating scheduling
T0 - CPU=[ ] RQ=[] ; simulation start
T0 - CPU=[ ] RQ=[1] ; P1 arrives
T0 - CPU=[1] RQ=[] ; P1 starts executing
T0 - CPU=[1] RQ=[2] ; P2 arrives, P1 continues executing
T1 - CPU=[1] RQ=[2,3] ; P3 arrives, P1 continues executing
T2 - CPU=[1] RQ=[2,3,4] ; P4 arrives
T3 - CPU=[1] RQ=[2,3,4,5] ; P5 arrives Process Arrival Burst
T4 - || P1 0 6
T5 - || P2 0 6
T6 - CPU=[2] RQ=[3,4,5] ; P1 done, P2 starts executing P3 1 3
T7 - || P4 2 8
T8 - || P5 3 2
…
T12 - CPU=[3] RQ=[4,5] ; P2 done, P3 starts executing
…
T15 - CPU=[4] RQ=[5] ; P3 done, P4 starts executing
…
T23 - CPU=[5] RQ=[] ; P4 done, P5 starts executing
…
T25 - CPU=[ ] RQ=[] ; P5 done 12
CPSC 457
FCFS - Simulating scheduling
T0 - CPU=[ ] RQ=[] ; simulation start
T0 - CPU=[ ] RQ=[1] ; P1 arrives
T0 - CPU=[1] RQ=[] ; P1 starts executing
T0 - CPU=[1] RQ=[2] ; P2 arrives, P1 continues executing
T1 - CPU=[1] RQ=[2,3] ; P3 arrives, P1 continues executing
T2 - CPU=[1] RQ=[2,3,4] ; P4 arrives
T3 - CPU=[1] RQ=[2,3,4,5] ; P5 arrives Process Arrival Burst
T4 - || P1 0 6
T5 - || P2 0 6
T6 - CPU=[2] RQ=[3,4,5] ; P1 done, P2 starts executing P3 1 3
T7 - || P4 2 8
T8 - || P5 3 2
… can construct Gantt
T12 chart from this
- CPU=[3] RQ=[4,5] ; P2 done, P3 starts executing
…
T15 - CPU=[4] RQ=[5] ; P3 done, P4 starts executing
…
T23 - CPU=[5] RQ=[] ; P4 done, P5 starts executing
…
T25 - CPU=[ ] RQ=[] ; P5 done 13
CPSC 457
FCFS scheduling
● let's construct a Gantt chart to visualize scheduling of 5 processes:
assume no I/O activity
P1 P2 P3 P4 P5
0 6 12 15 23 25
● avg. wait time = (0+6+11+13+20)/5 = 10 units
● number of context switches: 5
14
CPSC 457
FCFS scheduling - Convoy Effect
CPU-bound job
IO-bound job IO-bound job IO-bound job
15
CPSC 457
FCFS scheduling - Convoy Effect
CPU-bound job
IO-bound job IO-bound job IO-bound job
● example:
○ single CPU-bound process A, with 1.0s long CPU burst cycles
○ many I/O-bound processes Bi, each needing 1000 I/O operations, each 0.01s long
□ CPU-bursts of negligible length (eg. 0.000001s)
○ when A is present, each process Bi will take ~1000 seconds to execute
○ without A present, or if A could be preempted at the right moments, each process Bi would
finish in ~10 seconds
16
CPSC 457
FCFS scheduling - Convoy Effect
CPU-bound job
IO-bound job IO-bound job IO-bound job
17
CPSC 457
Round-robin scheduling (RR)
18
CPSC 457
RR scheduling
● construct a Gantt chart using quantum = 3 msec
● assume no I/O activity
19
CPSC 457
RR - Simulating scheduling
; Notation: <process>/<burst remaining>
1 1 1 2 2 2 3 3 3 4 4 4 5 5 1 1 1 2 2 2 4 4 4 4 4
0 3 6 9 12 14 17 20 25
● The performance of RR depends significantly on the size of the time quantum (Q) and the time
required for a context switch (S).
○ Example: if S=1ms and Q=4ms, then CPU will spend 1/(4+1)=20% of its time on useless tasks.
○ Very small Q implies heavy overhead, but highly responsive system.
○ Very large Q implies minimum overhead, but a non-responsive system.
● Conclusion: Although Q should be large compared to S, it should not be too large. A rule of thumb
is that 80% of the CPU bursts should be shorter than the time quantum. A quantum of around 20-50
msec is/was often a reasonable compromise.
22
CPSC 457
Shortest-job-first scheduling (SJF)
23
CPSC 457
SJF scheduling
● construct a Gantt chart
● assume no I/O activity
24
CPSC 457
SJF scheduling
● construct a Gantt chart
● assume no I/O activity
1 1 1 1 1 1 5 5 3 3 3 2 2 2 2 2 2 4 4 4 4 4 4 4 4
0 6 8 11 17 25
● disadvantages:
○ requires advance knowledge of how long a job will execute
□ although this is often known in batch job systems
○ has a potential for job starvation
□ long programs will never get to run if short programs are continuously added
□ can be solved by aging (increasing a job priority based on how long it has waited)
eg. priority = total wait time / estimated run time
and sorting ready queue based on priority
26
CPSC 457
Shortest-remaining-time-next scheduling (SRTN)
27
CPSC 457
SRTN scheduling
● construct a Gantt chart
● assume no I/O activity
28
CPSC 457
SRTN scheduling
● construct a Gantt chart
● assume no I/O activity
1 3 3 3 5 5 1 1 1 1 1 2 2 2 2 2 2 4 4 4 4 4 4 4 4
0 1 4 6 11 17 25
● execution order: P1, P3, P5, P1, P2, P4
● average wait time: 6.4 units, context switches: 6
29
CPSC 457
SRTN scheduling
30
CPSC 457
Summary
● scheduling:
○ CPU-bound and I/O-bound processes
○ preemptive v.s. nonpreemptive scheduling
○ FCFS, RR, SJF, SRTN
31
CPSC 457
Review
Assuming no I/O:
● Which one of the above scheduling algorithms achieves the optimal average turnaround time?
● Which one of the above scheduling algorithms may involve more than N context switches for N
processes?
● Identify the nonpreemptive algorithms and the preemptive algorithms.
32
CPSC 457
Questions?
33