Chapter 5: CPU Scheduling
Basic Concepts Maximum CPU utilization is obtained by multiprogramming CPU I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait Burst access CPU burst I/O burst When I/O burst stage, the CPU is idle 5.2 Silberschatz, Galvin and Gagne 2009
Alternating Sequence of CPU And I/O Bursts 5.3 Silberschatz, Galvin and Gagne 2009
Histogram of CPU-burst Times The large number of short CPU bursts. I/O bound program The small number of long CPU bursts. CPU bound program 5.4 Silberschatz, Galvin and Gagne 2009
CPU Scheduler 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. Switches from running to waiting state 2. Switches from running to ready state 3. Switches from waiting to ready 4. Terminates Scheduling under 1 and 4 is nonpreemptive All other scheduling could be preemptive 5.5 Silberschatz, Galvin and Gagne 2009
Dispatcher Dispatcher module gives control of the CPU to the process which is selected by the short-term scheduler; this involves: switching process context Mode change into user mode jumping to the proper location in the user program to restart that program Dispatch latency time to take for the dispatcher to stop one process and start another running 5.6 Silberschatz, Galvin and Gagne 2009
Scheduling Criteria CPU utilization keep the CPU as busy as possible Throughput # of processes that completes their execution per time unit Turnaround time amount of time to execute a particular process Interval from process submission to completion The policy which has the view of specific process 5.7 Silberschatz, Galvin and Gagne 2009
Scheduling Criteria Waiting time amount of time a process has been waiting in the ready queue Optimal criteria in case of shortest job first policy Response time Interactive system, turnaround time is not best solution Turnaround time is limited by the speed of the output device amount of time it takes from when a request was submitted until the first response is produced i.e., The time to start responding, not the time to completed output of response 5.8 Silberschatz, Galvin and Gagne 2009
Scheduling Criteria Confliction The confliction example In interactive system, response time is important criteria To enhance response time many context switching throughput is reduced. 5.9 Silberschatz, Galvin and Gagne 2009
Optimization Criteria Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time 5.10 Silberschatz, Galvin and Gagne 2009
First-Come, First-Served (FCFS) Scheduling FIFO scheduling Simplest scheme Processes dispatched according to arrival time Non-preemption policy Rarely used as primary scheduling algorithm 5.11 Silberschatz, Galvin and Gagne 2009
First-Come, First-Served (FCFS) Scheduling Process Burst 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: P 1 P 2 P 3 0 24 27 30 Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17 5.12 Silberschatz, Galvin and Gagne 2009
FCFS Scheduling (Cont.) Suppose that the processes arrive in the order P 2, P 3, P 1 The Gantt chart for the schedule is: P 2 P 3 P 1 0 3 6 30 Waiting time for P 1 = 6;P 2 = 0 ; P 3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3 Much better than previous case Convoy effect short process behind long process 5.13 Silberschatz, Galvin and Gagne 2009
Shortest-Job-First (SJF) Scheduling Associate with each process length of its next CPU burst. Use these lengths to schedule the process with the shortest time Two variations: nonpreemptive once CPU is 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, then preempt it. This scheme is known as the Shortest-Remaining-Time-First (SRTF) 5.14 Silberschatz, Galvin and Gagne 2009
Example of Non-Preemptive SJF Process Arrival Time Burst Time SJF (non-preemptive) p P 1 0.0 7 P 2 2.0 4 P 3 40 4.0 1 P 4 5.0 4 P 1 P 3 P 2 P 4 0 3 7 8 12 16 Average waiting time = (0 + 6 + 3 + 7)/4 = 4 5.15 Silberschatz, Galvin and Gagne 2009
Example of Preemptive SJF(SRTF) SJF (preemptive) p Process Arrival Time Burst Time P 1 0.0 7 P 2 2.0 4 P 3 40 4.0 1 P 4 5.0 4 P 1 P 2 P 3 P 2 P 4 P 1 0 2 4 5 7 11 16 Average waiting time = (9 + 1 + 0 +2)/4 = 3 5.16 Silberschatz, Galvin and Gagne 2009
Shortest-Job-First (SJR) Scheduling SJF is optimal gives minimum average waiting time for a given set of processes Real difficulty How do we know next burst of CPU? Exponential Average Next CPU burst is predicted as an exponential average of the measured lengths of previous CPU bursts. 5.17 Silberschatz, Galvin and Gagne 2009
Determining Length of Next CPU Burst Can only estimate the length Can be done by using the length of previous CPU bursts, using exponential averaging 1. t n = actual length of n th CPU burst 2. τ n + 1 = predicted value for the next CPU burst 3. α, 0 α 1 4. Define : τ = α + ( 1 α ). n t τ + 1 n n 5.18 Silberschatz, Galvin and Gagne 2009
Prediction of the Length of Next CPU Burst 5.19 Silberschatz, Galvin and Gagne 2009
Examples of Exponential Averaging ( ). τ n+ 1 t + τ n = α t + τ If α is ½, then recent history and past n 1 α history are equally weighted. So α controls relative weight of recent and past history 5.20 Silberschatz, Galvin and Gagne 2009
Priority Scheduling A priority number (integer) is associated with each process The CPU is allocated to the process with the highest priority Preemptive nonpreemptive SJF can be treated as a priority scheduling where priority is the predicted next CPU burst time Problem Starvation low priority processes may never execute 5.21 Silberschatz, Galvin and Gagne 2009
Priority Scheduling Solution Aging As time progresses, system increases the priority of the process HRRN (Highest Response Ratio Next) scheduling Response ratio = (waiting time + required CPU time) / required CPU time The problem of non-preemptive priority scheduling The possibility of Priority Inversion 5.22 Silberschatz, Galvin and Gagne 2009