CH 6
CH 6
CH 6
Operating Systems
Chapter 6:
CPU Scheduling
Basic Concepts
at a time; any others must wait until the CPU is free and
CPU Scheduler
Preemptive Scheduling
CPU-scheduling decisions may take place under the
following four circumstances:
1. When a process switches from the running state to
the waiting state (for example, as the result of an I/O
request or an invocation of wait for the termination of
one of the child processes)
2. When a process switches from the running state to
the ready state (for example, when an interrupt
occurs)
3. When a process switches from the waiting state to
the ready state (for example, at completion of I/O)
4. When a process terminates
8
situations 2 and 3.
preemptive.
9
Dispatcher
Another component involved in the CPU-scheduling
function is the dispatcher. The dispatcher is the module
that gives control of the CPU to the process selected by the
short-term scheduler.
The dispatcher should be as fast as possible, since it is
invoked during every process switch. The time it takes for
the dispatcher to stop one process and start another
running is known as the dispatch latency.
10
Scheduling 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
• Waiting time – amount of time a process has been waiting in
the ready queue
• 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)
11
• Max throughput
CPU-scheduling algorithms
1- First-Come, First-Served Scheduling (FCFS)
3- Priority Scheduling
P4 P1 P3 P2
0 3 9 16 24
P1 P2 P4 P1 P3
0 1 5 10 17 26
3- 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
▫ Nonpreemptive
• SJF is priority scheduling where priority is the inverse of
predicted next CPU burst time
• Problem Starvation – low priority processes may never
execute
• Solution Aging – as time progresses increase the priority
of the process
18
P1 P2 P1 P3 P4
0 1 6 16 18 19
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
• Three queues:
▫ Q0 – RR with time quantum 8
milliseconds
▫ Q1 – RR time quantum 16 milliseconds
▫ Q2 – FCFS
• Scheduling
▫ A new job enters queue Q0 which is
served FCFS
When it gains CPU, job receives 8
milliseconds
If it does not finish in 8 milliseconds,
job is moved to queue Q1
▫ At Q1 job is again served FCFS and
receives 16 additional milliseconds
If it still does not complete, it is
preempted and moved to queue Q2
Algorithm Evaluation
• How to select CPU-scheduling algorithm for an OS?
• Determine criteria, then evaluate algorithms
• Deterministic modeling
▫ Type of analytic evaluation
▫ Takes a particular predetermined workload and defines the
performance of each algorithm for that workload
• Consider 5 processes arriving at time 0:
Deterministic Evaluation
For each algorithm, calculate minimum average waiting time
Simple and fast, but requires exact numbers for input, applies
only to those inputs
FCS is 28ms:
RR is 23ms:
Simulations
• Queueing models limited
• Simulations more accurate
▫ Programmed model of computer system
▫ Clock is a variable
▫ Gather statistics indicating algorithm performance
▫ Data to drive simulation gathered via
Random number generator according to probabilities
Distributions defined mathematically or empirically
Trace tapes record sequences of real events in real
systems
Implementation
Even simulations have limited accuracy
Just implement new scheduler and test in real systems
High cost, high risk
Environments vary
Most flexible schedulers can be modified per-site or per-
system
Or APIs to modify priorities
But again environments vary
Any Questions?