Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 5: Process Scheduling
5.2 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms
5.3 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Objectives To introduce CPU scheduling, which is the basis for multiprogrammed operating systems To describe various CPU-scheduling algorithms
5.4 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition
5.5 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition 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 Process execution begins with a CPU burst…That is followed by an I/O burst, which is followed by another CPU burst, then another I/O burst, and so on. Eventually, the final CPU burst ends with a system request to terminate execution
5.6 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Histogram of CPU-burst Times The curve is generally characterized as exponential or hyper exponential The durations of CPU bursts vary greatly from process to process. There is large number of short CPU bursts and a small number of long CPU bursts. An I/O-bound program >>>> has many short CPU bursts. A CPU-bound program >>>> has a few long CPU bursts
5.7 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition CPU Scheduler CPU Scheduler (/ short-term scheduler): Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them The ready queue is not necessarily a first-in, first-out (FIFO) queue. It can be implemented as a FIFO queue, a priority queue, a tree, or an unordered linked list.
5.8 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Preemptive Scheduling Preemptive Scheduling: 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 non-preemptive (/cooperative). Under non-preemptive scheduling, once the CPU has been allocated to a process, the process keeps the CPU until it releases the CPU either by terminating or by switching to the waiting state Scheduling under 2 and 3 is preemptive
5.9 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Dispatcher Dispatcher : is the module gives control of the CPU to the process selected by the short-term scheduler. The dispatcher should be as fast as possible, since it is invoked during every process switch. Dispatch latency – time it takes for the dispatcher to stop one process and start another running
5.10 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition
5.11 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Scheduling Criteria CPU utilization – keep the CPU as busy as possible (represented as percentage) Throughput – # of processes that complete their execution per time unit (e.g. 10 processes /sec) Turnaround time – amount of time to execute a particular process Turnaround time is the sum of the periods spent waiting to get into memory, waiting in the ready queue, executing on the CPU, and doing I/O. Turnaround time is generally limited by the speed of the output device. Waiting time – amount of time a process has been waiting in the ready queue Waiting time is the sum of the periods spent waiting in the ready queue. Response time – amount of time it takes from when a request was submitted until the first response is produced (for time-sharing environment) It is the time it takes to start responding, not the time it takes to output the response.
5.12 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Scheduling Algorithm Optimization Criteria Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time
5.13 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition
5.14 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Scheduling Algorithms First-Come, First-Served Scheduling Shortest-Job-First Scheduling Priority Scheduling Round-Robin Scheduling
5.15 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition (1) First-Come, First-Served (FCFS) Scheduling The simplest CPU-scheduling algorithm Basic methodology: The process that requests the CPU first is allocated the CPU first. The implementation of the FCFS: Using a FIFO queue: When a process enters the ready queue, its PCB is linked onto the tail of the queue. When the CPU is free, it is allocated to the process at the head of the queue. The running process is then removed from the queue. FCFS algorithm is non-preemptive Gantt chart: is a bar chart that illustrates a particular schedule, including the start and finish times of each of the processes.
5.16 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition First-Come, First-Served (FCFS) Scheduling Example(1): Consider the following set of processes that arrive at time 0,with the length of the CPU burst given in milliseconds ProcessBurst Time(ms) 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.17 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition FCFS Scheduling (Cont.) Example(2): Consider the same previous set of processes arrive at time 0,with the length of the CPU burst in milliseconds ProcessBurst Time(ms) P 1 24 P 2 3 P 3 3 Suppose that the processes arrive in the order: P2, P3, P1 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 example (1) Convoy effect >>> short processes wait for the one big process to get off the CPU. This effect results in lower CPU and device utilization than might be possible if the shorter processes were allowed to go first. P1P1 P3P3 P2P
5.18 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition FCFS Scheduling (Cont.) FCFS Pros. (++): Simplest algorithm FCFS Cons. (--): The average waiting time is generally not minimal and affected by processes’ order. Lower CPU and device utilization because of convoy effect Not suitable for time-shared systems
5.19 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition (2) Shortest-Job-First (SJF) Scheduling Basic methodology : Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time If the next CPU bursts of two processes are the same, FCFS scheduling is used to select the next process Two schemes: Non-preemptive – 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) 19
5.20 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Shortest-Job-First Scheduling (cont.) Example(3): Consider the following set of processes with the length of the CPU burst given in milliseconds ProcessBurst Time(ms) P 1 6 P 2 8 P 3 7 P 4 3 SJF scheduling chart Average waiting time = ( ) / 4 = 7 P4P4 P3P3 P1P P2P2 24
5.21 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Shortest-Job-First Scheduling (cont.) Example(4): Consider the following set of processes with the length of the CPU burst given in milliseconds ProcessArrival TimeBurst Time P10.07 P22.04 P34.01 P45.04 Non-preemptive SJF Average waiting time = ( )/4 = 4 P1P1 P3P3 P2P P4P4 812
5.22 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Shortest-Job-First Scheduling (cont.) Example(5): Consider the following set of processes with the length of the CPU burst given in milliseconds ProcessArrival TimeBurst Time P10.07 P22.04 P34.01 P45.04 Preemptive SJF Average waiting time = ( )/4 = 3 P1P1 P3P3 P2P P4P4 57 P2P2 P1P1 16
5.23 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Shortest-Job-First Scheduling (cont.) SJF Pros. (++): SJF is optimal – gives minimum average waiting time for a given set of processes SJF Cons. (--): The difficulty is knowing the length of the next CPU request ( some times this time is predicted)
5.24 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition (3) Priority Scheduling Basic methodology : A priority number (integer) is associated with each process. The CPU is allocated to the process with the highest priority Equal-priority processes are scheduled in FCFS order. SJF is a priority scheduling where priority is the next CPU burst time Text book assumes (smallest integer highest priority) Two schemes: Non-preemptive – once CPU given to the process it cannot be preempted until completes its CPU burst Preemptive – if a new process arrives with priority is higher than the priority of the currently running process, the new process will preempt the CPU 24
5.25 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Priority scheduling (cont.) Example(6): Consider the following set of processes with the length of the CPU burst given in milliseconds ProcessBurst TimePriority P1103 P211 P324 P415 P5 52 Priority scheduling chart Average waiting time = ( )/5 = 8.2 ms 25
5.26 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Priority scheduling (cont.) Priority scheduling Pros. (++): Simple algorithm Priority scheduling Cons. (--): Main Problem >> Starvation ( /indefinite blocking)>>> low priority processes may never execute Solution >> Aging >>as time progresses increase the priority of the process
5.27 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition (4) Round Robin Scheduling (RR) (RR) algorithm is designed especially for timesharing systems. Basic methodology : Each process gets a small unit of CPU time (time quantum ). After this time has elapsed, the process is preempted and added to the end of the ready queue. Time quantum (/ time slice ) (q)>> usually ms. The ready queue is treated as a circular queue and implemented as FIFO queue 27
5.28 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Round Robin Scheduling (cont.) Example(6): Consider the following set of processes with the length of the CPU burst given in milliseconds And time quantum = 4 ms Process Burst Time P124 P2 3 P3 3 The Gantt chart is: Average waiting time = ( )/3 = 5.66 ms P1P1 P2P2 P3P3 P1P1 P1P1 P1P1 P1P1 P1P
5.29 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Round Robin Scheduling (cont.) 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. Performance (depends on the size of the time quantum) If q is very large RR is same as FCFS If q is very small decrease the performance because of context switch time and increase system overhead Switching the CPU to another process requires performing a state save of the current process and a state restore of a different process.
5.30 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Round Robin Scheduling (cont.) The effect of context switching on the performance of RR scheduling: Example(7): we have only one process of 10 time units. We want the time quantum to be large with respect to the context switch time. Generally A time quantum (10 to 100 milliseconds) & a context switch time <10 microseconds
5.31 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Round Robin Scheduling (cont.) Turnaround time depends on the size of the time quantum. The average turnaround time can be improved if most processes finish their next CPU burst in a single time quantum.
5.32 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Round Robin Scheduling (cont.) RR Scheduling Pros. (++): Suitable to time-shared system (better response time) RR Scheduling Cons. (--): The average waiting time under the RR policy is often long Context switch overhead is higher
5.33 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition (5) Multilevel Queue scheduling Processes are easily classified into different groups, Such as: Foreground (interactive) processes Background (batch) processes Basic methodology : Ready queue is partitioned into separate queues The processes are permanently assigned to one queue, Each queue has its own scheduling algorithm. E.g.: foreground processes >> scheduled by RR & background process >> scheduled by FCFS Scheduling must be done between the queues. For example: – fixed-priority preemptive scheduling-- the foreground queue may have absolute priority over the background queue – Use time slice (RR) – each queue gets a certain amount of CPU time which it can schedule amongst its processes; i.e., 80% to foreground in RR, 20% to background in FCFS 33 >>These types of processes have different response-time requirements and so different scheduling needs >>Foreground process may have higher priority
5.34 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Example (8): A multilevel queue scheduling algorithm with five queues, listed in order of priority Each queue has absolute priority over lower-priority queues. E.g. No process in the batch queue could run unless the queues for system processes, interactive processes, and interactive editing processes were all empty. If an interactive editing process entered the ready queue while a batch process was running, the batch process would be preempted. Multilevel Queue Scheduling (cont.) 34
5.35 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Multilevel Queue Scheduling (cont.) Multilevel Queue Scheduling Pros. (++): Low scheduling overhead Consider different process prosperities & requirements Multilevel Queue Scheduling Cons. (--): Inflexible: a process can’t change it’s queue Starvation possibility
5.36 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition (6) Multilevel Feedback Queue Basic methodology : A process can move between the various queues The idea is to separate processes according to the characteristics of their CPU bursts. If a process uses too much CPU time, it will be move d to a lower-priority queue. This scheme leaves I/O-bound and interactive processes in the higher-priority queues. A process that waits too long in a lower-priority queue may be moved to a higher-priority queue. This form of aging prevents starvation. 36
5.37 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Multilevel Feedback Queue (cont.) Example(9): consider a multilevel feedback queue scheduler with three queues: Q 0 – RR with time quantum 8 milliseconds (higher priority) Q 1 – RR time quantum 16 milliseconds Q 2 – FCFS Scheduling Processes in lower priority queue is selected if the higher queues are empty A new job enters queue Q 0 which is served RR. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q 1. If Q 0 is empty, process at Q 1 job is again served RR and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q Q 0 ( highest priority) Q1Q1 Q 2 (lowest priority)
5.38 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Multilevel Feedback Queue (cont.) Multilevel-feedback-queue scheduler defined by the following parameters: Number of queues Scheduling algorithms for each queue Method used to determine when to upgrade a process Method used to determine when to demote a process Method used to determine which queue a process will enter when that process needs service
5.39 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Multilevel Feedback Queue (cont.) Multilevel Feedback Queue Scheduling Pros. (++): Very flexible>>>it is the most general CPU-scheduling algorithm. It can be configured to match a specific system under design. Can be configured to prevent starvation. Multilevel Feedback Queue Scheduling Cons. (--): Most complex algorithm
5.40 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition
5.41 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Multiple-Processor Scheduling CPU scheduling is more complex when multiple CPUs are available. load sharing becomes possible Homogeneous processors -processors are identical in functionality (i.e. any processor can run any process in the ready queue) 41
5.42 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Multiple-Processor Scheduling (cont.) Approaches to Multiple-Processor Scheduling Asymmetric multiprocessing Master processor executes system code & slave processors execute user code Only the master processor has all scheduling decisions, I/O processing, and other system activities Simple & reduce the need for data sharing Symmetric multiprocessing (SMP) Each processor is self-scheduling, All processes in common ready queue, or each has its own private queue of ready processes If we have multiple processors trying to access a common data structure, the scheduler must be programmed carefully. ….(i.e. difficult) OS must ensure that two processors do not choose the same process and that processes are not lost from the queue.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition End of Chapter 5