Title: CPU Scheduling
1CPU Scheduling
2Chapter 6 CPU Scheduling
- Basic Concepts
- Scheduling Criteria
- Scheduling Algorithms
- Multiple-Processor Scheduling
- Real-Time Scheduling
- Algorithm Evaluation
3Basic Concepts
- Maximum CPU utilization obtained with
multiprogramming - CPUI/O Burst Cycle Process execution consists
of a cycle of CPU execution and I/O wait. - CPU burst distribution
4Ready Queue I/O Device Queues
5Process Scheduling Queues
- Job queue set of all processes in the system.
- Ready queue set of all processes residing in
main memory,ready and waiting to execute. - Device queues set of processes waiting for an
I/O device. - Process migration between the various queues.
6Schedulers
- Long-term scheduler (or job scheduler) selects
which processes should be brought into the ready
queue. - Short-term scheduler (or CPU scheduler) selects
which process should be executed next and
allocates CPU.
7Addition of Medium Term Scheduling
8Scheduler Duration Types
- Short-term scheduler is invoked very frequently
(milliseconds)? (must be fast). - Long-term scheduler is invoked very infrequently
(seconds, minutes) ? (may be slow). - The long-term scheduler controls the degree of
multiprogramming. - Processes can be described as either
- I/O-bound process spends more time doing I/O
than computations, many short CPU bursts. - CPU-bound process spends more time doing
computations few very long CPU bursts.
9Alternating Sequence of CPU and I/O Bursts
10Histogram of CPU-burst Times
11CPU Scheduler
- Selects from among the processes in memory that
are ready to execute, and allocates the CPU to
one of them. - CPU scheduling decisions may take place when a
process - 1. Switches from running to waiting state.
- 2. Switches from running to ready state.
- 3. Switches from waiting to ready.
- 4. Terminates.
- Scheduling under 1 and 4 is nonpreemptive.
- All other scheduling is preemptive.
12Diagram of Process States
13Representation of Process Scheduling
14Scheduler Types
- Nonpreemptive a process retains control of the
CPU until blocked or terminated. - Preemptive the scheduler may preempt a process
before it blocks or terminates, in order to
allocate the CPU to another process. - Preemptive scheduling is necessary on
user-interactive systems
15Dispatcher
- Dispatcher module gives control of the CPU to the
process selected by the short-term scheduler
this involves - switching context
- switching to user mode
- jumping to the proper location in the user
program to restart that program - Dispatch latency time it takes for the
dispatcher to stop one process and start another
running.
16CPU Process Switching
17Scheduling Criteria
- CPU utilization the percentage of time the CPU
is executing a process - Throughput number 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 the time it takes the system to
start responding to user inputs
18Scheduling Criteria
- Balanced Utilization the percentage of time
that all resources memory, I/O, CPU are
utilized - Predictability lack of variability, e.g. 2 sec
regular response better than 10 sec on occasion - Fairness the degree to which all processes are
given equal opportunity - Priority give preferential treatment to
processes with higher priorities - Proportionality response time matches user
expectations, e.g. expect short time to stop job - Optimize minimum, maximum, average or variance
19Scheduling Criteria for Different Systems
- All systems
- Fairness equal opportunity for all processes
- Balance keeping all parts of the system busy
- Policy enforcement seeing that stated policy is
carried out - Batch Systems
- Throughput maximize jobs per hour
- Turnaround time minimize time between
submission - and termination
- Waiting time minimize time in the waiting queue
- CPU utilization keep the CPU busy all the time
- e.g. varies 40 90 utilization
20Scheduling Criteria for Different Systems
- Interactive Systems
- Response time respond to requests quickly
- Proportionality meet users expectations
- Real-time Systems
- Meeting deadlines respond to requests quickly
- Predictability avoid quality degradation
- e.g. in multimedia systems
21Scheduling Algorithms
- First-come, first-served FCFS
- Shortest job first Nonpreemptive SJF
- Shortest remaining time SRTF Preemptive SJF
- Priority scheduling
- Round robin scheduling RR
- Multilevel queue scheduling
- Multilevel feedback queue MRQ
22First-Come, First-Served (FCFS) Scheduling
- Process Burst Time
- P1 24
- P2 3
- P3 3
- Suppose that the processes arrive in the order
P1 , P2 , P3 The Gantt Chart for the schedule
is - Waiting time for P1 0 P2 24 P3 27
- Average waiting time (0 24 27)/3 17
23First-Come, First-Served (FCFS) Scheduling
- Process Burst Time
- P1 24
- P2 3
- P3 3
- Suppose that the processes arrive in the order P2
, P3 , P1 . - Waiting time for P1 6 P2 0 P3 3
- Average waiting time (6 0 3)/3 3
24Problems with FCFS Scheduling
- Big variations in average waiting time.
- Convoy effect as short processes follow a long
process from CPU to I/O and back
CPU
I/O
P1
P2
P3
0
30
25Shortest-Job-First (SJR) Scheduling
- Associate with each process the length of its
next CPU burst. Use these lengths to schedule
the process with the shortest estimated 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 known
as the Shortest-Remaining-Time-First (SRTF). - SJF is optimal gives minimum average waiting
time for a given set of processes. - Depends on good prediction of the burst length
26Example of Nonpreemptive SJF
- Process Arrival Time Burst Time
- P1 0 7
- P2 2 4
- P3 4 1
- P4 5 4
- Average waiting time (0 6 3 7)/4 4
27Example of Preemptive SJF
- Process Arrival Time Burst Time
- P1 0 7
- P2 2 4
- P3 4 1
- P4 5 4
- Average waiting time (9 1 0 2)/4 3
28Prediction of the Length of the Next CPU Burst
29Determining Length of Next CPU Burst
- Can only estimate the length.
- Can be done by using the length of previous CPU
bursts, using exponential averaging.
30Examples of Exponential Averaging
- If ? 0
- ?n1 ?n
- Prediction based on stored values, ignores recent
history. - If ? 1
- ?n1 tn
- Only the actual last CPU burst counts.
- If we expand the formula, we get
- ?n1 ? tn (1 - ?) ? tn-1 (1 - ?
)j ? tn - j (1 - ? )n1 ?0 - Since both ? and (1 - ?) are less than or equal
to 1, each successive term has less weight than
its predecessor.
31Problems in SJF Scheduling
- SJF favours short jobs over long jobs.
- In extreme cases, the constant arrival of short
jobs can lead to starvation of a long job - Nonpreemptive SJF works best when jobs arrive
simultaneously - Short jobs arriving later can be delayed by an
earlier long job - Preemptive SJF depends on correct prediction of
burst times
32Priority Scheduling
- A priority number (integer) is associated with
each process - The CPU is allocated to the process with the
highest priority (say, smallest integer ? highest
priority). - Preemptive
- Nonpreemptive
- SJF is a priority scheduling scheme in which
priority is the predicted next CPU burst time. - Problem ? Starvation, or indefinite blocking
- low priority processes may never execute.
- Solution ? Aging as time progresses increase
the priority of the process.
33Priority Dispatch Queues
34Priority Dispatch Queues
- The kernel maintains a priority dispatch queue
for each priority value in the system. - Dispatch queues are placed in array dispq
- Each element in dispq is a dispq structure
- Each dispq structure consists of a list of
runnable proc structures, each linked via their
p_link member. - The scheduling algorithms select the process with
the highest priority to use the CPU next.
35Round Robin (RR) Scheduling
- Each process gets a small unit of CPU time (time
quantum), usually 10-100 milliseconds. After
this time has elapsed, the process is preempted
and added to the end (tail) of the ready queue. - If there are n processes in the ready queue and
the time quantum is q, then each process gets 1/n
of the CPU time in chunks of at most q time units
at once. No process waits more than (n-1)q time
units. - Performance
- q large ? FIFO
- q small ? q must be large with respect to context
switch, otherwise overhead is too high.
36Example of RR with Time Quantum 4
- Process Burst Time
- P1 24
- P2 3
- P3 3
-
-
- Average waiting time (0 0 17)/3 5.66
- (Average waiting under RR is quite long)
37Example of RR with Time Quantum 20
- Process Burst Time
- P1 53
- P2 17
- P3 68
- P4 24
-
- Typically, higher average turnaround than SJF,
but better response time.
38Time Quantum and Context Switch Time
Smaller time quantum means more time spent in
context switching.
39Turnaround Time Varies With The Time Quantum
40Turnaround Time Varies With The Time Quantum
- Generally, larger time quantum
- gt shorter average turnaround time
- If time quantum too large, get FCFS scheduling
- Rule of thumb
- 80 of CPU bursts should be shorter
- than the time quantum
41Scheduling Algorithms
- First-come, first-served Variable waiting convoy
effect - Shortest job first Shortest waiting for same
start time - Shortest remaining time Shortest waiting, with
good predict - Priority scheduling Enforces policy beware
starvation - Round robin scheduling Best response for time
share - Multilevel queue scheduling
- Multilevel feedback queue