7. Scheduling

advertisement
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
2
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
3
1.1. What is a Process?
dispatch
new
ready
Fig. 4.1, p.90
running
exit
interrupt
terminated
event
completed
OSes: 7. CPU Scheduling
waiting
I/O event
or wait
4
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
6
CPU Burst Duration (ms)
frequency
Fig. 5.2, p.125;
VUW CS 305
160
140
120
100
80
60
40
20
0
0
OSes: 7. CPU Scheduling
2
4
8
16
24
32
40
7
1.4. Scheduling Criteria
 CPU
utilization
– keep the CPU as busy as possible
 Throughput
– no. of processes completed per time unit
 Turnaround
time
– how long it takes to complete a process
OSes: 7. CPU Scheduling
continued
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
9
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.
 Simple
to implement
– 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
 Gantt
24
3
3
P1
Chart:
0
 Average
P2
24
27
P3
30
waiting time:
(0 + 24 + 27)/3 = 17 ms
OSes: 7. CPU Scheduling
12
Example (v.2)

Process
Burst Time
P2
P3
P1
 Gantt
3
3
24
P2
Chart:
0
 Average
P3
3
P1
6
30
waiting time:
(6 + 0 + 3)/3 = 3 ms
OSes: 7. CPU Scheduling
13
FCFS Features
 May
not give the best average waiting time.
 Average
times can vary a lot depending on
the order of the processes.
 Convoy
effect
– 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
OSes: 7. CPU Scheduling
be preemptive or non-preemptive
15
Non-preemptive Example

Process
Burst Time
P1
P2
P3
P4
 Gantt
6
8
7
3
Chart:
P4
0
 Average
p.131
P1
3
P3
9
P2
16
24
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
Process

p.133
Arrival Time
Burst Time
0
1
2
3
8
4
9
5
P1
P2
P3
P4
 Gantt
Chart:
P1 P2
0
OSes: 7. CPU Scheduling
1
P4
5
P1
10
P3
17
26
continued
20
 Average
waiting time:
( (10-1) + (1-1) + (17-2) + (5-3) )/4
= 6.5 ms
start time
 Non-preemptive
OSes: 7. CPU Scheduling
arrival time
SJF gives 7.75 ms
21
2.3. Priority Scheduling
 Associate
a priority with each process and
the CPU is allocated to the process with the
highest priority.
 FCFS,
 Low
OSes: 7. CPU Scheduling
SJF are special cases.
numbers = high priority.
22
Example
p.134
 Process
Burst Time
P1
P2
P3
P4
P5
 Gantt
10
1
2
1
5
1
 Average
OSes: 7. CPU Scheduling
3
1
3
4
2
Chart:
P2
0
Priority
P5
P1
6
P3
16
P4
18
19
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
ready queue is treated as a circular queue.
 The
CPU scheduler goes around the queue
giving each process one time quantum
– preemptive
OSes: 7. CPU Scheduling
25
Example
p.135
 Time
quantum = 4 ms. At time 0.
 Process
Burst Time
P1
P2
P3
 Gantt
24
3
3
Chart:
P1
0
 Average
OSes: 7. CPU Scheduling
P2
4
P3
7
P1
10
P1
14
P1
18
P1
22
P1
26
30
waiting time: 17/3 = 5.67 ms
26
RR Features
 Average
waiting time can be quite long.
 Context
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:
Average
Turnaround Time
12.5
12
Process Time
P1
6
P2
3
P3
1
P4
7
11.5
11
10.5
10
9.5
1
2
3
4
5
6
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
OSes: 7. CPU Scheduling
Fig. 5.7, p.140;
VUW CS 305
Q0
Q1 > Q0
Q2 > Q1
FCFS
31
3. Algorithm Evaluation
 3.1.
Deterministic Modelling
 3.2.
Queueing Models
 3.3.
Simulation
OSes: 7. CPU Scheduling
32
Common Issues
 Model/simulation
is based on some pattern
of use of a real machine
– e.g. student’s 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
time 0.
 Process
p.145
 At
P1
P2
P3
P4
P5
OSes: 7. CPU Scheduling
Burst Time
10
29
3
7
12
continued
35
Gantt Charts and Times
 1.
FCFS:
P1
0
 Average
P2
10
P3
39
P4
42
P5
49
61
waiting time:
(0 + 10 + 39 + 42 + 49)/5
= 28 ms
OSes: 7. CPU Scheduling
continued
36
 2.
Non-preemptive SJF:
P3
0
P4
3
 Average
P1
10
P5
20
P2
32
61
waiting time:
(10 + 32 + 0 + 3 + 20)/5
= 13 ms
OSes: 7. CPU Scheduling
continued
37
 3.
RR:
P1
0
 Average
P2
10
P3 P4
20 23
P5
30
P2
40
P5 P2
50 52 61
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.
 Requires
exact numbers.
 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.
 The
mathematics is difficult, and only
applies to limited cases.
OSes: 7. CPU Scheduling
41
3.3. Simulations
 Often
driven by random number generators
to model CPU burst durations, process
arrival, departure, etc.
 Trace
tapes
– records of actual events in a real system, which
can be used to drive simulations
OSes: 7. CPU Scheduling
42
Download