580 likes | 707 Views
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.
E N D
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.
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?
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
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
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
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
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
Software Architecture: The Scheduler From Other States Process Descriptor Ready Process Enqueue Ready List Context Switch Dispatch CPU
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
Scheduler as Resource Manager Scheduler Ready to run Release Dispatch Dispatch Release Release Process Dispatch Units of time for a time-multiplexed CPU
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]; }
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 ...
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)!!!
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; } }
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; } }
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
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?
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
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
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!
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)
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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?
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
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
Round Robin (TQ=50) i t(pi) 0 350 1 125 2 475 3 250 4 75 0 50 p0 W(p0) = 0
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
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
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
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
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
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
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
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
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