cpu schedulingpreemptive scheduling cpu scheduling decisions may take place when a process: 1....

42
CPU Scheduling CS8493 CPU Scheduling 1

Upload: others

Post on 08-Aug-2021

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

CPU Scheduling

CS8493 CPU Scheduling 1

Page 2: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

CPU Scheduling

CS8493 CPU Scheduling 2

• Basic Concepts• Scheduling Criteria• SchedulingAlgorithms• Multiple-Processor Scheduling• Real-Time CPU Scheduling

Page 3: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Objectives

CS8493 CPU Scheduling 3

• CPU scheduling is the basis for multiprogrammed operating systems

• By switching the CPU among processes, the operating system can make the computer more productive.

• To describe various CPU-scheduling algorithms• To discuss evaluation criteria for selecting a CPU-scheduling

algorithm for a particular system

Page 4: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Basic Concepts

CS8493 CPU Scheduling 4

• In a single-processor system, only one process can run at a time. Others must wait until the CPU is free and can be rescheduled.

• Maximum CPU utilization obtained with multiprogramming

• Several processes are kept in memory at one time.

• OS gives CPU to one process and others has to wait

• Fundamental OS functions is scheduling.

Page 5: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

CPU–I/O Burst Cycle

• Process execution consists of a cycle of CPU execution and I/O wait

• CPU burst followed by I/O burst then CPU burst and so on

• CPU burst distribution is of main concern

• Final CPU burst ends with a system request to terminate execution

CPUburstload store add store read from file

store increment indexwrite to file

load store add store read from file

wait for I/O

wait for I/O

wait for I/O

I/O burst

I/O burst

I/O burst

CPUburst

CPUburst

•••

•••

Alternate sequences of CPU & I/O burstsCS8493 CPU Scheduling 5

Page 6: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Histogram of CPU-burst Times

• Large number of short CPU bursts and a small number of long CPU bursts.

• An I/O-bound program typically has many short CPU bursts.• A CPU-bound program might have a few long CPU bursts.CS8493 CPU Scheduling 6

Page 7: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

CPU Scheduler

CS8493 CPU Scheduling 7

■ Short-term scheduler selects from among the processes in ready queue, and allocates the CPU to one of them● Ready queue is not necessarily a first-in, first-out (FIFO) queue,

may be ordered in various ways like a FIFO queue, a priority queue, a tree, or simply an unordered linked list.

● All the processes in the ready queue are lined up waiting for a chance to run on the CPU.

● The records in the queues are generally process control blocks (PCBs) of the processes.

Page 8: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Preemptive Scheduling

■ CPU scheduling decisions may take place when a process:1. Switches from running to waiting state - I/O request2. Switches from running to ready state - interrupt occurs3. Switches from waiting to ready state - completion of I/O4. Terminates

■ Scheduling under 1 and 4 is nonpreemptiveo The Process keeps the CPU until it releases the CPU eitherby

terminating or by switching to the waiting state.■ All other scheduling is preemptive - allows a running process to

be interrupted by a high priority process■ Preemptive scheduling incurs a cost associated:

● Race condition when data is shared among processes● Consider preemption while in kernel mode

CS●8493Consider interrupts occuCrPrUinSgchedduulinrging crucial OS activities 8

Page 9: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Dispatcher

CS8493 CPU Scheduling 9

• 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

Page 10: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Scheduling Criteria

CS8493 CPU Scheduling 10

• Many scheduling criteria have been suggested for comparing CPU-scheduling algorithms to use in a particular situation

It includes:• CPU utilization – keep the CPU as busy as possible• Throughput – # of processes that are completed their execution per time unit• Arrival time – Time at which the process enters ready state• Burst time – Amount of CPU time required by the process to finish its job• Turnaround time – amount of time to execute a particular process.

o TAT = Completion time - arrival time=Burst time + Waiting time• Waiting time – amount of time a process has been waiting in the ready queue

o WT = TAT – Burst Time• 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)o In preemptive, RT = CPU’s 1st schedule – Arrival timeo In nonpreemptive, RT = WT

