TDDI04 Concurrent Programming, Operating Systems, and Real-time Operating Systems Overview: CPU Scheduling § CPU bursts and I/O bursts § Scheduling Criteria § Scheduling Algorithms § Multiprocessor Scheduling CPU Scheduling § (Real-Time Scheduling Lecture on real-time OS) § Scheduling Algorithm Evaluation [SGG7] Chapter 5 Copyright Notice: The lecture notes are mainly based on Silberschatz’s, Galvin’s and Gagne’s book (“Operating System Concepts”, 7th ed., Wiley, 2005). No part of the lecture notes may be reproduced in any form, due to the copyrights reserved by Wiley. These lecture notes should only be used for internal teaching purposes at the Linköping University. Acknowledgment: The lecture notes are originally compiled by C. Kessler, IDA. Klas Arvidsson, IDA, Linköpings universitet. Basic Concepts TDDI04, K. Arvidsson, IDA, Linköpings universitet. 6.2 © Silberschatz, Galvin and Gagne 2005 CPU Scheduler § Maximum CPU utilization obtained with multiprogramming § CPU–I/O Burst Cycle – Process execution consists of a cycle of alternating CPU execution and I/O wait § Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them § CPU scheduling decisions may take place when a process: 1. 2. 3. 4. 5. § CPU burst distribution histogram Becomes ready to run Switches from waiting to ready Switches from running to ready state Switches from running to waiting state Terminates § Scheduling under 1 to 3 is preemptive • The OS (may) choose to activate scheduling § Scheduling under 4 to 5 is nonpreemptive • TDDI04, K. Arvidsson, IDA, Linköpings universitet. 6.3 © Silberschatz, Galvin and Gagne 2005 Dispatcher switching context 6.4 © Silberschatz, Galvin and Gagne 2005 § CPU utilization – Keep the CPU as busy as possible § Throughput – # of processes that complete their execution per time unit switching to user mode § Turnaround time – Total amount of time to complete execution of a particular process jumping to the proper location in the user program to restart that program § Waiting time – Amount of time a process has been waiting in the ready queue § Dispatch latency • TDDI04, K. Arvidsson, IDA, Linköpings universitet. Scheduling Criteria § Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves: • • • The process choose to activate OS scheduling (leave the CPU) time it takes for the dispatcher to stop one process and start another running § Response time – Amount of time it takes from when a request was submitted until the first response is produced, not including output (for time-sharing environment) § Deadlines met? – In real-time systems (later) TDDI04, K. Arvidsson, IDA, Linköpings universitet. 6.5 © Silberschatz, Galvin and Gagne 2005 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 6.6 © Silberschatz, Galvin and Gagne 2005 1 First-Come, First-Served (FCFS, FIFO) Scheduling Optimization Criteria Process P1 P2 P3 § Max CPU utilization § Max throughput § Min turnaround time Burst Time 24 3 3 § Suppose that the processes arrive in the order: P1 , P2 , P3 § The Gantt Chart for the schedule is: § Min waiting time § Min response time P1 P2 0 24 P3 27 § Waiting time for P1 = 0; P2 = 24; P3 = 27 FCFS normally used for non-preemptive batch scheduling, e.g. printer queues (i.e., burst time = job size) Waiting time Pi = start time Pi – time of arrival for Pi § Average waiting time: (0 + 24 + 27) / 3 = 17 TDDI04, K. Arvidsson, IDA, Linköpings universitet. © Silberschatz, Galvin and Gagne 2005 6.7 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 30 © Silberschatz, Galvin and Gagne 2005 6.8 FCFS Scheduling (Cont.) Shortest-Job-First (SJF) Scheduling Suppose that the processes arrive in the order P2 , P3 , P1 § Associate with each process the length of its next CPU burst. § The Gantt chart for the schedule is: § Two schemes: P2 0 P3 3 § Use these lengths to schedule the shortest ready process P1 6 30 § Waiting time for P1 = 6; P2 = 0, P3 = 3 § Average waiting time: (6 + 0 + 3)/3 = 3 • nonpreemptive SJF – once CPU given to the process, it cannot be preempted until it completes its CPU burst • preemptive SJF – preempt if a new process arrives with CPU burst length less than remaining time of current executing process. - much better! > Also known as Shortest-Remaining-Time-First (SRTF) § SJF is optimal – § Convoy effect – short process behind long process • Idea: shortest job first? TDDI04, K. Arvidsson, IDA, Linköpings universitet. © Silberschatz, Galvin and Gagne 2005 6.9 Example of Non-Preemptive SJF Process Arrival Time P1 0.0 P2 P3 P4 4 1 4 P2 P3 P4 P3 7 P2 8 P4 12 P1 16 § Average waiting time = (0 + 6 + 3 + 7) / 4 = 4 TDDI04, K. Arvidsson, IDA, Linköpings universitet. © Silberschatz, Galvin and Gagne 2005 6.10 Burst Time 7 2.0 4.0 5.0 4 1 4 § with preemptive SJF: P1 3 TDDI04, K. Arvidsson, IDA, Linköpings universitet. Process Arrival Time P1 0.0 § with non-preemptive SJF: 0 gives minimum average waiting time for a given set of processes Example of Preemptive SJF Burst Time 7 2.0 4.0 5.0 • 6.11 © Silberschatz, Galvin and Gagne 2005 0 P2 2 P3 4 P2 5 P4 P1 11 7 16 § Average waiting time = (9 + 1 + 0 +2) / 4 = 3 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 6.12 © Silberschatz, Galvin and Gagne 2005 2 Predicting Length of Next CPU Burst Examples of Exponential Averaging § Extreme cases: § Can only estimate the length • § Based on length of previous CPU bursts, using exponential averaging: α =0 > τn+1 = τn > Recent history does not count • 1. t n = actual lenght of n th CPU burst α =1 > τn+1 = α tn 2. τ n +1 = predicted value for the next CPU burst > Only the latest CPU burst counts 3. α , 0 ≤ α ≤ 1 4. Define : τ n+=1 = α t n + (1 − α )τ n . § Otherwise: Expand the formula: τn+1 = α tn + (1 - α)α tn-1 + … +(1 - α )j α tn-j + … +(1 - α )n +1 τ0 • TDDI04, K. Arvidsson, IDA, Linköpings universitet. © Silberschatz, Galvin and Gagne 2005 6.13 Priority Scheduling Since both α and (1 - α) are less than or equal to 1, each successive term has less weight than its predecessor TDDI04, K. Arvidsson, IDA, Linköpings universitet. 6.14 © Silberschatz, Galvin and Gagne 2005 Round Robin (RR) § A priority value (integer) is associated with each process § Each process gets a small unit of CPU time: § The CPU is allocated to the process with the highest priority (smallest integer ≡ highest priority) • time quantum, usually 10-100 milliseconds. § After this time has elapsed, the process is preempted and added to the end of the ready queue. § Given n processes in the ready queue and time quantum q, each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units. § Performance • • preemptive nonpreemptive § SJF is a priority scheduling where priority is the predicted next CPU burst time § Problem: Starvation – low-priority processes may never execute § Solution: Aging – as time progresses increase the priority of the process TDDI04, K. Arvidsson, IDA, Linköpings universitet. © Silberschatz, Galvin and Gagne 2005 6.15 Example: RR with Time Quantum q = 20 Process P1 P2 P3 P4 Burst Time 53 17 68 24 • • • q very large ⇒ FCFS q very small ⇒ many context switches q must be large w.r.t. context switch time, otherwise too high overhead TDDI04, K. Arvidsson, IDA, Linköpings universitet. 6.16 © Silberschatz, Galvin and Gagne 2005 Time Quantum and Context Switches Smaller time quantum ⇒ more context switches § The Gantt chart is: P1 0 P2 20 P3 37 P4 57 P1 77 P3 P4 P1 P3 P3 97 117 121 134 154 162 § Typically, higher average turnaround than SJF, but better response TDDI04, K. Arvidsson, IDA, Linköpings universitet. 6.17 © Silberschatz, Galvin and Gagne 2005 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 6.18 © Silberschatz, Galvin and Gagne 2005 3 RR: Turnaround Time Varies With Time Quantum RR: Turnaround Time Varies With Time Quantum Time Quantum = 1 Time Quantum = 2 P1 P2 P3 P4 P1 P2 P3 P4 3 + 9 + 15 + 17 =11 4 3 5 9 10 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 15 55 17 6.19 © Silberschatz, Galvin and Gagne 2005 RR: Turnaround Time Varies With Time Quantum 10 10 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 14 15 17 The average turnaround time can 5 +10 + 14 17 be improved if +most =11.5 4 processes finish their next CPU burst in a single time quantum. quantum. 6.20 © Silberschatz, Galvin and Gagne 2005 Problems with RR and Priority Schedulers § Priority based scheduling may cause starvation for some processes. § Round robin based schedulers are maybe too ”fair”... we sometimes want to prioritize some processes. The average turnaround time can in general be improved if most processes finish their next CPU burst in a single time quantum. quantum. TDDI04, K. Arvidsson, IDA, Linköpings universitet. 6.21 © Silberschatz, Galvin and Gagne 2005 Multilevel Queue 6.22 © Silberschatz, Galvin and Gagne 2005 § Fixed priority scheduling § Ready queue is partitioned into separate queues, e.g.: • Serve all from foreground queue, then from background queue. • Possibility of starvation. § Time slice foreground (interactive) background (batch) § Each queue has its own scheduling algorithm • • TDDI04, K. Arvidsson, IDA, Linköpings universitet. Scheduling between the queues Useful when processes are easily classified into different groups with different characteristica... characteristica... • • § Solution: Multilevel queue scheduling ...? foreground – RR • Each queue gets a certain share of CPU time which it can schedule amongst its processes • Example: 80% to foreground in RR, 20% to background in FCFS background – FCFS TDDI04, K. Arvidsson, IDA, Linköpings universitet. 6.23 © Silberschatz, Galvin and Gagne 2005 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 6.24 © Silberschatz, Galvin and Gagne 2005 4 Multilevel Queue Scheduling Multilevel Feedback Queue § A process can move between the various queues • aging can be implemented this way § Time-sharing among the queues in priority order • TDDI04, K. Arvidsson, IDA, Linköpings universitet. 6.25 © Silberschatz, Galvin and Gagne 2005 • • high Q1 – RR with q = 16 ms Q2 – FCFS § Scheduling: • • • • A new job enters queue Q0 which is served RR. When it gains CPU, the job receives 8 milliseconds. If it does not finish in 8 milliseconds, it is moved to Q1. low priority At Q1 the job is again served RR and receives 16 additional milliseconds. • If it still does not complete, it is preempted and moved to Q2. § In this case high priority to processes with short CPU bursts TDDI04, K. Arvidsson, IDA, Linköpings universitet. 6.27 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 6.26 © Silberschatz, Galvin and Gagne 2005 § Multilevel-feedback-queue scheduler defined by the following parameters: • • • • • number of queues • priority level of each queue scheduling algorithms for each queue method used to determine when to upgrade a process method used to determine when to demote a process method used to determine which queue a process will enter when that process needs service TDDI04, K. Arvidsson, IDA, Linköpings universitet. 6.28 Multiprocessor Scheduling Multiprocessor work sharing § CPU scheduling more complex when multiple CPUs are available § One centralized task queue • • • © Silberschatz, Galvin and Gagne 2005 Multilevel Feedback Queue Example of Multilevel Feedback Queue § Three queues: • Q0 – RR with q = 8 ms Processes in lower queues get CPU only if higher queues are empty Multiprocessor (SMP): homogeneous processors Multi-core processors, CMP § CPU local task queues § Processor affinity • • Multithreaded processors > HW-Contextswitch-on-Load © Silberschatz, Galvin and Gagne 2005 Decrease cache penalty Resource only available on one CPU § Load balancing (push/pull migration) > Cycle-by-cycle interleaving • • > Simultaneous multithreading OS task monitor and push work to less busy CPU Low load CPU pull work from busy CPU § Supercomputing applications often use a fixed-sized process configuration (“SPMD”, 1 thread per processor) and implement own load balancing methods § More in TDDC78 ... TDDI04, K. Arvidsson, IDA, Linköpings universitet. 6.29 © Silberschatz, Galvin and Gagne 2005 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 6.30 © Silberschatz, Galvin and Gagne 2005 5 Real-Time Scheduling Scheduling Algorithm Evaluation 1 § Hard real-time systems • required to complete a critical task within a guaranteed amount of time • missing a deadline can have catastrophic consequences § Deterministic modeling • • § Soft real-time computing • • requires that critical processes receive priority over less important ones missing a deadline leads to degradation of service > e.g., lost frames / pixelized images in digital TV § Often, periodic tasks or reactive computations • require special scheduling algorithms: RM, EDF, ... Type of analytical evaluation takes a particular predetermined workload and defines the performance of each algorithm for that workload. § Queuing models • Little’s formula: queue = arrival_rate x waiting_time • • Only approximations Questionable accuracy Separate lecture on Real-Time Operating Systems TDDI04, K. Arvidsson, IDA, Linköpings universitet. 6.31 © Silberschatz, Galvin and Gagne 2005 TDDI04, K. Arvidsson, IDA, Linköpings universitet. Scheduling Algorithm Evaluation 2 Summary § Simulation Models • Software system model § CPU Scheduler and Dispatcher • • • Process generation: > Mathematical distribution > Trace tape from real system • • • • CPU utilization, throughput, ... Preemptive vs Non-preemptive scheduling RR, FCFS, SJF Priority scheduling Multilevel queue and Multilevel feedback queue § Multiprocessor Scheduling § (Realtime Scheduling: see Lecture on realtime-OS) § In the book (Chapter 5): Scheduling in Solaris, Windows XP, Linux High cost 6.33 Enable multiprogramming § Scheduling Algorithms § Implementation • What is more accurate than implementing the scheduler in a real OS and test it? TDDI04, K. Arvidsson, IDA, Linköpings universitet. © Silberschatz, Galvin and Gagne 2005 § Goals: => Run simulation • 6.32 © Silberschatz, Galvin and Gagne 2005 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 6.34 © Silberschatz, Galvin and Gagne 2005 6