640 likes | 650 Views
This lecture covers the organization and implementation of schedulers, priority scheduling, scheduling methods, common scheduling algorithms, and priority inversion.
E N D
Operating Systems PrinciplesProcess Management and CoordinationLecture 5:Process and Thread Scheduling 主講人:虞台文
Content • Organization of Schedulers • Embedded and Autonomous Schedulers • Priority Scheduling • Scheduling Methods • A Framework for Scheduling • Common Scheduling Algorithms • Comparison of Methods • Priority Inversion
Operating Systems PrinciplesProcess Management and CoordinationLecture 5:Process and Thread Scheduling Organization of Schedulers
We use “scheduling” to refer to both. Process Scheduling/Dispatching • Process scheduling • Long term scheduling • Move process toReady List (“RL”) after creation (When and in which order?) • Process Dispatching • Short term scheduling • Select process fromReady List to run
Organization of Schedulers • Embedded • Called as function at end of kernel call • Runs as part of calling process • Autonomous • Separate process • May have dedicated CPU on a multiprocessor • On single-processor,run at every quantum:scheduler and other processesalternate pi: process S: scheduler
OS OS OS OS OS OS OS OS p1 p2 p3 p3 p1 p4 p4 p2 Organization of Schedulers • Embedded • Called as function at end of kernel call • Runs as part of calling process • Autonomous • Separate process • May have dedicated CPU on a multiprocessor • On single-processor,run at every quantum:scheduler and other processesalternate S S S S Windows 2000 OS Unix S
Who runs next? Based on priority Priority Scheduling • Priority function returns numerical value P for process p: P = Priority(p) • Static priority: unchanged for lifetime of p • Dynamic priority: changes at runtime
Priority-Leveled Processes • Priority divides processes into levels • Implemented as multilevel ready list, say, RL • p at RL[i] run before q at RL[j] if i > j • p, q at same level are ordered by other criteria, e.g., the order of arrival. RL[n]
Status.Type {running, ready_a, ready_s} The Processes in Ready List
The Scheduler The scheduler may be invoked • periodically • when some eventsaffect the priorities of existing processes, e.g., • a process is blocked/suspended • a process is destroyed • a new process created • a process is activated
The scheduler/dispatcher should always keep the np (#processors) highest-priority active processes running. The Scheduler The scheduler may be invoked • periodically • when some eventsaffect the priorities of existing processes, e.g., • a process is blocked/suspended • a process is destroyed • a new process created • a process is activated Some processes in RL can get CPU. Running processes may be preempted.
The caller to the scheduler may be a process to be • blocked/suspended or • awakened/activated Invoking the Scheduler
The caller to the scheduler may be a process to be • blocked/suspended or • awakened/activated Invoking the Scheduler
The caller to the scheduler may be a process to be • blocked/suspended or • awakened/activated 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); } If free CPUs are available, allocate these free CPUs to high-priority processes in ready_a state To preempt low-priority running processes by high-priority ready_a ones. If the caller is going to be idled, preemptitself.
The caller to the scheduler may be a process to be • blocked/suspended or • awakened/activated 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); } To preempt low-priority running processes by high-priority ready_a ones. If the caller is going to be idled, preemptitself.
The caller to the scheduler may be a process to be • blocked/suspended or • awakened/activated 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); } If the caller is going to be idled, preemptitself.
The caller to the scheduler may be a process to be • blocked/suspended or • awakened/activated 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); }
How to simplify the scheduler for a uniprocessor system? 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); }
Operating Systems PrinciplesProcess Management and CoordinationLecture 5:Process and Thread Scheduling Scheduling Methods
Scheduling Policy • Scheduling based on certain criteria. • For examples: • batch jobs vs. interactive users • System processes vs. user processes • I/O bound processes vs. CPU bound processes
Decision mode • Priority function • Arbitration Rule A scheduling policy is determined by: The General Framework • When to schedule? • Preemptive • Nonpreemptive • Who to schedule? • Priority evaluation • Arbitration to break ties Decision Mode
Decision mode • Priority function • Arbitration Rule A scheduling policy is determined by: The Decision Modes • Preemptive: scheduler called • periodically (quantum-oriented) or • when system state changes More costly • Nonpreemptive: scheduler called • when process terminates or blocks Not adequate in real-time or time-shared systems
Decision mode • Priority function • Arbitration Rule A scheduling policy is determined by: The Priority Function • Possible parameters: • Attainedservice time (a) • Realtime in system (r) • Totalservice time (t) • Period(d) • Deadline (explicit or implied by period) • External priority (e) • Memory requirements (mostly for batch) • System load (not process-specific)
Decision mode • Priority function • Arbitration Rule A scheduling policy is determined by: The Arbitration Rules • Break ties among processes of the same priority • Random • Chronological (First In First Out = FIFO) • Cyclic (Round Robin = RR)
Common Scheduling Algorithms • First-In/First-Out (FIFO) • Shortest-Job-First (SJF) • Shortest-Remaining-Time (SRT) • Round-Robin (RR) • Multilevel Priority (ML) • Multilevel Feedback (MLF) • Rate Monotonic (RM) • Earliest Deadline (EDF)
0 1 2 3 4 5 6 7 8 9 p1 p2 Example Processesfor Scheduling • First-In/First-Out (FIFO) • Shortest-Job-First (SJF) • Shortest-Remaining-Time (SRT) • Round-Robin (RR) • Multilevel Priority (ML) • Multilevel Feedback (MLF) • Rate Monotonic (RM) • Earliest Deadline (EDF)
0 1 2 3 4 5 6 7 8 9 p1 p2 p1 p2 CPU does other things Start Scheduling FIFO • First-In/First-Out (FIFO) • Shortest-Job-First (SJF) • Shortest-Remaining-Time (SRT) • Round-Robin (RR) • Multilevel Priority (ML) • Multilevel Feedback (MLF) • Rate Monotonic (RM) • Earliest Deadline (EDF)
0 1 2 3 4 5 6 7 8 9 p1 p2 p1 p2 CPU does other things Start Scheduling FIFO • First-In/First-Out (FIFO) • Shortest-Job-First (SJF) • Shortest-Remaining-Time (SRT) • Round-Robin (RR) • Multilevel Priority (ML) • Multilevel Feedback (MLF) • Rate Monotonic (RM) • Earliest Deadline (EDF)
0 1 2 3 4 5 6 7 8 9 p1 p2 p2 p1 CPU does other things Start Scheduling SJF • First-In/First-Out (FIFO) • Shortest-Job-First (SJF) • Shortest-Remaining-Time (SRT) • Round-Robin (RR) • Multilevel Priority (ML) • Multilevel Feedback (MLF) • Rate Monotonic (RM) • Earliest Deadline (EDF)
0 1 2 3 4 5 6 7 8 9 p1 p2 p2 p1 CPU does other things Start Scheduling SJF • First-In/First-Out (FIFO) • Shortest-Job-First (SJF) • Shortest-Remaining-Time (SRT) • Round-Robin (RR) • Multilevel Priority (ML) • Multilevel Feedback (MLF) • Rate Monotonic (RM) • Earliest Deadline (EDF)
0 1 2 3 4 5 6 7 8 9 p1 p2 p1 p1 p2 CPU does other things Start Scheduling SRT • First-In/First-Out (FIFO) • Shortest-Job-First (SJF) • Shortest-Remaining-Time (SRT) • Round-Robin (RR) • Multilevel Priority (ML) • Multilevel Feedback (MLF) • Rate Monotonic (RM) • Earliest Deadline (EDF)
0 1 2 3 4 5 6 7 8 9 p1 p2 p1 p1 p2 CPU does other things Start Scheduling SRT • First-In/First-Out (FIFO) • Shortest-Job-First (SJF) • Shortest-Remaining-Time (SRT) • Round-Robin (RR) • Multilevel Priority (ML) • Multilevel Feedback (MLF) • Rate Monotonic (RM) • Earliest Deadline (EDF)
0 1 2 3 4 5 6 7 8 9 p1 p2 p1 p1 p1 p2 CPU does other things Start Scheduling RR • First-In/First-Out (FIFO) • Shortest-Job-First (SJF) • Shortest-Remaining-Time (SRT) • Round-Robin (RR) • Multilevel Priority (ML) • Multilevel Feedback (MLF) • Rate Monotonic (RM) • Earliest Deadline (EDF) Assume a time-quantum of 0.1 time units.
0 1 2 3 4 5 6 7 8 9 p1 p2 p1 p1 p1 p2 CPU does other things Start Scheduling RR • First-In/First-Out (FIFO) • Shortest-Job-First (SJF) • Shortest-Remaining-Time (SRT) • Round-Robin (RR) • Multilevel Priority (ML) • Multilevel Feedback (MLF) • Rate Monotonic (RM) • Earliest Deadline (EDF) Assume a time-quantum of 0.1 time units.
ML • First-In/First-Out (FIFO) • Shortest-Job-First (SJF) • Shortest-Remaining-Time (SRT) • Round-Robin (RR) • Multilevel Priority (ML) • Multilevel Feedback (MLF) • Rate Monotonic (RM) • Earliest Deadline (EDF)
n Highest front rear n 1 2 1 Lowest Each process has a fixed priority. ML • First-In/First-Out (FIFO) • Shortest-Job-First (SJF) • Shortest-Remaining-Time (SRT) • Round-Robin (RR) • Multilevel Priority (ML) • Multilevel Feedback (MLF) • Rate Monotonic (RM) • Earliest Deadline (EDF)
n Highest front rear n 1 2 1 Lowest Each process has a fixed priority. ML (Nonpreemptive) • First-In/First-Out (FIFO) • Shortest-Job-First (SJF) • Shortest-Remaining-Time (SRT) • Round-Robin (RR) • Multilevel Priority (ML) • Multilevel Feedback (MLF) • Rate Monotonic (RM) • Earliest Deadline (EDF)
n Highest front rear n 1 2 1 Lowest Each process has a fixed priority. ML (Preemptive) • First-In/First-Out (FIFO) • Shortest-Job-First (SJF) • Shortest-Remaining-Time (SRT) • Round-Robin (RR) • Multilevel Priority (ML) • Multilevel Feedback (MLF) • Rate Monotonic (RM) • Earliest Deadline (EDF)
0 1 2 3 4 5 6 7 8 9 p1 p2 p1 p2 CPU does other things Start Scheduling ML (Nonpreemptive) • First-In/First-Out (FIFO) • Shortest-Job-First (SJF) • Shortest-Remaining-Time (SRT) • Round-Robin (RR) • Multilevel Priority (ML) • Multilevel Feedback (MLF) • Rate Monotonic (RM) • Earliest Deadline (EDF) ep1 = 15 ep2 = 14
First-In/First-Out (FIFO) • Shortest-Job-First (SJF) • Shortest-Remaining-Time (SRT) • Round-Robin (RR) • Multilevel Priority (ML) • Multilevel Feedback (MLF) • Rate Monotonic (RM) • Earliest Deadline (EDF) MLF • Like ML, but prioritychangesdynamically • Every process enters at highest leveln • Each levelPprescribes maximum timetP • tPincreases asPdecreases • Typically: tn = T (constant) tP = 2 tP+1
First-In/First-Out (FIFO) • Shortest-Job-First (SJF) • Shortest-Remaining-Time (SRT) • Round-Robin (RR) • Multilevel Priority (ML) • Multilevel Feedback (MLF) • Rate Monotonic (RM) • Earliest Deadline (EDF) MLF a: attained service time • The priority of a process is a function of a. • FindPfor givena: priorityattained time n a<T n – 1a<T+2T n – 2a<T+2T+4T . . . . . . n – i a<(2i+1–1)T P = n – i = n – lg2(a/T+1)
First-In/First-Out (FIFO) • Shortest-Job-First (SJF) • Shortest-Remaining-Time (SRT) • Round-Robin (RR) • Multilevel Priority (ML) • Multilevel Feedback (MLF) • Rate Monotonic (RM) • Earliest Deadline (EDF) MLF a: attained service time • The priority of a process is a function of a. • FindPfor givena: priorityattained time n a<T n – 1a<T+2T n – 2a<T+2T+4T . . . . . . n – i a<(2i+1–1)T P = n – i = n – lg2(a/T+1)
First-In/First-Out (FIFO) • Shortest-Job-First (SJF) • Shortest-Remaining-Time (SRT) • Round-Robin (RR) • Multilevel Priority (ML) • Multilevel Feedback (MLF) • Rate Monotonic (RM) • Earliest Deadline (EDF) RM & EDF • RM • Intended for periodic (real-time) processes • Preemptive • Highest priority: shortest period:P = –d • EDF • Intended for periodic (real-time) processes • Preemptive • Highest priority: shortest time to next deadline r dnumber of completed periods r % dtime in current period d – r % dtime remaining in current period P = –(d – r % d)
Comparison of Methods • FIFO, SJF, SRT: Primarily for batch systems • FIFO is the simplest • SJF & SRT have better average turnaround times: ri: the real time that the ith process spends in the system.
arrival service p1 t 4 p2 t 2 p3 t 3 1 Comparison of average turnaround time Example
Comparison of Methods • Time-sharing systems • Response time is critical • RR or MLF with RR within each queue are suitable
Comparison of Methods • Time-sharing systems • Response time is critical • RR or MLF with RR within each queue are suitable • Choice of quantum determines overhead • Whenq , RR approaches FIFO • Whenq 0, context switchoverhead 100% • When q >>context switchoverhead,nprocesses run concurrently at1/nCPU speed
Interactive Systems • Most dynamicschemes tend to move interactive and I/O-bound processes to the top of the priority queues and to let CPU-bound processes drift to lower levels. • MLF puts I/O-completed processes into highest priority queue.