Page 11: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Scheduling Algorithm Optimization Criteria

CS8493 CPU Scheduling 11

• Max CPU utilization• Max throughput• Min turnaround time• Min waiting time• Min response timeFor example, to guarantee that all users get good service, we may want to minimize the maximum response time.

Page 12: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Scheduling Algorithms

CS8493 CPU Scheduling 12

• First- Come, First-Served (FCFS) Scheduling• Shortest-Job-First (SJF) Scheduling• Priority Scheduling• Round Robin Scheduling• Multilevel Queue Scheduling• Multilevel Feedback Queue Scheduling

Page 13: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

First- Come, First-Served (FCFS) Scheduling

CS8493 CPU Scheduling 13

Page 14: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

First- Come, First-Served (FCFS) Scheduling

CS8493 CPU Scheduling 14

Criteria: Arrival time Mode: Non preemptive DS: Queue

ProcessP1

P2

P3

Burst Time(ms) 2433 with arrivat time as 0

P1 P2 P3

• Suppose that the processes arrive in the order: P1 , P2 ,P3The Gantt Chart for the schedule is:

0 24 27 30

Gantt chart, is a bar chart that illustrates a particular schedule, including the start and finish times of each of the participating processes

• Waiting time for P1 = 0; P2 = 24; P3 = 27ms• Average waiting time: (0 + 24 + 27)/3 = 17ms

Page 15: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

FCFS Scheduling (Cont.)

CS8493 CPU Scheduling 15

Suppose that the processes arrive in the order:P2 , P3 ,P1

■ The Gantt chart for the schedule is:

■ Average waiting time: (6 + 0 + 3)/3 = 3■ Much better than previous case■ Convoy effect - All the processes wait for the one big process to

get off the CPU.

P2 P3 P1

0 3 6

■ Waiting time for P1 = 6; P2 = 0; P3 =3

30

Page 16: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

FCFS Scheduling

CS8493 CPU Scheduling 16

Pno AT BT CT TAT WT1 0 42 1 33 2 14 3 25 4 5

Find the average TAT & average WT.

Page 17: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

FCFS Scheduling

CS8493 CPU Scheduling 17

Pno AT BT CT TAT WT1 0 4 4 4 02 1 3 7 6 33 2 1 8 6 54 3 2 10 7 55 4 5 15 11 6

p1 p2 p3 p4 p50 4 7 8

Avg TAT = (4+6+6+7+11)/5 = 6.8 ms Avg. WT = (0+3+5+5+6)/5 = 3.8 ms

10 15

Page 18: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Shortest-Job-First (SJF) Scheduling

CS8493 CPU Scheduling 18

Page 19: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Shortest-Job-First (SJF) Scheduling

CS8493 CPU Scheduling 19

• Associate with each process the length of its next CPU burst– CPU is assigned to the process that has the smallest next

CPU burst.– If the next CPU bursts of two processes are the same, FCFS

scheduling is used to break the tie– Appropriate term for this scheduling would be the shortest-

next- CPU-burst algorithm

• 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– Useful in long-term scheduling

Page 20: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Example of SJF

Process Burst Time6873

CS8493 CPU Scheduling 20

P1

P2

P3

P4

• SJF scheduling chart

• FCFS waiting time = ?

P4 P1 P3 P2

240 3 9 16

• Average waiting time = (0+3 + 9+16 ) / 4 = 7

Criteria: Burst time (low to high) Mode: Preemptive(shortest-remaining-time-first scheduling) or Non preemptive Data Structure: Min Heap

Page 21: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

SJF

21

Disadvantages Starvation to longer jobs

Advantages1. Maximum throughput2.Minimum average waiting time & turnaround time

It is not implementable because burst time of processes cannot be known ahead

Solution : Can only estimate the length – should be similar to the previous one• Then pick process with shortest predicted next CPU burst.

Prediction Techniques

Static• Process size•CSP84r93ocess type

Dynamic

* SimpleAveragingCPU Schedulin*g ExponentialAveraging

