You are on page 1of 42

Operating Systems

Certificate Program in Software Development CSE-TC and CSIM, AIT September -- November, 2003

7. CPU Scheduling
(Ch. 5, S&G)
ch 6 in the 6th ed.
Objectives

examine and compare some of the common CPU scheduling algorithms


OSes: 7. CPU Scheduling 1

Contents
1. CPU Scheduling
what it is, burst cycles, criteria

2. Scheduling Algorithms
FCFS, SJF, Priority, RR, multilevel

3. Algorithm Evaluation
deterministic, queueing, simulation

OSes: 7. CPU Scheduling

1. CPU Scheduling
One

of the main aims of an OS is to maximise the utilization of the CPU by switching it between processes
when should the switching occur? which processes should be executed next?

OSes: 7. CPU Scheduling

1.1. What is a Process?


dispatch new ready event completed

Fig. 4.1, p.90

running interrupt

exit

terminated waiting

I/O event or wait

OSes: 7. CPU Scheduling

1.2. Scheduling Opportunities


When

the running process yields the CPU:

the process enters a wait state the process terminates


When

an interrupt occurs:

the current process is still ready process switches from wait state to ready
Preemptive
OSes: 7. CPU Scheduling

vs. non-preemptive scheduling


5

1.3. CPU and I/O Burst Cycle


Fig. 5.1, p.124
: load store add store read from file wait for I/O store increment index write to file wait for I/O load store :
OSes: 7. CPU Scheduling

CPU burst I/O burst CPU burst

I/O burst
CPU burst

CPU Burst Duration (ms)


Fig. 5.2, p.125; VUW CS 305
160 140 120 100 80 60 40 20 0

frequency

16

24

32

40
7

OSes: 7. CPU Scheduling

1.4. Scheduling Criteria


CPU

utilization

keep the CPU as busy as possible


Throughput

no. of processes completed per time unit


Turnaround

time
continued

how long it takes to complete a process


OSes: 7. CPU Scheduling 8

Waiting

time

the total time a process is in the ready queue the measure used in chapter 5
Response

time

time a process takes to start responding

OSes: 7. CPU Scheduling

2. Scheduling Algorithms
2.1. First-Come, First-Served (FCFS)
2.2. Shortest Job First (SJF) 2.3. Priority Scheduling

2.4. Round Robin (RR)


2.5. Multilevel Queue Scheduling

2.6. Multilevel Feedback Queue Scheduling


OSes: 7. CPU Scheduling 10

2.1. FCFS Scheduling


When

the CPU is available, assign it to the process at the start of the ready queue. to implement

Simple

use a FIFO queue


Non-preemptive

OSes: 7. CPU Scheduling

11

Example (v.1)
All

p.129

processes arrive at time 0. Process Burst Time


P1 P2 P3 24 3 3 P1 0 P2 P3

Gantt

Chart: waiting time:

24

27

30

Average
OSes: 7. CPU Scheduling

(0 + 24 + 27)/3 = 17 ms
12

Example (v.2)

Process
P2 P3 P1

Burst Time
3 3 24 P2 0 P3 P1

Gantt

Chart:

30

Average

waiting time:
(6 + 0 + 3)/3 = 3 ms
13

OSes: 7. CPU Scheduling

FCFS Features
May

not give the best average waiting time.

Average

times can vary a lot depending on the order of the processes. effect

Convoy

small processes can get stuck behind a big process


OSes: 7. CPU Scheduling 14

2.2. Shortest Job First Scheduling (SJF)


When

the CPU is available, assign it to the process with the smallest next CPU burst duration
better name is shortest next CPU burst

Can

be preemptive or non-preemptive

OSes: 7. CPU Scheduling

15

Non-preemptive Example

p.131

Process
P1 P2 P3 P4

Burst Time
6 8 7 3
P4 P1 3 9 P3 16 P2 24

Gantt

Chart:
0

Average

waiting time:

(3 + 16 + 9 + 0)/4 = 7 ms FCFS gives 10.25 ms


OSes: 7. CPU Scheduling 16

SJF Features
Provably

optimal

gives the minimum average waiting time


Problem:

it is usually impossible to know the next CPU burst duration for a process
solution: guess (predict)

OSes: 7. CPU Scheduling

17

Predicting the next CPU burst time


Use

the formula:
Tn+1 = w tn + (1- w) Tn

Meaning:

Tn+1 = prediction for the next (n+1th) CPU burst duration tn = known duration of current (nth) CPU burst Tn = previous prediction (based on the sequence of old ti times) w = a weight (0 <= w <= 1); usually w = 1/2
OSes: 7. CPU Scheduling 18

Preemptive SJF
When

a new process arrives, if it has a shorter next CPU burst duration than what is left of the currently executing process then preempt the current process.

OSes: 7. CPU Scheduling

19

Example

p.133

Process
P1 P2 P3 P4

Arrival Time
0 1 2 3

Burst Time
8 4 9 5

Gantt

Chart:
P4 5 10 P1 17 P3 26

P1 P2 0
OSes: 7. CPU Scheduling

continued

20

Average

