Professional Documents
Culture Documents
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
Contents
1. CPU Scheduling
what it is, burst cycles, criteria
2. Scheduling Algorithms
FCFS, SJF, Priority, RR, multilevel
3. Algorithm Evaluation
deterministic, queueing, simulation
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?
running interrupt
exit
terminated waiting
an interrupt occurs:
the current process is still ready process switches from wait state to ready
Preemptive
OSes: 7. CPU Scheduling
I/O burst
CPU burst
frequency
16
24
32
40
7
utilization
time
continued
Waiting
time
the total time a process is in the ready queue the measure used in chapter 5
Response
time
2. Scheduling Algorithms
2.1. First-Come, First-Served (FCFS)
2.2. Shortest Job First (SJF) 2.3. Priority Scheduling
the CPU is available, assign it to the process at the start of the ready queue. to implement
Simple
11
Example (v.1)
All
p.129
Gantt
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
FCFS Features
May
Average
times can vary a lot depending on the order of the processes. effect
Convoy
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
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:
SJF Features
Provably
optimal
it is usually impossible to know the next CPU burst duration for a process
solution: guess (predict)
17
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.
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
21
a priority with each process and the CPU is allocated to the process with the highest priority.
SJF are special cases.
FCFS,
Low
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
Features
Internal/external Preemptive How
priorities.
or non-preemptive.
to avoid starvation?
aging
24
The
The
CPU scheduler goes around the queue giving each process one time quantum
preemptive
25
Example
Time
p.135
Chart:
P2 4 7 P3 10 P1 14 P1 18 P1 22 P1 26 P1 30
RR Features
Average
Context
continued
27
turnaround time of a set of processes does not necessarily improve as the time quantum size increase:
12.5 12 11.5
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
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
30
Q0
Q1 > Q0
Q2 > Q1
FCFS
31
3. Algorithm Evaluation
3.1. 3.2. 3.3.
Deterministic Modelling
Queueing Models Simulation
32
Common Issues
Model/simulation
How
33
34
Example
At
p.145
time 0. Process
P1 P2 P3 P4 P5
Burst Time
10 29 3 7 12
continued
35
FCFS:
P1 0 P2 P3 P4 P5
10
39
42
49
61
Average
waiting time:
(0 + 10 + 39 + 42 + 49)/5 = 28 ms
continued
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
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
38
Results
For
39
Requires Limited
40
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
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
42