Chapter 6 CPU Scheduling Operating System Concepts 8

  • Slides: 43
Download presentation
Chapter 6: CPU Scheduling Operating System Concepts – 8 th Edition Silberschatz, Galvin and

Chapter 6: CPU Scheduling Operating System Concepts – 8 th Edition Silberschatz, Galvin and Gagne © 2009

Chapter 5: CPU Scheduling n Basic Concepts n Scheduling Criteria n Scheduling Algorithms Operating

Chapter 5: CPU Scheduling n Basic Concepts n Scheduling Criteria n Scheduling Algorithms Operating System Concepts – 8 th Edition 5. 2 Silberschatz, Galvin and Gagne © 2009

Objectives n To introduce CPU scheduling, which is the basis for multiprogrammed operating systems

Objectives n To introduce CPU scheduling, which is the basis for multiprogrammed operating systems n To describe various CPU-scheduling algorithms Operating System Concepts – 8 th Edition 5. 3 Silberschatz, Galvin and Gagne © 2009

Basic Concepts Operating System Concepts – 8 th Edition 5. 4 Silberschatz, Galvin and

Basic Concepts Operating System Concepts – 8 th Edition 5. 4 Silberschatz, Galvin and Gagne © 2009

Basic Concepts n Maximum CPU utilization obtained with multiprogramming n CPU–I/O Burst Cycle l

Basic Concepts n Maximum CPU utilization obtained with multiprogramming n CPU–I/O Burst Cycle l Process execution consists of a cycle of CPU execution and I/O wait 4 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. 4 Eventually, the final CPU burst ends with a system request to terminate execution Operating System Concepts – 8 th Edition 5. 5 Silberschatz, Galvin and Gagne © 2009

Histogram of CPU-burst Times n The curve is generally characterized as exponential or hyper

Histogram of CPU-burst Times n The curve is generally characterized as exponential or hyper exponential n The durations of CPU bursts vary greatly from process to process. n There is large number of short CPU bursts and a small number of long CPU bursts. n An I/O-bound program >>>> has many short CPU bursts. n A CPU-bound program >>>> has a few long CPU bursts Operating System Concepts – 8 th Edition 5. 6 Silberschatz, Galvin and Gagne © 2009

CPU Scheduler n CPU Scheduler (/ short-term scheduler): l Selects from among the processes

CPU Scheduler n CPU Scheduler (/ short-term scheduler): l Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them l 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. Operating System Concepts – 8 th Edition 5. 7 Silberschatz, Galvin and Gagne © 2009

Preemptive Scheduling n Preemptive Scheduling: l CPU scheduling decisions may take place when a

Preemptive Scheduling n Preemptive Scheduling: l 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 n Scheduling under 1 and 4 is non-preemptive (/cooperative). 4 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 n Scheduling under 2 and 3 is preemptive Operating System Concepts – 8 th Edition 5. 8 Silberschatz, Galvin and Gagne © 2009

Dispatcher n Dispatcher : is the module gives control of the CPU to the

Dispatcher n Dispatcher : is the module gives control of the CPU to the process selected by the short-term scheduler. n The dispatcher should be as fast as possible, since it is invoked during every process switch. n Dispatch latency – time it takes for the dispatcher to stop one process and start another running Operating System Concepts – 8 th Edition 5. 9 Silberschatz, Galvin and Gagne © 2009

Scheduling Criteria Operating System Concepts – 8 th Edition 5. 10 Silberschatz, Galvin and

Scheduling Criteria Operating System Concepts – 8 th Edition 5. 10 Silberschatz, Galvin and Gagne © 2009

