31
03/30/22 CMP320 PUCIT Arif Butt 1

6/8/2015CMP320 PUCIT Arif Butt1. Note: Some slides and/or pictures are adapted from Lecture slides / Books of a. Text Book - OS Concepts by Silberschatz,

Embed Size (px)

Citation preview

04/18/23 CMP320 PUCIT Arif Butt 1

Note: Some slides and/or pictures are adapted from Lecture slides / Books of•a.• Text Book - OS Concepts by Silberschatz, Galvin, and Gagne.• Ref Books

• Operating Systems Design and Implementation by Tanenbaum.• Operating Systems by William Stalling.• Operating Systems by Colin Ritchie.

Today’s Agenda• Review of previous Lecture• What are CPU and IO bursts • CPU Scheduling and Scheduling Criteria• Scheduling Algorithms

• FCFS• SJF & SRTF• Priority Scheduling• RR• VRR• MQ &MFQ

• Evaluation/Comparison of Scheduling Algorithms

04/18/23 3

Scheduling

04/18/23 4

• Deciding which process/thread should occupy a resource (CPU, disk, etc.)

(CPU (horsepower))

Process 1 Process 2 Process 3

I want to ride

it

Whose turn is it?

CPU – I/O Burst CycleProcess execution consists of a cycle of CPU

execution and I/O waitProcesses move back & forth between these

two statesA process execution begins with a CPU burst,

followed by an I/O burst and then another CPU burst and so on

An alternating sequence of CPU & I/O bursts is shown on next slide

04/18/23 5

Alternating Sequence of CPU and I/O Bursts

04/18/23 6

CPU Scheduler When CPU becomes idle the OS must select one of the

processes in the Ready Queue to be executed CPU Scheduler selects a process from the processes

in memory that are ready to execute (from Ready Q), and allocates the CPU to one of them

The OS code that implements the CPU scheduling algorithm is known as CPU scheduler

CPU scheduling decisions may take place when a process:

1. Switches from running to waiting state

2. Terminates

3. Switches from running to ready state. (e.g. when time slice of a process expires or an interrupt occurs)

4. Switches from waiting to ready state. (e.g. on completion of I/O)

5. On arrival of a new process

04/18/23 7

In Pre-emptive scheduling the currently running process may be interrupted and moved to the ready state by OS (forcefully)

In Non-preemptive Scheduling, the running process can only lose the processor voluntarily by terminating or by requesting an I/O. OR, Once CPU given to a process it cannot be preempted until the process completes its CPU burst

Preemptive vs Non Preemptive

04/18/23 8

DispatcherDispatcher is an important component involved in

CPU schedulingThe OS code that takes the CPU away from the

current process and hands it over to the newly scheduled process is known as the dispatcher

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

Dispatcher performs following functions:switching contextswitching to user modejumping to the proper location in the user

program to restart that programDispatch latency – time it takes for the dispatcher

to stop one process and start executing another

04/18/23 9

Scheduling CriteriaCPU utilization – keep the CPU as busy as possibleThroughput – # of processes that complete their

execution per time unitWaiting time – amount of time a process has been

waiting in the ready queueFor Non preemptive Algos = S.T – A.TFor Preemptive Algos = F.T – A.T – B.T

Turnaround time – amount of time to execute a particular process. Finish Time – Arrival 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)

04/18/23 10

Optimization Criteria

Max CPU utilization

Max throughput

Min turnaround time

Min waiting time

Min response time

04/18/23 11

Single CPU–Scheduling Algorithms

04/18/23 12

• Batch systems– First Come First Serve (FCFS)– Shortest Job First

• Interactive Systems– Round Robin– Priority Scheduling– Multi Queue & Multi-level Feedback– Shortest process time– Guaranteed Scheduling– Lottery Scheduling– Fair Sharing Scheduling

First Come First ServeSimplest CPU scheduling algorithmNon preemptiveThe process that requests the CPU first is allocated the

CPU firstImplemented with a FIFO queue. When a process enters

the Ready Queue, its PCB is linked on to the tail of the Queue. When the CPU is free it is allocated to the process at the head of the Queue

LimitationsFCFS favor long processes as compared to short

ones. (Convoy effect)Average waiting time is often quite longFCFS is non-preemptive, so it is trouble some for time

sharing systems

04/18/23 13

Convoy Effect

04/18/23 14

“A convoy effect happens when a set of processes need to use a resource for a short time, and one process holds the

resource for a long time, blocking all of the other processes. Causes poor

utilization of the other resources in the system”

FCFS – ExampleThe process that enters the ready queue first is

scheduled first, regardless of the size of its next CPU burst

Example: Process Burst Time P1 24 P2 3 P3 3

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

FCFS – ExampleProcesses are served in the order: P1, P2, P3

The Gantt Chart for the schedule is:

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

P1 P2 P3

24 27 300

FCFS – ExampleSuppose that processes arrive in the order: P2 , P3 , P1

.The Gantt chart for the schedule is:

Waiting time for P1 = 6; P2 = 0; P3 = 3Average waiting time: (6 + 0 + 3)/3 = 3

P1P3P2

63 300

FCFS – Example

Process Duration/B.T Order Arrival Time

P1 24 1 0

P2 3 2 3

P3 4 3 4

04/18/23 18

The final schedule:

0

P1 (24)

24 27

P2 (3) P3 (4)

P1 waiting time: 0-0P2 waiting time: 24-3P3 waiting time: 27-4

The average waiting time: (0+21+23)/3 = 14.667

FCFS – Example

Draw the graph (Gantt chart) and compute average

waiting time for the following processes using FCFS

Scheduling algorithm.

Process Arrival time Burst Time

P1 1 16

P2 5 3

P3 6 4

P4 9 2

04/18/23 19

SJF & SRTF Scheduling…When the CPU is available it is assigned to the process

that has the smallest next CPU burstIf two processes have the same length next CPU bursts,

FCFS scheduling is used to break the tieComes in two flavorsShortest Job First (SJF)

It’s a non preemptive algorithm. Shortest Remaining Time First (SRTF)

It’s a Preemptive algorithm.

04/18/23 20

SJF Example

Process Duration/B.T Order Arrival Time

P1 6 1 0

P2 8 2 0

P3 7 3 0

P4 3 4 0

04/18/23 21

0 3

P4 (3) P1 (6)

9

P3 (7)

16P4 waiting time: 0-0P1 waiting time: 3-0P3 waiting time: 9-0P2 waiting time: 16-0

The total running time is: 24The average waiting time (AWT): (0+3+9+16)/4 = 7 time units

P2 (8)

24

SRTF Example

Process Duration Order Arrival Time

P1 10 1 0

P2 2 2 2

04/18/23 22

P1 waiting time: 4-2 =2P2 waiting time: 0

The average waiting time (AWT): (0+2)/2 = 1

Now run this using SJF!

0 122

P1 (8)P2 (2)

4

P1 (2)

SJF & SRTF – ExampleDraw the graph (Gantt chart) and compute waiting time and turn around time for the following processes using SJF & SRTF Scheduling algorithm. For SJF consider no arrival time and consider all processes arrive at time 0 in sequence P1, P2, P3, P4.

Process Arrival Time Burst TimeP1 0 8P2 1 4P3 2 9P4 3 5

04/18/23 23

SJF & SRTF – ExampleDraw the graph (Gantt chart) and compute waiting time and turn around time for the following processes using SJF & SRTF Scheduling algorithm.

Process Arrival Time Burst TimeP1 0 5P2 1 2P3 2 3P4 3 1

04/18/23 24

SJF & SRTF – Example 7Draw the graph (Gantt chart) and compute waiting time and turn around time for the following processes using SJF & SRTF Scheduling algorithm.

Process Arrival Time Burst TimeP1 0 9P2 3 6P3 6 2P4 9 1

04/18/23 25

SJF & SRTF Scheduling$100 QUESTION

How to compute the next CPU burst?

This algorithm cannot be implemented at the level of short-term CPU scheduling. There is no way to know the length of the next CPU burst

One approach is to try to approximate. We may not know the length of the next CPU burst, but we may be able to predict its value. We expect that the next CPU burst will be similar in length to the previous ones. Thus, by computing an approximation of the length of the next CPU burst, we can pick the process with the shortest predicted CPU burst

04/18/23 26

SJF & SRTF Scheduling…Exponential Averaging

04/18/23 27

Estimation based on historical data

tn = Actual length of nth CPU burst

n = Estimate for nth CPU burst

n+1 = Estimate for n+1st CPU burst

, 0 ≤ ≤1

n+1 = tn + (1- ) n

Exponential Averaging…n+1 = tn + (1- ) n

Plugging in value for n, we get

n+1 = tn + (1- )[tn-1 + (1- )n-1]

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

Again plugging in value for n-1, we get

n+1 = tn + (1- )tn-1 + (1- )2[tn-2 + (1- )n-2]

n+1 = tn + (1- )tn-1 + (1- )2 tn-2 (1- )3n-2

Continuing like this results in n+1 = tn+ (1 - ) tn-1 + …+ (1 - )j tn-j + … + (1 -

)n+1 0

04/18/23 28

Exponential Averaging…

Lets take two extreme values of If = 0

n+1 = nNext CPU burst estimate will be exactly equal to previous CPU burst

estimate.

If = 1

n+1 = tnNext CPU burst estimate will be equal to previous actual CPU burst.

04/18/23 29

n+1 = tn + (1- ) n

Exponential Averaging…

Typical value used for is ½. With this value, our (n+1)st estimate is

Note that as we move back in time we are giving less weight-age to the previous CPU bursts. Older histories are given exponentially less weight-age

04/18/23 30

n+1 = tn/2 + tn-1/22 + tn-2/23 + tn-3/24 + …

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

04/18/23 CMP320 PUCIT Arif Butt 31

PROF. SANJEEV KUMAR YADAV (Add. HOD - CSE)