Operating Systems
Topic: Process – Policies
(Scheduling)
Endadul Hoque
Acknowledgement
• Youjip Won (Hanyang University)
• OSTEP book – by Remzi and Andrea Arpaci-Dusseau
(University of Wisconsin)
Process Scheduling
• Workload assumptions:
1. Each job runs for the same amount of time.
2. All jobs arrive at the same time.
3. All jobs only use the CPU (i.e., they perform no I/O).
4. The run-time of each job is known.
Process Scheduling
• Performance metric: Turnaround time
– The time at which the job completes minus the
time at which the job arrived in the system.
𝑻𝒕𝒖𝒓𝒏𝒂𝒓𝒐𝒖𝒏𝒅 = 𝑻𝒄𝒐𝒎𝒑𝒍𝒆𝒕𝒊𝒐𝒏 − 𝑻𝒂𝒓𝒓𝒊𝒗𝒂𝒍
• Another metric is fairness.
– Performance and fairness are often at odds in
scheduling.
First in, First Out (FIFO)
• Also, known as First Come, First Served (FCFS)
– Very simple and easy to implement
• Example:
– A arrived just before B which arrived just before C.
– Each job runs for 10 seconds.
A B C
0 20 40 60 80 100 120
Time (Second)
𝟏𝟎 + 𝟐𝟎 + 𝟑𝟎
𝑨𝒗𝒆𝒓𝒂𝒈𝒆 𝒕𝒖𝒓𝒏𝒂𝒓𝒐𝒖𝒏𝒅 𝒕𝒊𝒎𝒆 = = 𝟐𝟎 𝒔𝒆𝒄
𝟑
Why is FIFO not that great? – Convoy effect
src: https://www.moveoverslowdrivers.com/speed-up-slowpoke-or-get-a-ticket/
Why is FIFO not that great? – Convoy effect
• Let’s relax assumption 1: Each job no longer
runs for the same amount of time.
• Example:
– A arrived just before B which arrived just before C.
– A runs for 100 seconds, B and C run for 10 each.
A B C
0 20 40 60 80 100 120
Time (Second)
𝟏𝟎𝟎 + 𝟏𝟏𝟎 + 𝟏𝟐𝟎
𝑨𝒗𝒆𝒓𝒂𝒈𝒆 𝒕𝒖𝒓𝒏𝒂𝒓𝒐𝒖𝒏𝒅 𝒕𝒊𝒎𝒆 = = 𝟏𝟏𝟎 𝒔𝒆𝒄
𝟑
Shortest Job First (SJF)
• Run the shortest job first, then the next shortest, and
so on
– Non-preemptive scheduler
• Example:
– A arrived just before B which arrived just before C.
– A runs for 100 seconds, B and C run for 10 each.
B C A
0 20 40 60 80 100 120
Time (Second)
𝟏𝟎 + 𝟐𝟎 + 𝟏𝟐𝟎
𝑨𝒗𝒆𝒓𝒂𝒈𝒆 𝒕𝒖𝒓𝒏𝒂𝒓𝒐𝒖𝒏𝒅 𝒕𝒊𝒎𝒆 = = 𝟓𝟎 𝒔𝒆𝒄
𝟑
SJF with Late Arrivals from B and C
• Let’s relax assumption 2: Jobs can arrive at any time.
• Example:
– A arrives at t=0 and needs to run for 100 seconds.
– B and C arrive at t=10 and each need to run for 10 seconds
[B,C arrive]
A B C
0 20 40 60 80 100 120
Time (Second)
𝟏𝟎𝟎 + 𝟏𝟏𝟎 − 𝟏𝟎 + (𝟏𝟐𝟎 − 𝟏𝟎)
𝑨𝒗𝒆𝒓𝒂𝒈𝒆 𝒕𝒖𝒓𝒏𝒂𝒓𝒐𝒖𝒏𝒅 𝒕𝒊𝒎𝒆 =
𝟑
= 𝟏𝟎𝟑. 𝟑𝟑 𝒔𝒆𝒄
Shortest Time-to-Completion First (STCF)
• Add preemption to SJF
– Also knows as Preemptive Shortest Job First (PSJF)
• A new job enters the system:
– Determine of the remaining jobs and new job
– Schedule the job which has the lowest time left (i.e.,
which requires the minimum time to finish)
Shortest Time-to-Completion First (STCF)
• Example:
– A arrives at t=0 and needs to run for 100 seconds.
– B and C arrive at t=10 and each need to run for 10 seconds
[B,C arrive]
A B C A
0 20 40 60 80 100 120
Time (Second)
(𝟏𝟐𝟎 − 𝟎) + 𝟐𝟎 − 𝟏𝟎 + (𝟑𝟎 − 𝟏𝟎)
𝑨𝒗𝒆𝒓𝒂𝒈𝒆 𝒕𝒖𝒓𝒏𝒂𝒓𝒐𝒖𝒏𝒅 𝒕𝒊𝒎𝒆 =
𝟑
= 𝟓𝟎 𝒔𝒆𝒄
New Scheduling Metric: Response Time
• The time from when the job arrives to the first time it
is scheduled.
𝑻𝒓𝒆𝒔𝒑𝒐𝒏𝒔𝒆 = 𝑻𝒇𝒊𝒓𝒔𝒕𝒓𝒖𝒏 − 𝑻𝒂𝒓𝒓𝒊𝒗𝒂𝒍
– STCF and related disciplines are not particularly good for
response time.
How can we build a scheduler that is
sensitive to response time?
Round Robin (RR) Scheduling
• Time slicing Scheduling
– Run a job for a time slice and then switch to the next
job in the run queue until the jobs are finished.
• Time slice is sometimes called a scheduling quantum.
– It repeatedly does so until the jobs are finished.
– The length of a time slice must be a multiple of the
timer-interrupt period.
RR is fair, but performs poorly on metrics
such as turnaround time
Round Robin (RR) Scheduling
• A, B and C arrive at the same time.
• They each wish to run for 5 seconds.
A B C
0 + 5 + 10
𝑇𝑎𝑣𝑒𝑟𝑎𝑔𝑒 𝑟𝑒𝑠𝑝𝑜𝑛𝑠𝑒 = = 𝟓𝒔𝒆𝒄
3
0 5 10 15 20 25 30
Time (Second)
SJF (Bad for Response Time)
A B CA B CA B CA B CA B C
0+1+2
𝑇𝑎𝑣𝑒𝑟𝑎𝑔𝑒 𝑟𝑒𝑠𝑝𝑜𝑛𝑠𝑒 = = 𝟏𝒔𝒆𝒄
3
0 5 10 15 20 25 30
Time (Second)
RR with a time-slice of 1sec (Good for Response Time)
The length of the time slice is critical
• The shorter time slice
– Better response time
– The cost of context switching will dominate overall
performance.
• The longer time slice
– Amortize the cost of switching
– Worse response time
Deciding on the length of the time slice presents
a trade-off to a system designer
Incorporating I/O
• Let’s relax assumption 3: All programs perform
I/O
• Example:
– A and B need 50ms of CPU time each.
– A runs for 10ms and then issues an I/O request
• I/Os each take 10ms
– B simply uses the CPU for 50ms and performs no I/O
– The scheduler runs A first, then B after
Incorporating I/O
A A A A A B B B B B
0 20 40 60 80 100 120 140
Time (msec)
Poor Use of Resources
A B A B A B A B A B
Maximize the
CPU utilization
0 20 40 60 80 100 120 140
Time (msec)
Overlap Allows Better Use of Resources
Incorporating I/O
• When a job initiates an I/O request.
– The job is blocked waiting for I/O completion.
– The scheduler should schedule another job on the
CPU.
• When the I/O completes
– An interrupt is raised.
– The OS moves the process from blocked back to the
ready state.
Reading Material
• Chapter 7 of OSTEP book – by Remzi and Andrea
Arpaci-Dusseau (University of Wisconsin)
http://pages.cs.wisc.edu/~remzi/OSTEP/cpu-sched.pdf
Questions?