CPU-Scheduling-and-Algorithm-
CPU-Scheduling-and-Algorithm-
Unit IV – CPU
Scheduling and Algorithm
Section 4.1 Scheduling types
Scheduling Objectives
Be Fair while allocating resources to the processes
Maximize throughput of the system
Maximize number of users receiving acceptable
response times.
Be predictable
Balance resource use
Avoid indefinite postponement
Enforce Priorities
Give preference to processes holding key resources
Give better service to processes that have desirable
behaviour patterns
Preemptive Scheduling:
Preemptive scheduling is used when a
process switches from running state to
ready state or from waiting state to ready
state.
The resources (mainly CPU cycles) are
allocated to the process for the limited
amount of time and then is taken away,
and the process is again placed back in the
ready queue if that process still has CPU
burst time remaining.
That process stays in ready queue till it gets
next chance to execute.
Non-Preemptive Scheduling:
Non-preemptive Scheduling is used when
a process terminates, or a process switches
from running to waiting state.
In this scheduling, once the resources
(CPU cycles) is allocated to a process, the
process holds the CPU till it gets
terminated or it reaches a waiting state.
In case of non-preemptive scheduling
does not interrupt a process running CPU
in middle of the execution.
Instead, it waits till the process complete
DEPT OF COMPUTER, SSMPTC
Operating System Handout
process may
starve.
Preemptive Non-preemptive
Overhe scheduling scheduling does
ad has overheads not have
of scheduling overheads.
the processes.
Flexibil Preemptive Non-preemptive
ity scheduling is scheduling is rigid.
flexible.
Cost Preemptive Non-preemptive
scheduling is scheduling is not
cost cost associative.
associated.
Scheduling Criteria
Example 1:
Example 2:
Consider the processes P1, P2, P3 given in the below table,
arrives for execution in the same order, with Arrival Time 0,
and given Burst Time,
PROCE ARRIVAL BURST
SS TIME TIME
P 0 24
1
DEPT OF COMPUTER, SSMPTC
Operating System Handout
P 0 3
2
P 0 3
3
Gantt chart
P P P
1 2 3
0 24 27 30
Average Turn Around time = (Total Turn Around Time) / (Total number of
processes)
= 81 / 3 = 27 ms
Throughput = 3 jobs/30 sec = 0.1 jobs/sec
Exam
ple 3:
Consider the processes P1, P2, P3, P4 given
in the below table, arrives for execution in
the same order, with given Arrival Time and
Burst Time.
PROC ARRIVA BURST
ESS L TIME TIME
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Gantt chart
P P P P
1 2 3 4
0 8 12 21 26
PROCE WAIT TIME TURN AROUND
DEPT OF COMPUTER, SSMPTC
Operating System Handout
SS TIME
P1 0 8–0=8
P2 8–1=7 12 – 1 = 11
P3 12 – 2 = 10 21 – 2 = 19
P4 21 – 3 = 18 26 – 3 = 23
61 ms
Advantages-
SRTF is optimal and guarantees the
minimum average waiting time.
It provides a standard for other
algorithms since no other algorithm
performs better than it.
Disadvantages-
It can not be implemented practically
since burst time of the processes can not
be known in advance.
It leads to starvation for processes with
larger burst time.
DEPT OF COMPUTER, SSMPTC
Operating System Handout
Now, we know-
Turn Around time = Exit time – Arrival
time
Waiting time = Turn Around time – Burst
time
DEPT OF COMPUTER, SSMPTC
Operating System Handout
P3 4 2
P4 0 6
P5 2 3
If the CPU scheduling policy is SJF pre-emptive,
calculate the average waiting time and average
turnaround time.
Solution-
Gantt Chart-
Now,
Example-03:
Now, we know-
Turn Around time = Exit time – Arrival
time
Waiting time = Turn Around time – Burst
time
Example -04:
Solutio
n-
Gantt
Chart-
Now, we know-
Turn Around time = Exit time – Arrival
time
Waiting time = Turn Around time – Burst
DEPT OF COMPUTER, SSMPTC
Operating System Handout
time
Example-05:
P2 15 25
P3 30 10
P4 45 15
Solution-
Gantt Chart-
Now, we know-
Turn Around time = Exit time – Arrival
time
Waiting time = Turn Around time – Burst
time
Thus,
Turn Around Time of process P2 = 55 – 15
= 40 unit
Waiting time of process P2 = 40 – 25 = 15
unit
(c) Round Robin Scheduling
CPU is assigned to the process on the basis of FCFS for a
fixed amount of time.
DEPT OF COMPUTER, SSMPTC
Operating System Handout
Advantages-
Disadvantages-
P3 2 1
P4 3 2
P5 4 3
Gantt Chart-
Now, we know-
Turn Around time = Exit time – Arrival
time
Waiting time = Turn Around time – Burst
time
Proc Exit Turn Waitin
ess time Around g time
Id time
P1 13 13 – 0 = 13 – 5
13 =8
P2 12 12 – 1 = 11 – 3
11 =8
P3 5 5–2=3 3–1=
2
P4 9 9–3=6 6–2=
DEPT OF COMPUTER, SSMPTC
Operating System Handout
4
P5 14 14 – 4 = 10 – 3
10 =7
Now,
Average Turn Around time = (13 + 11 + 3
+ 6 + 10) / 5 = 43 / 5 = 8.6 unit
Average waiting time = (8 + 8 + 2 + 4 +
7) / 5 = 29 / 5 = 5.8 unit
Problem-02:
Consider the set of 6 processes whose arrival
time and burst time are given below-
Proces Arriva Burst
s Id l time time
P1 0 4
P2 1 5
P3 2 2
P4 3 1
P5 4 6
P6 6 3
If the CPU scheduling policy is Round Robin
with time quantum = 2, calculate the average
waiting time and average turnaround time.
Solution-
Ready Queue- P5, P6, P2, P5, P6, P2, P5,
P4, P1, P3, P2, P1
Gantt chart-
Now, we know-
Turn Around time = Exit time – Arrival
time
Waiting time = Turn Around time – Burst
time
P1 5 5
P2 4 6
P3 3 7
P4 1 9
P5 2 2
P6 6 3
If the CPU scheduling policy is Round
Robin with time quantum = 3, calculate the
average waiting time and average
turnaround time.
Solution-
Ready Queue- P3, P1, P4, P2, P3, P6, P1,
P4, P2, P3, P5, P4
Gantt chart-
Now, we know-
Turn Around time = Exit time – Arrival
time
Waiting time = Turn Around time – Burst
time
Proce Exit Turn Waitin
ss Id time Around g time
time
P1 32 32 – 5 = 27 – 5
DEPT OF COMPUTER, SSMPTC
Operating System Handout
27 = 22
P2 27 27 – 4 = 23 – 6
23 = 17
P3 33 33 – 3 = 30 – 7
30 = 23
P4 30 30 – 1 = 29 – 9
29 = 20
P5 6 6–2=4 4–2=
2
P6 21 21 – 6 = 15 – 3
15 = 12
Now,
for CPU.
There is no idea of response time and
waiting time.
Problem-01:
Consider the set of 5 processes whose arrival
time and burst time are given below-
Proce Arriva Burst Prio
ss Id l time time rity
P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
Soluti
on-
Gantt
Chart
-
Now, we know-
Turn Around time = Exit time – Arrival
time
Waiting time = Turn Around time – Burst
time
Proce Exit Turn Waitin
ss Id time Around g time
time
P1 4 4–0=4 4–4=
0
P2 15 15 – 1 = 14 – 3
14 = 11
P3 12 12 – 2 = 10 – 1
10 =9
P4 9 9–3=6 6–5=
1
P5 11 11 – 4 = 7 7–2=
5
Now,
DEPT OF COMPUTER, SSMPTC
Operating System Handout
Now, we know-
DEPT OF COMPUTER, SSMPTC
Operating System Handout
time
Proce Exit Turn Waitin
ss Id time Around g time
time
P1 15 15 – 0 = 15 – 4
15 = 11
P2 12 12 – 1 = 11 – 3
11 =8
P3 3 3–2=1 1–1=
0
P4 8 8–3=5 5–5=
0
P5 10 10 – 4 = 6 6–2=
4
Now,
Average Turn Around time = (15 + 11 + 1
+ 5 + 6) / 5 = 38 / 5 = 7.6 unit
Average waiting time = (11 + 8 + 0 + 0 +
4) / 5 = 23 / 5 = 4.6 unit
Multilevel Queue Scheduling
(d)
A multi-level queue scheduling algorithm
partitions the ready queue into several separate
queues. The processes are permanently
assigned to one queue, generally based on
some property of the process, such as memory
size, process priority, or process type. Each
queue has its own scheduling algorithm.
Let us consider an example of a multilevel
queue-scheduling algorithm with five queues:
1. System Processes
2. Interactive Processes
3. Interactive Editing Processes
4. Batch Processes
5. Student Processes
4.3Deadlock
Deadlock is a situation where a set of
S Dea Star
r dloc vatio
. k n
Deadlock is a Starvation is a
1 situation where
situation where no
process got blocked the low priority
and no process process got
proceeds blocked and the
high priority
processes
proceed.
2 Deadlock is an infinite Starvation is a long
waiting. waiting but not
infinite.
3 Every Deadlock is Every starvation
always a starvation. need not be
deadlock.
4 The requested The requested
resource is blocked resource is
by the other process. continuously be
used by the higher
priority processes.
DEPT OF COMPUTER, SSMPTC
Operating System Handout
Deadlock Handling
The various strategies for handling deadlock
are-
1.Deadlock Prevention
2.Deadlock Avoidance
3.Deadlock Detection and Recovery
4.Deadlock Ignorance
1. Deadlock Prevention
No Preemption
Preemption of process resource
allocations can prevent this condition of
deadlocks, when it is possible.
Circular Wait
One way to avoid circular wait is to
number all resources, and to require that
processes request resources only in strictly
increasing ( or decreasing ) order.
2. Deadlock Avoidance
4. Deadlock Ignorance
This strategy involves ignoring the
concept of deadlock and assuming as if it
does not exist.
This strategy helps to avoid the extra
overhead of handling deadlock.
Windows and Linux use this strategy and it