Scheduling Criteria n CPU utilization – keep the CPU as busy as possible (represented

Scheduling Criteria n CPU utilization – keep the CPU as busy as possible (represented as percentage) n Throughput – # of processes that complete their execution per time unit (e. g. 10 processes /sec) n Turnaround time – amount of time to execute a particular process n l 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. l 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 l n 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) l It is the time it takes to start responding, not the time it takes to output the response. Operating System Concepts – 8 th Edition 5. 11 Silberschatz, Galvin and Gagne © 2009

Scheduling Algorithm Optimization Criteria n Max CPU utilization n Max throughput n Min turnaround

Scheduling Algorithm Optimization Criteria n Max CPU utilization n Max throughput n Min turnaround time n Min waiting time n Min response time Operating System Concepts – 8 th Edition 5. 12 Silberschatz, Galvin and Gagne © 2009

Scheduling Algorithms Operating System Concepts – 8 th Edition 5. 13 Silberschatz, Galvin and

Scheduling Algorithms Operating System Concepts – 8 th Edition 5. 13 Silberschatz, Galvin and Gagne © 2009

Scheduling Algorithms n First-Come, First-Served Scheduling n Shortest-Job-First Scheduling n Priority Scheduling n Round-Robin

Scheduling Algorithms n First-Come, First-Served Scheduling n Shortest-Job-First Scheduling n Priority Scheduling n Round-Robin Scheduling n Multilevel Queue Scheduling n Multilevel Feedback Queue Operating System Concepts – 8 th Edition 5. 14 Silberschatz, Galvin and Gagne © 2009

(1) First-Come, First-Served (FCFS) Scheduling n The simplest CPU-scheduling algorithm n Basic methodology: The

(1) First-Come, First-Served (FCFS) Scheduling n The simplest CPU-scheduling algorithm n Basic methodology: The process that requests the CPU first is allocated the CPU first. n The implementation of the FCFS: l Using a FIFO queue: 4 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. n FCFS algorithm is non-preemptive n Gantt chart: is a bar chart that illustrates a particular schedule, including the start and finish times of each of the processes. Operating System Concepts – 8 th Edition 5. 15 Silberschatz, Galvin and Gagne © 2009

First-Come, First-Served (FCFS) Scheduling n Example(1): Consider the following set of processes that arrive

First-Come, First-Served (FCFS) Scheduling n Example(1): Consider the following set of processes that arrive at time 0, with the length of the CPU burst given in milliseconds Process Burst Time(ms) P 1 24 P 2 3 P 3 3 l Suppose that the processes arrive in the order: P 1 , P 2 , P 3 n The Gantt Chart for the schedule is: P 1 P 2 0 24 P 3 27 30 n Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 n Average waiting time: (0 + 24 + 27)/3 = 17 Operating System Concepts – 8 th Edition 5. 16 Silberschatz, Galvin and Gagne © 2009

FCFS Scheduling (Cont. ) Example(2): Consider the same previous set of processes arrive at

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 Process Burst Time(ms) P 1 P 2 P 3 24 3 3 Suppose that the processes arrive in the order: P 2 , P 3 , P 1 n The Gantt chart for the schedule is: P 2 0 P 3 3 P 1 6 30 n Waiting time for P 1 = 6; P 2 = 0; P 3 = 3 n Average waiting time: (6 + 0 + 3)/3 = 3 >>>>Much better than example (1) n Convoy effect >>> short processes wait for the one big process to get off the CPU. l This effect results in lower CPU and device utilization than might be possible if the shorter processes were allowed to go first. Operating System Concepts – 8 th Edition 5. 17 Silberschatz, Galvin and Gagne © 2009

FCFS Scheduling (Cont. ) n FCFS Pros. (++): l Simplest algorithm n FCFS Cons.

FCFS Scheduling (Cont. ) n FCFS Pros. (++): l Simplest algorithm n FCFS Cons. (--): l The average waiting time is generally not minimal and affected by processes’ order. l Lower CPU and device utilization because of convoy effect l Not suitable for time-shared systems Operating System Concepts – 8 th Edition 5. 18 Silberschatz, Galvin and Gagne © 2009

(2) Shortest-Job-First (SJF) Scheduling 19 n Basic methodology : l Associate with each process

(2) Shortest-Job-First (SJF) Scheduling 19 n Basic methodology : l Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time l If the next CPU bursts of two processes are the same, FCFS scheduling is used to select the next process n Two schemes: l Non-preemptive – once CPU given to the process it cannot be preempted until completes its CPU burst l 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) Operating System Concepts – 8 th Edition 5. 19 Silberschatz, Galvin and Gagne © 2009

Shortest-Job-First Scheduling (cont. ) n Example(3): Consider the following set of processes with the