Page 22: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Determining Length of Next CPU Burst

CS8493 CPU Scheduling 22

• Static MethodProcess SizeMatch process size with old process size. If it is same, analyse old process execution• Pold having size 200 KB which is already executed and its Burst-time is

20 Units• Pnew having size 201 KB which is yet to be executed, then its BT = 20

units

Process type• Predict Burst-Time depending on the type of process.

• Operating System process(like scheduler, dispatcher, segmentation, fragmentation) are faster than User process (Gaming, application software).

• Burst-Time for any New OS process can be predicted from any old OS process of similar type and same for User process.

Page 23: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Determining Length of Next CPU Burst (cntd)

CS8493 CPU Scheduling 23

Dynamic method – Let ti be the actual Burst-Time of ith process and n+1 be the predicted Burst-time for n+1th process.

Simple average – Given n processes ( P1, P2… Pn)

n+1 = 1/n(Σi=1 to n ti)

Average burst time of P1, P2, P3, P4 to predict P5’s burst time is

Burst Time of P5 = (Burst time of P1+P2+P3+P4)/4

Page 24: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Determining Length of Next CPU Burst(cntd)

• Commonly, α set to ½• Preemptive version called shortest-remaining-time-first

Define:3. , 0 14.

• Next CPU burst done by using the length of previous CPU bursts, using exponential averaging

n+1 = α tn + (1 - α) n

1. tn actual length of nth CPU burst2. n1 predicted value for the next CPUburst

α, relative weight of recent and past history

CS8493 CPU Scheduling 24

Page 25: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Examples of Exponential Averaging

CS8493 CPU Scheduling 25

• =0– n+1 = n

– Recent history tn has no effect• =1

– n+1 = tn

– Only the actual last CPU burst counts• If we expand the formula, we get:

n+1 = tn+(1 - ) tn -1 + …+(1 - )j tn -j + …+(1 - )n +1 0

• Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor

Page 26: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Prediction of the Length of the Next CPU Burst

ti

i

time

CPU burst (ti) 6 4 6 4 13 13 13 …"guess" (i) 10 8 6 6 5 9 11 12 …

4

2

6

8

10

12

CS8493 CPU Scheduling 26

Page 27: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Example of Shortest-remaining-time-first

■ Now we add the concepts of varying arrival times and preemption to the analysis

Process Arrival Time Burst Time CT TAT(CT-AT) WTP1 0 8 17 17 9P2 1 4 5 4 0P3 2 9 26 24 15P4 3 5 10 7 2

P1 P2 P4 P1 P3

■ Preemptive SJF Gantt Chart

0 1 5 10 17 26

■ Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 =6.5 msec

CS8493 CPU Scheduling 27Nonpreemptive SJF avg WT=?

Page 28: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

GATE 2011

CS8493 CPU Scheduling 28

What is the average CT, TAT and WT using SRTF?

P No AT BT CT TAT WT1 0 92 1 43 2 9

Page 29: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

GATE 2011 Answer

CS8493 CPU Scheduling 29

P No AT BT CT TAT WT1 0 9 13 13 42 1 4 5 4 03 2 9 22 20 11

P1 P2 P2 P1 P3

0 1 2 5 13 22

Avg CT = (13+5+22)/3 Avg TAT = (13+4+20)/3 Avg WT = (4+0+11)/3

Page 30: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

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– Nonpreemptive

• Equal-priority processes are scheduled in FCFS order.• SJF is priority scheduling where priority is the inverse of predicted

next CPU burst time• The larger the CPU burst, the lower the priority, and vice versa.• Problem Starvation – low priority processes may never execute• Solution Aging – as time progresses increase the priority of the

process• Ex: Increase the priority of a waiting process by 1 every 15

CmS8i4n93utes. CPU Scheduling 30

Page 31: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Example of Priority Scheduling

Set of processes, assumed to have arrived at time 0 in the order P1, P2, ···, P5, with the length of the CPU burst given in milliseconds. Find average waiting time.

Process Burst Time PriorityP1 10 3P2 1 1P3 2 4P4 1 5P5 5 2

