You are on page 1of 33

CPSC 457

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

● recall multiprogramming … the main objective:


○ maximize CPU utilization by having a process running at all time
○ several processes are kept in memory at one time
○ a process runs until it must wait
○ instead of having the CPU sit idle, the OS takes the CPU away from the waiting process and
gives it to another process that is ready to run

● 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

● variety of situations when scheduling is needed, eg.:


○ process creation ― does child run immediately, or does parent continue to run (eg. until
time-slice expiration)
○ process termination ― which process runs next
○ blocking system call
□ eg. I/O, mutex, semaphore
○ I/O interrupt ― may unblock a process, does it go into ready queue? what if it is high
priority process?
○ periodic clock interrupt ― when used to implement a time-slice

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

● one of the simplest scheduling algorithms, very common in batch environments


● non-preemptive
● CPU assigned in the order the processes request it, using a FIFO ready queue
● new jobs are appended to the ready queue
● a running job keeps the CPU until it is either finished, or it blocks
● when running process blocks, next process from ready queue starts to execute
● when process is unblocked, it is appended at the end of the ready queue
● requires minimum number of context switches ― only N switches for N processes

10
CPSC 457
FCFS scheduling
● let's construct a Gantt chart to visualize scheduling of 5 processes
● assume no I/O activity

Process Arrival Burst Start Finish Turnaround Waiting


P1 0 6
P2 0 6
P3 1 3
P4 2 8
P5 3 2

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

Process Arrival Burst Start Finish Turnaround Waiting


P1 0 6 0 6 6 0
P2 0 6 6 12 12 6
P3 1 3 12 15 14 11
P4 2 8 15 23 21 13
P5 3 2 23 25 22 20

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

● big disadvantage: convoy effect


● scenario: one CPU-bound process + many I/O-bound processes
● result: the CPU-bound process will tie up the CPU,
making the I/O-bound processes run for much longer

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

● leads to long periods of idle I/O devices


○ when CPU-bound process is tying up the CPU, the I/O-bound jobs are sitting in RQ

17
CPSC 457
Round-robin scheduling (RR)

● a preemptive version of the FCFS algorithm


● each process is assigned a time interval, called a time slice, or quantum
e.g., 10 msec, during which it is allowed to run
● if the process exceeds the quantum, the process is preempted (context switch), and CPU is given to
the next process in ready queue
● preempted process goes at the back of the ready queue

18
CPSC 457
RR scheduling
● construct a Gantt chart using quantum = 3 msec
● assume no I/O activity

Process Arrival Burst Start Finish Turnaround Waiting


P1 0 6
P2 0 6
P3 1 3
P4 2 8
P5 3 2

19
CPSC 457
RR - Simulating scheduling
; Notation: <process>/<burst remaining>

T0 - CPU=[ ] RQ=[] ; simulation start


T0 - CPU=[ ] RQ=[1/6,2/6] ; P1 arrives, P2 arrives
T0 - CPU=[1/6] RQ=[2/6] ; P1 starts executing
T1 - CPU=[1/5] RQ=[2/6,3/3] ; P3 arrives
T2 - CPU=[1/4] RQ=[2/6,3/3,4/8] ; P4 arrives Process Arrival Burst
T3 - CPU=[1/3] RQ=[2/6,3/3,4/8,5/2] ; P5 arrives P1 0 6
T3 - CPU=[2/6] RQ=[3/3,4/8,5/2,1/3] ; P1 is preempted by P2 P2 0 6
T6 - CPU=[3/3] RQ=[4/8,5/2,1/3,2/3] ; P2 is preempted by P3 P3 1 3
T9 - CPU=[ ] RQ=[4/8,5/2,1/3,2/3] ; P3 done P4 2 8
T9 - CPU=[4/8] RQ=[5/2,1/3,2/3] ; P4 starts executing P5 3 2
T12 - CPU=[5/2] RQ=[1/3,2/3,4/5] ; P4 is preempted by P5
T14 - CPU=[ ] RQ=[1/3,2/3,4/5] ; P5 done
T14 - CPU=[1/3] RQ=[2/3,4/5] ; P1 executes
T17 - CPU=[ ] RQ=[2/3,4/5] ; P1 done
T17 - CPU=[2/3] RQ=[4/5] ; P2 executes
T20 - CPU=[ ] RQ=[4/5] ; P2 done
T20 - CPU=[4/5] RQ=[] ; P4 executes
T25 - CPU=[ ] RQ=[] ; P4 done, simulation ends 20
CPSC 457
RR scheduling
● construct a Gantt chart using quantum of 3 msec
● assume no I/O activity