Shortest-Job-First Scheduling (cont. ) n Example(3): Consider the following set of processes with the length of the CPU burst given in milliseconds Process P 1 P 2 P 3 P 4 Burst Time(ms) 6 8 7 3 n SJF scheduling chart P 4 0 P 3 P 1 3 9 P 2 16 24 n Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 Operating System Concepts – 8 th Edition 5. 20 Silberschatz, Galvin and Gagne © 2009

Shortest-Job-First Scheduling (cont. ) n Example(4): Consider the following set of processes with the

Shortest-Job-First Scheduling (cont. ) n Example(4): Consider the following set of processes with the length of the CPU burst given in milliseconds Process Arrival Time P 1 0. 0 7 P 2 2. 0 4 P 3 4. 0 1 P 4 5. 0 4 Burst Time n Non-preemptive SJF P 1 0 3 P 3 7 P 2 8 P 4 12 16 n Average waiting time = (0 + 6 + 3 + 7)/4 = 4 Operating System Concepts – 8 th Edition 5. 21 Silberschatz, Galvin and Gagne © 2009

Shortest-Job-First Scheduling (cont. ) n Example(5): Consider the following set of processes with the

Shortest-Job-First Scheduling (cont. ) n Example(5): Consider the following set of processes with the length of the CPU burst given in milliseconds Process Arrival Time P 1 0. 0 7 P 2 2. 0 4 P 3 4. 0 1 P 4 5. 0 4 Burst Time n Preemptive SJF P 1 0 P 2 2 P 3 4 P 2 5 P 4 P 1 11 7 16 n Average waiting time = (9 + 1 + 0 +2)/4 = 3 Operating System Concepts – 8 th Edition 5. 22 Silberschatz, Galvin and Gagne © 2009

Shortest-Job-First Scheduling (cont. ) n SJF Pros. (++): l SJF is optimal – gives

Shortest-Job-First Scheduling (cont. ) n SJF Pros. (++): l SJF is optimal – gives minimum average waiting time for a given set of processes n SJF Cons. (--): l The difficulty is knowing the length of the next CPU request ( some times this time is predicted) Operating System Concepts – 8 th Edition 5. 23 Silberschatz, Galvin and Gagne © 2009

(3) Priority Scheduling 24 n Basic methodology : l A priority number (integer) is

(3) Priority Scheduling 24 n Basic methodology : l A priority number (integer) is associated with each process. The CPU is allocated to the process with the highest priority l Equal-priority processes are scheduled in FCFS order. n SJF is a priority scheduling where priority is the next CPU burst time n Text book assumes (smallest integer highest priority) n Two schemes: l Non-preemptive – once CPU given to the process it cannot be preempted until completes its CPU burst l 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 Operating System Concepts – 8 th Edition 5. 24 Silberschatz, Galvin and Gagne © 2009

Priority scheduling (cont. ) 25 n Example(6): Consider the following set of processes with

Priority scheduling (cont. ) 25 n Example(6): Consider the following set of processes with the length of the CPU burst given in milliseconds Process P 1 P 2 P 3 P 4 Burst Time 10 3 1 1 2 4 1 5 P 5 5 Priority 2 n Priority scheduling chart n Average waiting time = (6 + 0 + 16 +18 + 1)/5 = 8. 2 ms Operating System Concepts – 8 th Edition 5. 25 Silberschatz, Galvin and Gagne © 2009

Priority scheduling (cont. ) n Priority scheduling Pros. (++): l Simple algorithm n Priority

Priority scheduling (cont. ) n Priority scheduling Pros. (++): l Simple algorithm n Priority scheduling Cons. (--): l Main Problem >> Starvation ( /indefinite blocking)>>> low priority processes may never execute 4 Solution >> Aging >>as time progresses increase the priority of the process Operating System Concepts – 8 th Edition 5. 26 Silberschatz, Galvin and Gagne © 2009

(4) Round Robin Scheduling (RR) 27 n (RR) algorithm is designed especially for timesharing

