You are on page 1of 47

CPU Scheduling

Basic Concepts
Scheduling Criteria
Scheduling Algorithms
NultipleProcessor Scheduling
RealTime Scheduling
Algorithm Evaluation
Basic Concepts
Naximum CPU utilization obtained with
multiprogramming
CPU-!/O Burst Cycle - Process execution consists of
a cycle of CPU execution and !/O wait.
CPU burst distribution
Alternating Sequence of CPU And !/O
Bursts
CPU Scheduler
Selects from among the processes in memory that
are ready to execute, and allocates the CPU to one of
them.
CPU scheduling decisions may take place when a
process:
1.Switches from running to waiting state.
2.Switches from running to ready state.
3.Switches from waiting to ready.
4.Terminates.
Scheduling under 1 and 4 is nonpreemptive.
All other scheduling is preemptive.
ispatcher
ispatcher module gives control of the CPU to the
process selected by the shortterm scheduler, this
involves:
switching context
switching to user mode
jumping to the proper location in the user
program to restart that program
ispatch latency - time it takes for the dispatcher to
stop one process and start another running.
Scheduling Criteria
CPU utilization - keep the CPU as busy as
possible
Throughput - # of processes that complete
their execution per time unit
Turnaround time - amount of time to execute a
particular process
Waiting time - amount of time a process has
been waiting in the ready queue
Response time - amount of time it takes from
when a request was submitted until the first
response is produced, 56; output (for time
sharing environment)
Optimization Criteria
Nax CPU utilization
Nax throughput
Nin turnaround time
Nin waiting time
Nin response time
irstCome, irstServed (CS) Scheduling
Process Burst Time
P
1
24
P
2
3
P
3
3
Suppose that the processes arrive in the order: P
1
, P
2
, P
3
The Cantt Chart for the schedule is:
Waiting time for P
1
= 0, P
2
= 24, P
3
= 27
Average waiting time: (0 + 24 + 27)/3 = 17
P
1
P
2
P
3
21 2Z 30 0
CS Scheduling (Cont.)
Suppose that the processes arrive in the order
P
2
, P
3
, P
1
.
The Cantt chart for the schedule is:
Waiting time for P
1
= 6, P
2
= 0
,
P
3
= 3
Average waiting time: (6 + 0 + 3)/3 = 3
Nuch better than previous case.
Convoy effect short process behind long process
P
1
P
3
P
2
3 30 0
Shortest]obirst (S]R) 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:
nonpreemptive - 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
ShortestRemainingTimeirst (SRT).
S] is optimal - gives minimum average waiting time for a given
set of processes.
Process Arrival Time Burst Time
P
1
0.0 7
P
2
2.0 4
P
3
4.0 1
P
4
S.0 4
S] (nonpreemptive)
Average waiting time = (0 + 6 + 3 + 7)/4 4
Example of NonPreemptive S]
P
1
P
3
P
2
Z 3 1 0
P
1
8 12
Example of Preemptive S]
Process Arrival Time Burst Time
P
1
0.0 7
P
2
2.0 4
P
3
4.0 1
P
4
S.0 4
S] (preemptive)
Average waiting time = (3 + 1 + 0 +2)/4 3
P
1
P
3
P
2
1 2
11 0
P
1
5 Z
P
2
P
1
1
etermining Length of Next CPU
Burst
Can only estimate the length.
Can be done by using the length of previous CPU
bursts, using exponential averaging.
: Define 4.
1 0 , 3.
burst CPU next the for value predicted 2.
burst CPU of lenght actual 1.
A A

- -
:
1 3
9
3
3 9
9
3 3 3
: - - :

1
1

