34
10CSE 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “Advanced Operating Systems”, Lecture Notes by Prof. Prof. Daniel Mosse , University Of Pittsburgh, http://www.cs.pitt.edu/~mosse/cs1651/ Extracted copy from CS 1651: Advanced Operating Systems, lecture CPU Scheduling CPU Scheduling 1 Operating Systems Design Concepts

10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Embed Size (px)

Citation preview

Page 1: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

10CSE10CSE

CPU Scheduling

Copyrights Lecture Slides adapted from “Advanced Operating Systems”, Lecture Notes by Prof. Prof. Daniel Mosse , University Of Pittsburgh, http://www.cs.pitt.edu/~mosse/cs1651/

Extracted copy from CS 1651: Advanced Operating Systems, lecture CPU Scheduling

CPU Scheduling1

Operating Systems Design Concepts

Page 2: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

CPU SchedulingCPU SchedulingScheduling the processor among

all ready processesThe goal is to achieve:

◦High processor utilization◦High throughput

number of processes completed per of unit time

◦Low response time time elapsed from the submission of a

request until the first response is produced

Page 3: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Classification of Scheduling Classification of Scheduling ActivityActivity

Long-term: which process to admit? Medium-term: which process to swap in or out? Short-term: which ready process to execute next?

Page 4: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Queuing Diagram for Queuing Diagram for SchedulingScheduling

Page 5: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Long-Term SchedulingLong-Term Scheduling

Determines which programs are admitted to the system for processing

Controls the degree of multiprogramming

Attempts to keep a balanced mix of processor-bound and I/O-bound processes◦CPU usage◦System performance

Page 6: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Medium-Term SchedulingMedium-Term Scheduling

Makes swapping decisions based on the current degree of multiprogramming◦Controls which remains resident in

memory and which jobs must be swapped out to reduce degree of multiprogramming

Page 7: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Short-Term SchedulingShort-Term SchedulingSelects from among ready processes in

memory which one is to execute next◦The selected process is allocated the CPU

It is invoked on events that may lead to choose another process for execution:◦Clock interrupts◦I/O interrupts◦Operating system calls and traps◦Signals

Page 8: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Characterization of Scheduling Characterization of Scheduling PoliciesPoliciesThe selection function determines which ready

process is selected next for executionThe decision mode specifies the instants in time

the selection function is exercised◦ Nonpreemptive

Once a process is in the running state, it will continue until it terminates or blocks for an I/O

◦ Preemptive Currently running process may be interrupted

and moved to the Ready state by the OS Prevents one process from monopolizing the

processor

Page 9: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Short-Term Scheduler Short-Term Scheduler DispatcherDispatcherThe dispatcher is the module that

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

The functions of the dispatcher include:◦Switching context◦Switching to user mode◦Jumping to the location in the user

program to restart executionThe dispatch latency must be minimal

Page 10: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

The CPU-I/O CycleThe CPU-I/O Cycle

Processes require alternate use of processor and I/O in a repetitive fashion

Each cycle consist of a CPU burst followed by an I/O burst ◦A process terminates on a CPU burst

CPU-bound processes have longer CPU bursts than I/O-bound processes

Page 11: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Short-Tem Scheduling Short-Tem Scheduling CriteriaCriteria

User-oriented criteria◦ Response Time: Elapsed time between the

submission of a request and the receipt of a response

◦ Turnaround Time: Elapsed time between the submission of a process to its completion

System-oriented criteria◦ Processor utilization◦ Throughput: number of process completed

per unit time◦ fairness

Page 12: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Scheduling AlgorithmsScheduling Algorithms

First-Come, First-Served SchedulingShortest-Job-First Scheduling

◦Also referred to asShortest Process NextPriority SchedulingRound-Robin SchedulingMultilevel Queue SchedulingMultilevel Feedback Queue

Scheduling

Page 13: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Process Mix ExampleProcess Mix Example

ProcessArriva

lTime

ServiceTime

1

2

3

4

5

0

2

4

6

8

3

6

4

5

2

Service time = total processor time needed in one (CPU-I/O) cycleJobs with long service time are CPU-bound jobs and are referredto as “long jobs”

Page 14: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

First Come First Served (FCFS)First Come First Served (FCFS)

Selection function: the process that has been waiting the longest in the ready queue (hence, FCFS)

Decision mode: non-preemptive◦a process runs until it blocks for an I/O

Page 15: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

FCFS drawbacksFCFS drawbacks

Favors CPU-bound processes◦ A CPU-bound process monopolizes the

processor◦ I/O-bound processes have to wait until

completion of CPU-bound process I/O-bound processes may have to wait even after

their I/Os are completed (poor device utilization)

◦ Better I/O device utilization could be achieved if I/O bound processes had higher priority

Page 16: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Shortest Job First (Shortest Job First (Shortest Process Shortest Process

NextNext))

Selection function: the process with the shortest expected CPU burst time◦ I/O-bound processes will be selected first

Decision mode: non-preemptiveThe required processing time, i.e., the CPU

burst time, must be estimated for each process

Page 17: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

SJF / SPN CritiqueSJF / SPN Critique

Possibility of starvation for longer processes Lack of preemption is not suitable in a time

sharing environmentSJF/SPN implicitly incorporates priorities

◦ Shortest jobs are given preferences◦ CPU bound process have lower priority, but a

process doing no I/O could still monopolize the CPU if it is the first to enter the system

