Contents CMSC 421 Spring 2004 Section 0202 � Basic Concepts � Scheduling Criteria � Scheduling Algorithms � Multiple-Processor Scheduling � Real-Time Scheduling � Algorithm Evaluation Part II: Process Management Chapter 6 CPU Scheduling Silberschatz, Galvin and Gagne 2002 Operating System Concepts 6.2 Basic Concepts Histogram of CPU-burst Times Maximum CPU utilization � obtained with multiprogramming � CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait. � CPU burst distribution is mostly exponential Silberschatz, Galvin and Gagne 2002 Silberschatz, Galvin and Gagne 2002 Operating System Concepts 6.3 Operating System Concepts 6.4 1
CPU Scheduler Preemptive Scheduling � Also referred to as “Short Term � Preemptive Scheduling Scheduler” before.. � Data Consistency Problem � Selects from among the processes in � Kernel Data Structures need to be managed carefully memory that are ready to execute, and � Code affected by interrupts must be guarded from allocates the CPU to one of them. CPU scheduling decisions may take simultaneous use � 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 only under 1 and 4 is � nonpreemptive . � All other scheduling is preemptive. Silberschatz, Galvin and Gagne 2002 Silberschatz, Galvin and Gagne 2002 Operating System Concepts 6.5 Operating System Concepts 6.6 Dispatcher Scheduling Criteria � Dispatcher module gives control of the CPU to the � CPU utilization – keep the CPU as busy as possible process selected by the short-term scheduler; this involves: � Throughput – # of processes that complete their execution per time unit � switching context � Turnaround time – amount of time to execute a � switching to user mode particular process � jumping to the proper location in the user program to restart that program � Time the process ends – time the process enters the system � Dispatch latency – time it takes for the dispatcher to stop one process and start another running. � Waiting time – amount of time a process has been waiting in the ready queue � Response time – amount of time it takes from when a request was submitted until the first response is produced � not the time taken to output the first response Silberschatz, Galvin and Gagne 2002 Silberschatz, Galvin and Gagne 2002 Operating System Concepts 6.7 Operating System Concepts 6.8 2
First-Come, First-Served (FCFS) Scheduling Optimization Criteria � Max CPU utilization Process Burst Time � Max throughput P 1 24 � Min turnaround time P 2 3 � Min waiting time P 3 3 � Min response time � Suppose that the processes arrive in the order: P 1 , P 2 , P 3 The Gantt Chart for the schedule is: � Other optimizations � Minimize variance in response time P 1 P 2 P 3 � Minimize the maximum response time � Each process typically consists of 100 of CPU/IO bursts 0 24 27 30 � We consider 1 CPU burst/process in the description of the algorithms � Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 � Average waiting time: (0 + 24 + 27)/3 = 17 Silberschatz, Galvin and Gagne 2002 Silberschatz, Galvin and Gagne 2002 Operating System Concepts 6.9 Operating System Concepts 6.10 FCFS Scheduling (Cont.) FCFS (Cont.) Suppose that the processes arrive in the order � Convoy Effect � Shorter processes (with smaller CPU bursts) waiting behind P 2 , P 3 , P 1 . a longer CPU-bound process � The Gantt chart for the schedule is: � FCFS in non-preemptive P 2 P 3 P 1 � CPU is held by the currently executing process UNTIL � An I/O request comes � Process terminates 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 Silberschatz, Galvin and Gagne 2002 Silberschatz, Galvin and Gagne 2002 Operating System Concepts 6.11 Operating System Concepts 6.12 3
Shortest-Job-First (SJF) Scheduling SJF (Cont.) � Associate with each process the length of its next CPU � SJF is optimal – gives minimum average waiting time for burst. Use these lengths to schedule the process with the a given set of processes. shortest CPU burst . � SJF could lead to starving processes � Two schemes: � Process waits for CPU while other processes with shorter bursts get priority � 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 the executing process. This scheme is know as the Shortest-Remaining-Time-First (SRTF). Silberschatz, Galvin and Gagne 2002 Silberschatz, Galvin and Gagne 2002 Operating System Concepts 6.13 Operating System Concepts 6.14 Example of Non-Preemptive SJF Example of Preemptive SJF Process Arrival Time Burst Time Process Arrival Time Burst Time P 1 0.0 7 P 1 0.0 7 P 2 2.0 4 P 2 2.0 4 P 3 4.0 1 P 3 4.0 1 P 4 5.0 4 P 4 5.0 4 � SJF (non-preemptive) � SJF (preemptive) P 1 P 3 P 2 P 4 P 1 P 2 P 3 P 2 P 4 P 1 0 3 7 8 12 16 11 16 0 2 4 5 7 � Average waiting time = (0 + 6 + 3 + 7)/4 = 4 � Average waiting time = (9 + 1 + 0 +2)/4 = 3 Silberschatz, Galvin and Gagne 2002 Silberschatz, Galvin and Gagne 2002 Operating System Concepts 6.15 Operating System Concepts 6.16 4
Prediction of the Length of the Next CPU Burst Determining Length of Next CPU Burst alpha=1/2 � Can only estimate the length. � Can be done by using the length of previous CPU bursts, using exponential averaging. 1. t = actual lenght of n th CPU burst n 2. τ = predicted value for the next CPU burst + n 1 3. α , 0 ≤ α ≤ 1 4. Define : ( ) τ = α + − α τ t 1 . + n 1 n n ( ) . τ = α + − α τ t 1 + n 1 n n Silberschatz, Galvin and Gagne 2002 Silberschatz, Galvin and Gagne 2002 Operating System Concepts 6.17 Operating System Concepts 6.18 Examples of Exponential Averaging Priority Scheduling � α =0 � A priority number (integer) is associated with each process � τ n+1 = τ n � The CPU is allocated to the process with the highest � Recent history does not count. priority (smallest integer ≡ highest priority). � α =1 � Preemptive � τ n+1 = t n � nonpreemptive � Only the actual last CPU burst counts. � SJF is a priority scheduling where priority is the predicted � If we expand the formula, we get: next CPU burst time. τ n+1 = α t n +( 1 - α ) α t n - 1 + … � Problem ≡ Starvation – low priority processes may never +(1 - α ) j α t n - j + … execute. +(1 - α ) n+1 τ 0 � Solution ≡ Aging – as time progresses increase the � Since both α and (1 - α ) are less than or equal to 1, each priority of the process. successive term has less weight than its predecessor. Silberschatz, Galvin and Gagne 2002 Silberschatz, Galvin and Gagne 2002 Operating System Concepts 6.19 Operating System Concepts 6.20 5
Round Robin (RR) Example of RR with Time Quantum = 20 � Each process gets a small unit of CPU time ( time quantum or Process Burst Time slice ), usually 10-100 milliseconds. P 1 53 � After this time slice has elapsed, the process is preempted and added to the end of the ready queue. P 2 17 If there are n processes in the ready queue and the time � P 3 68 quantum is q , then each process gets 1/ n of the CPU time in P 4 24 chunks of at most q time units at once. � No process waits more than ( n -1) q time units. � The Gantt chart is: � When deciding on the time slice need to consider the overhead due to the dispatcher � Performance characteristics P 1 P 2 P 3 P 4 P 1 P 3 P 4 P 1 P 3 P 3 � q large ⇒ FIFO 0 20 37 57 77 97 117 121 134 154 162 � q small ⇒ q must be large with respect to context switch, otherwise overhead is too high (processor sharing) � Typically, higher average turnaround than SJF, but better response . Silberschatz, Galvin and Gagne 2002 Silberschatz, Galvin and Gagne 2002 Operating System Concepts 6.21 Operating System Concepts 6.22 Time Quantum and Context Switch Time Turnaround Time Varies With The Time Quantum • Draw the Gantt chart for this Silberschatz, Galvin and Gagne 2002 Silberschatz, Galvin and Gagne 2002 Operating System Concepts 6.23 Operating System Concepts 6.24 6
Recommend
More recommend