Professional Documents
Culture Documents
CPU Scheduling
Edited by
Ghada Mohammed S AlOsaimi
CS Lecturer, CCIS, IMSIU
Outline
Basic Concepts
Overview of CPU Works
CPU I/O Burst Cycle
CPU Scheduler
Preemptive Scheduling vs. non preemptive scheduling
Dispatcher
Scheduling Criteria
Process
Scheduling Algorithms Management
Multilevel Queue
Multilevel Feedback Queue
Storage
Management
2
Objectives
Describe various CPU scheduling algorithms
3
Basic Concepts
In a single CPU core, only one process can run at a time; any others must wait until the CPU is free and can be rescheduled
Multiprogramming objective is to have some process running at all times , in order to maximize CPU utilization
Idea of process execution in multiprogramming OS
A process is executed until it must wait, typically for the completion of some IO request
CPU then just sits idle
All this waiting time is wasted ; no useful work is accomplished
With multiprogramming , try to use this time productively (CPU utilization)
Several processes are kept in memory at one time, when one process has to wait, OS takes CPU away from that
process and gives the CPU to another process
This pattern continues i.e. every time one process has to wait, another process can take over use of CPU
Almost all computer resources are scheduled before use, CPU is one of the primary computer resources
CPU scheduling is central to OS design
CPU scheduling is a mechanism to migrate processes to various states from/to various queues
4
Basic Concepts (cont.)
On a CPU multicore system, this concept of keeping CPU busy is extended to all processing cores on the system
A system has k processes ready to run with n ≥ 1 CPUs that can run them
Which process should we assign to which CPU(s)?!
This is the role of CPU Scheduling
5
Basic Concepts (cont.)
CPU – IO Burst Cycle – process execution consists of a cycle of CPU execution state and IO wait state
Processes alternate between these two states
Process execution starts and terminates with a CPU burst
CPU burst followed by I/O burst then followed by another CPU burst then another IO burst and so on
Durations of CPU bursts have been measured extensively, they are varying from process to process
CPU burst distribution showed by histogram below and curve is characterized as exponential:
This distribution can be important in the selection of an appropriate CPU
Many short CPU bursts (IO bound program)
Few long CPU bursts (CPU bound program )
6
CPU Scheduler – 1st Scheduling Component
Whenever the CPU becomes idle, OS must select one of the processes in the ready queue to be executed
The selection process is carried out by the short-term scheduler (or CPU scheduler)
Scheduler selects a process from the processes in memory that are ready to execute and allocates CPU to that process
Note that the ready queue is not necessarily a first in, first out (FIFO) queue
Therefore, queue may be ordered in various ways
Ready queue can be implemented as a FIFO queue, a priority queue, a tree, or simply an unordered linked list
depend on various scheduling algorithms
All the processes in the ready queue are lined up waiting for a chance to run on CPU
The records in the queues are generally process control blocks (PCBs) of the processes
7
CPU Scheduler (cont.)
Under non-preemptive scheduling , once CPU has been allocated to a process, the process keeps at CPU until it releases CPU
either by terminating or by switching to the waiting state
Unfortunately, preemptive scheduling has several issues:
Consider access to shared data
Example: Case of two processes that share data, while one is updating the data, it is preempted so that the
second process can run. The second process then tries to read the data, but they are in an inconsistent state.
In such situations, we need new mechanisms to coordinate access to shared data as studied in chapter 6.
Consider preemption while in kernel mode
Preemption also affects the design of OS kernel. During processing of a system call, the kernel may be busy
with an activity in a process such as changing IO queues. What happens if the process is preempted in the
middle of these changes and the kernel (or the device driver) needs to read or modify IO queue ?!
Consider interrupts occurring during crucial OS activities
Because interrupts can, by definition, occur at any time, and because they cannot always be ignored by the
kernel
9
Dispatcher – 2nd Scheduling Component
Dispatcher – it is a 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
The dispatcher should be as fast as possible, since it is invoked during every process switch
Dispatch latency – a time it takes for the dispatcher to stop one process and start another running
10
CPU Scheduling Criteria
Many criteria have been suggested for comparing CPU scheduling algorithms. The criteria include the following:
CPU utilization – keep the CPU as busy as possible
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 – amount of time it takes from when a request was submitted until the first response is
produced, not output (for time-sharing environment)
Scheduling algorithm optimization criteria are:
Max CPU utilization
Max throughput
Min turnaround time
Min waiting time
Min response time
11
CPU Scheduling Algorithms
CPU scheduling deals with the problem of deciding which of the processes in the ready queue is to be allocated the CPU
There are many different CPU scheduling algorithms
First Come, First Served (FCFS) scheduling algorithm
Shortest Job First (SJF) scheduling + Shortest Remaining Job First (SRJF)
12
First- Come, First-Served (FCFS) Scheduling
P1 P2 P3
0 24 27 30
13
FCFS Scheduling (cont.) Process Burst Time
P1 24
P2 3
Suppose that the processes arrive in the order: P2 , P3 , P1 P3 3
P2 P3 P1
0 3 6 30
14
Example 1
PNO AT BT CT TAT WT
(process no) (arrival time) (burst time) (completion time) (turnaround time) (wait time)
P1 0 4
P2 1 3
P3 2 1
P4 3 2
P5 4 5
15
Example 1 – Solution
PNO AT BT CT TAT WT
(process no) (arrival time) (burst time) (completion time) (turnaround time) (wait time)
P1 0 4 4 4 0
P2 1 3 7 6 3
P3 2 1 8 6 5
P4 3 2 10 7 5
P5 4 5 15 11 6
16
Example 2
PNO AT BT CT TAT WT
(process no) (arrival time) (burst time) (completion time) (turnaround time) (wait time)
P1 0 2
P2 3 1
P3 5 6
17
Example 2 – Solution
PNO AT BT CT TAT WT
(process no) (arrival time) (burst time) (completion time) (turnaround time) (wait time)
P1 0 2 2 2 0
P2 3 1 4 1 0
P3 5 6 11 6 0
18
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
If the next CPU bursts of two processes are the same, FCFS scheduling is used to break the tie
SJF is optimal – gives minimum average waiting time for a given set of processes
The difficulty is knowing the length of the next CPU request
Could ask the user
Suppose that the processes arrive in the order: P1 , P2 , P3
Process Burst Time
P1 6
P2 8
P3 7 P4 P1 P3 P2
P4 3 0 3 9 16 24
PNO AT BT CT TAT WT
(process no) (arrival time) (burst time) (completion time) (turnaround time) (wait time)
P1 1 7
P2 2 5
P3 3 1
P4 4 2
P5 5 8
20
Example – SJF with Non-preemptive Mode
PNO AT BT CT TAT WT
(process no) (arrival time) (burst time) (completion time) (turnaround time) (wait time)
P1 1 7 8 7 0
P2 2 5 16 14 9
P3 3 1 9 6 5
P4 4 2 11 7 5
P5 5 8 24 19 11
21
Determining Length of Next CPU Burst
Remember: Although SJF algorithm is optimal, it has difficulty to know the length of the next CPU burst
We may not know the length of the next CPU burst, but we may be able to predict its value
Two technique for prediction of Burst Time (BT)
Static technique – Compute or estimate approximation of BT depend on process size and type
Expect that the next CPU burst will be similar in length to the previous ones
Then pick process with shortest predicted next CPU burst
Dynamic technique – Compute or estimate BT using exponential averaging of previous BTs (CPU bursts)
Depend on formula:
τ n =1 = α t n + (1 − α )τ n .
22
Determining Length of Next CPU Burst (cont.)
Dynamic Technique:
1. t n = actual length of n th CPU burst
2. τ n +1 = predicted value for the next CPU burst
3. α , 0 ≤ α ≤ 1 , commonly, α set to ½
4. Define : τ n =1 = α t n + (1 − α )τ n .
23
Examples of Exponential Averaging
α =0
τn+1 = τn
Recent history does not count
α =1
τn+1 = α tn
Only the actual last CPU burst counts
If we expand the formula, we get:
τn+1 = α tn+(1 - α)α tn -1 + …
+(1 - α )j α tn -j + …
+(1 - α )n +1 τ0
Since both α and (1 - α) are less than or equal to 1, each successive term has
less weight than its predecessor
24
Example of Shortest-remaining-time-first
P1 P2 P4 P1 P3
0 1 5 10 17 26
PNO AT BT CT TAT WT
(process no) (arrival time) (burst time) (completion time) (turnaround time) (wait time)
P1 1 7
P2 2 5
P3 3 1
P4 4 2
P5 5 8
26
Example – SJF with Preemptive Mode
PNO AT BT CT TAT WT
(process no) (arrival time) (burst time) (completion time) (turnaround time) (wait time)
P1 1 7 6 16 15 8
P2 2 5 4 10 8 3
P3 3 1 4 1 0
P4 4 2 1 6 2 0
P5 5 8 24 19 11
27
Round Robin (RR) Scheduling
Each process gets a small unit of CPU time (time quantum q), usually 10-100 milliseconds. After this time has
elapsed, the process is preempted and added to the end 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
Timer interrupts every quantum to schedule next process
Performance
q large → FIFO
q small → q must be large with respect to context switch, otherwise overhead is too high
28
Example of RR with Time Quantum = 4
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Typically, higher average turnaround than SJF, but better response time
q should be large compared to context switch time
q usually 10ms to 100ms, context switch < 10 usec
29
Example
PNO AT BT CT TAT WT
(process no) (arrival time) (burst time) (completion time) (turnaround time) (wait time)
P1 0 4
P2 1 5
P3 2 2
P4 3 1
P5 4 6
P6 6 3
PNO AT BT CT TAT WT
(process no) (arrival time) (burst time) (completion time) (turnaround time) (wait time)
P1 0 4 2 0 8 8 4
P2 1 5 3 1 0 18 17 12
P3 2 2 0 6 4 2
P4 3 1 0 9 6 5
P5 4 6 4 2 0 21 17 11
P6 6 3 1 19 13 10
31
RR Performance
Time Quantum and Context Switch Time Turnaround Time Varies With The Time Quantum
32
Priority Scheduling
34
Example – Priority with Non-preemptive Mode
36
Example – Priority with Preemptive Mode
38
Multilevel Queue
39
Multilevel Feedback Queue
A process can move between the various queues; aging can be implemented this way
Multilevel-feedback-queue scheduler defined by the following parameters:
Number of queues
Method used to determine which queue a process will enter when that process needs service
40
Example of Multilevel Feedback Queue
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
41