Page 18: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Is SJF/SPN optimal?Is SJF/SPN optimal?

If the metric is turnaround time (response time), is SJF or FCFS better?

For FCFS, resp_time=(3+9+13+18+20)/5 = ?◦ Note that Rfcfs = 3+(3+6)+(3+6+4)+…. = ?

For SJF, resp_time=(3+9+11+15+20)/5 = ?◦ Note that Rfcfs = 3+(3+6)+(3+6+4)+…. = ?

Which one is smaller? Is this always the case?

Page 19: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Is SJF/SPN optimal?Is SJF/SPN optimal?

Take each scheduling discipline, they both choose the same subset of jobs (first k jobs).

At some point, each discipline chooses a different job (FCFS chooses k1 SJF chooses k2)

Rfcfs=nR1+(n-1)R2+…+(n-k1)Rk1+….+(n-k2) Rk2+….+Rn

Rsjf=nR1+(n-1)R2+…+(n-k2)Rk2+….+(n-k1) Rk1+….+Rn

Which one is smaller? Rfcfs or Rsjf?

Page 20: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

PrioritiesPriorities

Implemented by having multiple ready queues to represent each level of priority

Scheduler the process of a higher priority over one of lower priority

Lower-priority may suffer starvationTo alleviate starvation allow

dynamic priorities◦The priority of a process changes based

on its age or execution history

Page 21: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Round-RobinRound-Robin

Selection function: same as FCFS Decision mode: preemptive

a process is allowed to run until the time slice period (quantum, typically from 10 to 100 ms) has expired

a clock interrupt occurs and the running process is put on the ready queue

Page 22: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

RR Time QuantumRR Time Quantum

Quantum must be substantially larger than the time required to handle the clock interrupt and dispatching

Quantum should be larger then the typical interaction ◦but not much larger, to avoid

penalizing I/O bound processes

Page 23: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

RR Time QuantumRR Time Quantum

Page 24: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Round Robin: critiqueRound Robin: critique

Still favors CPU-bound processes◦ An I/O bound process uses the CPU for a time

less than the time quantum before it is blocked waiting for an I/O

◦ A CPU-bound process runs for all its time slice and is put back into the ready queue May unfairly get in front of blocked

processes

Page 25: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Multilevel Feedback Multilevel Feedback SchedulingScheduling

Preemptive scheduling with dynamic priorities

N ready to execute queues with decreasing priorities: ◦P(RQ0) > P(RQ1) > ... > P(RQN)

Dispatcher selects a process for execution from RQi only if RQi-1 to RQ0 are empty

Page 26: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Multilevel Feedback Multilevel Feedback SchedulingScheduling

New process are placed in RQ0

After the first quantum, they are moved to RQ1 after the first quantum, and to RQ2 after the second quantum, … and to RQN after the Nth quantum

I/O-bound processes remain in higher priority queues. ◦CPU-bound jobs drift downward. ◦Hence, long jobs may starve

Page 27: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Multiple Feedback Queues Multiple Feedback Queues

Different RQs may have different quantum values

Page 28: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Time Quantum for feedback Time Quantum for feedback SchedulingScheduling

With a fixed quantum time, the turn around time of longer processes can be high

To alleviate this problem, the time quantum can be increased based on the depth of the queue◦ Time quantum of RQi = 2i-1

May still cause longer processes to suffer starvation.◦ Possible fix is to promote a process to higher queue

after some time

Page 29: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Algorithm ComparisonAlgorithm Comparison

Which one is the best?The answer depends on many factors:

◦the system workload (extremely variable)◦hardware support for the dispatcher◦relative importance of performance

criteria (response time, CPU utilization, throughput...)

◦The evaluation method used (each has its limitations...)

Page 30: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Back to SJF: CPU Burst Back to SJF: CPU Burst EstimationEstimation

Let T[i] be the execution time for the ith instance of this process: the actual duration of the ith CPU burst of this process

Let S[i] be the predicted value for the ith CPU burst of this process. The simplest choice is:

◦S[n+1] =(1/n)(T[1]+…+T[n])=(1/n)_{i=1

to n} T[i]This can be more efficiently calculated as:

◦S[n+1] = (1/n) T[n] + ((n-1)/n) S[n]This estimate, however, results in equal

weight for each instance

Page 31: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Estimating the required CPU Estimating the required CPU burstburst

Recent instances are more likely to better reflect future behavior

A common technique to factor the above observation into the estimate is to use exponential averaging :

◦S[n+1] = T[n] + (1-) S[n] ; 0 < < 1

Page 32: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

CPU burst Estimate Exponential CPU burst Estimate Exponential AverageAverage

Recent instances have higher weights, whenever > 1/n

Expanding the estimated value shows that the weights of past instances decrease exponentially◦ S[n+1] = T[n] + (1-)T[n-1] + ... (1-

)^{i}T[n-i] + ... + (1-)^{n}S[1]◦ The predicted value of 1st instance, S[1], is

usually set to 0 to give priority to to new processes

Page 33: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Exponentially Decreasing Exponentially Decreasing CoefficientsCoefficients

Page 34: 10CSE CPU Scheduling Copyrights Lecture Slides adapted from “ Advanced Operating Systems ”, Lecture Notes by Prof. Prof. Daniel Mosse, University Of Pittsburgh,

Exponentially Decreasing Exponentially Decreasing CoefficientsCoefficients

S[1] = 0 to give high priority to new processesExponential averaging tracks changes in process

behavior much faster than simple averaging