Chapter 4 CPU Scheduling Chapter 4 CPU Scheduling

  • Slides: 41
Download presentation
Chapter 4: CPU Scheduling

Chapter 4: CPU Scheduling

Chapter 4: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Evaluation

Chapter 4: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Evaluation

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

Objectives To introduce CPU scheduling, which is the basis for multiprogrammed operating systems To describe various CPU-scheduling algorithms

Basic Concepts Almost all programs have some alternating cycle of CPU number crunching and

Basic Concepts Almost all programs have some alternating cycle of CPU number crunching and waiting for I/O of some kind. In a simple system running a single process, the time spent waiting for I/O is wasted, and those CPU cycles are lost forever. A scheduling system allows one process to use the CPU while another is waiting for I/O, thereby making full use of otherwise lost CPU cycles.

Alternating Sequence of CPU and I/O Bursts Almost all processes alternate between two states

Alternating Sequence of CPU and I/O Bursts Almost all processes alternate between two states in a continuing cycle, A CPU burst of performing calculations, and An I/O burst, waiting for data transfer in or out of the system.

Histogram of CPU-burst Times

Histogram of CPU-burst Times

CPU Scheduler Selects from among the processes in memory that are ready to execute,

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 non-preemptive All other scheduling is preemptive

Dispatcher module gives control of the CPU to the process selected by the short-term

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

Scheduling Criteria CPU utilization – keep the CPU as busy as possible Throughput –

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)

Scheduling Algorithm Optimization Criteria Max CPU utilization Max throughput Min turnaround time Min waiting

Scheduling Algorithm Optimization Criteria Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time

First-Come First-Serve Scheduling, FCFS is very simple - Just a FIFO queue, like customers

First-Come First-Serve Scheduling, FCFS is very simple - Just a FIFO queue, like customers waiting in line at the bank or the post office or at a copying machine. Unfortunately, however, FCFS can yield some very long average wait times, particularly if the first process to get there takes a long time.

First-Come, First-Served (FCFS) Scheduling Process Burst Time P 1 24 P 2 3 P

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: P 1 0 P 2 24 P 3 27 Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17 30

FCFS Scheduling (Cont. ) Suppose that the processes arrive in the order: P 2

FCFS Scheduling (Cont. ) Suppose that the processes arrive in the order: P 2 , P 3 , P 1 The Gantt chart for the schedule is: P 2 P 3 P 1 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 Convoy effect short process behind long process

Example-FCFS Scheduling

Example-FCFS Scheduling

Example-FCFS Scheduling

Example-FCFS Scheduling

Example-FCFS Scheduling

Example-FCFS Scheduling

Example-FCFS Scheduling

Example-FCFS Scheduling

Shortest-Job-First (SJF) Scheduling The idea behind the SJF algorithm is to pick the quickest

Shortest-Job-First (SJF) Scheduling The idea behind the SJF algorithm is to pick the quickest fastest little job that needs to be done, get it out of the way first, and then pick the next smallest fastest job to do next. Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time. SJF is optimal – gives minimum average waiting time for a given set of processes The difficulty is knowing the length of the next CPU request.

Example of SJF Process Arrival Time Burst Time P 1 0. 0 6 P

Example of SJF Process Arrival Time Burst Time P 1 0. 0 6 P 2 2. 0 8 P 3 4. 0 7 P 4 5. 0 3 SJF scheduling chart P 4 P 1 0 3 P 3 9 P 2 16 24 Average waiting time = (3 + 16 + 9 + 0) / 4 = 7

Example of SJF

Example of SJF

Example of SJF

Example of SJF

Prediction of the Length of the Next CPU Burst

Prediction of the Length of the Next CPU Burst

Determining Length of Next CPU Burst Can only estimate the length Can be done

Determining Length of Next CPU Burst Can only estimate the length Can be done by using the length of previous CPU bursts, using exponential averaging n+1 = t n + (1 - ) n.

Prediction of the Length of the Next CPU Burst In this scheme the previous

