CPU Scheduling
Dr. Manmath N. Sahoo
Dept. of CSE, NIT Rourkela
CPU-I/O Burst Cycle
CPU–I/O Burst Cycle – Process
execution consists of a cycle of CPU
execution and I/O wait.
CPU burst is length of time process
needs to use CPU before it next
makes a I/O request.
I/O burst is the length of time
process spends waiting for I/O to
complete.
Dr. Manmath N. Sahoo (CS) 2
Histogram of CPU-burst Times
Typical CPU burst distribution
Dr. Manmath N. Sahoo (CS) 3
CPU Scheduler
Allocates CPU to one of processes that are ready
to execute (in ready queue)
CPU scheduling decisions may take place when a
process:
1. Switches from running to waiting state (e.g. I/O request)
2. Terminates
3. Switches from waiting to ready (e.g. I/O completion)
4. Switches from running to ready state (e.g. priority based
interruption)
Dr. Manmath N. Sahoo (CS) 4
Dispatcher
Dispatcher gives control of the CPU to the process
selected by the short-term scheduler; this involves:
• switching context
• switching to user mode (if CPU was executing the previous
process in kernel mode and preempted)
• jumping to the proper location in the user program to restart that
program (i.e., last action is to set program counter)
Dispatch latency – time it takes for the dispatcher
to switch between processes and start new one
running
Dr. Manmath N. Sahoo (CS) 5
Scheduling Criteria
CPU utilization: i.e. CPU usage – want to maximize
Throughput: number of processes that complete their
execution per time unit – want to maximize
Turnaround time: amount of time to execute a particular
process – want to minimize
Waiting time: amount of time a process has been waiting
in the ready queue – want to minimize
Response time: amount of time it takes from when a job
was submitted until it initiates its first response (output) –
want to minimize
Dr. Manmath N. Sahoo (CS) 6
First-Come, First-Served Scheduling
Schedule: order of arrival of process in ready queue
Example: Process Burst Time
P1 24
P2 3
P3 3
Suppose that the 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
Dr. Manmath N. Sahoo (CS) 7
FCFS Scheduling
Suppose that the processes arrive in the
order P2 , P3 , P1 .
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
Dr. Manmath N. Sahoo (CS) 8
FCFS Scheduling
+ Simple to implement by FIFO queue.
+ Code is easy to write and understand
- Average waiting time is not minimal
- Not suitable for time-sharing systems
- Convoy effect: short I/O intensive processes
behind long CPU intensive process
NOTE: FCFS Scheduling is non-preemptive
Dr. Manmath N. Sahoo (CS) 9
Large CPU bound process
• C1 (1200ms): 1000ms computation + 200ms I/O
• 200cpu – 50io – 200cpu – 50io – 200cpu – 50io – 200cpu – 50io – 200cpu
Small I/O bound processes
• I1, I2, …, I10
• (8.5ms): 2.5ms computation + 8ms I/O
• 0.5cpu – 2io – 0.5cpu – 2io – 0.5cpu – 2io – 0.5cpu – 2io – 0.5cpu
May lead to less CPU utilization and I/O utilization →
Convoy effect
Dr. Manmath N. Sahoo (CS) 10
Shortest-Job-First (SJF) Scheduling
Schedule: order of the CPU burst of the
processes.
Two schemes:
• Non-Preemptive SJF / Shortest Job Next (SJN)
• once CPU given to the process it cannot be preempted until
completes its CPU burst.
• Preemptive SJF / Shortest Remaining Time First
(SRTF)
• if a new process arrives with CPU burst length less than
remaining time of current executing process, preempt.
Dr. Manmath N. Sahoo (CS) 11
Non-Preemptive SJF (SJN)
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
Gantt Chart:
P1 P3 P2 P4
0 3 7 8 12 16
Average waiting time = (0 + 6 + 3 + 7)/4 = 4
Dr. Manmath N. Sahoo (CS) 12
Preemptive SJF (SRTF)
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
Gantt Chart:
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
Average waiting time = (9 + 1 + 0 +2)/4 = 3
Dr. Manmath N. Sahoo (CS) 13
Example
Process Arrival Time Burst Time
P1 0 16
P2 2 8
P3 4 12
P4 6 2
P5 8 4
Draw Gantt chat and find the average waiting time
in each of the following cases:
• SRTF Scheduling
• SJN Scheduling
• FCFS Scheduling
Dr. Manmath N. Sahoo (CS) 14
Answer to Example Process Arrival Time Burst Time
P1 0 16
P2 2 8
P3 4 12
P4 6 2
P5 8 4
SRTF
P1 P2 P2 P4 P2 P5 P3 P1
0 2 4 6 8 12 16 28 42
Average waiting time = (26+6+12+0+0)/5
= 8.8
Dr. Manmath N. Sahoo (CS) 15
Answer to Example Process Arrival Time Burst Time
P1 0 16
P2 2 8
P3 4 12
P4 6 2
P5 8 4
SJN
P1 P4 P5 P2 P3
0 16 18 22 30 42
Average waiting time = (0+20+26+10+10)/5
= 13.2
Dr. Manmath N. Sahoo (CS) 16
Answer to Example Process Arrival Time Burst Time
P1 0 16
P2 2 8
P3 4 12
P4 6 2
P5 8 4
FCFS
P1 P2 P3 P4 P5
0 16 24 36 38 42
Average waiting time = (0+14+20+30+30)/5
= 18.8
Dr. Manmath N. Sahoo (CS) 17
Answer to Example
Scheduling Algorithm Average Waiting Time
FCFS 18.8
SJN 13.2
SRTF 8.8
Dr. Manmath N. Sahoo (CS) 18
Shortest Job First
+ SRTF is Optimal: Minimum Average waiting time.
- Longer processes may be starved.
- Difficult to know the CPU burst of processes before their
execution
Dr. Manmath N. Sahoo (CS) 19
Round Robin (RR) Scheduling
Each process gets a time quantum, usually 10-
100 milliseconds.
After this time has elapsed, the process is
preempted and added to the end of the ready
queue.
If there are n processes in the ready queue and
the time quantum is q, then each process gets
1/n of the CPU time.
No process waits more than (n-1)q time units.
Dr. Manmath N. Sahoo (CS) 20
Example: RR with q= 20
Process Burst Time
P1 53
P2 17
P3 68
P4 24
The Gantt chart is:
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 37 57 77 97 117 121 134 154 162
Typically, higher average turnaround than SJF, but
better response.
Dr. Manmath N. Sahoo (CS) 21
Round Robin (RR) Scheduling
FCFS + Preemption = RR
Performance
• q large ⇒ behaves similar to FCFS
• q small ⇒ more context switch overhead
Dr. Manmath N. Sahoo (CS) 22
Modified RR Scheduling
Based on variable time quantum
A preempted process, on its next turn will
use an increased time quantum
RR Scheduling
• P1: 300ms, q=20ms, time per context switch=2ms
• #of preemptions due to this process = 14
• Preemption overhead = 28ms
Dr. Manmath N. Sahoo (CS) 23
Modified RR Scheduling
Modified RR Scheduling – q is doubled in
next turn.
• P1: 300ms, q=20ms (initial)
• #of preemptions due to this process = 3.
(20+40+80+160=300)
• Preemption overhead = 6ms
Dr. Manmath N. Sahoo (CS) 24
Priority Scheduling
A priority number (integer) is associated with each
process and the CPU is allocated to the process with
the highest priority
Some schemes use smallest integer ≡ highest
priority.
SJF is effectively priority scheduling where priority
is defined on next CPU burst time.
FCFS is effectively priority scheduling where
priority is defined on arrival time.
Dr. Manmath N. Sahoo (CS) 25
Priority Scheduling
Problem: Starvation – low priority
processes may never execute.
Solution: Aging – as time progresses
increase the priority of the process.
• e.g. in every 10ms increase the priority of process by 1
Dr. Manmath N. Sahoo (CS) 26
Highest Response Ratio Next
(HRRN) Scheduling
It gives priority to shorter jobs similar to SJN
Implements Aging implicitly
Non-preemptive dynamic priority based
algorithm with initial priority 1
p=1+(wait time/burst time)
Dr. Manmath N. Sahoo (CS) 27
Highest Response Ratio Next
(HRRN) Scheduling
Process Arrival Time Burst Time
P1 0 3
P2 2 6
P3 4 4
P4 6 5
P5 8 2
P1 P2 P3 P5 P4
0 3 9 13 15 20
Dr. Manmath N. Sahoo (CS) 28
Multi level Queue Scheduling
Ready queue is partitioned into separate
queues.
Each queue holds a particular category of
processes.
Each queue can implement whatever
scheduling algorithm is most appropriate for
that type of processes.
Dr. Manmath N. Sahoo (CS) 29
Multi level Queue Scheduling
Dr. Manmath N. Sahoo (CS) 30
Multilevel Queue Scheduling
Scheduling must be done between the
queues.
• Priority scheduling; But possibility of starvation.
• Time slice – each queue gets a certain amount of CPU
time which it can schedule amongst its processes;
Dr. Manmath N. Sahoo (CS) 31
Multilevel Queue Scheduling
Objectives:
• To optimize turnaround time.
• To minimize response time (to make a system feel
responsive to interactive users).
THE CRUX:
HOW TO SCHEDULE WITHOUT PERFECT KNOWLEDGE?
How can we design a scheduler that both minimizes response time
for interactive jobs while also minimizing turnaround time without
a priori knowledge of job length and job behaviour?
Dr. Manmath N. Sahoo (CS) 32
Multilevel Feedback Queue Scheduling -
Basic Rules
>1 number of queues, each assigned a different priority level
Processes are executed from top priority queue to low priority
queue.
• Rule 1: If Priority(A) > Priority(B),
A runs (B doesn’t).
• Rule 2: If Priority(A) = Priority(B),
A & B run in RR/SJN/any other.
Dr. Manmath N. Sahoo (CS) 33
MLFQ: Basic Rules
MLFQ varies the priority (dynamic) of a job
based on its observed behavior.
If a process repeatedly relinquishes the CPU,
MLFQ will keep its priority high.
If a job uses the CPU intensively for long
periods of time, MLFQ will reduce its priority.
Dr. Manmath N. Sahoo (CS) 34
MLFQ Priority Adjustment: Attempt#1
• Rule 3: When a job enters the system, it is placed at the highest
priority (the topmost queue).
• Rule 4a: If a job uses up an entire time slice while running, its
priority is reduced (i.e., it moves down one queue).
• Rule 4b: If a job gives up the CPU before the time slice is up, it
stays at the same priority level.
Dr. Manmath N. Sahoo (CS) 35
Example 1: A Single Long-Running Job
Dr. Manmath N. Sahoo (CS) 36
Example 2: Along Came A Short Job
(20ms) at 100ms
Dr. Manmath N. Sahoo (CS) 37
Example 3: What About I/O?
An interactive job B needs the CPU only for 1ms before performing
an I/O of 9ms.
Dr. Manmath N. Sahoo (CS) 38
Problems With Current MLFQ
Starvation to long jobs.
A smart user could rewrite their program to
game the scheduler.
A process may change its behavior over time
- from CPU bound to a phase of interactivity.
Dr. Manmath N. Sahoo (CS) 39
Attempt #2: The Priority Boost
guarantees that CPU-bound jobs will make
some progress (even if it is not much).
• Rule 5: After some time period S, move all the jobs in the system to
the topmost queue.
Dr. Manmath N. Sahoo (CS) 40
Attempt #2: The Priority Boost
interactive jobs need CPU only for 5ms before performing an I/O of 5ms.
(a) Without Priority Boost (b) With Priority Boost in every 50ms
Dr. Manmath N. Sahoo (CS) 41
Attempt #3: Better Accounting
To prevent gaming of the scheduler, Rules 4a and 4b are
rewritten to the following single rule:
• Rule 4: Once a job uses up its time quantum at a given level
(regardless of how many times it has given up the CPU), its
priority is reduced (i.e., it moves down one queue).
Dr. Manmath N. Sahoo (CS) 42
Effect of anti-Gaming rule
(a) Without anti-Gaming rule (b) With anti-Gaming rule
Dr. Manmath N. Sahoo (CS) 43
Uniprocessor vs Multiprocessor
Single CPU with Cache Two CPUs with Caches
Dr. Manmath N. Sahoo (CS) 44
Multiprocessor Scheduling
Single Queue Multiprocessor Scheduling
Multi Queue Multiprocessor Scheduling
Dr. Manmath N. Sahoo (CS) 45
SQMS
LTS loads all jobs to the common ready
queue.
However, there are multiple STSs.
+ Simple to implement
- Synchronization overhead (lock overhead)
- Doesn’t preserve Cache affinity
Dr. Manmath N. Sahoo (CS) 46
SQMS
RR Scheduling on 04 CPUs:
processes bounce from CPU to CPU.
Solution – SQMS must adopt some affinity mechanism (requires migration)
Dr. Manmath N. Sahoo (CS) 47
SQMS
Schedule with migration
Dr. Manmath N. Sahoo (CS) 48
MQMS
2 Processors: CPU0, CPU1.
• A goes to CPU0
• B goes to CPU1
• C goes to CPU0
• D goes to CPU1
Dr. Manmath N. Sahoo (CS) 49
MQMS
+ Synchronization overhead (lock overhead)
+ Preserves Cache affinity
- Load imbalance
Dr. Manmath N. Sahoo (CS) 50
MQMS
- Load imbalance
• C finishes
• A finishes
Dr. Manmath N. Sahoo (CS) 51
MQMS
Solution to load imbalance -> migration
Solution: Simply move one of B or D to CPU 0
Dr. Manmath N. Sahoo (CS) 52
MQMS
A single migration doesn’t help.
Solution: In every 40ms migrate one job.
Dr. Manmath N. Sahoo (CS) 53