CPU Scheduling in Operating Systems
CPU scheduling is a process used by the operating system to
decide which task or process gets to use the CPU at a particular
time. This is important because a CPU can only handle one task at
a time, but there are usually many tasks that need to be
processed. The following are different purposes of a CPU
scheduling time.
● Maximize the CPU utilization
● Minimize the response and waiting time of the process.
What is the Need for a CPU Scheduling
Algorithm?
CPU scheduling is the process of deciding which process will own
the CPU to use while another process is suspended. The main
function of CPU scheduling is to ensure that whenever the CPU
remains idle, the OS has at least selected one of the processes
available in the ready-to-use line.
In Multiprogramming, if the long-term scheduler selects multiple
I/O binding processes then most of the time, the CPU remains
idle. The function of an effective program is to improve resource
utilization.
Terminologies Used in CPU Scheduling
● Arrival Time: The time at which the process arrives in the
ready queue.
● Completion Time: The time at which the process
completes its execution.
● Burst Time: Time required by a process for CPU execution.
● Turn Around Time: Time Difference between completion
time and arrival time.
○
Turn Around Time = Completion Time – Arrival Time
○
● Waiting Time(W.T): Time Difference between turn around
time and burst time.
○
Waiting Time = Turn Around Time – Burst Time
○
Things to Take Care While Designing a CPU
Scheduling Algorithm
Different CPU Scheduling algorithms have different structures
and the choice of a particular algorithm depends on a variety of
factors.
● CPU Utilization: The main purpose of any CPU algorithm is
to keep the CPU as busy as possible. Theoretically, CPU
usage can range from 0 to 100 but in a real-time system, it
varies from 40 to 90 percent depending on the system
load.
● Throughput: The average CPU performance is the number
of processes performed and completed during each unit.
This is called throughput. The output may vary depending
on the length or duration of the processes.
● Turn Round Time: For a particular process, the important
conditions are how long it takes to perform that process.
The time elapsed from the time of process delivery to the
time of completion is known as the conversion time.
Conversion time is the amount of time spent waiting for
memory access, waiting in line, using CPU, and waiting
for I / O.
● Waiting Time: The Scheduling algorithm does not affect
the time required to complete the process once it has
started performing. It only affects the waiting time of the
process i.e. the time spent in the waiting process in the
ready queue.
● Response Time: In a collaborative system, turn around
time is not the best option. The process may produce
something early and continue to computing the new
results while the previous results are released to the user.
Therefore another method is the time taken in the
submission of the application process until the first
response is issued. This measure is called response time.
Different Types of CPU Scheduling Algorithms
There are mainly two types of scheduling methods:
● Preemptive Scheduling: Preemptive scheduling is used
when a process switches from running state to ready
state or from the waiting state to the ready state.
● Non-Preemptive Scheduling: Non-Preemptive scheduling
is used when a process terminates , or when a process
switches from running state to waiting state.
CPU Scheduling
Please refer Preemptive vs Non-Preemptive Scheduling for
details.
CPU Scheduling Algorithms
Let us now learn about these CPU scheduling algorithms in
operating systems one by one:
● FCFS – First Come, First Serve
● SJF – Shortest Job First
● SRTF – Shortest Remaining Time First
● Round Robin
● Priority Scheduling
● HRRN – Highest Response Ratio Next
● Multiple Queue Scheduling
● Multilevel Feedback Queue Scheduling
Comparison of CPU Scheduling Algorithms
Here is a brief comparison between different CPU scheduling
algorithms:
Avera
ge
waiti
Algorit Alloca Preem Perfor
Comple ng Starvat
hm tion ption mance
xity time ion
(AWT
)
Accor
ding
to the
arrival
Simple
time Slow
and
of the No No perform
FCFS easy to Large.
proces ance
implem
ses,
ent
the
CPU is
allocat
ed.
Based
on the Minimu
More Small
lowest m
complex er No Yes
SJF CPU Average
than than
burst Waiting
FCFS FCFS
time Time
(BT).
Same
as SJF
the
Depe
allocat
nding
ion of
on
the The
some
CPU is preferen
meas
based More ce is
ures
on the complex Yes Yes given to
SRTF e.g.,
lowest than the
arrival
CPU FCFS short
time,
burst jobs
proce
time
ss
(BT).
size,
But it
etc
is
preem
ptive.
Accor
ding
to the Large
order as Each
The
of the comp process
complex
proces ared has
ity
s to SJF Yes No given a
RR depends
arrives and fairly
on Time
with Priorit fixed
Quantu
fixed y time
m size
time sched
quant uling.
um
(TQ)
Accor
ding Well
to the perform
priorit This Small ance but
Priority contain
y. The type is er Yes Yes
Pre-em a
bigger less than
ptive starvatio
priorit complex FCFS
y task n
execut problem
es first
Accor
ding
to the
priorit This
Pree
y with type is Most
mptiv
Priority monit less benefici
e
non-pr oring complex No Yes al with
Small
eempti the than batch
er
ve new Priority systems
than
incomi preempt
FCFS
ng ive
higher
priorit
y jobs
Accor
ding
to the More Good
proces complex perform
s that than the Small ance but
reside priority er contain
MLQ No Yes
s in scheduli than a
the ng FCFS starvatio
bigger algorith n
queue ms problem
priorit
y
Small
Accor It is the
er
ding most
than
to the Comple
all Good
proces x but its
sched No No perform
MFLQ s of a complex
uling ance
bigger ity rate
types
priorit depends
in
y on the
many
queue. TQ size
cases
Questions for Practice
Question: Which of the following is false about SJF?
S1: It causes minimum average waiting time
S2: It can cause starvation
(A) Only S1
(B) Only S2
(C) Both S1 and S2
(D) Neither S1 nor S2
Answer: (D) S1 is true SJF will always give minimum average
waiting time. S2 is true SJF can cause starvation.
Question: Consider the following table of arrival time and
burst time for three processes P0, P1 and P2.
Process Arrival time Burst Time
P0 0 ms 9 ms
P1 1 ms 4 ms
P2 2 ms 9 ms
The pre-emptive shortest job first scheduling algorithm is used.
Scheduling is carried out only at arrival or completion of
processes. What is the average waiting time for the three
processes?
(A) 5.0 ms
(B) 4.33 ms
(C) 6.33
(D) 7.33
Solution: (A)
Process P0 is allocated processor at 0 ms as there is no other
process in the ready queue. P0 is preempted after 1 ms as P1
arrives at 1 ms and burst time for P1 is less than remaining time of
P0. P1 runs for 4ms. P2 arrived at 2 ms but P1 continued as burst
time of P2 is longer than P1. After P1 completes, P0 is scheduled
again as the remaining time for P0 is less than the burst time of
P2. P0 waits for 4 ms, P1 waits for 0 ms and P2 waits for 11 ms. So
average waiting time is (0+4+11)/3 = 5.
Question: Consider the following set of processes, with the
arrival times and the CPU-burst times given in
milliseconds.
Process Arrival time Burst Time
P1 0 ms 5 ms
P2 1 ms 3 ms
P3 2 ms 3 ms
P4 4 ms 1 ms
What is the average turnaround time for these processes with the
preemptive Shortest Remaining Processing Time First algorithm
?
(A) 5.50
(B) 5.75
(C) 6.00
(D) 6.25
Answer: (A)
Solution: The following is Gantt Chart of execution
P1 P2 P4 P3 P1
1 4 5 8 12
Turn Around Time = Completion Time – Arrival Time Avg Turn
Around Time = (12 + 3 + 6+ 1)/4 = 5.50
○