Chapter 5: CPU Scheduling Chapter 5: CPU Scheduling 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Chapter 5: CPU Scheduling Chapter 5: CPU Scheduling ■ Basic Concepts ■ Scheduling Criteria ■ Scheduling Algorithms ■ Multiple-Processor Scheduling ■ Real-Time Scheduling ■ Thread Scheduling ■ Operating Systems Examples ■ Java Thread Scheduling ■ Algorithm Evaluation 5.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Basic Concepts Basic Concepts ■ 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 5.4 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Alternating Sequence of CPU And I/O Bursts Alternating Sequence of CPU And I/O Bursts 5.5 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Histogram of CPU-burst Times Histogram of CPU-burst Times 5.6 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 CPU Scheduler 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 is preemptive 5.7 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Dispatcher Dispatcher ■ Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves: ● switching context ● switching to user mode ● jumping to the proper location in the user program to restart that program ■ Dispatch latency – time it takes for the dispatcher to stop one process and start another running 5.8 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Scheduling Criteria Scheduling 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 ■ 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 output (for time- sharing environment) 5.9 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Optimization Criteria Optimization Criteria ■ Max CPU utilization ■ Max throughput ■ Min turnaround time ■ Min waiting time ■ Min response time 5.10 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 First-Come, First-Served (FCFS) Scheduling 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: ■ Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 ■ Average waiting time: (0 + 24 + 27)/3 = 17 P 1 P 2 P 3 24 27 300 [...]... s Windows XP scheduling s Linux scheduling Operating System Concepts – 7th Edition, Feb 2, 20 05 5.33 Silberschatz, Galvin and Gagne ©20 05 Solaris 2 Scheduling Operating System Concepts – 7th Edition, Feb 2, 20 05 5.34 Silberschatz, Galvin and Gagne ©20 05 Solaris Dispatch Table Operating System Concepts – 7th Edition, Feb 2, 20 05 5. 35 Silberschatz, Galvin and Gagne ©20 05 Windows XP Priorities Operating... preempted and moved to queue Q2 Operating System Concepts – 7th Edition, Feb 2, 20 05 5.26 Silberschatz, Galvin and Gagne ©20 05 Multilevel Feedback Queues Operating System Concepts – 7th Edition, Feb 2, 20 05 5.27 Silberschatz, Galvin and Gagne ©20 05 Multiple-Processor Scheduling s CPU scheduling more complex when multiple CPUs are available s Homogeneous processors within a multiprocessor s Load sharing... CPU burst 2 τ n +1 = predicted value for the next CPU burst 3 α , 0 ≤ α ≤ 1 τ n =1 = α t n + (1 − α )τ n 4 Define : Operating System Concepts – 7th Edition, Feb 2, 20 05 5. 15 Silberschatz, Galvin and Gagne ©20 05 Prediction of the Length of the Next CPU Burst Operating System Concepts – 7th Edition, Feb 2, 20 05 5.16 Silberschatz, Galvin and Gagne ©20 05 Examples of Exponential Averaging s α =0 q q s τn+1... 2, 20 05 5.28 Silberschatz, Galvin and Gagne ©20 05 Real-Time Scheduling s Hard real-time systems – required to complete a critical task within a guaranteed amount of time s Soft real-time computing – requires that critical processes receive priority over less fortunate ones Operating System Concepts – 7th Edition, Feb 2, 20 05 5.29 Silberschatz, Galvin and Gagne ©20 05 Thread Scheduling s Local Scheduling. .. Edition, Feb 2, 20 05 5.19 Silberschatz, Galvin and Gagne ©20 05 Example of RR with Time Quantum = 20 Process P1 17 P3 68 P4 24 The Gantt chart is: P1 s 53 P2 s Burst Time 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 Operating System Concepts – 7th Edition, Feb 2, 20 05 5.20 Silberschatz, Galvin and Gagne ©20 05 Time Quantum... Feb 2, 20 05 5.21 Silberschatz, Galvin and Gagne ©20 05 Turnaround Time Varies With The Time Quantum Operating System Concepts – 7th Edition, Feb 2, 20 05 5.22 Silberschatz, Galvin and Gagne ©20 05 Multilevel Queue s Ready queue is partitioned into separate queues: foreground (interactive) background (batch) s Each queue has its own scheduling algorithm q q s foreground – RR background – FCFS Scheduling. .. Arrival Time 5. 0 4 SJF (preemptive) P1 0 s P2 2 P3 4 P2 5 P4 P1 11 7 16 Average waiting time = (9 + 1 + 0 +2)/4 = 3 Operating System Concepts – 7th Edition, Feb 2, 20 05 5.14 Silberschatz, Galvin and Gagne ©20 05 Determining Length of Next CPU Burst s Can only estimate the length s Can be done by using the length of previous CPU bursts, using exponential averaging 1 t n = actual length of n th CPU burst... System Concepts – 7th Edition, Feb 2, 20 05 5.12 Silberschatz, Galvin and Gagne ©20 05 Example of Non-Preemptive SJF Process Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 s Arrival Time 5. 0 4 SJF (non-preemptive) P1 0 s 3 P3 7 P2 8 P4 12 16 Average waiting time = (0 + 6 + 3 + 7)/4 = 4 Operating System Concepts – 7th Edition, Feb 2, 20 05 5.13 Silberschatz, Galvin and Gagne ©20 05 Example of Preemptive SJF Process... process needs service Operating System Concepts – 7th Edition, Feb 2, 20 05 5. 25 Silberschatz, Galvin and Gagne ©20 05 Example of Multilevel Feedback Queue s Three queues: q q Q1 – RR time quantum 16 milliseconds q s Q0 – RR with time quantum 8 milliseconds Q2 – FCFS Scheduling q A new job enters queue Q0 which is served FCFS When it gains CPU, job receives 8 milliseconds If it does not finish in 8 milliseconds,... the threads library decides which thread to put onto an available LWP s Global Scheduling – How the kernel decides which kernel thread to run next Operating System Concepts – 7th Edition, Feb 2, 20 05 5.30 Silberschatz, Galvin and Gagne ©20 05 Pthread Scheduling API #include #include #define NUM THREADS 5 int main(int argc, char *argv[]) { int i; pthread t tid[NUM THREADS]; pthread . Chapter 5: CPU Scheduling Chapter 5: CPU Scheduling 5. 2 Silberschatz, Galvin and Gagne ©20 05 Operating System Concepts – 7 th Edition, Feb 2, 20 05 Chapter 5: CPU Scheduling Chapter 5: CPU Scheduling ■ Basic. Sequence of CPU And I/O Bursts 5. 5 Silberschatz, Galvin and Gagne ©20 05 Operating System Concepts – 7 th Edition, Feb 2, 20 05 Histogram of CPU- burst Times Histogram of CPU- burst Times 5. 6 Silberschatz,. another running 5. 8 Silberschatz, Galvin and Gagne ©20 05 Operating System Concepts – 7 th Edition, Feb 2, 20 05 Scheduling Criteria Scheduling Criteria ■ CPU utilization – keep the CPU as busy as