Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
M C Paull 2005
Units
Job: A Job is associated with a program. It consists of the total of all CPU and I/O action
necessary for completion of a given run of a program. The raw time for a Job is the time it
would take if it alone had access to CPU and I/O.
CPU Bursts: The CPU. receives a series of uses each called a burst. These come from
different processes, but their time distribution, independent of where they come from, can
be used in evaluation of different scheduling algorithms.
Measures Of Performance Usually interest is in the average value of these Performance Measures.
Turnaround: The total time Interval [tfinish(Pi)-tstart(Pi)] between availability for running and
completion of a job, including I/O, CPU and waiting time. Particularly Important In batch
environment. Avg and Max.
It can be applied to CPU burst in which case It is the time interval from the instant the process
generating the burst enters the ready state (queue) till it completes its burst in the CPU.
(User Satisfaction)
Turnaroundave = i=1to M [tfinish(Pi)-tstart(Pi)] / M M = Number of Jobs
Throughput: The Number of Jobs completed per unit time
(Overall Efficiency)
Throughput = Number of Jobs / [tfinish(all Jobs)tstart(1st Job)]
(1/Throughput = The average Time (Spent Computing) per Process.)
Response Time: The CPU Interval Between request for attention and delivery of attention.
Important in time sharing environments.
(User Satisfaction)
Waiting Time: The time between entering the ready state, (entering the ready queue), and
entering the active state, Applied to CPU bursts.
(User Satisfaction)
CPU Utilization: The percentage or fraction of total time the CPU is in use excluding OS.
If all processes other than OS are doing I/O the CPU is not in use.- This is effected by I/O
usage, context switches and MM size.
(Overall Efficiency)
J1 J2
J3 J4
time complete
J1 J2
J3 J4
J2
I/O
J4
I/O
J3
I/O
time
job submitted
At Terminal
Requested
Request To
Program Runs
TURNAROUND TIME(BATCH)
Run Program
time job complete Typed
RESPONSE TIME
SCHEDULING BASICS
M C Paull 2005
P6
os
os
ReponseTime
P6
P7
os
os
P7
os
os
P5
P5
P8
P8
os
P4
to
os
P3
tp
os
P1
os
os
P4
P1
Quanta
os
os
Context = Context
Switch
Switch
time
time
<
Quanta
P2
os
P3
os
os
P2
Waiting Time
For 8 processes
treturn = 8to + 7tp
For n processes:
treturn = nto + (n-1)tp
treturn is the response time, the interval between a
request for attention and getting that attention. To
make this reasonably small it is desirable to
keep tp, the quantum, LOW.
Utilization
fuseful = tp /( tp + to ) = 1 / (1 + to /tp )
fuseful is the fraction of the time that processes are
active. To keep this reasonably high it is desirable to
keep tp, the quantum HIGH..
Also Turnaround improves with larger quanta.
Waiting
Utilization
CPU
Ready Queue
4
5 enters 0 5
.8 6
3 2 1
1 leaves
4 3 2
5
1
1
4 3
2
2
1.6 7
6 5 4
2.4 8
7 6 5
3.2 9
8 7 6
4.8 10
9 8 7
3
3
5 leaves
4
5
Example
Death rate(leaving queue) = Number alive today(in queue) /Average time alive(in queue) = Birth rate
Rate people arrive at diner = Number in Diner(in queue) / Average time in Diner (in queue)
100 wait queue occupants(in Diner), average time in wait queue (stay in Diner) = 2 hours
leaving rate (= arrival rate) = 100/2 = 50 per hour. (if they leave uniformly? 25 will have left
in 1/2 hour which means if there are 25 ahead of you you can expect to wait 1/2 hour.
USES
Arrivals
Turnaround Time For Job J is the time from its submission to its
completion. For a CPU burst it is the time from entry in the Ready Q
to entry into state Active
J5
J4
J3
J2
J1
2+6+7+8+9/5=32/5=6+2/5
turnaround
J4
J3
J2
In the following chart the time required to run the ith job, Ji ,started in
n t0 + (n-1)
t1 + . . . +
2 t n-2 + 1 t n-1
0 1 2 3 4 5 6 7 8 9
All arrive at time 0 Shortest First Optimal
Ave # in Q = [4*1 + 3*1 +2*1 +1*2] / 9 =11/9
Ave Wait in Q = [1 + 2 + 3 + 5] / 5 = 11/5
Arrival Rate = 5/9
Arrival Rate = Ave # in Q / Ave Wait in Q
J2
J1
Arrivals2
Total
J2
J1
J5
J4
J3
2+6+4+5+6=23/5
J5
J4
J3
0 1 2 3 4 5 6 7 8 9
More than 1 arrival time
Shortest First Without Preemption
Non-Optimal
2 +1+2+3+9**=17**/5
J2
J1
Arrivals2
nth
t0 + t1 + t2 + . . . + tn-2
t0 + t1 + t2 + . . . + tn-2 + tn-1
1+2+3+5+9/5=20/5=4
turnaround
Arrivals1
n-1st
.
.
.
Arrivals
Order
Of
Entry
t0
Arrivals1
Turnaround Time
t0 + t1
t0 + t1 + t2
J1
0 1 2 3 4 5 6 7 8 9
All arrive at time 0 Non-Optimal Ordering
Ave # in Q = [4(jobs)*2(time) + 3*4+2*1+1*1] / 9
= 23/9
Ave Wait in Q = [2(till entry)+ 6+7+8] / 5 = 23/5
Arrival Rate = 5/9
Arrival Rate = Ave # in Q / Ave Wait in Q
5/9 = [23/9] / [23/]5 Littles Law
batch mode is ti
1st
2nd
3rd
.
.
.
Examples
J5
J5
J4
J3
Time save
and/or restore
job stateContext Switch
0 1 2 3 4 5 6 7 8 9
More than 1 arrival time
Shortest First With Preemption
Is Optimal if context switch uses little time
SHORTEST RUN TIME FIRST (NEXT)- BATCH, CPU BURSTS TURNAROUND TIME
M C Paull 2005
J1 J2 J3
J3 J1 J2
J3 J2 J1
Job
completed
t1 =
t1 + t2 =
t1 + t2 + t3 =
Sum
t1 = time to complete J1 when running round robin
t1 + t2 = time to complete J2 when running round robin
t1 + t2 + t3 = time to complete J3 when running round robin
EXAMPLE
There are n jobs, J1, the smallest, through Jn, requiring respectively N1 < N2<...<Nn quanta
Ni+1-Ni = Di+1,i , Qi is the number of quanta in Round Robin executed to before Ji is completed.
After the i-1th job is completed its column is removed. di is the number of the remaining columns
starting from the leftmost one, that have to be passed to reach the column (and including that
column) at which the ith job is located.
The total of all turnaround times =j=1Qi.. The only variation in the result due to reordering of the Jobs i
is the value of di . di = 1, for all i, is the smallest it can be. It takes this value if the jobs are ordered
so that the quanta for the ith smallest job is done ith.
j=n
dj = (n + n-1 +. . .+ 1) =n(n-1)/2 = (n2-n)/2 Worst Case
j=1
j=n
dj = (1 + 1 +. . .+ 1) = n
Best Case
j=1
(n2-n)/2 - n = (n2-3n)/2 = O(n2)
Difference
The difference in average turnaround between the best and worst ordering is low compared to that
difference when the same number of jobs is run in batch mode.
CPU
IO
Process P2
CPU Bound
Process P1
IO Bound
Poor Scheduling
CPU
IO
Process P1
IO Bound
Process P2
IO Bound
Process P3
CPU Bound
Context Switch
No IO Overlap
IO Overlap
Suppose a Process, P1, consistently blocks early in its Quanta on a Semaphor (down)
This implies it has a fast response to whatever its partner Process, P2, did when it
ran-or perhaps P2 did not run at all since P1 last left. Decreasing P1's and/or
Increasing P2's Priority should allow P1 to run for a longer time when it is made active
since the partner P2 will have more time to produce work for P1. .
85 |---6---| 79
Size Of
Quantum
79
90
90 |--- 6 ---| 84
84 |--- 6---| 78
80
80
80
80
80 |---6-
WITHOUT I/O
109
wr
109
80 |--- 6---| 74
74
wr
109
|---5--|
---|
91
97 |--- 6
90
109
rd
(6/5)90
90
90 |--4--| 135
80
80
80
(6/4)90
rd
tm
135 |---6 ---| 129
Compensates Small
bursts by giving more
bursts
WITH I/O
Priority Decreases By 1 Quantum After Each Timeout And Increases To:
Priority = Quantum / Time_Used When The Time_Used < Quantum
EXAMPLE
Quantum = 6
Priority is designed to Gives Smaller CPU Bursts (I/O Bound Jobs) High Priority. If smaller CPU bursts
are done earlier then the Average Queue Waiting Time is Minimized, as is the Average (CPU burst)
Turnaround time. Future use of IO is assumed proportional to last use of I/O. The nature of Jobs
is that some are I/O bound and others CPU bound.
Also instead of running the highest Priority process one may let the Priority determine the
probability that a Process will run.
Priority Levels:-Queues
(Assumes groups of Processes have essentially same Priority)
Hi
-3
-2
.
UNIX Numbering
(So Usage Can Be
Added To Priority
and Result In
Lower Priority)
-1
0
Round Robin At Each level
1
2
3
Lo 4
USAGE CALCULATIONS
[Discounts Past]
GENERAL
f<1
usage(0) = estimated usage = T
usage(n) = f(usage(n-1))+ [1 - f ](ticks(n-1-to-n)0 per process
f = 1/2
usage(0) = estimated usage = T
usage(n) = 1/2(usage(n-1)) + 1/2(ticks(n-1-to-n)) per process
usage(1) = 1/2(usage(0)) + 1/2(ticks(0-to-1)) = 1/2T + 1/2(ticks(0-to-1))
usage(2) = 1/2(usage(1)) + 1/2(ticks(1-to-2))
= 1/4T + 1/4(ticks(0-to-1)) + 1/2(ticks(1-to-2))
usage(3) = 1/2(usage(2)) + 1/2(ticks(2-to-3))
= 1/8T + 1/8(ticks(0-to-1)) + 1/4(ticks(1-to-2)) + 1/2(ticks(1-to-2))
recursive definition
xT if f =1/x
closed form
Proof
(1- fn+1)T / (1-f) = f * [(1- fn)T / (1-f)] +T
(1- fn+1)T = f * (1- fn)T + T(1-f) = f * (1- fn)T + T(1-f) = fT - fn+1T + T - Tf = fn+1T + T = (1- fn+1)T
10
241
300
Number Of ticksf
202
priority= -2 + 5 + 0 = 3
2 02
2 02
202
4 2 4 0 |--6--| 2 8 0
3 0 0 |--6--|
2
1 rd
terminal
-2
280
280
1 2-2 |--4--| 1 5 -2
204
204
103
103
2 0 0 |--6--| 2 6 0 1 rd -5
usage=(1/2(2)+4) =5 1 user
2 0 2
disk
1 Reset priorities-Priority 1 level has most usage decrease priority (increase number)
2 Priority 0 level runs for some time, but one goes to IO so others in priority level 0 get lots of usage.
3 When I/O completes returning process gets high priority -2
4At next Priority Reset priorities many changes in priority levels of processes.
equally
distributed
Priority Actual
Number Priority
+
-
input gets -3
I/O gets -5
11
T/3/2=T/6
T/3
T/3
T/3/4=T/12
P1
T/3
P1
P1
P2
T/3
P1
P2
P3
P2
P3
P4
user1 user2 user3
P2
T/3
T/3
P1
P1
P2
P2
P3
P3
T/4 T4 T4 T4 T4/4=T/16
user1 user2 P1
P1
P1
P1
P2
P2
P2
P3
P3 P2 P3 P4
user1user2user3user4
Each User Is given equal time Decide on how much to give each Process
tentitled_user_i
7 Current Users
tcurrent
tlogin_user_i
tentitled_user_i =
12
All processes are given a number of tiickets. When the OS is to choose a member of the Ready Queue
to run. Assume Pj has xj tickets. These are tickets are now numbered for each Pj Processes are
ordered so that P1 has x1 tickets numbered 1 to x1 P2 has x2 tickets numbered 1+ x1 to x1 + x2 etc.
Then OS chooses a number in the range of the assigned numbers randomly. The process assigned
that number is given control of the CPU next.
xi
Priority = Probability Of Pj Getting Control =
i=n
xi
i=1
1
2
3
4
5
6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
2/10 1/10
3/10
1/10
2/10 1/10
Example
Pj
tickets
Probability Of Pj
Lottery Scheduling
A Probabilistic Way Of Specifying Priority
Gives Precise Meaning To Priority
To multiply the probability of being chosen by k it is necessary to know how many additional tickets are
necessary. So suppose process P has x tickets and there are a total of S tickets So Ps probability of
being chosen when ready = x/S. Now we wish to change the probability to kx/S. We have to decide the
numberof tickets, t, to be added or subtracted:
kx / S = ( x+t ) /(S+t)
Sx+St = kxS + kxt
t(S-kx) = xS(k-1)
t = x(k-1) (1 - kx/S) Note k-1 is positive if k>1, otherwise negative
But now there are a different number of tickets, so one whose previous probability was y/S now has
probability y /S+t = qy/S (q times its previous probability) and q = S/S+t . This is inevitable since the
total of all probabilies must be 1.
Note On Changing Probabilities
Increasing and Decreasing Priority-Give and Take a Tickets
13
SOFT REAL-TIME: Must Guarantee That Each Process Gets The Assigned Priority.
(Can Usually Be Handled by a General System)
HARD REAL-TIME: Must Guarantee: Each Process Gets The Needed Time (Often Requires Special
Hardware- In a general System Page Replacements, etc Are Not Sufficiently Predictable
Hard Real Time Scheduling: The scheduling of multiple competing processes some or all of which have
deadlines.
Examples:
A real-time multi-media scheduler each running periodically-maybe sending out successive frames
of a movie-sent out at different rates because of the terminal facilities for handling different rates
(different band-widths), and the fact that sound and video require different rates,and because video
compression-decompression is often involved.
So what we have n periodically active processes. The key aspects are the periods or times from
the start of one burst of activity to the next of each process. These are P1, P2,....Pn , and the length
,
of the bursts; L1, L2,....Ln , From these we calculate a deadline for each process, that is the
maximum time after the start of a processes burst by which it must again start another burst.
With pre-emption for the ith process this is tcurrent + Pi - remainder of (Li).
Schedulable
Here is a necessary, not itself sufficient condition for periodic processes to all be able to
run on a single processor, M in an acceptable order, but sufficient to all fit in the alloted time.
If there are n real time processes to be run on processor M, and the jth must be active a fraction Fj of
each of its period (interval) Pj, then they cannot all be handled unless
j=n
Fj 1
j=1
If the jth real time process is periodic with a period Pj during which time it must uses processor M
for time Cj , then Fj = Cj / Pj
So
j=n
[ Cj / Pj ] 1
j=1
This is a necessary, but not sufficient condition. Sufficiency depends on the extent to
which the Cj s can be broken up,
14
So it could all be done if, for example, the distributions above were OK
C1
F1 = C1 / P1 = 1/2
P1
C2
F2 = C2 / P2 = 1/2
P2
non-preemptive
preemptive
RMS
EDF
j=2
Fj = 1/2 + 1/2
j=1
EXAMPLE
1
B
RMS
EDF
OR
EXAMPLES
15
start<MID1>
<more>
end<MID1>
priority
start<HI>
down(resource)
<share>
up(resource)
<more>
if(time OK) sleep;
else (shut down)
end<Hi>
T1
up
down
up
down
Proc P Lo
wake
sleep
state
up
down
procH Hi
start<MID2>
<more>
down(resource)
<share>
up(resource)
<more>
end<MID2>
up
sleep
up
down
down
wake
sleep
up
down
PRIORITY INVERSION
Soft Real Time Problem
M C Paull 2005
17