Pdf of Slides
Total Page:16
File Type:pdf, Size:1020Kb
Separation of Scheduling: Policy and Mechanism Policy and Mechanism Scheduling policy answers the question: Which process/thread, among all those ready to run, should be given the chance to run next? In what order do the processes/threads get to run? For how long? “Why and What” vs. “How” Mechanisms are the tools for supporting the process/thread Objectives and strategies vs. data structures, abstractions and affect how the scheduling policy can be implemented. (this is review) hardware and software implementation • How the process or thread is represented to the system - process issues. or thread control blocks. • What happens on a context switch. Process abstraction vs. Process machinery • When do we get the chance to make these scheduling decisions (timer interrupts, thread operations that yield or block, user p rogram system calls) More Specific Mechanisms for CPU Scheduling Policy Scheduling The CPU scheduler makes a sequence of “moves” that determines the interleaving of threads. • Preemption • Programs use synchronization to prevent “bad moves”. • Priorities • …but otherwise scheduling choices appear (to the • Queuing strategies program) to be nondeterministic. Wakeup or ReadyToRun GetNextToRun() Scheduler’s ready pool CONTEXT SWITCH 1 Preemption Priority Some goals can be met by incorporating a notion of priority into a Scheduling policies may be preemptive or non- “base” scheduling discipline. preemptive. Each job in the ready pool has an associated priority value; the Preemptive: scheduler may unilaterally force a task to scheduler favors jobs with higher priority values. relinquish the processor before the task blocks, yields, External priority values: or completes. • imposed on the system from outside • timeslicing prevents jobs from monopolizing the CPU • reflect external preferences for particular users or tasks Scheduler chooses a job and runs it for a quantum of CPU “All jobs are equal, but some jobs are more equal than others.” time. • Example: Unix nicesystem call to lower priority of a task. A job executing longer than its quantum is forced to yield by scheduler code running from the clock interrupt • Example: Urgent tasks in a real-time process control system. handler. Internal priorities • use preemption to honor priorities • scheduler dynamically calculates and uses for queuing discipline. System adjusts priority values internally as as an Preempt a job if a higher priority job enters the ready state. implementation technique within the scheduler. Internal Priority Keeping Your Priorities Straight Priorities must be handled carefully when there are dependencies among tasks with different priorities. • Drop priority of tasks consuming more than their share • A task with priority P should never impede the progress • Boost tasks that already hold resources that are in of a task with priority Q > P. demand This is called priority inversion, and it is to be avoided. • Boost tasks that have starved in the recent past • The basic solution is some form of priority inheritance. • Adaptive to observed behavior: typically a continuous, When a task with priority Q waits on some resource, the dynamic, readjustment in response to observed holder (with priority P) temporarily inherits priority Q if conditions and events Q > P. May be visible and controllable to other parts of the system Inheritance may also be needed when tasks coordinate with IPC. Priority reassigned if I/O bound (large unused portion of quantum) or if CPU bound (nothing left) • Inheritance is useful to meet deadlines and preserve low-jitter execution, as well as to honor priorities. 2 Multilevel Feedback Queue CPU Scheduling Policy Many systems (e.g., Unix variants) use a multilevel The CPU scheduler makes a sequence of “moves” that determines feedback queue. the interleavingof threads. • multilevel. Separate queue for each of N priority levels. • Programs use synchronization to prevent “bad moves”. • feedback. Factor previous behavior into new job • …but otherwise scheduling choices appear (to the priority. program) to be nondeterministic. The scheduler’s moves are dictated by a scheduling policy. high I/O bound jobs waiting for CPU jobs holding resouces Wakeup or jobs with high external priority ReadyToRun GetNextToRun() Scheduler’s ready pool GetNextToRun selects job at the head of the highest low CPU-bound jobs Priority of CPU-bound priority queue. jobs decays with system CONTEXT SWITCH ready queues load and service received. constant time, no sorting indexed by priority Scheduler Policy Goals & Metrics of Success Classic Scheduling Algorithms • Response time or latency (to minimize the average time between arrival to completion of requests) SJF - Shortest Job First (provably optimal in minimizing How long does it take to do what I asked? ( R) Arrival -> done. average response time, assuming we know service times • Throughput (to maximize productivity) in advance) How many operations complete per unit of time? (X ) FIFO, FCFS • Utilization (to maximize use of some device) What percentage of time does the CPU (and each device) spend Round Robin doing useful work? ( U) Multilevel Feedback Queuing time-in-use / elapsed time • Fairness Priority Scheduling What does this mean? Divide the pie evenly? Guarantee low variance in response times? Freedom from starvation? Proportional sharing of resources • Meet deadlines and guarantee jitter-free periodic tasks real time systems (e.g. process control, continuous media) 3 A Simple Policy: FCFS Evaluating FCFS The most basic scheduling policy is first-come -first- How well does FCFS achieve the goals of a scheduler? served, also called first-in-first-out (FIFO). • throughput. FCFS is as good as any non-preemptive • FCFS is just like the checkout line at the QuickiMart. policy. Maintain a queue ordered by time of arrival. ….if the CPU is the only schedulable resource in the system. GetNextToRunselects from the front of the queue. • fairness. FCFS is intuitively fair…sort of. • FCFS with preemptive timeslicing is called round robin. “The early bird gets the worm”…and everyone else is fed eventually. • response time. Long jobs keep everyone else waiting. Wakeup or ReadyToRun GetNextToRun() RemoveFromHead D=3 D=2 D=1 List::Append 3 5 6 CPU Time ready list R = (3 + 5 + 6)/3 = 4.67 Minimizing Response Time: SJF SJF in Practice Shortest Job First (SJF) is provably optimal if the goal is Pure SJF is impractical: scheduler cannot predict D values. to minimize R. However, SJF has value in real systems: Example: express lanes at the MegaMart • Many applications execute a sequence of short CPU Idea: get short jobs out of the way quickly to minimize bursts with I/O in between. the number of jobs waiting while a long job runs. • E.g., interactive jobs block repeatedly to accept user input. Intuition: longest jobs do the least possible damage to the wait times of their competitors. Goal: deliver the best response time to the user. • E.g., jobs may go through periods of I/O-intensive D=1 D=2 D=3 activity. 1 3 6 Goal: request next I/O operation ASAP to keep devices busy and deliver the best overall throughput. R = (1 + 3 + 6)/3 = 3.33 • Use adaptive internal priority to incorporate SJF into RR. Weather report strategy: predict future D from the recent past. 4 Preemptive FCFS: Round Robin Evaluating Round Robin D=5 D=1 Preemptive timeslicing is one way to improve fairness of FCFS. R = (5+6)/2 = 5.5 If job does not block or exit, force an involuntary context switch R = (2+6 + e)/2 = 4 + e after each quantum Q of CPU time. Preempted job goes back to the tail of the ready list. • Response time. RR reduces response time for With infinitesimal Q round robin is called processor sharing. short jobs. For a given load, a job’s wait time is proportional to its D. FCFS D=3 D=2 D=1 • Fairness. RR reduces variance in wait times. round robin But: RR forces jobs to wait for other jobs that arrived later. 3+e 5 6 • Throughput. RR imposes extra context switch quantum Q=1 overhead. Q is typically R = (3 + 5 + 6 + e)/3 = 4.67 + e 5-100 milliseconds preemption CPU is only Q/(Q+e) as fast as it was before. In this case, R is unchanged by timeslicing. overhead = e Is this always true? Degrades to FCFS with large Q. Considering I/O Two Schedules for CPU/Disk In real systems, overall system performance is determined by the interactions of multiple service centers. 1. Naive Round Robin start (arrival rate ?) 5 5 1 1 4 CPU busy 25/37: U = 67% CPU Disk busy 15/37: U = 40% I/O completion I/O request 2. Preemptive RR/SJF exit 33% performance improvement I/O device (throughput ? until some center saturates) CPU busy 25/25: U = 100% Disk busy 15/25: U = 60% 5 Multilevel Feedback Queue Concrete Implementation Many systems (e.g., Unix variants) use a multilevel 4.4BSD example: feedback queue. multilevel feedback queues based on • multilevel. Separate queue for each of N priority levels. calculated priority, round-robin within level. • feedback. Factor previous behavior into new job priority. • Use quantum - reenter queue you came off of. • Changing priority (every 4 ticks) high I/O bound jobs waiting for CPU priority = user_base_priority + [p_estcpu/4] + jobs holding resouces jobs with high external priority 2 * p_nice GetNextToRun selects job at the head of the highest low CPU-bound jobs Priority of CPU-bound priority queue. jobs decays with system ready queues load and service received. constant time, no sorting indexed by priority Real Time Schedulers p_estcpu is incremented each tick during which the process is found running and adjusted each second via decay filter Real-time schedulers must support regular, periodic (for runnable) execution of tasks (e.g., continuous media). p_estcpu = (2*load)/(2*load +1) p_estcpu + p_nice. • CPU Reservations “I need to execute for X out of every Y units.” Load over previous minute interval - sampled ave.