(4) Round Robin Scheduling (RR) 27 n (RR) algorithm is designed especially for timesharing systems. n Basic methodology : l 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. n Time quantum (/ time slice ) (q)>> usually 10 -100 ms. n The ready queue is treated as a circular queue and implemented as FIFO queue Operating System Concepts – 8 th Edition 5. 27 Silberschatz, Galvin and Gagne © 2009

Round Robin Scheduling (cont. ) 28 Example(6): Consider the following set of processes with

Round Robin Scheduling (cont. ) 28 Example(6): Consider the following set of processes with the length of the CPU burst given in milliseconds n And time quantum = 4 ms n Process P 1 P 2 P 3 Burst Time 24 3 3 n The Gantt chart is: P 1 0 P 2 4 P 3 7 P 1 10 P 1 14 P 1 18 22 P 1 26 30 n Average waiting time = (6 + 4 + 7 )/3 = 5. 66 ms Operating System Concepts – 8 th Edition 5. 28 Silberschatz, Galvin and Gagne © 2009

Round Robin Scheduling (cont. ) n If there are n processes in the ready

Round Robin Scheduling (cont. ) n If there are n processes in the ready queue and the time quantum is q, then : l l 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. n Performance (depends on the size of the time quantum) l If q is very large RR is same as FCFS l 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. Operating System Concepts – 8 th Edition 5. 29 Silberschatz, Galvin and Gagne © 2009

Round Robin Scheduling (cont. ) n The effect of context switching on the performance

Round Robin Scheduling (cont. ) n The effect of context switching on the performance of RR scheduling: l Example(7): we have only one process of 10 time units. n We want the time quantum to be large with respect to the context switch time. n Generally A time quantum (10 to 100 milliseconds) & a context switch time <10 microseconds Operating System Concepts – 8 th Edition 5. 30 Silberschatz, Galvin and Gagne © 2009

Round Robin Scheduling (cont. ) n Turnaround time depends on the size of the

Round Robin Scheduling (cont. ) n Turnaround time depends on the size of the time quantum. n The average turnaround time can be improved if most processes finish their next CPU burst in a single time quantum. Operating System Concepts – 8 th Edition 5. 31 Silberschatz, Galvin and Gagne © 2009

Round Robin Scheduling (cont. ) n RR Scheduling Pros. (++): l Suitable to time-shared

Round Robin Scheduling (cont. ) n RR Scheduling Pros. (++): l Suitable to time-shared system (better response time) n RR Scheduling Cons. (--): l The average waiting time under the RR policy is often long l Context Operating System Concepts – 8 th Edition switch overhead is higher 5. 32 Silberschatz, Galvin and Gagne © 2009

(5) Multilevel Queue scheduling 33 n Processes are easily classified into different groups, Such

(5) Multilevel Queue scheduling 33 n Processes are easily classified into different groups, Such as: Foreground (interactive) processes l Background (batch) processes l n Basic methodology : >>These types of processes have different response-time requirements and so different scheduling needs >>Foreground process may have higher priority Ready queue is partitioned into separate queues l The processes are permanently assigned to one queue, l Each queue has its own scheduling algorithm. l 4 l E. g. : foreground processes >> scheduled by RR & background process >> scheduled by FCFS Scheduling must be done between the queues. 4 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 Operating System Concepts – 8 th Edition 5. 33 Silberschatz, Galvin and Gagne © 2009

Multilevel Queue Scheduling (cont. ) n Example (8): A multilevel 34 queue scheduling algorithm

Multilevel Queue Scheduling (cont. ) n Example (8): A multilevel 34 queue scheduling algorithm with five queues, listed in order of priority n Each queue has absolute priority over lower-priority queues. l 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. n If an interactive editing process entered the ready queue while a batch process was running, the batch process would be preempted. Operating System Concepts – 8 th Edition 5. 34 Silberschatz, Galvin and Gagne © 2009

Multilevel Queue Scheduling (cont. ) n Multilevel Queue Scheduling Pros. (++): l l Low

Multilevel Queue Scheduling (cont. ) n Multilevel Queue Scheduling Pros. (++): l l Low scheduling overhead Consider different process prosperities & requirements n Multilevel Queue Scheduling Cons. (--): l Inflexible: a process can’t change it’s queue l Starvation possibility Operating System Concepts – 8 th Edition 5. 35 Silberschatz, Galvin and Gagne © 2009