Prediction of the Length of the Next CPU
Burst
Examples of Exponential Averaging
- =0
:
n+1
= :
n
Recent history does not count.
- =1
:
n+1
= t
n
Only the actual last CPU burst counts.
!f we expand the formula, we get:
:
n+1
= - t
n
+(1 -) - t
n
1 + .
+(1 - )
j
- t
n
1 + .
+(1 - )
n=1
t
n
:
0
Since both - and (1 -) are less than or equal to 1, each
successive term has less weight than its predecessor.
Priority Scheduling
A priority number (integer) is associated with each
process
The CPU is allocated to the process with the highest
priority (smallest integer highest priority).
Preemptive
nonpreemptive
S] is a priority scheduling where priority is the
predicted next CPU burst time.
Problem Starvation - low priority processes may
never execute.
Solution Aging - as time progresses increase the
priority of the process.
Round Robin (RR)
Each process gets a small unit of CPU time (time
quantum), usually 10100 milliseconds. After this
time has elapsed, the process is preempted and
added to the end of the ready queue.
!f there are n processes in the ready queue and the
time quantum is q, then each process gets 1/n of the
CPU time in chunks of at most q time units at once.
No process waits more than (n1)q time units.
Performance
q large !O
q small q must be large with respect to context
switch, otherwise overhead is too high.
Example of RR with Time Ouantum = 20
Process Burst Time
P
1
S3
P
2
17
P
3
68
P
4
24
The Cantt chart is:
Typically, higher average turnaround than S],
but better response.
P
1
P
2
P
3
P
1
P
1
P
3
P
1
P
1
P
3
P
3
0 20 3Z 5Z ZZ 9Z 11Z 121 131 151 12
Time Ouantum and Context Switch Time
Turnaround Time varies With The Time
Ouantum
Nultilevel Oueue
Ready queue is partitioned into separate queues:
foreground (interactive)
background (batch)
Each queue has its own scheduling algorithm,
foreground - RR
background - CS
Scheduling must be done between the queues.
ixed 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 CS
Nultilevel Oueue Scheduling
Nultilevel eedback Oueue
A process can move between the various queues,
aging can be implemented this way.
Nultilevelfeedbackqueue scheduler defined by the
following parameters:
number of queues
scheduling algorithms for each queue
method used to determine when to upgrade a
process
method used to determine when to demote a
process
method used to determine which queue a process
will enter when that process needs service
Example of Nultilevel eedback
Oueue
Three queues:
O
0
- time quantum 8 milliseconds
O
1
- time quantum 16 milliseconds
O
2
- CS
Scheduling
A new job enters queue O
0
which is served CS. When it
gains CPU, job receives 8 milliseconds. !f it does not finish
in 8 milliseconds, job is moved to queue O
1
.
At O
1
job is again served CS and receives 16 additional
milliseconds. !f it still does not complete, it is preempted
and moved to queue O
2
.
Nultilevel eedback Oueues
NultipleProcessor Scheduling
CPU scheduling more complex when multiple CPUs
are available.
Homogeneous processors within a multiprocessor.
Load sharing
Asymmetric multiprocessing - only one processor
accesses the system data structures, alleviating the
need for data sharing.
RealTime Scheduling
Hard realtime systems - required to complete a
critical task within a guaranteed amount of time.
Soft realtime computing - requires that critical
processes receive priority over less fortunate ones.
ispatch Latency
Algorithm Evaluation
eterministic modeling - takes a particular
predetermined workload and defines the performance
of each algorithm for that workload.
Oueueing models
!mplementation
Evaluation of CPU Schedulers by
Simulation
Solaris 2 Scheduling
Windows 2000 Priorities
Process and Thread Scheduling
Process scheduling
Long term scheduling
Nove process to Ready List (RL") after creation
(When and in which order?)
ispatching
Short term scheduling
Select process from Ready List to run
We use scheduling" to refer to both
Organization of Schedulers
Embedded
Called as function at end of
kernel call
Runs as part of calling
process
Autonomous
Separate process
Nay have dedicated CPU on
a multiprocessor
On singleprocessor,
run at every quantum:
scheduler and other
processes alternate
Figure 5-1
Priority Scheduling
Priority function returns numerical value P for
process p: P = Pri6ri;p)
Static priority: unchanged for lifetime of p
ynamic priority: changes at runtime
Priority divides processes into levels
implemented as multilevel RL
p at RL|i] run before q at RL|j] if i>j
p, q at same level are ordered by other criteria
An Embedded Scheduler
Scheduler() {
do {
Find highest priority ready_a process p;
Find a free cpu;
if (cpu != NIL) Allocate_CPU(p,cpu);
} while (cpu != NIL);
do {
Find highest priority ready_a process p;
Find lowest priority running process q;
if (Priority(p) > Priority(q)) Preempt(p,q);
} while (Priority(p) > Priority(q));
if (self->Status.Type!='running') Preempt(p,self);
}
Scheduling Nethods
When is scheduler invoked?
ecision mode
Preemptive: scheduler called periodically
(quantumoriented) or when system state changes
Nonpreemptive: scheduler called when process terminates
or blocks
How does it select highest priority process?
Priority function: P = Pri6ri;p)
Arbitration rule: break ties
Random
Chronological (irst !n irst Out = !O)
Cyclic (Round Robin = RR)
Priority function
Possible parameters:
Attained service time (.)
Real time in system (r)
Total service time (;)
Period (1)
eadline (explicit or implied by period)
External priority ()
Nemory requirements (mostly for batch)
System load (not processspecific)
Scheduling algorithms
Name, ecision mode, Priority, Arbitration
!O: nonpreemptive P = r random
S]: nonpreemptive P = -; chronolgcl/random
SRT: preemptive P = -;-.) chronolgcl/random
RR: preemptive P = 0 cyclic
NL: preemptive P = cyclic
nonpreemptive P = chronological
n fixed priority levels
level P is serviced when n through P+1 empty
NL (Nultilevel eedback)
Like NL, but priority
changes dynamically
Every process enters at highest
level 5
Each level P prescribes
maximum time ;
P
;
P
increases as P decreases
Typically:
;
5
= T 065:;.5;)
;
P
= 2 L ;
P+1
Figure 5-3
Scheduling algorithms
NL priority function:
ind P = 5-i for given .:
priority attained time
5 .T
5-1 .T+2T
5-2 .T+2T+4T
. . . . . .
5-i .2
i+1
-1)T
ind smallest i such that .2
i+1
-1)T:
Solve for i: i = 3
2
.T+1)
P = 5-i = 5- 3
2
.T+1)
Scheduling Algorithms
Rate Nonotonic (RN):
!ntended for periodic (realtime) processes
Preemptive
Highest priority: shortest period: P = -1
Earliest eadline irst (E):
!ntended for periodic (realtime) processes
Preemptive
Highest priority: shortest time to next deadline
r 1 number of completed periods
r % 1 time in current period
1 - r % 1 time remaining in current period
P = -1 - r % 1)
Comparison of Nethods
!O, S], SRT: Primarily for batch systems
!O simplest, S] S SRT have better average
turnaround times: r1+r2+.+r5)5
FIFO: ((05)
(32))/2 5.0
SRT: ((25)
(02))/2 4.5
Figure 5-2
Comparison of Nethods
Timesharing systems
Response time is critical
RR or NL with RR within each queue are
suitable
Choice of quantum determines overhead
When q F C, RR approaches !O
When q F 0, context switch overhead F
100%
When q >> context switch overhead,
n processes run concurrently at 1/n CPU
speed
Comparison of Nethods
Realtime systems
easible = All deadlines are met
CPU utilization is defined as: U=7 t
i
/d
i
Schedule is feasible if U A 1
E always yields feasible schedule (if U A 1)
RN yields feasible schedule if U A 0.7
Figure 5-9
Priority !nversion Problem
Assume priority order p1>p2>p3
(Unrelated) p2 may delay p1 indefinitely.
Naive solution": Always run CS at priority of highest process that
shares the CS.
Problem: p1 cannot interrupt lowerpriority process inside CS a
different form of priority inversion.
Figure 5-10
Priority !nversion Problem
Solution: ynamic Priority !nheritance
p3 is in its CS
p1 attempts to enter its CS
p3 inherits p1's (higher) priority for the duration of CS
Figure 5-11

You might also like