5. Process and thread scheduling

Download Report

Transcript 5. Process and thread scheduling

5. Process and thread scheduling
5.1 Organization of Schedulers
– Embedded and Autonomous Schedulers
5.2 Scheduling Methods
– A Framework for Scheduling
– Common Scheduling Algorithms
– Comparison of Methods
5.3 Priority Inversion
Operating Systems
1
Process and Thread Scheduling
• Scheduling occurs at two levels:
• Process/job scheduling
– Long term scheduling (seconds, minutes, …)
– Move process to Ready List (RL) after creation
(When and in which order?)
• Dispatching
– Short term scheduling (milliseconds)
– Select process from Ready List to run
• We use the term scheduling to refer to both
Operating Systems
2
Organization of Schedulers
• Embedded
– Called as function at
end of kernel call
– Runs as part of calling
process
• Autonomous
– Separate process
– Multiprocessor: may
have dedicated CPU
– Single-processor:
scheduler and other
processes alternate
(every quantum)
Operating Systems
3
An Embedded Scheduler
Scheduler() {
do {
find highest priority process p with p.status==ready_a;
find a free cpu;
if (cpu != NIL) Allocate_CPU(p,cpu);
} while (cpu != NIL);
do {
find highest priority process p with p.status==ready_a;
find lowest priority process q with p.status==running;
if (Priority(p) > Priority(q)) Preempt(p,q);
} while (Priority(p) > Priority(q));
if (self->Status.Type!=running) Preempt(p,self);
}
Operating Systems
4
Scheduling Methods
• Priority determines who runs
– Can be static or dynamic
– Given by Priority function: P = Priority(p)
• p are parameters
– Arbitration rule to break ties
• Random
• Chronological (FIFO)
• Cyclic (Round Robin = RR)
• When is scheduler invoked?—Decision mode
• Preemptive: scheduler called periodically
(quantum-oriented) or when system state changes
• Nonpreemptive: scheduler called when process
terminates or blocks
Operating Systems
5
Priority function Parameters
• Possible parameters:
– Attained service time (a)
– Real time in system (r)
– Total service time (t)
– Period (d)
– Deadline (explicit or implied by period)
– External priority (e)
– System load (not process-specific)
Operating Systems
6
Scheduling algorithms
Name
FIFO:
SJF:
SRT:
RR:
ML:
Decision mode
Priority
Arbitration
nonpreemptive
P=r
random
nonpreemptive
P = –t
chronological/random
preemptive
P = –(t–a)
chronological/random
preemptive
P=0
cyclic
preemptive
P=e
cyclic
nonpreemptive
P=e
chronological
• n fixed priority levels
• level P is serviced when n through P+1 empty
Operating Systems
7
Scheduling algorithms
• MLF (Multilevel
Feedback)
• Like ML, but priority
changes dynamically
• Every process enters at
highest level n
• Each level P prescribes
maximum time tP
• tP increases as P
decreases
• Typically:
tn = T (a constant)
tP = 2  tP+1
Operating Systems
8
Scheduling algorithms
• MLF priority function:
– depends on level n, and attained time, a
P = n– log2(a/T+1)
– derivation is in the book
– but note:
• there is no need to ever compute P
• priority is known automatically by position in queue
• MLF is preemptive: CPU is taken away whenever process
exhausts tP
• Arbitration rule (at highest non-empty level): cyclic (at
quantum) or chronological (at tP)
Operating Systems
9
Scheduling Algorithms
RM (Rate Monotonic):
– Intended for periodic (real-time) processes
– Preemptive
– Highest priority: shortest period: P = –d
EDF (Earliest Deadline First):
– Intended for periodic (real-time) processes
– Preemptive
– Highest priority: shortest time to next deadline:
P = –(d – r % d)
• derivation
rd
number of completed periods
r%d
time in current period
d–r%d
time remaining in current period
Operating Systems
10
Comparison of Methods
• FIFO, SJF, SRT: Primarily for batch systems
– total length of computation is the same for all
– FIFO simplest
– SJF & SRT have better average turnaround times:
(r1+r2+…+rn)/n
– Example:
Average turnaround times:
FIFO: ((0+5)+(3+2))/2 = 5.0
SRT: ((2+5)+(0+2))/2 = 4.5
SJF: same as FIFO (p1 arrived
first, non-preemptive)
Operating Systems
11
Comparison of Methods
• Time-sharing systems
– Response time is critical
– Use RR or MLF with RR within each queue
– Choice of quantum determines overhead
• When q  , RR approaches FIFO
• When q  0, context switch overhead  100%
• When q is much greater than context switch
overhead, n processes run concurrently at 1/n CPU
speed
Operating Systems
12
Comparison of Methods
• Real-time systems
– Feasible schedule: All deadlines are met
– CPU utilization is defined as: U=∑ ti/di for all pi
– If schedule is feasible, U  1 (but not vice versa):
• EDF always yields feasible schedule if U  1.
• RM yields feasible schedule if U<0.7
(approximately), otherwise, it may fail
Operating Systems
13
Comparison of Methods
• Example
– p1 has service time 1.5, period 4
– p2 has service time 3, period 5
– U=(1.5/4) +3/5=.975 < 1
– EDF succeeds but RM fails
Operating Systems
14