OS 09 05 Cpu Scheduling
OS 09 05 Cpu Scheduling
OS 09 05 Cpu Scheduling
Session 4
CPU Scheduling
CPU Scheduling
CPU 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
First-Come First-Served (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
• SJF (non-preemptive)
P1 P3 P2 P4
0 3 7 8 12 16
• Average waiting time = (0 + 6 + 3 + 7)/4 = 4
Example of Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SJF (Preemptive)
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
= ½ and 0 = 10
Exponential Averaging
• =0
– n+1 = n
– Recent history does not count
• =1
– n+1 = tn
– Only the actual last CPU burst counts
• If we expand the formula, we get:
n+1 = tn+(1 - ) tn -1 + …
+(1 - )j tn -j + …
+(1 - )n +1 0
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
• Scheduling
– A new job enters queue Q0 which is served FCFS.
When it gains CPU, job receives 8 milliseconds. If
it does not finish in 8 milliseconds, job is moved
to queue Q1.
– At Q1 job is again served FCFS and receives 16
additional milliseconds. If it still does not
complete, it is preempted and moved to queue Q2.
Example of Multilevel Feedback Queue
Multilevel Feedback Queue
• Discussions:
– Favors I/O bound processes to achieve good device
utilization
– Separates processes into categories based on their CPU
need
– Each time a process leaves the queue it is stamped with the
identity of the lowest level queue in which it last resided.
– When the process reenters the fray for CPU, it is sent
directly to the queue where it last completed its execution.
– (the past behavior is a good indicator of the future behavior)
– If the process changes its characteristics from CPU bound
to I/O bound,
1) the process is stamped with the time spent last time on
the CPU.
2) if a process voluntarily relinquishes the CPU, it may be
moved up to the next level queue.
Selfish Round Robin (SRR)
• Objective :
– To give better service to processes that have been
executing for a while than to newcomers.
• Implementation :
– Processes in the ready list are partitioned into two
lists: new and accepted.
– new processes wait.
– accepted processes are serviced by RR.
– priority of a new process increases at rate a.
– priority of an accepted process increases at rate b .
Selfish Round Robin (SRR)
• Implementation :
– a and b are parameters to tune the method.
– When the priority of a new process
reaches the priority of an accepted
process, that new process becomes
accepted.
– If all accepted processes finish, the
highest priority new process is accepted.
Selfish Round Robin (SRR)
• Implementation :
– Assume that there are no ready processes, when the
first one, A, arrives. It has priority 0 to begin with.
Since there are no other accepted processes, A is
accepted immediately.
– After a while another process, B, arrives. As long as b
/ a < 1, B’s priority will eventually catch up to A’s, so it
is accepted; now both A and B have the same
priority.
– All accepted processes share a common priority
(which rises at rate b ); that makes this policy easy to
implement.
– Even if b / a > 1, A will eventually finish, and then B
can be accepted.
Selfish Round Robin (SRR)
• Adjusting the parameters a and b :
– If b / a >= 1, a new process is not accepted
until all the accepted processes have finished,
so SRR becomes FCFS.
– If b / a = 0, all processes are accepted
immediately, so SRR becomes RR.
– If 0 < b / a < 1, accepted processes are
selfish, but not completely.
• Demonstration a = 2 and b=1
Selfish Round Robin (SRR)
• Demonstration a = 2 and b=1
Lottery Scheduling: Flexible Proportional-Share
Resource Management
• Novelty :
– Lottery scheduling provides efficient, responsive
control over the relative execution rates of
computations.
– Such control is beyond the capabilities of
conventional schedulers.
– Desirable in systems that service requests of varying
importance, such as databases, media-based
applications, and networks.
Lottery Scheduling: Flexible Proportional-Share
Resource Management
• Lottery Scheduling:
– Lottery scheduling is a randomized resource
allocation mechanism.
– Resource rights are represented by lottery tickets.
– Each allocation is determined by holding a lottery;
the resource is granted to the client with the winning
ticket.
– This effectively allocates resources to competing
clients in proportion to the number of tickets that
they hold.
Lottery Scheduling: Flexible Proportional-Share
Resource Management
• Demonstration:
Lottery Scheduling: Flexible Proportional-Share
Resource Management
• Resource Rights:
– Lottery tickets encapsulate resource rights that are abstract,
relative, and uniform.
abstract:
– They quantify resource rights independently of machine details.
relative:
– since the fraction of a resource that they represent varies
dynamically in proportion to the contention for that resource.
Thus, a client will obtain more of a lightly contended resource
than one that is highly contended.
uniform:
– because rights for heterogeneous resources can be
homogeneously represented as tickets.
Lottery Scheduling: Flexible Proportional-Share
Resource Management
• Lotteries:
– Scheduling by lottery is probabilistically fair. The
expected allocation of resources to clients is
proportional to the number of tickets that they hold.
• Lotteries:
– The number of lotteries won by a client has a
binomial distribution.
– Probability p that a client holding t tickets will
win a given lottery with total T tickets is given
by: p = t / T.
– After n identical lotteries, the expected
number of wins w is given by: E[w] = np with
variance σw2 = n.p.(1-p).
Lottery Scheduling: Flexible Proportional-Share
Resource Management
• Lotteries:
– The coefficient of variation for the observed
proportion of wins is
σw / E[w] =
– Thus, a client’s throughput is proportional to its ticket
allocation, with accuracy that improves with √n.
Lottery Scheduling: Flexible Proportional-Share
Resource Management
• Lotteries:
– The number of lotteries required for a client’s first
win has a geometric distribution.
– The expected number of lotteries n that a client must
wait before its first win is given by:
E[n] = 1/p
with variance σn2 = (1-p) / p2 .
– So, a clients average response time is inversely
proportional to ticket allocation.
Lottery Scheduling: Flexible Proportional-Share
Resource Management
• Lotteries:
– Since any client with a non-zero number of tickets
will eventually win a lottery, the conventional
problem of starvation does not exist.
– The lottery mechanism also operates fairly when the
number of clients or tickets varies dynamically.
– For each allocation, every client is given a fair
chance of winning proportional to its share of the
total number of tickets. Since any changes to
relative ticket allocations are immediately reflected in
the next allocation decision, lottery scheduling is
extremely responsive.
Lottery Scheduling: Flexible Proportional-Share
Resource Management
• Gives
– Good response time for interactive processes
– Good throughput for CPU bound processes
Above 25 14 11 9 7 5
normal
Normal 24 13 10 8 6 4 BASE
PRIORITY
Below 23 12 9 7 5 3
normal
Lowest 22 11 8 6 4 2
Idle 16 1 1 1 1 1
Case study: Windows 2000
SCHED_RR Scheduling
• SCHED_RR tasks have timeslices and are always
preempted by SCHED_FIFO tasks.
• SCHED_RR tasks are scheduled by priority, and within
a certain priority they are scheduled in a round-robin
fashion.
• Each SCHED_RR task within a certain priority runs for
its allotted timeslice, and then returns to the bottom of
the list in its priority array queue.
End of Session 4