(6) Multilevel Feedback Queue 36 n Basic methodology : l A process can move

(6) Multilevel Feedback Queue 36 n Basic methodology : l A process can move between the various queues n The idea is to separate processes according to the characteristics of their CPU bursts. l If a process uses too much CPU time, it will be move d to a lower-priority queue. 4 This scheme leaves I/O-bound and interactive processes in the higher-priority queues. l 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. Operating System Concepts – 8 th Edition 5. 36 Silberschatz, Galvin and Gagne © 2009

Multilevel Feedback Queue (cont. ) 37 n Example(9): consider a multilevel feedback queue scheduler

Multilevel Feedback Queue (cont. ) 37 n Example(9): consider a multilevel feedback queue scheduler with three queues: l Q 0 – RR with time quantum 8 milliseconds (higher priority) l Q 1 – RR time quantum 16 milliseconds l Q 2 – FCFS Q 0 ( highest priority) n Scheduling l Processes in lower priority queue is selected if the higher queues are empty l 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. l 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 2. Operating System Concepts – 8 th Edition 5. 37 Q 1 Q 2 (lowest priority) Silberschatz, Galvin and Gagne © 2009

Multilevel Feedback Queue (cont. ) n Multilevel-feedback-queue scheduler defined by the following parameters: l

Multilevel Feedback Queue (cont. ) n Multilevel-feedback-queue scheduler defined by the following parameters: l Number of queues l Scheduling algorithms for each queue l Method used to determine when to upgrade a process l Method used to determine when to demote a process l Method used to determine which queue a process will enter when that process needs service Operating System Concepts – 8 th Edition 5. 38 Silberschatz, Galvin and Gagne © 2009

Multilevel Feedback Queue (cont. ) n Multilevel Feedback Queue Scheduling Pros. (++): l Very

Multilevel Feedback Queue (cont. ) n Multilevel Feedback Queue Scheduling Pros. (++): l Very flexible>>>it is the most general CPU-scheduling algorithm. It can be configured to match a specific system under design. l Can be configured to prevent starvation. n Multilevel Feedback Queue Scheduling Cons. (--): l Most complex algorithm Operating System Concepts – 8 th Edition 5. 39 Silberschatz, Galvin and Gagne © 2009

Multiple-Processor Scheduling Operating System Concepts – 8 th Edition 5. 40 Silberschatz, Galvin and

Multiple-Processor Scheduling Operating System Concepts – 8 th Edition 5. 40 Silberschatz, Galvin and Gagne © 2009

Multiple-Processor Scheduling 41 n CPU scheduling is more complex when multiple CPUs are available.

Multiple-Processor Scheduling 41 n CPU scheduling is more complex when multiple CPUs are available. n load sharing becomes possible n Homogeneous processors -processors are identical in functionality (i. e. any processor can run any process in the ready queue) Operating System Concepts – 8 th Edition 5. 41 Silberschatz, Galvin and Gagne © 2009

Multiple-Processor Scheduling (cont. ) n Approaches to Multiple-Processor Scheduling l l Asymmetric multiprocessing 4

Multiple-Processor Scheduling (cont. ) n Approaches to Multiple-Processor Scheduling l l Asymmetric multiprocessing 4 Master processor executes system code & slave processors execute user code 4 Only the master processor has all scheduling decisions, I/O processing, and other system activities 4 Simple & reduce the need for data sharing Symmetric multiprocessing (SMP) 4 Each processor is self-scheduling, 4 All processes in common ready queue, or each has its own private queue of ready processes 4 If we have multiple processors trying to access a common data structure, the scheduler must be programmed carefully. …. (i. e. difficult) 4 OS must ensure that two processors do not choose the same process and that processes are not lost from the queue. Operating System Concepts – 8 th Edition 5. 42 Silberschatz, Galvin and Gagne © 2009

End of Chapter 5 Operating System Concepts – 8 th Edition Silberschatz, Galvin and

End of Chapter 5 Operating System Concepts – 8 th Edition Silberschatz, Galvin and Gagne © 2009