Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Operating Systems Principles Process Management and Coordination Lecture 5: Process and Thread Scheduling 主講人:虞台文 Content Organization of Schedulers – – Scheduling Methods – – – Embedded and Autonomous Schedulers Priority Scheduling A Framework for Scheduling Common Scheduling Algorithms Comparison of Methods Priority Inversion Operating Systems Principles Process Management and Coordination Lecture 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 to Ready List (“RL”) after creation (When and in which order?) Process Dispatching – – Short term scheduling Select process from Ready 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 processes alternate pi: process S : scheduler Organization of Schedulers Embedded – – Called as function at end of kernel call Windows Runs as part2000 of calling process OS S OS S OS S OS S p1 p2 p3 p4 OS OS OS OS OS p1 p2 p3 p4 S Autonomous – – – Separate process May have dedicated CPU on a multiprocessor Unix On single-processor, run at every quantum: scheduler and other processes alternate 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. Highest front n n1 RL[n] 2 Lowest 1 rear Status.Type {running, ready_a, ready_s} The Processes in Ready List n n1 2 1 front rear The Scheduler The Scheduler The scheduler may be invoked periodically when some events affect 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 Some processes in The scheduler may be invoked RL can get CPU. Running processes may be preempted. periodically when some events affect 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 caller to the scheduler may be a process to be • blocked/suspended or • awakened/activated Invoking the Scheduler Request(res) { if (Free(res)) Allocate(res, self) else { Block(self, res); Scheduler(); } } Release(res) { Deallocate(res, self); if (Process_Blocked_in(res,pr)) { Allocate(res, pr); Unblock(pr, res); Scheduler(); } } The caller to the scheduler may be a process to be • blocked/suspended or • awakened/activated Invoking the Scheduler Create(s0, m0, pi, pid) { p = Get_New_PCB(); pid = Get_New_PID(); p->ID = pid; p->CPU_State = s0; p->Memory = m0; p->Priority = pi; p->Status.Type = ’ready_s’; p->Status.List = RL; p->Creation_Tree.Parent = self; p->Creation_Tree.Child = NULL; insert(self-> Creation_Tree.Child, p); insert(RL, p); Activate(); Scheduler(); } Activate(pid) Activate(pid) {{ pp == Get_PCB(pid); Get_PCB(pid); if if (p->Status.Type (p->Status.Type == == ’ready_s’) ’ready_s’) {{ p->Status.Type p->Status.Type == ’ready_a’; ’ready_a’; Scheduler(); Scheduler(); }} else else p->Status.Type p->Status.Type == ’blocked_a’; ’blocked_a’; }} Suspend(pid) Suspend(pid) {{ pp == Get_PCB(pid); Get_PCB(pid); ss == p->Status.Type; p->Status.Type; if if ((s==’blocked_a’)||(s==’blocked_s’)) ((s==’blocked_a’)||(s==’blocked_s’)) p->Status.Type p->Status.Type == ’blocked_s’; ’blocked_s’; else else p->Status.Type p->Status.Type == ’ready_s’; ’ready_s’; if (s==’running’) { if (s==’running’) { cpu cpu == p->Processor_ID; p->Processor_ID; p->CPU_State p->CPU_State == Interrupt(cpu); Interrupt(cpu); Scheduler(); Scheduler(); }} }} Destroy(pid) Destroy(pid) {{ pp == Get_PCB(pid); Get_PCB(pid); Kill_Tree(p); Kill_Tree(p); Scheduler(); 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; If free CPUs are available, allocate these free CPUs to highFind a free cpu; priority processes in ready_a state if (cpu != NIL) Allocate_CPU(p,cpu); } while (cpu != NIL); do { Find highest priority ready_a process p; To preempt low-priority running processes by high-priority Find lowest priority running process q; ready_a ones. 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, preempt itself. } 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; To preempt low-priority running processes by high-priority Find lowest priority running process q; ready_a ones. 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, preempt itself. } 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, preempt itself. } 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 Principles Process Management and Coordination Lecture 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 A scheduling policy is determined by: • Decision mode • Priority function • Arbitration Rule The General Framework When to schedule? – – Preemptive Decision Mode Nonpreemptive Who to schedule? – Priority evaluation – Arbitration to break ties A scheduling policy is determined by: • Decision mode • Priority function • Arbitration Rule 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 A scheduling policy is determined by: • Decision mode • Priority function • Arbitration Rule The Priority Function 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) Memory requirements (mostly for batch) System load (not process-specific) A scheduling policy is determined by: • Decision mode • Priority function • Arbitration Rule 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) 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) Example Processes for Scheduling 0 p1 p2 1 2 3 4 5 6 7 8 9 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) FIFO 0 1 2 3 4 5 6 7 8 p1 p2 p1 CPU does other things Start Scheduling p2 9 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) FIFO 0 1 2 3 4 5 6 7 8 p1 p2 p1 CPU does other things Start Scheduling p2 9 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) SJF 0 1 2 3 4 5 6 7 p1 p2 p2 CPU does other things Start Scheduling p1 8 9 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) SJF 0 1 2 3 4 5 6 7 p1 p2 p2 CPU does other things Start Scheduling p1 8 9 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) SRT 0 1 2 3 4 5 6 p1 p2 p1 CPU does other things Start Scheduling p1 p2 7 8 9 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) SRT 0 1 2 3 4 5 6 p1 p2 p1 CPU does other things Start Scheduling p1 p2 7 8 9 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) RR 0 1 2 3 4 5 6 7 8 9 p1 p2 p1 CPU does other things Start Scheduling p1 p1 p2 Assume a time-quantum of 0.1 time units. 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) RR 0 1 2 3 4 5 6 7 8 9 p1 p2 p1 CPU does other things Start Scheduling p1 p1 p2 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) Each process has a fixed priority. ML Highest n n1 2 Lowest 1 front rear 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) Each process has a fixed priority. ML (Nonpreemptive) Highest n n1 2 Lowest 1 front rear 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) Each process has a fixed priority. ML (Preemptive) Highest n n1 2 Lowest 1 front rear 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) 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) ML (Nonpreemptive) 0 1 2 3 4 5 6 7 8 ep1 = 15 p1 ep2 = 14 p2 p1 CPU does other things Start Scheduling p2 9 MLF • 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 (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. • Find P for given a: priority n n–1 n–2 ... n–i attained time a<T a<T+2T a<T+2T+4T ... 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. • Find P for given a: priority n n–1 n–2 ... n–i attained time a<T a<T+2T a<T+2T+4T ... 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 – – – 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) 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 rd number of completed periods r%d time in current period d–r%d time remaining in current period P = –(d – r % d) Common Scheduling Algorithms Comparison of Methods • FIFO, SJF, SRT: Primarily for batch systems • FIFO is the simplest • SJF & SRT have better average turnaround times: 1 n AVT ri n i 1 ri: the real time that the ith process spends in the system. Example p1 p2 p3 arrival t service t t3 2 1 4 Comparison of average turnaround time 1 n AVT ri n i 1 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 – When q , RR approaches FIFO – When q 0, context switch overhead 100% – When q >> context switch overhead, n processes run concurrently at 1/n CPU speed Interactive Systems n front rear n1 2 1 Most dynamic schemes 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. Comparison of Methods • Real-time systems – Feasible = All deadlines are met – CPU utilization is defined as: U=∑ ti/di – Schedule is feasible if U 1 – EDF always yields feasible schedule (if U 1) – RM yields feasible schedule if U 0.7 CPU Utilization U t1 t2 1 3 0.85 d1 d 2 4 5 Example: RM and EDF 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 p1 p2 RM EDF p1 p2 p1 p2 CPU Utilization U t1 t2 1.5 3 0.975 d1 d 2 4 5 Example: RM and EDF 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 p1 p2 RM EDF p1 p2 p1 p2 fail Operating Systems Principles Process Management and Coordination Lecture 5: Process and Thread Scheduling Priority Inversion • Assume priority order p1 > p2 > p3. • p1 and p3 share common resource. • p2 is independent of p1 and p3 . Priority Inversion Problem p1 p2 p3 P(mutex) P(mutex) CS_1 CS_3 V(mutex) Program1 Program2 V(mutex) Program3 • Assume priority order p1 > p2 > p3. • p1 and p3 share common resource. • p2 is independent of p1 and p3 . Priority Inversion Problem p1 p2 P(mutex) CS_1 V(mutex) Program1 p3 P(mutex) Program2 CS_3 V(mutex) Program3 • Assume priority order p1 > p2 > p3. • p1 and p3 share common resource. • p2 is independent of p1 and p3 . Priority Inversion Problem p1 p2 P(mutex) CS_1 p3 Program2 V(mutex) Program1 P(mutex) CS_3 V(mutex) Program3 • Assume priority order p1 > p2 > p3. • p1 and p3 share common resource. • p2 is independent of p1 and p3 . Priority Inversion Problem p1 p2 P(mutex) CS_1 p3 Program2 V(mutex) Program1 P(mutex) CS_3 V(mutex) Program3 (Unrelated) p2 may delay p1 indefinitely. Solutions 1. Make CSs nonpreemptable 2. Practical is CSs are short and few Unsatisfactory if high-priority process often found themselves waiting for lower-priority process, especially unrelated ones. Naïve “solution”: Always run CS at priority of highest process that shares the CS 3. Problem: p1 cannot preempt lower-priority process inside CS, even when it does not try to enter CS -- a different form of priority inversion. Dynamic Priority Inheritance See Next Sha, Rajkumar, and Lehocsky 1990 Dynamic Priority Inheritance p3 is in its CS p1 attempts to enter its CS p3 inherits p1’s (higher) priority for the duration of CS p1 p2 P(mutex) CS_1 V(mutex) Program1 p3 P(mutex) Program2 CS_3 V(mutex) Program3 • Assume priority order p1 > p2 > p3. • p1 and p3 share common resource. • p2 is independent of p1 and p3 . p3 is in its CS p1 attempts to enter its CS p3 inherits p1’s (higher) priority for the duration of CS Dynamic Priority Inheritance p1 p2 P(mutex) CS_1 V(mutex) Program1 p3 P(mutex) Program2 CS_3 V(mutex) Program3 • Assume priority order p1 > p2 > p3. • p1 and p3 share common resource. • p2 is independent of p1 and p3 . p3 is in its CS p1 attempts to enter its CS p3 inherits p1’s (higher) priority for the duration of CS Dynamic Priority Inheritance p1 P(mutex) CS_1 V(mutex) Program1 p2 p3 CS_3 V(mutex) P(mutex) Program2 Program3 • Assume priority order p1 > p2 > p3. • p1 and p3 share common resource. • p2 is independent of p1 and p3 . References Rensselaer Polytechnic Institute http://www.cs.rpi.edu/academics/courses/fall04/os/c8/ University of east London Inside the Windows NT Scheduler, Part 1 Inside the Windows NT Scheduler, Part 2 Reading Assignment Chapter 6 CPU Scheduling in Operating System Concepts, Seventh Edition, by Abraham Silberschatz, Peter Baer Galvin, Greg Gagne, John Wiley & Sons, Inc.