1 / 64

Process and Thread Scheduling: Principles and Methods

This lecture covers the organization and implementation of schedulers, priority scheduling, scheduling methods, common scheduling algorithms, and priority inversion.

mcornish
Download Presentation

Process and Thread Scheduling: Principles and Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operating Systems PrinciplesProcess Management and CoordinationLecture 5:Process and Thread Scheduling 主講人:虞台文

  2. Content • Organization of Schedulers • Embedded and Autonomous Schedulers • Priority Scheduling • Scheduling Methods • A Framework for Scheduling • Common Scheduling Algorithms • Comparison of Methods • Priority Inversion

  3. Operating Systems PrinciplesProcess Management and CoordinationLecture 5:Process and Thread Scheduling Organization of Schedulers

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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]

  9. Status.Type {running, ready_a, ready_s} The Processes in Ready List

  10. The Scheduler

  11. 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

  12. 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.

  13. The caller to the scheduler may be a process to be • blocked/suspended or • awakened/activated Invoking the Scheduler

  14. The caller to the scheduler may be a process to be • blocked/suspended or • awakened/activated Invoking the Scheduler

  15. 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.

  16. 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.

  17. 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.

  18. 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); }

  19. 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); }

  20. Operating Systems PrinciplesProcess Management and CoordinationLecture 5:Process and Thread Scheduling Scheduling Methods

  21. 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

  22. 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

  23. 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

  24. 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)

  25. 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)

  26. 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)

  27. 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)

  28. 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)

  29. 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)

  30. 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)

  31. 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)

  32. 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)

  33. 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)

  34. 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.

  35. 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.

  36. 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)

  37. 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)

  38. 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)

  39. 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)

  40. 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

  41. 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

  42. 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)

  43. 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)

  44. 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)

  45. Common Scheduling Algorithms

  46. 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.

  47. arrival service p1 t 4 p2 t 2 p3 t  3 1 Comparison of average turnaround time Example

  48. Comparison of Methods • Time-sharing systems • Response time is critical • RR or MLF with RR within each queue are suitable

  49. 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

  50. 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.

More Related