waiting time:
( (10-1) + (1-1) + (17-2) + (5-3) )/4 = 6.5 ms
start time arrival time

Non-preemptive

SJF gives 7.75 ms

OSes: 7. CPU Scheduling

21

2.3. Priority Scheduling


Associate

a priority with each process and the CPU is allocated to the process with the highest priority.
SJF are special cases.

FCFS,

Low

numbers = high priority.


22

OSes: 7. CPU Scheduling

Example
Process

p.134

Burst Time
10 1 2 1 5 P5
6

Priority
3 1 3 4 2

P1 P2 P3 P4 P5
Gantt 0

Chart:
P2
1

P1
16

P3
18

P4
19

Average
OSes: 7. CPU Scheduling

waiting time: 8.2 ms


23

Features
Internal/external Preemptive How

priorities.

or non-preemptive.

to avoid starvation?

aging

OSes: 7. CPU Scheduling

24

2.4. Round Robin Scheduling (RR)


A

small unit of time (a time quantum, a time slice) is defined


typically 10 - 100 ms

The
The

ready queue is treated as a circular queue.

CPU scheduler goes around the queue giving each process one time quantum
preemptive
25

OSes: 7. CPU Scheduling

Example
Time

p.135

quantum = 4 ms. At time 0. Process Burst Time


P1 P2 P3 Gantt 24 3 3 P1 0 Average
OSes: 7. CPU Scheduling

Chart:
P2 4 7 P3 10 P1 14 P1 18 P1 22 P1 26 P1 30

waiting time: 17/3 = 5.67 ms


26

RR Features
Average
Context

waiting time can be quite long.

switching is an important overhead when the time quantum is small.

OSes: 7. CPU Scheduling

continued

27

Fig. 5.5, p.137 Average

turnaround time of a set of processes does not necessarily improve as the time quantum size increase:
12.5 12 11.5

Average Turnaround Time

11
10.5 10 9.5 1 2 3 4 5 6 7

Process Time P1 6 P2 3 P3 1 P4 7

Time Quantum
OSes: 7. CPU Scheduling 28

2.5. Multilevel Queue Scheduling


Fig. 5.6, p.138 highest priority

system processes
interactive processes interactive editing processes

batch processes
student processes lowest priority
OSes: 7. CPU Scheduling

continued

29

Scheduling

between queues:

fixed priority preemptive scheduling varying time slices between the queues

OSes: 7. CPU Scheduling

30

2.6. Multilevel Feedback Queue Scheduling


Allow

a process to move between queues


priority sinks when quantum exceeded greater discrimination against longer jobs better response for shorter jobs

Fig. 5.7, p.140; VUW CS 305

Q0

Q1 > Q0
Q2 > Q1

FCFS

OSes: 7. CPU Scheduling

31

3. Algorithm Evaluation
3.1. 3.2. 3.3.

Deterministic Modelling
Queueing Models Simulation

OSes: 7. CPU Scheduling

32

Common Issues
Model/simulation

is based on some pattern of use of a real machine


e.g. students use of bazooka: lots of interactive, small jobs, and a few large ones

How

to represent the changes in usage:

changes in problems, programs, users

OSes: 7. CPU Scheduling

33

3.1. Deterministic Modelling


Take

a given workload and calculate the performance of each scheduling algorithm:


FCFS, SJF, and RR (quantum = 10 ms)

OSes: 7. CPU Scheduling

34

Example
At

p.145

time 0. Process
P1 P2 P3 P4 P5

Burst Time
10 29 3 7 12

OSes: 7. CPU Scheduling

continued

35

Gantt Charts and Times


1.

FCFS:
P1 0 P2 P3 P4 P5

10

39

42

49

61

Average

waiting time:
(0 + 10 + 39 + 42 + 49)/5 = 28 ms
continued

OSes: 7. CPU Scheduling

36

2.

Non-preemptive SJF:
P3 0 3 P4 10 P1 20 P5 32 P2 61

Average

waiting time:
(10 + 32 + 0 + 3 + 20)/5 = 13 ms

OSes: 7. CPU Scheduling

continued

37

3.

RR:
P1 0 10 P2 P3 P4 20 23 30 P5 40 P2 P5 P2 50 52 61

Average

waiting time:
(0 + 32 + 20 + 23 + 40)/5 = 23 ms

OSes: 7. CPU Scheduling

38

Results
For

this mix of processes and CPU burst durations:


SJF 13 ms RR 23 ms FCFS 28 ms

OSes: 7. CPU Scheduling

39

Deterministic Modelling Features


Simple

and fast to calculate.


exact numbers.

Requires Limited

generality, but with enough cases it may reveal some trends.

OSes: 7. CPU Scheduling

40

3.2. Queueing Models


Usually

the process mix varies greatly in an OS, but it may still be possible to determine statistical distributions for the CPU and I/O bursts.
mathematics is difficult, and only applies to limited cases.

The

OSes: 7. CPU Scheduling

41

3.3. Simulations
Often

driven by random number generators to model CPU burst durations, process arrival, departure, etc.
tapes

Trace

records of actual events in a real system, which can be used to drive simulations

OSes: 7. CPU Scheduling

42

You might also like