Prediction of the Length of the Next CPU Burst In this scheme the previous estimate contains the history of all previous times, and alpha serves as a weighting factor for the relative importance of recent data versus past history. If alpha is 1. 0, then past history is ignored, and we assume the next burst will be the same length as the last burst. If alpha is 0. 0, then all measured burst times are ignored, and we just assume a constant burst time. Most commonly alpha is set at 0. 5

Examples of Exponential Averaging =0 n+1 = n Recent history does not count. =1

Examples of Exponential Averaging =0 n+1 = n Recent history does not count. =1 n+1 = tn Only the actual last CPU burst counts.

SJF with Preemption Shortest Remaining Time First When a process arrives to RQ, sort

SJF with Preemption Shortest Remaining Time First When a process arrives to RQ, sort it in and select the SJF including the running process, possibly interrupting it (Remember: SJF schedules a new process only when the running is finished)

SJF with Preemption Process Burst Time Arrival P 1 8 0 P 2 4

SJF with Preemption Process Burst Time Arrival P 1 8 0 P 2 4 1 P 3 9 2 P 4 5 3

Example

Example

Example

Example

Priority Scheduling A priority number (integer) is associated with each process The CPU is

Priority Scheduling A priority number (integer) is associated with each process The CPU is allocated to the process with the highest priority (smallest integer highest priority) Preemptive Non-preemptive SJF is a priority scheduling where priority is the predicted next CPU burst time Problem Starvation – low priority processes may never execute Solution Aging – as time progresses increase the priority of the process

Priority Scheduling Process Burst Time Priority P 1 10 3 P 2 1 1

Priority Scheduling Process Burst Time Priority P 1 10 3 P 2 1 1 P 3 2 4 P 4 1 5 P 5 5 2

Round Robin (RR) Each process gets a small unit of CPU time (time quantum),

Round Robin (RR) Each process gets a small unit of CPU time (time quantum), 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. Performance q large FIFO q small q must be large with respect to context switch, otherwise overhead is too high

Example of RR with Time Quantum = 4 Process Burst Time P 1 24

Example of RR with Time Quantum = 4 Process Burst Time P 1 24 P 2 3 P 3 3 The Gantt chart is: P 1 0 P 2 4 P 1 P 3 7 10 P 1 14 P 1 18 22 P 1 26 P 1 30 Typically, higher average turnaround than SJF, but better response

Time Quantum and Context Switch Time

Time Quantum and Context Switch Time

Multilevel Queue When processes can be readily categorized, then multiple separate queues can be

Multilevel Queue When processes can be readily categorized, then multiple separate queues can be established, each implementing whatever scheduling algorithm is most appropriate for that type of job, and/or with different parametric adjustments. Scheduling must also be done between queues, that is scheduling one queue to get time relative to other queues . Two common options are strict priority ( no job in a lower priority queue runs until all higher priority queues are empty ) and round-robin ( each queue gets a time slice in turn, possibly of different sizes. ) Note that under this algorithm jobs cannot switch from queue to queue - Once they are assigned a queue, that is their queue until they finish.

Multilevel Queue Scheduling

Multilevel Queue Scheduling

Multilevel Feedback Queue A process can move between the various queues; aging can be

Multilevel Feedback Queue A process can move between the various queues; aging can be implemented this way. Multilevel-feedback-queue following parameters: scheduler defined by the 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

Example of Multilevel Feedback Queue Three queues: Q 0 – RR with time quantum

Example of Multilevel Feedback Queue Three queues: Q 0 – RR with time quantum 8 milliseconds Q 1 – RR time quantum 16 milliseconds Q 2 – FCFS Scheduling A new job enters queue Q 0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q 1. At Q 1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q 2.

Multilevel Feedback Queues

Multilevel Feedback Queues

Assignment-1 Scheduling Algorithm Evaluation Deterministic Modeling Takes a predetermined workload and compute the performance

Assignment-1 Scheduling Algorithm Evaluation Deterministic Modeling Takes a predetermined workload and compute the performance of each algorithm for that workload Queuing Models Mathematical Approach for handling stochastic workloads Implementation / Simulation Build system which allows actual algorithms to be run against actual data. Most flexible / general.

End of Chapter 4

End of Chapter 4