Professional Documents
Culture Documents
CPU Scheduling Policies
CPU Scheduling Policies
Maximize
CPU utilization: keep the CPU as busy as
possible
Throughput: the number of processes
completed per unit time
Goals for a Scheduler
Minimize
Response time: the time of submission of any
request (to the OS) to the time the first response
is produced
Wait time: total time spent waiting in the ready
queue
Turnaround time: the time of submission of any
process (to the OS) to the time of completion
(when the process terminates)
Goals for a Scheduler
Achieve fairness
There are tensions among these goals
Pending Discussion
Assumptions
Non-Preemptive Scheduling
Preemptive Scheduling
Non-Preemptive Scheduling
A scheduling discipline is said to be non-preemptive if,
once a process has been given the CPU, it cannot be
taken away from that process (i.e. the process cannot be
preempted).
All jobs are given equal fair treatment, thus, the short
jobs (i.e. those requiring less amount of time for
completion) are made to wait by longer jobs.
Response times are more predictable because incoming
high priority jobs cannot displace (i.e. shift) the waiting
jobs.
Preemptive Scheduling
+ Simple
- Short jobs can get stuck behind long jobs
First-Come, First-Served (FCFS) Scheduling
P1 P2 P3
0 24 27 30
Waiting time for P1 = 0; P2 = 24; P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 17
Operating System Concepts – 7th Edition, Feb 2, 2005 5.15 Silberschatz, Galvin and Gagne ©2005
FCFS Scheduling (Cont.)
P2 P3 P1
0 3 6 30
Waiting time for P1 = 6; P2 = 0; P3 = 3
Average waiting time: (6 + 0 + 3)/3 = 3
Much better than previous case
Convoy Effect short process behind long process
FCFS favors CPU-bound processes. Why?
Operating System Concepts – 7th Edition, Feb 2, 2005 5.16 Silberschatz, Galvin and Gagne ©2005
Round Robin
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
Operating System Concepts – 7th Edition, Feb 2, 2005 5.19 Silberschatz, Galvin and Gagne ©2005
Time Quantum and Context Switch Time
Operating System Concepts – 7th Edition, Feb 2, 2005 5.20 Silberschatz, Galvin and Gagne ©2005
FIFO vs. Round Robin
turnaround time of C
turnaround time of B
turnaround time of A
A B C Time
FIFO
FIFO vs. Round Robin
Round Robin
+ Shorter response time
+ Fair sharing of CPU
- Not all jobs are preemptive
- Not good for jobs of the same length
Shortest Job First (SJF)
turnaround time of C
turnaround time of B
turnaround time of A
wait time of C
wait time of B
wait time of A = 0
response time of C
response time of B
response time of A = 0
A B C Time
Shortest Job First
Shortest Remaining Time First
(SRTF)
SRTF: a preemptive version of SJF
If a job arrives with a shorter time to completion,
SRTF preempts the CPU for the new job
Also known as SRTCF (shortest remaining time
to completion first)
Generally used as the base case for comparisons
Shortest-Job-First (SJF) Scheduling
Associate with each process the length of its next CPU burst. Use
these lengths to schedule the process with the shortest time
Two schemes:
Non-preemptive – once CPU given to the process it cannot be
preempted until completes its CPU burst
Preemptive – if a new process arrives with CPU burst length
less than remaining time of current executing process,
preempt. This scheme is know as the
Shortest-Remaining-Time-First (SRTF)
SJF is optimal – gives minimum average waiting time for a given
set of processes
Decision is based on expected processor burst time
SJF favors short jobs over long ones
Operating System Concepts – 7th Edition, Feb 2, 2005 5.27 Silberschatz, Galvin and Gagne ©2005
Example of Non-Preemptive SJF
P1 P3 P2 P4
0 3 7 8 12 16
Operating System Concepts – 7th Edition, Feb 2, 2005 5.28 Silberschatz, Galvin and Gagne ©2005
Example of Preemptive SJF
0 2 4 5 7 11 16
Operating System Concepts – 7th Edition, Feb 2, 2005 5.29 Silberschatz, Galvin and Gagne ©2005
Drawbacks of Shortest Job First
0% I/O Time
FIFO
CPU A B C
I/O C
SRTCF
CPU C A C A C A C B C B
I/O C C C C C
Non-preemptive
Priority 1: A
Priority 2: B
+ Generalization of SJF
With SJF, priority = 1/requested_CPU_time
- Starvation: high priority jobs keep on
preempting low priority jobs
Multilevel Feedback Queue
A process can move between the various queues;
aging can be implemented this way
Multilevel-feedback-queue scheduler defined by the
following parameters:
number of queues
process
method used to determine when to demote a
process
method used to determine which queue a process
Three queues:
Q – RR with time quantum 8 milliseconds
0
Approximates SRTF
A CPU-bound job drops like a rock
I/O-bound jobs stay near the top
Still unfair for long running jobs
Counter-measure: Aging
Increase the priority of long running jobs if they are not
serviced for a period of time
Tricky to tune aging
Multilevel Feedback Queues
Time
Multilevel Feedback Queues
time = 1
A Time
Multilevel Feedback Queues
time = 2
A B Time
Multilevel Feedback Queues
time = 3
A B C Time
Multilevel Feedback Queues
time = 3
A B C Time
Multilevel Feedback Queues
time = 3
A B C Time
Multilevel Feedback Queues
time = 5
A B C B Time
Multilevel Feedback Queues
time = 6
A B C B A Time
Multilevel Feedback Queues
time = 8
A B C B A C Time
Multilevel Feedback Queues
time = 9
A B C B A C C Time
Multilevel Queue Scheduling
Ready queue is partitioned into separate queues:
foreground queue (interactive) – 80%
background queue (batch) – 20%
Each queue has its own scheduling algorithm
foreground – RR
background – FCFS
Scheduling must be done between the queues
Fixed priority scheduling; (i.e., serve all from foreground then
from background). Possibility of starvation.
Time slice – each queue gets a certain amount of CPU time
which it can schedule amongst its processes; i.e., 80% to
foreground in RR
20% to background in FCFS
Operating System Concepts – 7th Edition, Feb 2, 2005 5.53 Silberschatz, Galvin and Gagne ©2005
Multilevel Queue Scheduling
Operating System Concepts – 7th Edition, Feb 2, 2005 5.54 Silberschatz, Galvin and Gagne ©2005
Lottery Scheduling
(Homework Reading Assignment)
Lottery scheduling is an adaptive
scheduling approach to address the fairness
problem
Each process owns some tickets
On each time slice, a ticket is randomly picked
On average, the allocated CPU time is
proportional to the number of tickets given to each
job
Lottery Scheduling