OS - Lecture 04 - CPU Scheduling
OS - Lecture 04 - CPU Scheduling
CPU Scheduling
Topic & Structure of the lesson
CPU scheduling
Preemptive scheduling algorithms
Non-preemptive scheduling algorithms
Calculation using preemptive scheduling
algorithms
Learning Outcomes
At the end of this session you should be
able to:-
explain the importance of CPU scheduling.
distinguish between preemptive and non-
preemptive algorithms.
use algorithms to calculate turnaround time.
CPU Scheduling
A CPU scheduler is tasked with choosing
which process to run first from the ready
queue.
A scheduling algorithm is used to choose the
next process.
Scheduling is a fundamental function of an
operating system.
Scheduling is done to ensure that the CPU is
not idle.
Aims of Scheduling
Fairness
to make sure all processes get a fair share of the
CPU time.
avoid starvation.
Efficiency
maximise CPU utilisation and keep the CPU busy
close to 100% of the time.
Response time
consistent response time.
minimise response time.
Aims of Scheduling
Turnaround time
minimise time between submission and job
completion.
minimise output time.
Throughput
maximise number of job completed within a given
time period.
Preemptive Scheduling
Allows for running processes to be
temporarily suspended.
Processes releases CPU upon receiving a
command.
Preemptive scheduling algorithms:-
round robin
multilevel queue
multilevel feedback queue
Non-preemptive Scheduling
Processes release the CPU only after
completion.
Processes releases CPU voluntarily.
Non-preemptive scheduling algorithms:-
first come first serve (FCFS) or first in first out (FIFO)
shortest job first (SJF)
priority
Quick Review Questions
1. Why is CPU scheduling important?
2. What is the difference between
preemptive and non-preemptive
algorithms?
3. Give 2 examples each for a preemptive
and non-preemptive algorithms.
Calculation Keywords
CPU utilisation
Throughput
Turnaround time
Average turnaround time
Waiting time
Average waiting time
Response time
Burst time
FIFO / FCFS
The simplest CPU scheduling algorithm.
The process that requests the CPU first is allocated
the CPU first.
The implementation of the FIFO policy is easily
managed with a FIFO queue.
Average waiting time for FIFO is quite long.
Once the CPU has been allocated the process, the
process keeps the CPU until termination or by
requesting for I/O.
FIFO / FCFS
Step 1:- Draw Gantt Chart to represent timing for all processes
P1 P2 P3 P4 P5
0 10 11 1314 19
Step 2:- Calculate waiting times and average waiting time
Burst Time
TW(P1) = 0 Process
(Milliseconds)
TW(P2) = 10 P1 10
TW(P3) = 11
P2 1
TW(P4) = 13
P3 2
TW(P5) = 14
P4 1
TW(average) = (0+10+11+13+14)/5
P5 5
= 9.6 Milliseconds
FIFO / FCFS
Step 3:- Calculate turnaround times and average turnaround time
TT = Tw+TB
P1 P2 P3 P4 P5
0 10 11 1314 19
TT(P1) = (0+10) = 10
TT(P2) = (10+1) = 11 Burst Time
Process
TT(P3) = (11+2) = 13 (Milliseconds)
TT(P4) = (13+1) = 14 P1 10
P2 1
TT(P5) = (14+5) = 19
P3 2
Average turnaround time is P4 1
(10+11+13+14+19)/5 = (67/5) P5 5
=13.4 Milliseconds
Example
Process Burst Time Arrival Waiting Turnaround
P1 24 0 0 24
P2 3 0 24 27
P3 4 0 27 31
FIFO Q P1 P2 P3
Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-14
Exercise
Process Burst Time Arrival Waiting Turnaround
P1 48 0 0 48
P2 45 0 48 93
P3 35 3 93 128
P4 22 9 128 150
FIFO Q
Process Q P1 P2 P3 p4
Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-15
Exercise
Process Burst Time Arrival Waiting Turnaround
P1 6 2 11 17
P2 2 5 21 23
P3 8 1 3 11
P4 3 0 0 3
P5 4 4 17 21
FIFO Q
Process Q P4 P3 P1 P5 p2
Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-16
Shortest Job First (SJF)
Scheduling is done by examining the length of the next CPU
burst of a process.
If the CPU is free, the next process with the smallest next
CPU burst is assigned.
If two processes have the same CPU burst, FIFO is used to
break the tie.
The difficulty with SJF is to determine the length of the next
process.
The advantage of this algorithm is that it is optimal by
providing the minimum average waiting time.
Shortest Job First (SJF)
Step 1:- Draw Gantt Chart to represent the timing for all processes
P2 P4 P3 P5 P1
0 1 2 4 9 19
TT(P1) = (9+10) = 19
Burst Time
Process
TT(P2) = (0+1) = 1 (Milliseconds)
TT(P3) = (2+2) = 4
P1 10
TT(P4) = (1+1) = 2 P2 1
TT(P5) = (4+5) = 9 P3 2
Average turnaround time is P4 1
(19+1+4+2+9)/5 = (35/5) P5 5
=7 Milliseconds
Example
Process Burst Time Arrival Waiting Turnaround
P1 3 2 2 5
P2 4 0 5 9
P3 2 4 0 2
P4 4 5 9 13
Process Q
P3 P1 P2 p4
Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-20
Exercise
Process Burst Time Arrival Waiting Turnaround
P1 48 0 102 150
P2 45 0 57 102
P3 35 3 22 57
P4 22 9 0 22
Process Q
P4 P3 P2 p1
Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-21
Exercise
Process Burst Time Arrival Waiting Turnaround
P1 6 2 9 15
P2 2 5 0 2
P3 8 1 15 23
P4 3 0 2 5
P5 4 4 5 9
Process Q
P2 P4 P5 P1 p3
Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-22
Priority
Priority is associated with each process.
The CPU is allocated the job with highest priority.
Equal priority processes are scheduled using FIFO.
Priority can be high or low; however 0 can mean high
priority.
Can be preemptive or non-preemptive.
A problem with priority algorithm is starvation.
Aging is a technique to gradually increase a processes
priority.
Priority
Step 1:- Draw Gantt Chart to represent timing for all processes
P4 P1 P3 P5 P2
0 1 11 13 18 19
TW(average) = (1+18+11+0+13)/5 P3 2 3
P4 1 4
= 8.6 Milliseconds P5 5 2
Priority
Step 3:- Calculate turnaround times and average turnaround time
TT = Tw+TB
P4 P1 P3 P5 P2
0 1 11 13 18 19
TT(P1) = (1+10) = 11
TT(P2) = (18+1) = 19 Burst Time
Process Priority
TT(P3) = (11+2) = 13 (ms)
TT(P4) = (0+1) = 1 P1 10 3
P2 1 1
TT(P5) = (13+5) = 18 P3 2 3
Average turn around time is P4 1 4
(11+19+13+1+18)/5 = (62/5) P5 5 2
=12.4 Milliseconds
Example
Process Burst Time Arrival Waiting Turnaround Priority
P1 3 2 6 9 3
P2 4 0 9 13 1
P3 2 4 0 2 4
P4 4 5 2 6 4
Process Q P3 P4 P1 p2
Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-26
Exercise
Process Burst Time Arrival Waiting Turnaroun Priority
d
P1 48 0 102 150 1
P2 45 0 22 67 3
P3 35 3 67 102 2
P4 22 9 0 22 4
Process Q
P4 P2 P3 P1
Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-27
Exercise
Process Burst Time Arrival Waiting Turnaroun Priority
d
P1 6 2 2 8 4
P2 2 5 0 2 5
P3 8 1 8 16 3
P4 3 0 16 19 1
P5 4 4 19 23 1
Process Q P2 P1 P3 P4 p5
Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-28
Starvation and Aging
Starvation is associated with Priority
scheduling.
Process may begin to run but needs because of
low priority.
Computer heavily loaded with high priority
processes.
Prevent low priority process can never get the
chance to use the CPU.
Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-29
Process Burst Priority
1 10 90
2 11 12
3 7 1
4 12 2
5 8 3
6 6 87
7 3 9
1 6 2 7 5 4 3
0 10 16 27 30 38 50 57
Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-30
Starvation and Aging
Aging is a technique of gradually
increasing the priority of processes that
wait in the system for a long time.
Example:
Priority range: 0(low) -127(high)
Processes that have low priority will be increase
in priority within a certain weight.
1 for every 15 minutes.
Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-31
Starvation and Aging
Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-32
Drawbacks of Non-preemptive
Scheduling
1. It can lead to starvation especially in
real-time application.
2. Bugs in the machine can cause a
machine to freeze.
3. It can make real-time and priority
scheduling difficult.
4. Poor response time for processes
Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-33
Round Robin
P1 P2 P3 P4 P5 P1 P5 P1 P5 P1 P1
0 2 3 5 6 8 10 12 14 15 17 19
Step 2:- Calculate waiting times and average waiting time
Time Slice = 2 Milliseconds
TW(P1) = (0+6+2+1)=9
Burst Time
TW(P2) = 2 Process
(Milliseconds)
TW(P3) = 3 P1 10
TW(P4) = 5 P2 1
TW(P5) = (6+2+2)=10 P3 2
TW(average)=(9+2+3+5+10)/5 P4 1
=5.8 Milliseconds P5 5
Round-robin
Step 3:- Calculate turnaround times and average turnaround time
TT = Tw+TB
P1 P2 P3 P4 P5 P1 P5 P1 P5 P1 P1
0 2 3 5 6 8 10 12 14 15 17 19
Process Q
Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-38
Exercise
Process Burst Time Arrival Waiting Turnaround
P1 8 0
P2 3 0
P3 1 3
P4 6 9
Process Q
Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-39
Exercise
Process Burst Time Arrival Waiting Turnaround
P1 3 2
P2 2 5
P3 1 1
P4 3 0
P5 4 4
Time slice = 2ms
FIFO Q
Process Q
Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-40
Round Robin
Average waiting time is usually long.
Performance depends on the size of the time quantum
(10 – 100 milliseconds).
Process switch requires time, while the time quantum
clock is already running.
Time quantum which is set too short would result in
too many process switches and this reduces CPU
efficiency.
Time quantum which is too long would cause poor
response to short interactive request.
Multilevel Queue (MLQ)
Classifies processes into different groups.
The ready queue is separated into several separate
queues.
Each process is permanently assigned to one queue
based on process priority, size or process type.
Each queue would have its own scheduling algorithm.
Each queue has priority over lower level queues.
Time slices can be allocated to queues.
Multilevel Queue
highest priority
System Processes
Interactive Processes
Batch Processes
Student Processes
lowest priority
Example
Process Arrival Burst Queue Waiting Turnaround
Number Time time
1 0 4 1
2 0 3 1
3 0 8 2
4 10 5 1
Chapter 4:
OS Performance