Chapter 5: CPU Scheduling
5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Scheduler What is the job of a scheduler Terms CPU burst IO burst (???)
5.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Scheduling Which processes get control of the CPU and for how long One of the most important jobs for the OS Can have a huge impact on system performance But why? To understand let’s examine the behavior of a typical program… Programs typically operate in bursts Some bursts involve CPU intensive operations Some involve I/O operations
5.4 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 General Program Behavior Most job loads yield the following CPU burst distribution CPU burst durations tend to be relatively short A few, however, have very long CPU burst lengths
5.5 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 The Job of a CPU Scheduler Selects from among the processes in memory that are ready to execute (in ready Q), and allocates the CPU to one of them One sure way for a process to get suspended and placed in a wait Q: perform an I/O operation What would happen to CPU utilization if a scheduler consistently chose processes that would have the longest CPU burst? What would happen to average wait time? What if a scheduler consistently chose processes with shortest CPU burst? CPU Utilization Average Wait Time
5.6 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 What criteria? What makes a good scheduling algorithm? Average wait time? Processor utilization? What’s more important: user happiness or processor utilization?
5.7 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Some criteria CPU utilization – keep the CPU as busy as possible Throughput – # of processes that complete their execution per time unit Turnaround time – amount of time to execute a particular process: from time of job submission to job completion Waiting time – amount of time a process has been waiting in the ready queue Response time – similar to waiting time, but instead of measuring the total amount of time spent in the ready queue, only count the time from a request for data (like a mouse click) to the time when “begin” to respond.
5.8 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 What control does the OS have? Determines who is next in line to get access to the CPU Determines how long they get the CPU
5.9 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Preemptive or Non-preemptive If allow a process to run until it is done or it performs I/O Non-preemptive or cooperative If put a limit on the amount of time that a process can stay on the CPU Preemptive
5.10 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Context Switch If preempt Put back in ready Q Save state (registers, current PC, etc.) Pull in next process (set PC, set registers)
5.11 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Time Quantum and Context Switch Time Quantum = time allowed on processor Should be very short with respect to quantum
5.12 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Dispatcher Conceptually broken up into two separate tasks Scheduling: determine which process is next Dispatch: actual process of context switch Save state Move process to ready Q Bring in next process (set PC) Re-establish state Dispatch latency – time it takes for the dispatcher to stop one process and start another running
5.13 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Scheduling Algorithms There are a number of common scheduling algorithms First come, first served Shortest job first Priority scheduling Round robin We will examine strengths and weaknesses of each
5.14 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 First-Come, First-Served (FCFS) Scheduling Note: no preemption ProcessBurst Time P 1 24 P 2 3 P 3 3 Suppose that the processes arrive in the order: P 1, P 2, P 3 The Gantt Chart for the schedule is: Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 Average waiting time: ( )/3 = 17 P1P1 P2P2 P3P
5.15 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 FCFS Scheduling (Cont.) Suppose that the processes arrive in the order P 2, P 3, P 1 The Gantt chart for the schedule is: Waiting time for P 1 = 6; P 2 = 0 ; P 3 = 3 Average waiting time: ( )/3 = 3 Much better than previous case Convoy effect short process behind long process P1P1 P3P3 P2P
5.16 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Round Robin (RR) Similar to FCFS but with preemption After quantum (10 to 100 ms) go to back of line Quantum must be large with respect to context switch, otherwise overhead is too high
5.17 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Shortest-Job-First (SJF) Scheduling Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time Two schemes: nonpreemptive – once CPU given to the process it cannot be preempted until completes its CPU burst preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining-Time-First (SRTF) SJF is optimal – gives minimum average waiting time for a given set of processes SJF Optimal for Wait Time
5.18 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 ProcessArrival TimeBurst Time P P P P SJF (non-preemptive), P1 goes first because no other process is in the Q when it arrives Average waiting time = ( )/4 = 4 Example of Non-Preemptive SJF P1P1 P3P3 P2P P4P4 812
5.19 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Example of Preemptive SJF ProcessArrival TimeBurst Time P P P P SJF (preemptive) Average waiting time = ( )/4 = 3 P1P1 P3P3 P2P P4P4 57 P2P2 P1P1 16
5.20 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length of Next CPU Burst How do we know what the shortest job is? Can only estimate the length Can be done by using the length of previous CPU bursts Banker’s maxim: Figures quoted represent past performance, which is no guarantee of future results
5.21 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Priority Scheduling A priority number (integer) is associated with each process The CPU is allocated to the process with the highest priority (smallest integer highest priority) Preemptive Non-preemptive 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
5.22 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Example of RR with Time Quantum = 20 ProcessBurst Time P 1 53 P 2 17 P 3 68 P 4 24 The Gantt chart is: Typically, higher average turnaround (start to finish) than SJF, but better response (first access to CPU) Why can’t we say this definitively? Why typically? P1P1 P2P2 P3P3 P4P4 P1P1 P3P3 P4P4 P1P1 P3P3 P3P
5.23 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Turnaround Time Varies With The Time Quantum Also, if nature of job list changes… Implication: No single best scheduling algorithm—performance varies with nature of work load
5.24 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Multilevel Queue Ready queue is partitioned into separate queues: foreground (interactive) background (batch) Each queue has its own scheduling algorithm For example foreground – RR background – FCFS
5.25 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Multilevel Queue Scheduling must be done between the queues: which Q does the OS pull-from Fixed priority scheduling; (i.e., serve all from foreground then from background). Possibility of starvation. Time slice – each queue gets a certain amount of CPU time which it can schedule amongst its processes; e.g. 80% to foreground in RR 20% to background in FCFS
5.26 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Multilevel Feedback Queue If allow process behavior to dictate Q… Each Q might have different: Scheduling algorithms Priority Quantum
5.27 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005Example Scheduling New job enters queue Q 0 which has a quantum of 8 ms. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q 1.
5.28 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Example Q 1 job, lower priority, 16 milliseconds. If it still does not complete, it is preempted and moved to queue Q 2. Q2 only gets access to CPU if Q’s 0 and 1 are empty A job arriving in a higher priority Q will preempt a lower pri Q Gives highest priority to processes with CPU bursts < 8ms, then those < 24 (8+16), finally the rest
5.29 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Multiple-Processor Scheduling Can have Q’s for each processor or one Q to service all Where does scheduler run? Must design for Symmetric multiprocessing Asymmetric multiprocessing only one processor accesses the system data structures, alleviating the need for data sharing Load sharing If each proc has own cache… Best if a process stays with a given processor
5.30 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Real-Time Scheduling Typically used in systems that control hardware Hard real-time systems – required to complete a critical task within a guaranteed amount of time Soft real-time computing – requires that critical processes receive priority over less fortunate ones When it absolutely, positively has to get there
5.31 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Operating System Examples Solaris scheduling 6 classes of apps each with different scheduling algorithms and priorities Good response time for interactive processes Good throughput for CPU bound Windows XP scheduling Variable priority Window with focus gets high pri Give high-I/O procs higher pri’s CPU bound lower I/O devices stay busy CPU bound use CPU cycles in background Linux scheduling Two priority ranges: real-time and other Higher priority tasks get longer quanta (unlike Solaris and Windows) All three support Priority scheduling Preemption Server threads
5.32 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Algorithm Evaluation How select a CPU-scheduling algorithm? Best approach is to generate a “typical” workload and run on simulator with different schedulers deterministic modeling How describe List of jobs including – start-time – list of times and durations » CPU and I/O bursts
5.33 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Queuing models What is a typical workload Workloads and job types can vary over time Instead can generate sim-jobs based upon a stochastic distribution of CPU and I/O bursts Queuing models On average, which scheduling algorithm performs best Job Set Generator Randomly Generated Job Sets Simulator Evaluate
End of Chapter 5