• Priority scheduling Gantt Chart

C•S84A93verage waiting time = 8.2 msec 31

Page 32: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Round Robin (RR)

CS8493 CPU Scheduling 32

• Each process gets a small unit of CPU time (time quantum q), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue.

• Similar to FCFS scheduling with preemption• 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.

• Timer interrupts every quantum to schedule next process• Performance depends heavily on the size of the time quantum.

– q large FIFO– q small q must be large with respect to context switch,

otherwise overhead is too high

Page 33: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Example of RR with Time Quantum = 4

CS8493 CPU Scheduling 33

Process Burst Time CT TAT(CT-AT) WT(TAT-BT)P1 24 30 30 6P2 3 7 7 4P3 3 10 10 7

P1 P2 P3 P1 P1 P1 P1 P1

Processes arrives at time 0.• The Gantt chart is:

0 4 7 10 14 18 22 26 30

• Avg. WT = (6+4+7)/3= 5.66ms• Typically, higher average turnaround than SJF, but better

response• q should be large compared to context switch time but not too

large• q usually 10ms to 100ms, context switch < 10µsec

Page 34: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Time Quantum and Context Switch Time

How a smaller time quantum increases context switches.

CS8493 CPU Scheduling 34

Page 35: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Turnaround Time Varies With The Time Quantum

80% of CPU bursts should be shorter than q

CS8493 CPU Scheduling 35

Page 36: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Example of RR with Time Quantum = 2ms

CS8493 CPU Scheduling 36

PNO AT BT CT TAT WT1 0 42 1 53 2 24 3 15 4 66 6 3

Find avg TAT and Avg WT.

Page 37: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Example of RR with Time Quantum = 2ms

PNO AT BT CT TAT WT1 0 4 8 8 42 1 5 18 17 123 2 2 6 4 24 3 1 9 6 55 4 6 21 17 116 6 3 19 13 10

P1 P2 P3 P1 P4 P5 P2 P6 P5 P2 P6 P5

P1 P2 P3 P1 P4 P5 P2 P6 P5 P2 P6 P5

Order of Arrival in Queue

Gantt Chart

Avg TAT = 10.83ms21 Avg WT = 7.33ms

37

0 2 4 6 8 9 11 13 15 17 18 19

At time ‘2’, P2, P3 arrived. Put P1 in queue after P2, P3.

Page 38: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Multilevel Queue Scheduling

• Ready queue is partitioned into several separate queues, eg:– foreground (interactive)– background (batch)

• Process permanently assigned to one queue based on memory size, priority or process type

• Each queue has its own scheduling algorithm:– foreground – RR– background – FCFS

• Scheduling must be done between the queues:– Fixed priority scheduling; (i.e., serve all from foreground

then from background). Possibility of starvation.– Time slice – each queue gets a certain amount of CPU time

which it can schedule amongst its processes; i.e., 80% toCS8493 foreground in RR 20%CPtUoSbchaedcuklingground in FCFS 38

Page 39: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Multilevel Queue Scheduling

CS8493 CPU Scheduling 39

Page 40: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Multilevel Feedback Queue Scheduling• A process can move between the various queues; separate

processes according to their CPU bursts• Process uses too much CPU time, will be moved to a lower-

priority queue.• Aging can be implemented to prevent starvation• 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 to

higher priority– method used to determine when to demote a process to lower

priority– method used to determine which queue a process will enter

CS8493 when that process needs service 40

Page 41: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Example of Multilevel Feedback Queue

• Three queues:– Q0 – RR with time quantum 8

milliseconds– Q1 – RR time quantum 16 milliseconds– Q2 – FCFS

• Scheduling– 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, job is moved to queue Q1

– At Q1 job is again served FCFS and receives 16 additional milliseconds• If it still does not complete, it is

preempted and moved to queue Q2CS8493 41

Page 42: CPU SchedulingPreemptive Scheduling CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state - I/O request 2. Switches from running to ready

Thank You

CS8493 CPU Scheduling 42