Module 6: CPU Scheduling
Download
Report
Transcript Module 6: CPU Scheduling
Chapter 5: CPU Scheduling
Scheduler
What is the job of a scheduler
Terms
CPU burst
IO burst (???)
Operating System Concepts – 7th Edition, Feb 2, 2005
5.2
Silberschatz, Galvin and Gagne ©2005
Scheduling
Which processes get control of the CPU and for
how long
One of the most important jobs for the OS
Can have a huge impact on system
performance
But why?
To understand let’s examine the behavior of a
typical program…
Programs typically operate in bursts
Some bursts involve CPU intensive
operations
Some involve I/O operations
Operating System Concepts – 7th Edition, Feb 2, 2005
5.3
Silberschatz, Galvin and Gagne ©2005
What criteria?
What makes a good scheduling algorithm?
Average wait time?
Processor utilization?
What’s more important: user happiness or
processor utilization?
Operating System Concepts – 7th Edition, Feb 2, 2005
5.6
Silberschatz, Galvin and Gagne ©2005
Some criteria
CPU utilization – keep the CPU as busy as possible
Throughput – # of processes that complete their execution
per time unit
Turnaround time – amount of time to execute a particular
process: from time of job submission to job completion
Waiting time – amount of time a process has been waiting
in the ready queue
Response time – similar to waiting time, but instead of
measuring the total amount of time spent in the ready
queue, only count the time from a request for data (like a
mouse click) to the time when “begin” to respond.
Operating System Concepts – 7th Edition, Feb 2, 2005
5.7
Silberschatz, Galvin and Gagne ©2005
What control does the OS have?
Determines who is next in line to get access to the CPU
Determines how long they get the CPU
Operating System Concepts – 7th Edition, Feb 2, 2005
5.8
Silberschatz, Galvin and Gagne ©2005
Preemptive or Non-preemptive
If allow a process to run until it is done or it performs I/O
Non-preemptive or cooperative
If put a limit on the amount of time that a process can stay on
the CPU
Preemptive
Operating System Concepts – 7th Edition, Feb 2, 2005
5.9
Silberschatz, Galvin and Gagne ©2005
Dispatcher
Conceptually broken up into two separate
tasks
Scheduling: determine which process
is next
Dispatch: actual process of context
switch
Save state
Move process to ready Q
Bring in next process (set PC)
Re-establish state
Dispatch latency – time it takes for the
dispatcher to stop one process and start
another running
Operating System Concepts – 7th Edition, Feb 2, 2005
5.12
Silberschatz, Galvin and Gagne ©2005
Scheduling Algorithms
There are a number of common scheduling algorithms
First come, first served
Shortest job first
Priority scheduling
Round robin
We will examine strengths and weaknesses of each
Operating System Concepts – 7th Edition, Feb 2, 2005
5.13
Silberschatz, Galvin and Gagne ©2005
First-Come, First-Served (FCFS) Scheduling
Note: no preemption
Process
P1
P2
P3
Burst Time
24
3
3
Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:
P1
P2
0
24
P3
27
30
Waiting time for P1 = 0; P2 = 24; P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 17
Operating System Concepts – 7th Edition, Feb 2, 2005
5.14
Silberschatz, Galvin and Gagne ©2005
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order
P2 , P3 , P1
The Gantt chart for the schedule is:
P2
0
P3
3
P1
6
30
Waiting time for P1 = 6; P2 = 0; P3 = 3
Average waiting time: (6 + 0 + 3)/3 = 3
Much better than previous case
Convoy effect short process behind long process
Operating System Concepts – 7th Edition, Feb 2, 2005
5.15
Silberschatz, Galvin and Gagne ©2005
Round Robin (RR)
Similar to FCFS but with
preemption
After quantum (10 to 100 ms)
go to back of line
Quantum must be large with
respect to context switch,
otherwise overhead is too
high
Operating System Concepts – 7th Edition, Feb 2, 2005
5.16
Silberschatz, Galvin and Gagne ©2005
Shortest-Job-First (SJF) Scheduling
Associate with each process the length of its next CPU burst. Use
these lengths to schedule the process with the shortest time
Two schemes:
nonpreemptive – once CPU given to the process it cannot be
preempted until completes its CPU burst
preemptive – if a new process arrives with CPU burst length
less than remaining time of current executing process,
preempt. This scheme is know as the
Shortest-Remaining-Time-First (SRTF)
SJF is optimal – gives minimum average waiting time for a given
set of processes
SJF Optimal for
Wait Time
Operating System Concepts – 7th Edition, Feb 2, 2005
5.17
Silberschatz, Galvin and Gagne ©2005
Example of Non-Preemptive SJF
Process
Arrival Time
Burst Time
P1
0.0
7
P2
2.0
4
P3
4.0
1
P4
5.0
4
SJF (non-preemptive), P1 goes first because no other process is in
the Q when it arrives
P1
0
3
P3
7
P2
8
P4
12
16
Average waiting time = (0 + 6 + 3 + 7)/4 = 4
Operating System Concepts – 7th Edition, Feb 2, 2005
5.18
Silberschatz, Galvin and Gagne ©2005
Example of Preemptive SJF
Process
Arrival Time
Burst Time
P1
0.0
7
P2
2.0
4
P3
4.0
1
P4
5.0
4
SJF (preemptive)
P1
0
P2
2
P3
4
P2
5
P4
P1
11
7
16
Average waiting time = (9 + 1 + 0 +2)/4 = 3
Operating System Concepts – 7th Edition, Feb 2, 2005
5.19
Silberschatz, Galvin and Gagne ©2005
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
Non-preemptive
SJF is a priority scheduling where priority is the predicted next CPU
burst time
Problem Starvation – low priority processes may never execute
Solution Aging – as time progresses increase the priority of the
process
Operating System Concepts – 7th Edition, Feb 2, 2005
5.21
Silberschatz, Galvin and Gagne ©2005
Example of RR with Time Quantum = 20
Process
P1
P2
P3
P4
The Gantt chart is:
P1
0
P2
20
P3
37
Burst Time
53
17
68
24
P4
57
P1
77
P3
97 117
P4
P1
P3
P3
121 134 154 162
Typically, higher average turnaround (start to finish) than SJF, but
better response (first access to CPU)
Why can’t we say this definitively? Why typically?
Operating System Concepts – 7th Edition, Feb 2, 2005
5.22
Silberschatz, Galvin and Gagne ©2005
Multilevel Queue
Ready queue is partitioned into
separate queues:
foreground (interactive)
background (batch)
Each queue has its own
scheduling algorithm
For example
foreground – RR
background – FCFS
Operating System Concepts – 7th Edition, Feb 2, 2005
5.24
Silberschatz, Galvin and Gagne ©2005
Multilevel Queue
Scheduling must be done between the queues: which Q does the
OS pull-from
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; e.g.
80% to foreground in RR
20% to background in FCFS
Operating System Concepts – 7th Edition, Feb 2, 2005
5.25
Silberschatz, Galvin and Gagne ©2005
Multilevel Feedback Queue
If allow process behavior to dictate Q…
Each Q might have different:
Scheduling algorithms
Priority
Quantum
Operating System Concepts – 7th Edition, Feb 2, 2005
5.26
Silberschatz, Galvin and Gagne ©2005
Multiple-Processor Scheduling
Can have Q’s for each processor or one Q to service all
Where does scheduler run?
Must design for
Symmetric multiprocessing
Asymmetric multiprocessing
only one processor accesses the system data
structures, alleviating the need for data sharing
Load sharing
If each proc has own cache…
Best if a process stays
with a given processor
Operating System Concepts – 7th Edition, Feb 2, 2005
5.29
Silberschatz, Galvin and Gagne ©2005
Operating System Examples
Solaris scheduling
6 classes of apps each with different
scheduling algorithms and priorities
Good response time for interactive
processes
Good throughput for CPU bound
Windows XP scheduling
Variable priority
All three support
•Priority scheduling
•Preemption
•Server threads
Window with focus gets high pri
Give high-I/O procs higher pri’s
CPU bound lower
I/O devices stay busy
CPU bound use CPU cycles in background
Linux scheduling
Two priority ranges: real-time and other
Higher priority tasks get longer quanta
(unlike Solaris and Windows)
Operating System Concepts – 7th Edition, Feb 2, 2005
5.31
Silberschatz, Galvin and Gagne ©2005
Algorithm Evaluation
How select a CPU-scheduling
algorithm?
Best approach is to generate a “typical”
workload and run on simulator with
different schedulers
deterministic modeling
How describe
List of jobs including
–
start-time
–
list of times and durations
»
CPU and I/O bursts
Operating System Concepts – 7th Edition, Feb 2, 2005
5.32
Silberschatz, Galvin and Gagne ©2005
End of Chapter 5