Process Arrival Burst Start Finish Turnaround Waiting


P1 0 6 0 17 17 11
P2 0 6 3 20 20 14
P3 1 3 6 9 8 5
P4 2 8 9 25 23 15
P5 3 2 12 14 11 9

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

● execution order: P1, P2, P3, P4, P5, P1, P2, P4


● average wait time: 10.8 units, context switches: 8
21
CPSC 457
RR: time slice

● 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)

● another non-preemptive scheduling algorithm


○ applicable to batch systems, where job length (execution time) is known in advance
○ can be adjusted to be preemptive (eg. preemption when new job arrives, or existing one
unblocks)
● when the CPU is available, it is assigned to the shortest job
○ shortest = shortest execution time
○ ties are resolved using FCFS
● SJF is similar to FCFS, but ready queue is sorted:
○ basic: sorted on static execution time, based on submitted estimate
○ advanced: execution time can be dynamically computed based on the history of CPU bursts

23
CPSC 457
SJF scheduling
● construct a Gantt chart
● assume no I/O activity

Process Arrival Burst Start Finish Turnaround Waiting


P1 0 6
P2 0 6
P3 1 3
P4 2 8
P5 3 2

24
CPSC 457
SJF scheduling
● construct a Gantt chart
● assume no I/O activity

Process Arrival Burst Start Finish Turnaround Waiting


P1 0 6 0 6 6 0
P2 0 6 11 17 17 11
P3 1 3 8 11 10 7
P4 2 8 17 25 23 15
P5 3 2 6 8 5 3

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

● execution order: P1, P5, P3, P2, P4


● average wait time: 7.2 units, context switches: 5
25
CPSC 457
SJF scheduling
● advantages:
○ minimum number of context switches (just like FCFS)
○ optimal turnaround time if all jobs arrive simultaneously
□ by minimizing average waiting time

● 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)

● preemptive version of SJF


● next job is picked based on remaining time
○ remaining time = total time - time already spent on CPU
● SRTN is similar to RR
○ ready queue is sorted by remaining time
□ remaining time can be static, or dynamically calculated
○ context switch can happen as a result of adding a job

27
CPSC 457
SRTN scheduling
● construct a Gantt chart
● assume no I/O activity

Process Arrival Burst Start Finish Turnaround Waiting


P1 0 6
P2 0 6
P3 1 3
P4 2 8
P5 3 2

28
CPSC 457
SRTN scheduling
● construct a Gantt chart
● assume no I/O activity

Process Arrival Burst Start Finish Turnaround Waiting


P1 0 6 0 11 11 5
P2 0 6 11 17 17 11
P3 1 3 1 4 3 0
P4 2 8 17 25 23 15
P5 3 2 4 6 3 1

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

● similar advantages to SJF


○ optimal turnaround time even if jobs don't arrive at the same time
○ but slightly more context switches
● similar disadvantages to SJF
○ requires advance knowledge of how long a job will execute
○ has a potential for job starvation
○ and also needs to consider cost of context switch

30
CPSC 457
Summary

● scheduling:
○ CPU-bound and I/O-bound processes
○ preemptive v.s. nonpreemptive scheduling
○ FCFS, RR, SJF, SRTN

Reference: 2.3.3, 2.4.1 - 2.4.2 (Modern Operating Systems)


6.3, 6.4, 5.1 - 5.3 (Operating System Concepts)

31
CPSC 457
Review

A. First come, first serve (FCFS)

B. Shortest job first (SJF)

C. Shortest remaining time next (SRTN)

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

You might also like