Operating Systems: Internals and Design Principles, 6/E William Stallings Chapter 9 Uniprocessor Scheduling Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Roadmap • Types of Processor Scheduling • Scheduling Algorithms • Traditional UNIX Scheduling Scheduling • An OS must allocate resources amongst competing processes. • The resource provided by a processor is execution time • The resource is allocated by means of a scheduling Overall Aim of Scheduling • Aim of processor scheduling: -- assign processes to be executed by the processor over time, – in a way that meets system objectives, such as response time, throughput, and processor efficiency. Scheduling Objectives • The scheduling function should – Allocate time fairly among processes – Prevent starvation of a process – Use the processor efficiently – Have low overhead – Prioritize processes when necessary (e.g. real time deadlines) Types of Scheduling a new process part of the swapping function a process Two Suspend States • Remember this diagram from Chapter 3 Scheduling and Process State Transitions Nesting of Scheduling Functions Queuing Diagram (Simplified) Long-Term Scheduling • Determines which programs are admitted to the system for processing – May be first-come-first-served – Or based on criteria such as priority, I/O requirements or expected execution time • Controls the degree of multiprogramming • More processes, smaller percentage of time each process is executed Medium-Term Scheduling • Part of the swapping function • Swapping-in decisions are based on - the need to manage the degree of multiprogramming - the resources (memory, I/O) the processes require • Typically, memory is the limiting resource and the memory manager acts as the medium-term scheduler Short-Term Scheduling • Known as the dispatcher • Executes most frequently • Must be very fast • Invoked when an event occurs – – – – Clock interrupts I/O interrupts Operating system calls Signals (e.g., semaphores) Roadmap • Types of Processor Scheduling • Scheduling Algorithms • Traditional UNIX Scheduling Aim of Short Term Scheduling • Main objective is to allocate processor time to optimize certain aspects of system behaviour. • A set of criteria is needed to evaluate the scheduling policy. Short-Term Scheduling Criteria: User vs System • User-oriented – Response Time • Elapsed time between the submission of a request until there is output. • System-oriented – Effective and efficient utilization of the processor Short-Term Scheduling Criteria: Performance vs non-performance • Performance-related – Quantitative, easily measured – e.g. response time and throughput • Non-performance related – Qualitative – Hard to measure Interdependent Scheduling Criteria Interdependent Scheduling Criteria (cont.) Use of Priorities for Scheduling • Scheduler will always choose a process of higher priority over one of lower priority • Have multiple ready queues, one for each level of priority Priority Queuing (Simplified) highest lowest Starvation • Problem with pure priority scheduling: – Lower-priority may suffer starvation if there is a steady supply of high priority processes. • Solution – Allow a process to change its priority based on its age or execution history Alternative Scheduling Policies FCFS: First-Come-First-Served RR: Round Robin SPN: Shortest Process Next SRT: Shortest Remaining Time HRRN: Highest Response Ratio Next Each policy has its own selection function, which determines the process to be selected next for execution. Selection Function • Determines which process is selected next for execution • May be based on priority, resource requirements, or the execution characteristics • Important quantities of execution characteristics: • w = time spent waiting in system so far • e = time spent in execution so far • s = total service time required by the process, including e (estimated or supplied by the user) Decision Mode • Specifies the instants in time at which the selection function is exercised. • Two categories: (see next slide) – Nonpreemptive – Preemptive Nonpreemptive vs Premeptive • Non-preemptive – Once a process is in the running state, it will continue until it terminates or blocks itself for I/O • Preemptive – Currently running process may be interrupted and moved to ready state by the OS – Preemption may occur when new process arrives, on an interrupt, or periodically. Alternative Scheduling Policies The process that has spend the most time waiting in the system is selected The process that will take the least service time waiting is selected The longer the wait, the higher the ratio, the sooner to be chosen This accounts for aging Focus on the time that has been spent in execution so far. Penalize processes that has been running longer Process Scheduling Example • Example set of processes, consider each a batch job – Service time represents total execution time First-Come-First-Served • Each process joins the Ready queue • When the current process ceases to execute, the process in the Ready queue the longest is selected Time of arrival : 0 Arrivals: A 2 B 4 C 6 D 8 E 3 6 4 5 2 Pr. that finishes: Finish time: Turnaround time: A 3 3 B 9 7 C 13 9 D 18 12 E 20 12 First-Come-First-Served • A short process may have to wait a very long time before it can execute • Favors CPU-bound processes – FCFS performs better for long processes than short ones – I/O processes have to wait until CPU-bound process completes – Convoy effect: All other processes wait for the big process to finish Round Robin • Uses preemption based on a clock – also known as time slicing, because each process is given a slice of time before being preempted. Time line: 0 5 10 15 20 The only process in the system Pr. that finishes: Finish time: Turnaround time A 4 4 E C B D 15 17 18 20 7 13 16 14 Round Robin • Clock interrupt is generated at periodic intervals • When an interrupt occurs, the currently running process is placed in the ready queue – Next ready job is selected Effect of Size of Preemption Time Quantum Short quantum: Short processes finish execution relatively quickly Processing overhead is high Very short quanta should be avoided Long quantum: Favors CPU-bound processes Very long quanta behaves like FCFS Useful quide: Time quantum should be slightly greater than the time required for a typical interaction Effect of Size of Preemption Time Quantum In this case, most processes will require at least two time quanta ‘Virtual Round Robin’ • A refinement to the R.R. scheduling • Avoids the unfairness of R.R. Get preference over the main ready queue When a process is blocked for I/O, it joins the I/O queue as usual Shortest Process Next • Non-preemptive policy • Process with shortest expected processing time is selected next • Short process jumps ahead of longer processes Time line: Arrivals: 0 1 2 3 4 5 6 7 8 9 10 … A B C D E Finishes: Finish time: Turnaround time: A 3 3 B 9 7 E 11 3 C 15 11 D 20 14 Shortest Process Next • Advantage: Performance is significantly improved in terms of response time. • Disadv: Predictability of longer processes is reduced • Disadv: Possibility of starvation for longer processes • Difficulty: need to know / estimate the required processing time of each process. • If estimated time for process not correct, the operating system may abort it Calculating Program ‘Burst’ • Where: – Ti = processor execution time for the i-th instance (turn) of this process – Si = predicted value for the i-th instance – S1 = predicted value for first instance; not calculated Sn+1 =(1/n) (T1 +T2 + … +Tn) Sn =(1/(n-1)) (T1 +T2 + … +Tn-1) Sn+1=(1/n) Tn + ((n-1) / n) Sn Calculating Program ‘Burst’ • Where: – Ti = processor execution time for the i-th instance (turn) of this process – Si = predicted value for the i-th instance – S1 = predicted value for first instance; not calculated equal-weight averaging Sn+1 =(1/n) (T1 +T2 + … +Tn) Sn =(1/(n-1)) (T1 +T2 + … +Tn-1) Sn+1=(1/n) Tn + ((n-1) / n) Sn Exponential Averaging Typically, we would like to give greater weight to more recent instances A common technique for predicting a future value on the basis of a time series of past values is exponential averaging 0<α<1 k n Sn+1 = αTn + (1-α)αTn-1 + … + (1-α) αTn-k + … + (1-α) S1 for = 0.8 Sn+1 = 0.8Tn + 0.16Tn-1 + 0.032Tn-2 + 0.0064Tn-3 + … Exponential Smoothing Coefficients Use Of Exponential Averaging Use Of Exponential Averaging Shortest Remaining Time • Preemptive version of shortest process next policy • Must estimate processing time and choose the shortest B is preempted at t = 4, due to R(B) = 5 R(C) = 4 the arrival of C, which has shorter remaining time R(B) = 5 R(D) = 5 R(E) = 2 R(B) = 5 R(D) = 5 Highest Response Ratio Next • Choose next process with the greatest ratio Non-preemptive R(C) = (5+4) / 4 = 2.25 R(D) = (3+5) / 5 = 1.60 R(E) = (1+2) / 2 = 1.50 R(D) = (7+5) / 5 = 2.40 R(E) = (5+2) / 2 = 3.50 Feedback Scheduling • If we have no indication of the relative length of the processes, then none of SPN, SRT, HRRN can be used • Use feedback scheduling to penalize jobs that have been running longer RR (q=1) RR (q=2) • Preemptive at time quantum FCFS • Favors newer, shorter processes over older, longer processes lowerst priority queue Also called multilevel feedback Feedback Performance • Variations exist, simple version pre-empts periodically, similar to round robin – But can lead to starvation Performance Comparison • Queuing Analysis - Poisson arrivals & exponential service time assumed Observation: Any scheduling discipline that chooses the next item to be served independent of service time obeys the relationship: Formulas Overall Normalized Response Time Normalized Response Time for Shorter Process Normalized Response Time for Longer Processes Normalized Turnaround Time Fair-Share Scheduling • User’s application runs as a collection of processes (threads) • User is concerned about the performance of the application • Need to make scheduling decisions based on process sets, instead of individual processes Fair-Share Scheduler Roadmap • Types of Processor Scheduling • Scheduling Algorithms • Traditional UNIX Scheduling Traditional UNIX Scheduling • Multilevel feedback using round robin within each of the priority queues • If a running process does not block or complete within 1 second, it is preempted • Priority is based on process type and execution history. Scheduling Formula Bands • Priorities are recomputed once per second • Base priority divides all processes into fixed bands of priority levels – – – – – Swapper (highest) Block I/O device control File manipulation Character I/O device control User processes (lowest) Example of Traditional UNIX Process Scheduling