1 / 58

Chapter 7: Scheduling

Chapter 7: Scheduling. Prof. Steven A. Demurjian, Sr. † Computer Science & Engineering Department The University of Connecticut 191 Auditorium Road, Box U-155 Storrs, CT 06269-3155. steve@engr.uconn.edu http://www.engr.uconn.edu/~steve (860) 486 - 4818.

Download Presentation

Chapter 7: Scheduling

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. Chapter 7: Scheduling Prof. Steven A. Demurjian, Sr. † Computer Science & Engineering Department The University of Connecticut 191 Auditorium Road, Box U-155 Storrs, CT 06269-3155 steve@engr.uconn.edu http://www.engr.uconn.edu/~steve (860) 486 - 4818 † These slides have been modified from a set of originals by Dr. Gary Nutt.

  2. Purpose of this Chapter • What is Role of Scheduling in an OS? • What are the Different Components of a Scheduler? • How Does a Scheduler Work in an OS? • How Does Scheduler Strategy Affect and Dictate Process Behavior? • What are Different Scheduling Policies? • Voluntary • Preemptive • What is the Role of Interrupts in Scheduling? • How Does Priority of Process Impact Scheduling?

  3. What is Role of Scheduling in an OS? • Assuming Multi-Programmed OS • Multiple Processes Loaded and Active in Executable Memory • CPU Time-Multiplexing Among Active Processes • Scheduler is Needed to … • Allow OS and Application Processes to Share CPU • Permit Processes Performing/Waiting for I/O to Release CPU • Scheduler Part of the Process Manager (Chapter 6) • Removal of Running Process from CPU • Selection of Another Process Based on Strategy

  4. Recall: Process Manager Overview Process Process Process Abstract Computing Environment Synchronization Process Description File Manager Protection Deadlock Process Manager Device Manager Memory Manager Resource Manager Resource Manager Scheduler Resource Manager Devices Memory CPU Other H/W

  5. What are the Different Components of a Scheduler? • Scheduling Policy Determines • When a Process is to be Removed from CPU • and Which Ready Process Should Run Next • Scheduling Mechanism Determines • How Process Manager Knows When to Time-Multiplex CPU • and How Process (De)Allocated From/To CPU • Typical Components Include: • Ready List • Enqueuer • Context Switcher • Dispatcher

  6. What Functions Does Each Components in a Scheduler? • Ready List: Process Manager Data Structure • Queue of Pointers to Process Descriptors • Each Pointer References Ready Process • Enqueuer: Moves Process Between States • Process is Changed to Ready State • Process Descriptor is Updated • Pointer to Process Descriptor in Ready List • May Also • Compute Priority for Allocating CPU to Process During List Insertion or Removal

  7. What Functions Does Each Components in a Scheduler? • Context Switcher: Change Running Process • Saves Content of Registers (PC, IR, etc.) for Process Being Removed in Descriptor • Voluntary: Invoked by Running Process that Intends to Release CPU • Preemptive: Interrupt that Causes Handler that Invokes Context Switch • Context Switch Must Also Occur Between Scheduler Processes & Application Processes • Dispatcher: Select “Next” Process to Run • Context Switch to Run Dispatcher • Select Process from Ready List Based on Scheduler Policy • Context Switch from Itself to Selected Process

  8. Software Architecture: The Scheduler From Other States Process Descriptor Ready Process Enqueue Ready List Context Switch Dispatch CPU

  9. How Does a Scheduler Work in an OS? Preemption or Voluntary Yield New Process Ready List Scheduler CPU Done Resource Manager Allocate Request Resources • Scheduler is Key Component of OS • Top Loop - Process in Ready State • Bottom Loop - Process Blocked Until Request is Satisfied by Resource Manager

  10. Scheduler as Resource Manager Scheduler Ready to run Release Dispatch Dispatch Release Release Process Dispatch Units of time for a time-multiplexed CPU

  11. Voluntary CPU Sharing • Initiated by Process via yield Command to Release CPU • Pi Can Be “Automatically” Determined From the Processor Status Registers • What’s Major Problem in this Strategy? yield(pi.pc, pj.pc) { memory[pi.pc] = PC; PC = memory[pj.pc]; } yield(*, pj.pc) { memory[pi.pc] = PC; PC = memory[pj.pc]; }

  12. More on Yield • Pi and Pj Can Resume One Another’s Execution • Suppose Pj is the Scheduler: yield(*, pj.pc); . . . yield(*, pi.pc); . . . yield(*, pj.pc); . . . // p_i yields to scheduler yield(*, pj.pc); // scheduler chooses pk yield(*, pk.pc); // pk yields to scheduler yield(*, pj.pc); // scheduler chooses ...

  13. Voluntary Sharing • Every Process Periodically Yields to the Scheduler • Relies on Correct Process Behavior • Malicious • Accidental • Need a Mechanism to Override Running Process • Overall • Not Utilized in Practice • Lack of Fairness • Depends on Software Engineers • In What Situations May Voluntary Sharing be Utilized? EMBEDDED OS/APPLICATIONS (AVIONICS)!!!

  14. Involuntary or PreemptiveCPU Sharing • Programmable Interval Timer • Device to Produce a Periodic Interrupt • Re-Programmable Period • Logically Equivalent to yield IntervalTimer() { InterruptCount--; if(InterruptCount <= 0) { InterruptRequest = TRUE; InterruptCount = K; } } SetInterval(programmableValue) { K = programmableValue: InterruptCount = K; } }

  15. Involuntary or Preemptive CPU Sharing (Continued) • Interval Timer Device Handler • Can Keep an In-Memory Clock Up-to-date • Can Invoke the Scheduler IntervalTimerHandler() { Time++; // update the clock TimeToSchedule--; if(TimeToSchedule <= 0) { <invoke scheduler>; TimeToSchedule = TimeSlice; } }

  16. Scheduling and Performance • Scheduler Can Control: • CPU Utilization (Always Keep Busy) • Average Time a Process Waits for Service in Ready List • Average Amount of Time to Complete a Job Preemption or Voluntary Yield New Process Ready List Scheduler CPU Done Resource Manager Allocate Request

  17. Historical Perspective • Schedulers Studied in Last 30 Years • Scheduler Key to Performance of Multi-Programming OS • Individual Performance of Processes • Overall Performance of System (CPU and I/O Utilization) • Long Studied Problem in OR • Theoretical CS • Is it Possible to … • Predict Performance? • Guarantee Equitable Sharing of CPU? • Optimize Based on “Class” of Process?

  18. How Does Scheduler Strategy Affect and Dictate Process Behavior? • Strategy = Policy the Scheduler Mechanism Uses to Choose From the Ready List • Strive for Any Of: • Equitability or Fairness • Favor Very Short or Long Jobs • Meet Priority and/or Deadline Requirements • Different Policies for Different Requirements • Mechanism Never Changes • Scheduling Strategy Depends on • Goals of OS, Process Priorities, Fairness, Resource Utilization, Throughput, Turnaround Time, Response Time, System Availability

  19. ContemporaryScheduling Mechanism • Involuntary CPU Sharing -- Timer Interrupts • Time Quantum Determined by Interval Timer • Usually Fixed for Every Process in the System • Sometimes Called the Time Slice Length • Priority-Based Process (Job) Selection • Dispatcher Selects the Highest Priority Process • Priority Reflects Policy • May or May Not Have Preemption • Combination of Processes in Ready List, Preemptive Scheduler, & Specific Goal for Scheduling Can Lead to Optimal Schedule

  20. Optimal Scheduling • Suppose the Scheduler Knows Each Process pi’s Service Time, t(pi) • Could Look at Every pi and t(pi) in the Ready List • Choose the Scheduler That Optimized on Any Desired Criteria • But, … • Other Processes May Arrive While These Processes are Being Serviced • The t(pi) are Almost Certainly Just Estimates • Algorithm to Choose Optimal Schedule is O(n2), Which is Costly in Practice!

  21. Talking About Scheduling ... • Let P = {pi | 0  i < n} = Set of Processes • Let S(pi)  {Running, Ready, Blocked} • Let t(pi) = Service Time Process Needs to Be in a Running State • Let W(pi) = Wait Time that pi is in Ready State Before First Transition to a Running State • Let TTRnd(pi) = Turnaround Time From When pi First Enters Ready List to Last Exit Ready List • Batch Throughput Rate = Inverse of Avg TTRnd • Timesharing Response Time = W(pi)

  22. Simple Scheduling Model • Only Consider Running and Ready States • Ignore Resource Manager (Time in Blocked State) • “Process Created When it Enters Ready” • “Process is Destroyed When it Enters Blocked” • Just Looking at “Small Phases” of a Process • Resource Management Time Increases TTRnd for Process • But Doesn’t Impact Scheduling Directly Preemption or voluntary yield New Process Ready List Scheduler CPU Done

  23. Non-Preemptive Schedulers • Processes Run to Completion Once Scheduled and the CPU is Allocated • Similar in Concept to OR Research • Service Time for Bank, Airport, Supermarket • Easy to build and analyze • Easy to analyze performance • No issue of voluntary/involuntary sharing • Under What Situations is Non-Preemptive Scheduling Still Relevant? New Process Ready List Scheduler CPU Done

  24. First-Come-First-Served i t(pi) 0 350 1 125 2 475 3 250 4 75 0 350 p0 TTRnd(p0) = t(p0) = 350 W(p0) = 0

  25. First-Come-First-Served i t(pi) 0 350 1 125 2 475 3 250 4 75 350 475 p0 p1 TTRnd(p0) = t(p0) = 350 TTRnd(p1) = (t(p1) +TTRnd(p0)) = 125+350 = 475 W(p0) = 0 W(p1) = TTRnd(p0) = 350

  26. First-Come-First-Served i t(pi) 0 350 1 125 2 475 3 250 4 75 350 475 950 p0 p1 p2 TTRnd(p0) = t(p0) = 350 TTRnd(p1) = (t(p1) +TTRnd(p0)) = 125+350 = 475 TTRnd(p2) = (t(p2) +TTRnd(p1)) = 475+475 = 950 W(p0) = 0 W(p1) = TTRnd(p0) = 350 W(p2) = TTRnd(p1) = 475

  27. First-Come-First-Served i t(pi) 0 350 1 125 2 475 3 250 4 75 350 475 950 1200 p0 p1 p1 p2 p3 TTRnd(p0) = t(p0) = 350 TTRnd(p1) = (t(p1) +TTRnd(p0)) = 125+350 = 475 TTRnd(p2) = (t(p2) +TTRnd(p1)) = 475+475 = 950 TTRnd(p3) = (t(p3) +TTRnd(p2)) = 250+950 = 1200 W(p0) = 0 W(p1) = TTRnd(p0) = 350 W(p2) = TTRnd(p1) = 475 W(p3) = TTRnd(p2) = 950

  28. First-Come-First-Served i t(pi) 0 350 1 125 2 475 3 250 4 75 350 475 950 1200 1275 p0 p1 p2 p3 p4 TTRnd(p0) = t(p0) = 350 TTRnd(p1) = (t(p1) +TTRnd(p0)) = 125+350 = 475 TTRnd(p2) = (t(p2) +TTRnd(p1)) = 475+475 = 950 TTRnd(p3) = (t(p3) +TTRnd(p2)) = 250+950 = 1200 TTRnd(p4) = (t(p4) +TTRnd(p3)) = 75+1200 = 1275 W(p0) = 0 W(p1) = TTRnd(p0) = 350 W(p2) = TTRnd(p1) = 475 W(p3) = TTRnd(p2) = 950 W(p4) = TTRnd(p3) = 1200

  29. FCFS Average Wait Time • Easy to Implement • Ignores Service Time, Etc. • Not a Great Performer i t(pi) 0 350 1 125 2 475 3 250 4 75 350 475 950 1200 1275 p0 p1 p2 p3 p4 TTRnd(p0) = t(p0) = 350 TTRnd(p1) = (t(p1) +TTRnd(p0)) = 125+350 = 475 TTRnd(p2) = (t(p2) +TTRnd(p1)) = 475+475 = 950 TTRnd(p3) = (t(p3) +TTRnd(p2)) = 250+950 = 1200 TTRnd(p4) = (t(p4) +TTRnd(p3)) = 75+1200 = 1275 W(p0) = 0 W(p1) = TTRnd(p0) = 350 W(p2) = TTRnd(p1) = 475 W(p3) = TTRnd(p2) = 950 W(p4) = TTRnd(p3) = 1200 TTRnd = (350+475+950+1200+1275)/5 = 4250/5 = 850 Wavg = (0+350+475+950+1200)/5 = 2975/5 = 595

  30. Shortest Job Next i t(pi) 0 350 1 125 2 475 3 250 4 75 0 75 p4 W(p4) = 0 TTRnd(p4) = t(p4) = 75

  31. Shortest Job Next i t(pi) 0 350 1 125 2 475 3 250 4 75 0 75 200 p4 p1 W(p1) = 75 W(p4) = 0 TTRnd(p1) = t(p1)+t(p4) = 125+75 = 200 TTRnd(p4) = t(p4) = 75

  32. Shortest Job Next i t(pi) 0 350 1 125 2 475 3 250 4 75 0 75 200 450 p4 p1 p3 W(p1) = 75 W(p3) = 200 W(p4) = 0 TTRnd(p1) = t(p1)+t(p4) = 125+75 = 200 TTRnd(p3) = t(p3)+t(p1)+t(p4) = 250+125+75 = 450 TTRnd(p4) = t(p4) = 75

  33. Shortest Job Next i t(pi) 0 350 1 125 2 475 3 250 4 75 0 75 200 450 800 p4 p1 p3 p0 TTRnd(p0) = t(p0)+t(p3)+t(p1)+t(p4) = 350+250+125+75 = 800 TTRnd(p1) = t(p1)+t(p4) = 125+75 = 200 TTRnd(p3) = t(p3)+t(p1)+t(p4) = 250+125+75 = 450 TTRnd(p4) = t(p4) = 75 W(p0) = 450 W(p1) = 75 W(p3) = 200 W(p4) = 0

  34. Shortest Job Next i t(pi) 0 350 1 125 2 475 3 250 4 75 0 75 200 450 800 1275 p4 p1 p3 p0 p2 TTRnd(p0) = t(p0)+t(p3)+t(p1)+t(p4) = 350+250+125+75 = 800 TTRnd(p1) = t(p1)+t(p4) = 125+75 = 200 TTRnd(p2) = t(p2)+t(p0)+t(p3)+t(p1)+t(p4) = 475+350+250+125+75 = 1275 TTRnd(p3) = t(p3)+t(p1)+t(p4) = 250+125+75 = 450 TTRnd(p4) = t(p4) = 75 W(p0) = 450 W(p1) = 75 W(p2) = 800 W(p3) = 200 W(p4) = 0

  35. TTRnd(p0) = t(p0)+t(p3)+t(p1)+t(p4) = 350+250+125+75 = 800 TTRnd(p1) = t(p1)+t(p4) = 125+75 = 200 TTRnd(p2) = t(p2)+t(p0)+t(p3)+t(p1)+t(p4) = 475+350+250+125+75 = 1275 TTRnd(p3) = t(p3)+t(p1)+t(p4) = 250+125+75 = 450 TTRnd(p4) = t(p4) = 75 W(p0) = 450 W(p1) = 75 W(p2) = 800 W(p3) = 200 W(p4) = 0 Shortest Job Next • Minimizes Wait Time • May Starve Large Jobs • Must Know Service Times in Advance! i t(pi) 0 350 1 125 2 475 3 250 4 75 0 75 200 450 800 1275 p4 p1 p3 p0 p2 TTRnd = (800+200+1275+450+75)/5 = 2800/5 = 560 Wavg = (450+75+800+200+0)/5 = 1525/5 = 305

  36. Priority Scheduling • Reflects Importance of External Use • May Cause Starvation • Can Address Starvation With Aging i t(pi) Pri 0 350 5 1 125 2 2 475 3 3 250 1 4 75 4 0 250 375 850 925 1275 p3 p1 p2 p4 p0 TTRnd(p0) = t(p0)+t(p4)+t(p2)+t(p1) )+t(p3) = 350+75+475+125+250 = 1275 TTRnd(p1) = t(p1)+t(p3) = 125+250 = 375 TTRnd(p2) = t(p2)+t(p1)+t(p3) = 475+125+250 = 850 TTRnd(p3) = t(p3) = 250 TTRnd(p4) = t(p4)+ t(p2)+ t(p1)+t(p3) = 75+475+125+250 = 925 W(p0) = 925 W(p1) = 250 W(p2) = 375 W(p3) = 0 W(p4) = 850 TTRnd = (1275+375+850+250+925)/5 = 3675/5 = 735 Wavg = (925+250+375+0+850)/5 = 2400/5 = 480

  37. Comparing Non-Preemptive Scheduling • First-Come-First-Serve • Shortest Job Next • Priority Scheduling • Observations re. Behavior and Tradeoffs? TTRnd = (350+475+950+1200+1275)/5 = 4250/5 = 850 Wavg = (0+350+475+950+1200)/5 = 2975/5 = 595 TTRnd = (800+200+1275+450+75)/5 = 2800/5 = 560 Wavg = (450+75+800+200+0)/5 = 1525/5 = 305 TTRnd = (1275+375+850+250+925)/5 = 3675/5 = 735 Wavg = (925+250+375+0+850)/5 = 2400/5 = 480

  38. 200 550 575 1050 1275 125 1025 0 p1 p4 p0 p2 p3 75 p4 p1 p0 p2 p3 75 425 p4 p0 p1 p2 p3 Deadline Scheduling i t(pi) Deadline 0 350 575 1 125 550 2 475 1050 3 250 (none) 4 75 200 • Must Receive Service by Deadline • May Not Be Attainable in Practice! • What are TTRnd(pi) and Average TTRnd? • What are W(pi) and Average W?

  39. Preemptive Schedulers • Highest Priority Process is Guaranteed to Be Running at All Times • Or at Least at the Beginning of a Time Slice • Lower Priority Processes Made to Yield • Tradeoff with Equitability for All Processes • Dominant Form of Contemporary Scheduling • But Complex to Build & Analyze • What’s Not Shown in Figure Below? CONTEXT SWITCHING AND ASSOC. COST!!! Preemption or voluntary yield New Process Ready List Scheduler CPU Done

  40. Round Robin Scheduling • Employs Combination of Following: • Time Slicing for All Processes • Context Switch Between Processes • Timer-Interrupt Driven • Timer Functions as Follows • Allow Process to Run for its Time Quantum • Generate Interrupt • Results in Interrupt Handler that Calls Scheduler • Scheduler Removes Current Process • Dispatcher/Context Switcher Starts Next Ready Process

  41. Round Robin (TQ=50) i t(pi) 0 350 1 125 2 475 3 250 4 75 0 50 p0 W(p0) = 0

  42. Round Robin (TQ=50) i t(pi) 0 350 1 125 2 475 3 250 4 75 0 100 p0 p1 W(p0) = 0 W(p1) = 50

  43. Round Robin (TQ=50) i t(pi) 0 350 1 125 2 475 3 250 4 75 0 100 p0 p1 p2 W(p0) = 0 W(p1) = 50 W(p2) = 100

  44. Round Robin (TQ=50) i t(pi) 0 350 1 125 2 475 3 250 4 75 0 100 200 p0 p1 p2 p3 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150

  45. Round Robin (TQ=50) i t(pi) 0 350 1 125 2 475 3 250 4 75 0 100 200 p0 p1 p2 p3 p4 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200

  46. Round Robin (TQ=50) i t(pi) 0 350 1 125 2 475 3 250 4 75 0 100 200 300 p0 p1 p2 p3 p4 p0 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200

  47. Round Robin (TQ=50) i t(pi) 0 350 1 125 2 475 3 250 4 75 0 100 200 300 400 475 550 p0 p1 p2 p3 p4 p0 p1 p2 p3 p4 p0 p1 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200 TTRnd(p4) = 475

  48. Round Robin (TQ=50) i t(pi) 0 350 1 125 2 475 3 250 4 75 0 100 200 300 400 475 550 p0 p1 p2 p3 p4 p0 p1 p2 p3 p4 p0 p1 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200 TTRnd(p1) = 550 TTRnd(p4) = 475

  49. Round Robin (TQ=50) i t(pi) 0 350 1 125 2 475 3 250 4 75 0 100 200 300 400 475 550 650 p0 p1 p2 p3 p4 p0 p1 p2 p3 p4 p0 p1 p2 p3 650 750 850 950 p0 p2 p3 p0 p2 p3 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200 TTRnd(p1) = 550 TTRnd(p3) = 950 TTRnd(p4) = 475

  50. Round Robin (TQ=50) i t(pi) 0 350 1 125 2 475 3 250 4 75 0 100 200 300 400 475 550 650 p0 p1 p2 p3 p4 p0 p1 p2 p3 p4 p0 p1 p2 p3 650 750 850 950 1050 p0 p2 p3 p0 p2 p3 p0 p2 p0 TTRnd(p0) = 1100 TTRnd(p1) = 550 TTRnd(p3) = 950 TTRnd(p4) = 475 W(p0) = 0 W(p1) = 50 W(p2) = 100 W(p3) = 150 W(p4) = 200

More Related