Operating Systems Lecture 16 Scheduling II Operating System Concepts 6.1 Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 Recall Scheduling Criteria CPU utilization –fraction of time the CPU is busy. CPU efficiency – fraction of time the CPU is executing user code. Throughput – # of processes completed per unit time Average Turnaround time – average delay between job submission and job completion. Normalized turnaround time – Ratio of turnaround time to service time per process. Indicates the relative delay experienced by a process. 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. Operating System Concepts 6.2 Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 Example: Computing Criteria Suppose two processes arrive at time zero. ti = service time for process i to complete. P1: t1 = 7 P2: t2 = 11 Assume 1 unit of time for dispatch of process. Assume non-preemptive scheduling. a) Draw the Gantt diagram for these processes. Operating System Concepts 6.3 Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 Example: Computing Criteria (continued) Suppose two processes arrive at time zero. P1: t1 = 7 P2: t2 = 11 Assume 1 unit of time for dispatch of process. Assume non-preemptive scheduling. b) Compute the following: Avg. service time: Throughput: CPU utilization: CPU efficiency: Avg. Turnaround: Avg. Wait time: (Note: Avg wait time = avg. turnaround - avg service - avg dispatch) Normalized turnaround for P1 : Normalized turnaround for P2 : c) Suppose P2 arrives at time 4. What statistics change? What are their new values? Operating System Concepts 6.4 Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 Assumptions in next examples Assume dispatch time = 0 (unless otherwise noted) CPU efficiency = CPU utilization Assume each process has one and only one CPU burst (unless otherwise noted). Variable definitions: w = time spent in system (wait time, blocked time, execution time). e = time spent in execution thus far. s = total service time required, including e. Operating System Concepts 6.5 Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 Example with FCFS scheduling FCFS (first come first served) selection function: max(w) Example: P1 : t1 = 60 (arrival at 0 - e) P2 : t2 = 10 (arrival at 0) P3 : t3 = 30 (arrival at 0 + e) P4 : t4 = 20 (arrival at 0 + 2*e) e = an extremely small unit of time. a) Draw the Gantt diagram. Operating System Concepts 6.6 Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 Example with FCFS continued Example: P1 : t1 = 60 (arrival at 0 - e) P2 : t2 = 10 (arrival at 0) P3 : t3 = 30 (arrival at 0 + e) P4 : t4 = 20 (arrival at 0 + 2*e) e = an extremely small unit of time. b) Compute the following quantities: Avg. service time: Throughput: CPU utilization: CPU efficiency: Avg turnaround time: Avg wait time: Normalized turnaroundtime for each process: Operating System Concepts 6.7 Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 Example with FCFS continued Suppose order changes to: P2 P3 P4 P1 Example: P1 : t1 = 60 (arrival at 0 + 2*e) P2 : t2 = 10 (arrival at 0- e) P3 : t3 = 30 (arrival at 0 ) P4 : t4 = 20 (arrival at 0 + e) e = an extremely small unit of time. a) Draw the Gantt diagram. b) What values change? What are the new values? Operating System Concepts 6.8 Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 Notes on FCFS FCFS is unfair to processes with short CPU bursts. (They may have long wait times compared to their service requirements). Question: Is starvation possible with FCFS? FCFS is generally used only with batch systems. (May be used as part of another system). Operating System Concepts 6.9 Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 Example of Non-Preemptive SJF Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 SJF (non-preemptive) Selection criterion: min(s) P1 0 3 P3 7 P2 8 P4 12 16 Average waiting time = (0 + 3 + 6 + 7)/4 = 4 Avg service time = Throughput = Avg turnaround = Check consistency. Wait = (Turnaround - Service - Dispatch) Operating System Concepts 6.10 Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 Notes on SJF SJF is a priority algorithm Priority is based on the predicted next CPU burst Question: Is starvation possible with SJF? Operating System Concepts 6.11 Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 Example of Preemptive SJF Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 SJF (preemptive) Selection criterion: min(s - e) P1 0 P2 2 P3 4 P2 5 P4 7 P1 11 16 Average waiting time = (9 + 1 + 0 + 2)/4 = 3 What statistics are different from non-preemptive SJF? What are the values of these stats? Operating System Concepts 6.12 Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 Determining Length of Next CPU Burst Can be done by using the length of previous CPU bursts, using exponential averaging. 1. tn actual lenght of nthCPU burst 2. n1 predicted value for the next CPU burst 3. , 0 1 4. Define : n 1 t n 1 n . If we expand the formula, we get: n+1 = tn+(1 - ) tn -1 + … +(1 - )j tn -j + … +(1 - )n+1 0 Operating System Concepts 6.13 Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 Priority Scheduling A priority number (integer) is associated with each process. The CPU is allocated to the process with the highest priority Some systems have a high number represent high priority. Other systems have a low number represent high priority. Text uses a low number to represent high priority. Priority scheduling may be preemptive or nonpreemptive. Operating System Concepts 6.14 Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 Assigning Priorities SJF is a priority scheduling where priority is the predicted next CPU burst time. Other bases for assigning priority: Memory requirements Number of open files Avg I/O burst / Avg CPU burst External requirements (amount of money paid, political factors, etc). Problem: Starvation -- low priority processes may never execute. Solution: Aging -- as time progresses increase the priority of the process. Operating System Concepts 6.15 Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 Round Robin Scheduling Each process gets a small unit of CPU time (time quantum). A time quantum is usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. If there are n processes in the ready queue and the time quantum is q, then 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. Operating System Concepts 6.16 Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 Example of RR, time quantum = 20 Process P1 P2 P3 P4 The Gantt chart is: P1 0 P2 20 37 P3 Burst Time 53 17 68 24 P4 57 P1 77 P3 97 117 P4 P1 P3 P3 121 134 154 162 Compute: Avg service time, Throughput, avg turnaround, avg wait: Typically, higher average turnaround than SJF, but better response. Suppose P1 arrives at 0, P2 at 19, P3 at 23 and P4 at 25. What changes? What are the new values? Operating System Concepts 6.17 Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 RR Performance Performance varies with the size of the time slice, but not in a simple way. Short time slice leads to faster interactive response. Problem: Adds lots of context switches. High Overhead. Longer time slice leads to better system throughput (lower overhead), but response time is worse. If time slice is too long, RR becomes just like FCFS. Time slice vs process switch time: If time slice = 20 msec and process switch time = 5 msec, then 5/25 = 20% of CPU time spent on overhead. If time slice = 500 msec, then only 1% of CPU used for overhead. The time slice should be large compared to the process switch time. A typical time slice is 1 sec (4.3 BSD UNIX) RR makes the implicit assumption that all processes are equally important. Cannot use RR is you want different processes to have different priorities. Silberschatz, Galvin and Gagne 2002 Operating System Concepts 6.18 Modified for CSCI 399, Royden, 2005