0% found this document useful (0 votes)
36 views33 pages

Unit IV OSY Handout Revised 07.08.2023

Uploaded by

dhanashree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views33 pages

Unit IV OSY Handout Revised 07.08.2023

Uploaded by

dhanashree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Unit IV – CPU Scheduling and Algorithm

COURSE INFORMATION SHEET


Program- Computer Engineering
Semester- Fifth Scheme-I
Course- Operating System(OSY) Course Code-22516
Name of faculty: Mr.Vijay N. Kukre
Theory- 3 Hrs/ Week Practical:- Hrs/Week- 2
Course Outcomes:-
CO Course Outcome (CO) Statements PO addressed
C22516.d Apply Scheduling Algorithms to calculate turnaround time
1,2,3,4,5,6
and average waiting time.

List of Reference Books


Sr.No Title Author Publications
.
1. Operating System Concepts Silberschatz Galvin John Wiley and Sones
9th Edition - 2015
2. Operating System Godbole Achyut S Tata McGraw Hill
Education 2015
3. Operating System: Internals William Stalling Pearson 8th Edition
and Design Principles 2015
4. Unix Concepts and Sumitabh Das McGraw Hill Education
Programming 2015
5. Operating System Dhananjay Dhamdhere McGraw Hill Education
2015
6. Operating System Dr. Rajendra Kawale Devraj Publications
Mumbai
7. Linux Command Line and Richard Blum Wiley Publishing
Shell Scripting-Bible
List of Web Sites
1. http://staff.um.edu.mt/csta1/courses/lectures/csm202/os8.html
2. https://www.tutorialspoint.com/operating_system/os_process_scheduling_algorith
ms.htm
3. https://www.geeksforgeeks.org/cpu-scheduling-in-operating-systems/
4. https://www.scaler.com/topics/operating-system/deadlock-in-os/
5. https://www.javatpoint.com/os-deadlocks-introduction
6. https://www.geeksforgeeks.org/introduction-of-deadlock-in-operating-system/
7. https://www.tutorialspoint.com/process-deadlocks-in-operating-system
8. https://www.studytonight.com/operating-system/deadlocks
List of PPTs
1 Ppts on all chapters

Mr. V.N.Kukre Page 1 of 33


Unit IV – CPU Scheduling and Algorithm

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

CPU and I/O Burst Cycle:


 Process execution consists of a cycle of CPU execution and I/O wait.
 Processes alternate between these two states.
 Process execution begins with a CPU burst, followed by an I/O burst, then another CPU
burst ... etc
 The last CPU burst will end with a system request to terminate execution rather than
with another I/O burst.
 The duration of these CPU burst have been measured.
 An I/O-bound program would typically have many short CPU bursts, A CPU-bound
program might have a few very long CPU bursts.
 This can help to select an appropriate CPU-scheduling algorithm.

Mr. V.N.Kukre Page 2 of 33


Unit IV – CPU Scheduling and Algorithm

Pre-emptive Scheduling:
 Pre-emptive 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-Pre-emptive Scheduling:
 Non-pre-emptive 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-pre-emptive scheduling does not interrupt a process running CPU in
middle of the execution.
 Instead, it waits till the process complete its CPU burst time and then it can allocate the
CPU to another process.

Basis for
Pre-emptive Scheduling Non Pre-emptive Scheduling
Comparison
Once resources are allocated to a
The resources are allocated to a process, the process holds it till it
Basic
process for a limited time. completes its burst time or switches to
waiting state.
Process can be interrupted in Process cannot be interrupted till it
Interrupt
between. terminates or switches to waiting state.
If a high priority process frequently If a process with long burst time is
Starvation arrives in the ready queue, low running CPU, then another process with
priority process may starve. less CPU burst time may starve.
Pre-emptive scheduling has
Non-pre-emptive scheduling does not
Overhead overheads of scheduling the
have overheads.
processes.
Flexibility Pre-emptive scheduling is flexible. Non-pre-emptive scheduling is rigid.
Pre-emptive scheduling is cost Non-pre-emptive scheduling is not cost
Cost
associated. associative.

Scheduling Criteria
 There are several different criteria to consider when trying to select the "best"
scheduling algorithm for a particular situation and environment, including:
o CPU utilization - Ideally the CPU would be busy 100% of the time, so as to
waste 0 CPU cycles. On a real system CPU usage should range from 40%
( lightly loaded ) to 90% ( heavily loaded. )
o Throughput - Number of processes completed per unit time. May range from
10 / second to 1 / hour depending on the specific processes.

Mr. V.N.Kukre Page 3 of 33


Unit IV – CPU Scheduling and Algorithm

o Turnaround time - Time required for a particular process to complete, from


submission time to completion.
o Waiting time - How much time processes spend in the ready queue waiting
their turn to get on the CPU.
o Response time - The time taken in an interactive program from the issuance of
a command to the commence of a response to that command.
In brief:
 Arrival Time: Time at which the process arrives in the ready queue.
 Completion Time: Time at which 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 turnaround time and burst
time.
Waiting Time = Turn Around Time – Burst Time

4.2 Types of Scheduling Algorithm


(a) First Come First Serve (FCFS)
In FCFS Scheduling
 The process which arrives first in the ready queue is firstly assigned the CPU.
 In case of a tie, process with smaller process id is executed first.
 It is always non-pre-emptive in nature.
 Jobs are executed on first come, first serve basis.
 It is a non-pre-emptive, pre-emptive scheduling algorithm.
 Easy to understand and implement.
 Its implementation is based on FIFO queue.
 Poor in performance as average wait time is high.
Advantages-
 It is simple and easy to understand.
 It can be easily implemented using queue data structure.
 It does not lead to starvation.
Disadvantages-
 It does not consider the priority or burst time of the processes.
 It suffers from convoy effect i.e. processes with higher burst time arrived before
the processes with smaller burst time.

Mr. V.N.Kukre Page 4 of 33


Unit IV – CPU Scheduling and Algorithm

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,
PROCESS ARRIVAL TIME BURST TIME
P1 0 24
P2 0 3
P3 0 3
Gantt chart

P1 P2 P3
0 24 27 30

Mr. V.N.Kukre Page 5 of 33


Unit IV – CPU Scheduling and Algorithm

PROCESS WAIT TIME TURN AROUND TIME


P1 0 24
P2 24 27
P3 27 30

Total Wait Time = 0 + 24 + 27 = 51 ms

Average Waiting Time = (Total Wait Time) / (Total number of processes) = 51/3 = 17 ms

Total Turn Around Time: 24 + 27 + 30 = 81 ms

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
Example 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.
PROCESS ARRIVAL TIME BURST TIME
P1 0 8
P2 1 4
P3 2 9
P4 3 5

Gantt chart
P1 P2 P3 P4
0 8 12 21 26

PROCESS WAIT TIME TURN AROUND 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

Total Wait Time:= 0 + 7 + 10 + 18 = 35 ms

Average Waiting Time = (Total Wait Time) / (Total number of processes)= 35/4 = 8.75 ms

Total Turn Around Time: 8 + 11 + 19 + 23 = 61 ms

Average Turn Around time = (Total Turn Around Time) / (Total number of processes)
61/4 = 15.25 ms

Throughput: 4 jobs/26 sec = 0.15385 jobs/sec

Mr. V.N.Kukre Page 6 of 33


Unit IV – CPU Scheduling and Algorithm

(b) Shortest Job First (SJF)


 Process which have the shortest burst time are scheduled first.
 If two processes have the same bust time, then FCFS is used to break the tie.
 This is a non-pre-emptive, pre-emptive scheduling algorithm.
 Best approach to minimize waiting time.
 Easy to implement in Batch systems where required CPU time is known in advance.
 Impossible to implement in interactive systems where required CPU time is not
known.
 The processer should know in advance how much time process will take.
 Pre-emptive mode of Shortest Job First is called as Shortest Remaining Time
First (SRTF).
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 cannot 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.
 Priorities cannot be set for the processes.
 Processes with larger burst time have poor response time.
Example-01:
Consider the set of 5 processes whose arrival time and burst time are given below-
Process Id Arrival time Burst time
P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3

Solution-

If the CPU scheduling policy is SJF non-preemptive, calculate the average waiting
time and average turnaround time.
Gantt Chart-

Now, we know-
 Turn Around time = Exit time – Arrival time
 Waiting time = Turn Around time – Burst time

Mr. V.N.Kukre Page 7 of 33


Unit IV – CPU Scheduling and Algorithm

Process Id Exit time Turn Around time Waiting time


P1 7 7–3=4 4–1=3
P2 16 16 – 1 = 15 15 – 4 = 11
P3 9 9–4=5 5–2=3
P4 6 6–0=6 6–6=0
P5 12 12 – 2 = 10 10 – 3 = 7
Now,
 Average Turn Around time = (4 + 15 + 5 + 6 + 10) / 5 = 40 / 5 = 8 unit
 Average waiting time = (3 + 11 + 3 + 0 + 7) / 5 = 24 / 5 = 4.8 unit

Example-02:
Consider the set of 5 processes whose arrival time and burst time are given below-
Process Id Arrival time Burst time
P1 3 1
P2 1 4
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-

Process Id Exit time Turn Around time Waiting time


P1 4 4–3=1 1–1=0
P2 6 6–1=5 5–4=1
P3 8 8–4=4 4–2=2
P4 16 16 – 0 = 16 16 – 6 = 10
P5 11 11 – 2 = 9 9–3=6

Now,

 Average Turn Around time = (1 + 5 + 4 + 16 + 9) / 5 = 35 / 5 = 7 unit


 Average waiting time = (0 + 1 + 2 + 10 + 6) / 5 = 19 / 5 = 3.8 unit

Mr. V.N.Kukre Page 8 of 33


Unit IV – CPU Scheduling and Algorithm

Example-03:

Consider the set of 6 processes whose arrival time and burst time are given below-

Process Id Arrival time Burst time


P1 0 7
P2 1 5
P3 2 3
P4 3 1
P5 4 2
P6 5 1

If the CPU scheduling policy is shortest remaining time first, calculate the average
waiting time and average turnaround time.
Solution-
Gantt Chart-

Now, we know-
 Turn Around time = Exit time – Arrival time
 Waiting time = Turn Around time – Burst time

Process Id Exit time Turn Around time Waiting time


P1 19 19 – 0 = 19 19 – 7 = 12
P2 13 13 – 1 = 12 12 – 5 = 7
P3 6 6–2=4 4–3=1
P4 4 4–3=1 1–1=0
P5 9 9–4=5 5–2=3
P6 7 7–5=2 2–1=1

Now,
 Average Turn Around time = (19 + 12 + 4 + 1 + 5 + 2) / 6 = 43 / 6 = 7.17 unit
 Average waiting time = (12 + 7 + 1 + 0 + 3 + 1) / 6 = 24 / 6 = 4 unit

Mr. V.N.Kukre Page 9 of 33


Unit IV – CPU Scheduling and Algorithm

Example -04:

Consider the set of 3 processes whose arrival time and burst time are given below-

Process Id Arrival time Burst time


P1 0 9
P2 1 4
P3 2 9

If the CPU scheduling policy is SRTF, calculate the average waiting time and average
turn around time.
Solution-
Gantt Chart-

Now, we know-
 Turn Around time = Exit time – Arrival time
 Waiting time = Turn Around time – Burst time

Process Id Exit time Turn Around time Waiting time


P1 13 13 – 0 = 13 13 – 9 = 4
P2 5 5–1=4 4–4=0
P3 22 22- 2 = 20 20 – 9 = 11
Now,
 Average Turn Around time = (13 + 4 + 20) / 3 = 37 / 3 = 12.33 unit
 Average waiting time = (4 + 0 + 11) / 3 = 15 / 3 = 5 unit

Example-05:
Consider the set of 4 processes whose arrival time and burst time are given below-

Process Id Arrival time Burst time


P1 0 20
P2 15 25
P3 30 10
P4 45 15
If the CPU scheduling policy is SRTF, calculate the waiting time of process P2.

Mr. V.N.Kukre Page 10 of 33


Unit IV – CPU Scheduling and Algorithm

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.
 This fixed amount of time is called as time quantum or time slice.
 After the time quantum expires, the running process is preempted and sent to the
ready queue.
 Then, the processor is assigned to the next arrived process.
 It is always pre-emptive in nature.

Advantages-
 It gives the best performance in terms of average response time.
 It is best suited for time sharing system, client server architecture and
interactive system.
Disadvantages-
 It leads to starvation for processes with larger burst time as they have to repeat
the cycle many times.
 Its performance heavily depends on time quantum.
 Priorities cannot be set for the processes.
With decreasing value of time quantum,
 Number of context switch increases
 Response time decreases
 Chances of starvation decreases
Thus, smaller value of time quantum is better in terms of response time.

Mr. V.N.Kukre Page 11 of 33


Unit IV – CPU Scheduling and Algorithm

With increasing value of time quantum,


 Number of context switch decreases
 Response time increases
 Chances of starvation increases

Thus, higher value of time quantum is better in terms of number of context


switch.
 With increasing value of time quantum, Round Robin Scheduling tends to
become FCFS Scheduling.
 When time quantum tends to infinity, Round Robin Scheduling becomes FCFS
Scheduling.
 The performance of Round Robin scheduling heavily depends on the value of
time quantum.
 The value of time quantum should be such that it is neither too big nor too
small.
Example-01:
Consider the set of 5 processes whose arrival time and burst time are given below-

Process Id Arrival time Burst time


P1 0 5
P2 1 3
P3 2 1
P4 3 2
P5 4 3
If the CPU scheduling policy is Round Robin with time quantum = 2 unit, calculate
the average waiting time and average turnaround time.
Solution-
Ready Queue- P5, P1, 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
Process Id Exit time Turn Around time Waiting time
P1 13 13 – 0 = 13 13 – 5 = 8
P2 12 12 – 1 = 11 11 – 3 = 8
P3 5 5–2=3 3–1=2
P4 9 9–3=6 6–2=4
P5 14 14 – 4 = 10 10 – 3 = 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

Mr. V.N.Kukre Page 12 of 33


Unit IV – CPU Scheduling and Algorithm

Problem-02:
Consider the set of 6 processes whose arrival time and burst time are given below-
Process Id Arrival time Burst 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
Process Id Exit time Turn Around time Waiting time
P1 8 8–0=8 8–4=4
P2 18 18 – 1 = 17 17 – 5 = 12
P3 6 6–2=4 4–2=2
P4 9 9–3=6 6–1=5
P5 21 21 – 4 = 17 17 – 6 = 11
P6 19 19 – 6 = 13 13 – 3 = 10
Now,
 Average Turn Around time = (8 + 17 + 4 + 6 + 17 + 13) / 6 = 65 / 6 = 10.84 unit
 Average waiting time = (4 + 12 + 2 + 5 + 11 + 10) / 6 = 44 / 6 = 7.33 unit
Problem-03: Consider the set of 6 processes whose arrival time and burst time are
given below-
Process Id Arrival 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.

Mr. V.N.Kukre Page 13 of 33


Unit IV – CPU Scheduling and Algorithm

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
Process Id Exit time Turn Around time Waiting time
P1 32 32 – 5 = 27 27 – 5 = 22
P2 27 27 – 4 = 23 23 – 6 = 17
P3 33 33 – 3 = 30 30 – 7 = 23
P4 30 30 – 1 = 29 29 – 9 = 20
P5 6 6–2=4 4–2=2
P6 21 21 – 6 = 15 15 – 3 = 12
Now,
 Average Turn Around time = (27 + 23 + 30 + 29 + 4 + 15) / 6 = 128 / 6 = 21.33 unit
 Average waiting time = (22 + 17 + 23 + 20 + 2 + 12) / 6 = 96 / 6 = 16 unit
(d) Priority Scheduling
 Out of all the available processes, CPU is assigned to the process having the
highest priority.
 In case of a tie, it is broken by FCFS Scheduling.
 Priority Scheduling can be used in both preemptive and non-preemptive mode.

 The waiting time for the process having the highest priority will always be zero
in preemptive mode.
 The waiting time for the process having the highest priority may not be zero in
non-preemptive mode.
Priority scheduling in preemptive and non-preemptive mode behaves exactly same
under following conditions-
 The arrival time of all the processes is same
 All the processes become available
Advantages-
 It considers the priority of the processes and allows the important processes to
run first.
 Priority scheduling in pre-emptive mode is best suited for real time operating
system.

Mr. V.N.Kukre Page 14 of 33


Unit IV – CPU Scheduling and Algorithm

Disadvantages-
 Processes with lesser priority may starve 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-
Process Id Arrival time Burst time Priority
P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
If the CPU scheduling policy is priority non-preemptive, calculate the average waiting time
and average turnaround time. (Higher number represents higher priority)
Solution-
Gantt Chart-

Now, we know-
 Turn Around time = Exit time – Arrival time
 Waiting time = Turn Around time – Burst time
Process Id Exit time Turn Around time Waiting time
P1 4 4–0=4 4–4=0
P2 15 15 – 1 = 14 14 – 3 = 11
P3 12 12 – 2 = 10 10 – 1 = 9
P4 9 9–3=6 6–5=1
P5 11 11 – 4 = 7 7–2=5
Now,
 Average Turn Around time = (4 + 14 + 10 + 6 + 7) / 5 = 41 / 5 = 8.2 unit
 Average waiting time = (0 + 11 + 9 + 1 + 5) / 5 = 26 / 5 = 5.2 unit

Problem-02: Consider the set of 5 processes whose arrival time and burst time are
given below-
Process Id Arrival time Burst time Priority
P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
If the CPU scheduling policy is priority pre-emptive, calculate the average waiting time
and average turnaround time. (Higher number represents higher priority).

Mr. V.N.Kukre Page 15 of 33


Unit IV – CPU Scheduling and Algorithm

Solution-
Gantt Chart-

Now, we know-
 Turn Around time = Exit time – Arrival time
 Waiting time = Turn Around time – Burst time
Process Id Exit time Turn Around time Waiting time
P1 15 15 – 0 = 15 15 – 4 = 11
P2 12 12 – 1 = 11 11 – 3 = 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
(d) Multilevel Queue Scheduling
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
Each queue has absolute priority over lower-priority queues. No process in the batch queue,
for example, could run unless the queues for system processes, interactive processes, and
interactive editing processes were all empty. If an interactive editing process entered the ready
queue while a batch process was running, the batch process will be pre-empted.

Mr. V.N.Kukre Page 16 of 33


Unit IV – CPU Scheduling and Algorithm

4.3 Deadlock
 Deadlock is a situation where a set of processes are blocked because each process is
holding a resource and waiting for another resource acquired by some other process.
 For example, in the below diagram, Process 1 is holding Resource 1 and waiting for
resource 2 which is acquired by process 2, and process 2 is waiting for resource 1.

Deadlock can arise if following four necessary conditions hold simultaneously.


1. Mutual Exclusion: One or more than one resource are non-sharable means Only one
process can use at a time.
2. Hold and Wait: A process is holding at least one resource and waiting for another
resources.
3. No Pre-emption: A resource cannot be taken from a process unless the process releases
the resource means the process which once scheduled will be executed till the completion
and no other process can be scheduled by the scheduler meanwhile.
4. Circular Wait: A set of processes are waiting for each other in circular form means All
the processes must be waiting for the resources in a cyclic manner so that the last process
is waiting for the resource which is being held by the first process.
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
 Deadlocks can be prevented by preventing at least one of the four required
conditions:
Mutual Exclusion
 Shared resources such as read-only files do not lead to deadlocks.
 Unfortunately, some resources, such as printers and tape drives, require exclusive
access by a single process.
Hold and Wait
 To prevent this condition processes must be prevented from holding one or more
resources while simultaneously waiting for one or more others.

Mr. V.N.Kukre Page 17 of 33


Unit IV – CPU Scheduling and Algorithm

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.
a. Deadlock Avoidance
 In deadlock avoidance, the operating system checks whether the system is in safe state
or in unsafe state at every step which the operating system performs.
 The process continues until the system is in safe state.
 Once the system moves to unsafe state, the OS has to backtrack one step.
 In simple words, The OS reviews each allocation so that the allocation doesn't cause
the deadlock in the system.
3. Deadlock detection and recovery
 This strategy involves waiting until a deadlock occurs.
 After deadlock occurs, the system state is recovered.
 The main challenge with this approach is detecting the deadlock.
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 is the most widely used method.
Banker's Algorithm
 Banker's algorithm is a deadlock avoidance or detection algorithm and named so
because this algorithm is used in banking systems to determine whether a loan can be
granted or not.
 Whenever a new process is created, it must specify the maximum number of instances
of each resource type that it needs, exactly.
 This number must not be more than the total number of resources in the system. Now,
when a new process requests resources, the system must calculate whether the
allocation of the requested resources will leave the computer system in a safe state. If
so then the process will get allocated the requested resources, otherwise it must wait
until some other process releases the requested resources.
 By following this practice, the banker’s algorithm avoids deadlock and allocate
resources safely.
 The data structures that are used to implement the banker's algorithm are:
a. Available
 It is an array of length m. It represents the number of available resources of each type.
If Available[j] = k, then there are k instances available, of resource type Rj.
b. Max
 It is an n x m matrix which represents the maximum number of instances of each
resource that a process can request. If Max[i][j] = k, then the process Pi can request
atmost k instances of resource type Rj.

Mr. V.N.Kukre Page 18 of 33


Unit IV – CPU Scheduling and Algorithm

c. Allocation
 It is an n x m matrix which represents the number of resources of each type currently
allocated to each process. If Allocation[i][j] = k, then process Pi is currently allocated
k instances of resource type Rj.
d. Need
 It is a two-dimensional array. It is an n x m matrix which indicates the remaining
resource needs of each process. If Need[i][j] = k, then process Pi may need k more
instances of resource type Rj to complete its task.

Need[i][j] = Max[i][j] - Allocation [i][j]

Banker’s algorithm comprises of two algorithms:


1. Safety algorithm
2. Resource request algorithm
1. Safety Algorithm
A safety algorithm is an algorithm used to find whether or not a system is in its safe
state. The algorithm is as follows:
Step 1: Let Work and Finish be vectors of length m and n, respectively.
Initially, Work = Available
Finish[i] =false for i = 0, 1, ... , n - 1.
This means, initially, no process has finished and the number of available resources is
represented by the Available array.
Step 2: Find an index i such that both
Finish[i] ==false
Needi <= Work
If there is no such i present, then proceed to step 4.
It means, we need to find an unfinished process whose needs can be satisfied by the available
resources. If no such process exists, just go to step 4.
Step 3: Perform the following:
Work = Work + Allocation
Finish[i] = true
Go to step 2.
When an unfinished process is found, then the resources are allocated and the process is marked
finished. And then, the loop is repeated to check the same for all other processes.
Step 4: If Finish[i] == true for all i, then the system is in a safe state.
That means if all processes are finished, then the system is in safe state.
2. Resource request algorithm
Now the next algorithm is a resource-request algorithm and it is mainly used to determine
whether requests can be safely granted or not.
Let Requesti be the request vector for the process Pi. If Requesti[j]==k, then process Pi wants
k instance of Resource type Rj. When a request for resources is made by the process Pi, the
following are the actions that will be taken:

Mr. V.N.Kukre Page 19 of 33


Unit IV – CPU Scheduling and Algorithm

Step 1: If Requesti <= Needi, then go to step 2; else raise an error condition, since the process
has exceeded its maximum claim.
Step 2: If Requesti <= Availablei then go to step 3; else Pi must have to wait as resources are
not available.
Step 3: Now we will assume that resources are assigned to process Pi and thus perform the
following steps:
Available= Available-Requesti;
Allocationi=Allocationi +Requesti;
Needi =Needi - Requesti;
If the resulting resource allocation state comes out to be safe, then the transaction is completed
and, process Pi is allocated its resources. But in this case, if the new state is unsafe, then Pi
waits for Requesti, and the old resource-allocation state is restored.
Example:
Consider a system that contains five processes P1, P2, P3, P4, P5 and the three resource types
A, B and C. Following are the resources types: A has 10, B has 5 and the resource type C has
7 instances available.

Process Allocation Max Available


A B C A B C (Total Available-
Total Allocated)
A=10 B=5 C=7

P1 0 1 0 7 5 3 10-7=3 5-2=3 7-5=2

P2 2 0 0 3 2 2

P3 3 0 2 9 0 2

P4 2 1 1 2 2 2

P5 0 0 2 4 3 3

Total 7 2 5
Allocated

Need [i] = Max [i] - Allocation [i]


Need for P1: (7, 5, 3) - (0, 1, 0) = 7, 4, 3
Need for P2: (3, 2, 2) - (2, 0, 0) = 1, 2, 2
Need for P3: (9, 0, 2) - (3, 0, 2) = 6, 0, 0
Need for P4: (2, 2, 2) - (2, 1, 1) = 0, 1, 1
Need for P5: (4, 3, 3) - (0, 0, 2) = 4, 3, 1

Mr. V.N.Kukre Page 20 of 33


Unit IV – CPU Scheduling and Algorithm

Process Allocation\ Max Available Need


A B C A B C A B C A B C

P1 0 1 0 7 5 3 3 3 2 7 4 3

P2 2 0 0 3 2 2 1 2 2

P3 3 0 2 9 0 2 6 0 0

P4 2 1 1 2 2 2 0 1 1

P5 0 0 2 4 3 3 4 3 1

Apply the Banker's Algorithm:


Available Resources of A, B and C are 3, 3, and 2. Now we check if each type of resource
request is available for each process.

Step 1: For Process P1: Need <= Available 7, 4, 3 <= 3, 3, 2 condition is false.
So, we examine another process, P2.
Step 2: For Process P2: Need <= Available 1, 2, 2 <= 3, 3, 2 condition true
New available = available + Allocation
(3, 3, 2) + (2, 0, 0) => 5, 3, 2

Process Allocation\ Max Available Need


A B C A B C A B C A B C

P1 0 1 0 7 5 3 3 3 2 7 4 3

P2 2 0 0 3 2 2 5 3 2 P2-Finish

P3 3 0 2 9 0 2 6 0 0

P4 2 1 1 2 2 2 0 1 1

P5 0 0 2 4 3 3 4 3 1

Similarly, we examine another process P3.


Step 3: For Process P3: P3 Need <= Available 6, 0, 0 < = 5, 3, 2 condition is false.
Similarly, we examine another process, P4.
Step 4: For Process P4: P4 Need <= Available 0, 1, 1 <= 5, 3, 2 condition is true
New Available resource = Available + Allocation
5, 3, 2 + 2, 1, 1 => 7, 4, 3

Mr. V.N.Kukre Page 21 of 33


Unit IV – CPU Scheduling and Algorithm

Process Allocation\ Max Available Need


A B C A B C A B C A B C

P1 0 1 0 7 5 3 3 3 2 7 4 3

P2 2 0 0 3 2 2 5 3 2 P2-Finish

P3 3 0 2 9 0 2 7 4 3 6 0 0

P4 2 1 1 2 2 2 P4-Finish

P5 0 0 2 4 3 3 4 3 1
Similarly, we examine another process P5.
Step 5: For Process P5: P5 Need <= Available 4, 3, 1 <= 7, 4, 3 condition is true
New available resource = Available + Allocation
7, 4, 3 + 0, 0, 2 => 7, 4, 5

Process Allocation\ Max Available Need


A B C A B C A B C A B C

P1 0 1 0 7 5 3 3 3 2 7 4 3

P2 2 0 0 3 2 2 5 3 2 P2-Finish

P3 3 0 2 9 0 2 7 4 3 6 0 0

P4 2 1 1 2 2 2 7 4 5 P4-Finish

P5 0 0 2 4 3 3 P5-Finish
Now, we again examine each type of resource request for processes P1 and P3.
Step 6: For Process P1: P1 Need <= Available 7, 4, 3 <= 7, 4, 5 condition is true
New Available Resource = Available + Allocation
7, 4, 5 + 0, 1, 0 => 7, 5, 5

Process Allocation\ Max Available Need


A B C A B C A B C A B C

P1 0 1 0 7 5 3 3 3 2 P1-Finish

P2 2 0 0 3 2 2 5 3 2 P2-Finish

P3 3 0 2 9 0 2 7 4 3 6 0 0

P4 2 1 1 2 2 2 7 4 5 P4-Finish

P5 0 0 2 4 3 3 7 5 5 P5-Finish

Mr. V.N.Kukre Page 22 of 33


Unit IV – CPU Scheduling and Algorithm

So, we examine Last process P3.


Step 7: For Process P3: P3 Need <= Available 6, 0, 0 <= 7, 5, 5 condition is true
New Available Resource = Available + Allocation
7, 5, 5 + 3, 0, 2 => 10, 5, 7

Process Allocation\ Max Available Need


A B C A B C A B C A B C

P1 0 1 0 7 5 3 3 3 2 P1-Finish

P2 2 0 0 3 2 2 5 3 2 P2-Finish

P3 3 0 2 9 0 2 7 4 3 P3-Finish

P4 2 1 1 2 2 2 7 4 5 P4-Finish

P5 0 0 2 4 3 3 7 5 5 P5-Finish

New Recourse available after the execution of all


processes should be equal to Total Resources 10 5 7
available at the start

Hence, we execute the banker's algorithm to find the safe state and the safe sequence is P2, P4,
P5, P1 and P3.
4.4 Starvation:
Starvation is the problem that occurs when high priority processes keep executing and low
priority processes get blocked for indefinite time. In heavily loaded computer system, a
steady stream of higher-priority processes can prevent a low-priority process from ever
getting the CPU. In starvation resources are continuously utilized by high priority processes.
Problem of starvation can be resolved using Aging. In Aging priority of long waiting
processes is gradually increased.
Some of the common causes of starvation are as follows −
1. If a process is never provided the resources it requires for execution because of faulty
resource allocation decisions, then starvation can occur.
2. A lower priority process may wait forever if higher priority processes constantly
monopolize the processor.
3. Starvation may occur if there are not enough resources to provide to every process as
required.
4. If random selection of processes is used then a process may wait for a long time
because of non-selection.
Some solutions that can be implemented in a system to handle starvation are as follows −
1. An independent manager can be used for allocation of resources. This resource
manager distributes resources fairly and tries to avoid starvation.
2. Random selection of processes for resource allocation or processor allocation should
be avoided as they encourage starvation.

Mr. V.N.Kukre Page 23 of 33


Unit IV – CPU Scheduling and Algorithm

3. The priority scheme of resource allocation should include concepts such as aging,
where the priority of a process is increased the longer it waits. This avoids starvation.
Difference between Starvation and Deadlock
Sr. Deadlock Starvation

Starvation is a situation where the low


Deadlock is a situation where no process got
1 priority process got blocked and the high
blocked and no process proceeds
priority processes proceed.

Starvation is a long waiting but not


2 Deadlock is an infinite waiting.
infinite.

3 Every Deadlock is always a starvation. Every starvation need not be deadlock.

The requested resource is blocked by the The requested resource is continuously be


4
other process. used by the higher priority processes.

Deadlock happens when Mutual exclusion,


It occurs due to the uncontrolled priority
5 hold and wait, No preemption and circular
and resource management.
wait occurs simultaneously.

E-learning material (Video lectures)


1. https://www.youtube.com/watch?v=bWHFY8-rL5I –Scheduling criterion
2. https://www.youtube.com/watch?v=eoGUQ5sl0vw –Scheduling criterion
3. https://www.youtube.com/watch?v=zFnrUVqtiOY –Scheduling Types
4. https://www.youtube.com/watch?v=MZdVAVMgNpA – FCFS Scheduling algorithm
5. https://www.youtube.com/watch?v=VCIVXPoiLpU – SJF Scheduling algorithm
6. https://www.youtube.com/watch?v=hoN7_VMzw_g –SRTN Scheduling algorithm
7. https://www.youtube.com/watch?v=TxjIlNYRZ5M – RR Scheduling Algorithm
8. https://www.youtube.com/watch?v=rsDGfFxSgiY – Priority Scheduling Algorithm
9. https://www.youtube.com/watch?v=hBPYP0ZEvS8 – Multi level Queue Scheduling
10. https://www.youtube.com/watch?v=rWFH6PLOIEI – Deadlock
11. https://www.youtube.com/watch?v=01DiVzZbRjY – Starvation and Aging in OS
12. https://www.youtube.com/watch?v=7gMLNiEz3nw – Bankers’s Algorithm

Questions Bank:
Q.1. List any two/four/eight objective of scheduling. (R-2/4 marks)
Q.2. Describe CPU and I/O burst cycle with suitable diagram. (U-4 Marks)
Q.3. Describe pre-emptive and non pre-emptive scheduling. (U-4 Marks)
Q.4. Differentiate between pre-emptive and non pre-emptive scheduling. (A-4 Marks)
Q.5. Define following (R-2/4 Marks)
a. Throughput
b. Turnaround time
c. Waiting time
d. Response time

Mr. V.N.Kukre Page 24 of 33


Unit IV – CPU Scheduling and Algorithm

Q.6. Describe FCFS Scheduling algorithm with suitable example. (U-4 Marks)
Q.7. State any two advantages and two disadvantages of FCFS Scheduling algorithm. (R-4
Marks)
Q.8. 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. Calculate average turnaround
time and average waiting time using FCFS Scheduling algorithm. (A-4 Marks)

PROCESS ARRIVAL TIME BURST TIME


P1 0 8
P2 1 4
P3 2 9
P4 3 5

Q.9. Describe SJF Scheduling algorithm with suitable example. (U-4 Marks)
Q.10. State any two advantages and two disadvantages of SJF Scheduling algorithm. (R-4
Marks)
Q.11. Consider the set of 5 processes whose arrival time and burst time are given below. If
the CPU scheduling policy is SJF non-preemptive, calculate the average waiting time
and average turnaround time. (A-4 Marks)

Process Id Arrival time Burst time


P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3

Q.12. Consider the set of 5 processes whose arrival time and burst time are given below. If
the CPU scheduling policy is SJF preemptive, calculate the average waiting time and
average turnaround time. (A-4 Marks)

Process Id Arrival time Burst time


P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3

Q.13. Describe Round Robin Scheduling algorithm with suitable example. (U-4 Marks)
Q.14. State any two advantages and two disadvantages of Round Robin Scheduling algorithm.
(R-4 Marks)
Q.15. Consider the set of 5 processes whose arrival time and burst time are given below. If
the CPU scheduling algorithm is Round Robin with time quantum = 2 unit, calculate
the average waiting time and average turnaround time. (A-4 Marks)

Mr. V.N.Kukre Page 25 of 33


Unit IV – CPU Scheduling and Algorithm

Process Id Arrival time Burst time


P1 0 5
P2 1 3
P3 2 1
P4 3 2
P5 4 3
Q.16. Describe Priority Scheduling algorithm with suitable example. (U-4 Marks)
Q.17. State any two advantages and two disadvantages of Priority Scheduling algorithm. (R-
4 Marks)
Q.18. Consider the set of 5 processes whose arrival time and burst time are given below. If
the CPU scheduling policy is priority non-preemptive, calculate the average waiting
time and average turnaround time. (Higher number represents higher priority) (A-4
Marks)

Process Id Arrival time Burst time Priority


P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
Q.19. Consider the set of 5 processes whose arrival time and burst time are given below. If
the CPU scheduling policy is priority preemptive, calculate the average waiting time
and average turnaround time. (Higher number represents higher priority) (A-4 Marks)

Process Id Arrival time Burst time Priority


P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
Q.20. Describe Multilevel Queue Scheduling algorithm with suitable example. (U-4 Marks)
Q.21. Define deadlock with suitable example. (R-2 Marks)
Q.22. Describe deadlock with suitable example. (U-4 Marks)
Q.23. Describe four necessary condition to occur deadlock (U-4 Marks)
Q.24. Differentiate between Deadlock and Starvation. (Any two/four points) (A-2/4 Marks)
Q.25. Describe deadlock prevention. (U-4 Marks)
Q.26. Describe deadlock avoidance. (U-4 Marks)

Mr. V.N.Kukre Page 26 of 33


Unit IV – CPU Scheduling and Algorithm

Questions in MSBTE Question papers


MSBTE Examination Winter 2019
1. Explain any four scheduling criteria. (U/2)
2. Describe I/o burst and CPU burst cycle with neat diagram. (U/4)
3. Explain deadlock? What are necessary conditions for deadlock? (U/4)
4. Explain Round Robin algorithm with suitable example. (U/4)
5. The jobs are scheduled for execution as follows (A/6)

Solve the problem using:


(i) SJF
(ii) FCFS
Also find average waiting time using Gantt chart.
Ans:
(a) Shortest Job First (SJF)
Gant Chart:

Turnaround Time(TAT) = Completion Time (CT) – Arrival Time(AT)


P1TAT = 7 - 0 = 7
P2TAT = 11 – 1 = 10
P3TAT = 35 – 2 = 33
P4TAT = 17 – 3 = 14
P5TAT = 25 – 4 = 21
(𝟕+ 𝟏𝟎+𝟑𝟑+𝟏𝟒+𝟐𝟏) 𝟖𝟓
AverageTAT = = = 17 ms
𝟓 𝟓
Waiting Time (WT) = Turnaround Time(TAT) – Burst Time (BT)
P1WT = 7 - 7 = 0
P2WT = 10 – 4 = 6
P3WT = 33 – 10 = 23
P4WT = 14 – 6 = 8
P5WT = 21 – 8 =13
(𝟎+ 𝟔+𝟐𝟑+𝟖+𝟏𝟑) 𝟓𝟎
AverageWT = = = 10 ms
𝟓 𝟓
(b) First Come First Serve (FCFS)
Gant Chart

Mr. V.N.Kukre Page 27 of 33


Unit IV – CPU Scheduling and Algorithm

Turnaround Time(TAT) = Completion Time (CT) – Arrival Time(AT)


P1TAT = 7 - 0 = 7
P2TAT = 11 – 1 = 10
P3TAT = 21 – 2 = 19
P4TAT = 27 – 3 = 24
P5TAT = 35 – 4 = 31
(𝟕+ 𝟏𝟎+𝟏𝟗+𝟐𝟒+𝟑𝟏) 𝟗𝟏
AverageTAT = = = 18.2 ms
𝟓 𝟓
Waiting Time (WT) = Turnaround Time(TAT) – Burst Time (BT)
P1WT = 7 - 7 = 0
P2WT = 10 – 4= 6
P3WT = 19 – 10 = 9
P4WT = 24 – 6 = 18
P4WT = 31 – 8 = 23
(𝟎+ 𝟔+𝟗+𝟏𝟖+𝟐𝟑) 𝟓𝟔
AverageWT = = = 11.2 ms
𝟓 𝟓
MSBTE Examination Summer 2022
1. State difference between pre-emptive scheduling and non-pre-emptive scheduling.
(A/2)
2. State and describe any two scheduling criteria. (U/4)
3. Describe prevention of deadlock occurrence with respect to hold and wait necessary
condition. (U/4)
4. Write steps required for Banker’s algorithm to avoid deadlock. (U/4)
5. Calculate average waiting time for following data using First Come First Served
(FCFS) and Shortest Job First (SJF) algorithms. (A/6)

Ans:
(a) First Come First Serve (FCFS)
Gant Chart

Turnaround Time(TAT) = Completion Time (CT) – Arrival Time(AT)


P0TAT = 8 - 0 = 8
P1TAT = 12 – 1 = 11
P2TAT = 17 – 2 = 15
P3TAT = 20 – 3 = 17
(𝟖+ 𝟏𝟏+𝟏𝟓+𝟏𝟕) 𝟓𝟏
AverageTAT = = = 12.75 ms
𝟒 𝟒

Mr. V.N.Kukre Page 28 of 33


Unit IV – CPU Scheduling and Algorithm

Waiting Time (WT) = Turnaround Time(TAT) – Burst Time (BT)


P0WT = 8 - 8 = 0
P1WT = 11 – 4= 7
P2WT = 15 – 5 = 10
P3WT = 17 – 3 = 14
(𝟎+ 𝟕+𝟏𝟎+𝟏𝟒) 𝟑𝟏
AverageWT = = = 07.75 ms
𝟒 𝟒
(b) Shortest Job First (SJF)
Gant Chart

Turnaround Time(TAT) = Completion Time (CT) – Arrival Time(AT)


P0TAT = 8 - 0 = 8
P1TAT = 15 – 1 = 14
P2TAT = 20 – 2 = 18
P3TAT = 11 – 3 = 8
(𝟖+ 𝟏𝟒+𝟏𝟖+𝟖) 𝟒𝟖
AverageTAT = = = 12 ms
𝟒 𝟒
Waiting Time (WT) = Turnaround Time(TAT) – Burst Time (BT)
P0WT = 8 - 8 = 0
P1WT = 14 – 4= 10
P2WT = 18 – 5 = 13
P3WT = 8 – 3 = 5
(𝟎+ 𝟏𝟎+𝟏𝟑+𝟓) 𝟐𝟖
AverageWT = = = 07 ms
𝟒 𝟒

MSBTE Examination Winter 2022


1. Define CPU and I/O burst cycle. (R/2)
2. Describe different scheduling criteria. (U/4)
3. Describe conditions for deadlock prevention. (U/4)
4. Solve given problem by using SJF and FCFS scheduling algorithm using Gantt
chart. Calculate the average waiting time for each algorithm. (A/4)

Ans: Assume arrival times of Processes as given below

Mr. V.N.Kukre Page 29 of 33


Unit IV – CPU Scheduling and Algorithm

(a) Shortest Job First (SJF)


Gant Chart:

Turnaround Time(TAT) = Completion Time (CT) – Arrival Time(AT)


P1TAT = 9 - 0 = 9
P2TAT = 19 – 1 = 18
P3TAT = 12 – 2 = 10
P4TAT = 26 – 3 = 23
(𝟗+ 𝟏𝟖+𝟏𝟎+𝟐𝟑) 𝟔𝟏
AverageTAT = = = 15.25 ms
𝟒 𝟒
Waiting Time (WT) = Turnaround Time(TAT) – Burst Time (BT)
P1WT = 9 - 9 = 0
P2WT = 18 – 7 = 11
P3WT = 10 – 3 = 07
P4WT = 23 – 7 = 16
(𝟎+ 𝟏𝟏+𝟕+𝟏𝟔) 𝟑𝟒
AverageWT = = = 08.5 ms
𝟒 𝟒
(b) First Come First Serve (FCFS)
Gant Chart

Turnaround Time(TAT) = Completion Time (CT) – Arrival Time(AT)


P1TAT = 9 - 0 = 9
P2TAT = 16 – 1 = 15
P3TAT = 19 – 2 = 17
P4TAT = 26 – 3 = 23
(𝟗+ 𝟏𝟓+𝟏𝟕+𝟐𝟑) 𝟔𝟒
AverageTAT = = = 16 ms
𝟒 𝟒
Waiting Time (WT) = Turnaround Time(TAT) – Burst Time (BT)
P1WT = 9 - 9 = 0
P2WT = 15 – 7 = 8
P3WT = 17 – 3 = 14
P4WT = 23 – 7 = 16
(𝟎+ 𝟖+𝟏𝟒+𝟏𝟔) 𝟑𝟖
AverageWT = = = 09.5 ms
𝟒 𝟒

5. Solve given problem by using (A/6)


(i) Pre-emptive SJF
(ii) Round Robin (Time Slice = 3 ms)
Calculate average waiting time using Gantt Chart.

Mr. V.N.Kukre Page 30 of 33


Unit IV – CPU Scheduling and Algorithm

(a) Pre-emptive SJF


Gant Chart:

Turnaround Time(TAT) = Completion Time (CT) – Arrival Time(AT)


P11TAT = 17 - 0 = 17
P12TAT = 05 – 1 = 04
P13TAT = 26 – 2 = 24
P14TAT = 10 – 3 = 7
(𝟏𝟕+ 𝟎𝟒+𝟐𝟒+𝟎𝟕) 𝟓𝟐
AverageTAT = = = 13 ms
𝟒 𝟒
Waiting Time (WT) = Turnaround Time(TAT) – Burst Time (BT)
P11WT = 17 - 8 = 9
P12WT = 4 – 4 = 0
P13WT = 24 – 9 = 15
P14WT = 7 – 5 = 2
(𝟗+ 𝟎+𝟏𝟓+𝟐) 𝟐𝟔
AverageWT = = = 6.5 ms
𝟒 𝟒
(b) Round Robin (Time Slice=3 ms)
Gant Chart

Turnaround Time(TAT) = Completion Time (CT) – Arrival Time(AT)


P11TAT = 23 - 0 = 23
P12TAT = 16 – 1 = 15
P13TAT = 26 – 2 = 24
P14TAT = 21 – 3 = 18
(𝟐𝟑+ 𝟏𝟓+𝟐𝟒+𝟏𝟖) 𝟖𝟎
AverageTAT = = = 20 ms
𝟒 𝟒
Waiting Time (WT) = Turnaround Time(TAT) – Burst Time (BT)
P11WT = 23 - 8 = 15
P12WT = 15 – 4 = 11
P13WT = 24 – 9 = 15
P14WT = 18 – 5 = 13
(𝟏𝟓+ 𝟏𝟏+𝟏𝟓+𝟏𝟑) 𝟓𝟒
AverageWT = = = 13.5 ms
𝟒 𝟒

Mr. V.N.Kukre Page 31 of 33


Unit IV – CPU Scheduling and Algorithm

MSBTE Examination Summer 2023


1. Write the difference between pre-emptive and non-pre-emptive scheduling. (A/2)
2. Define Deadlock. (R/2)
3. State and explain four scheduling criteria. (U/4)
4. Describe any four condition for deadlock. (U/4)
5. With neat diagram explain multilevel queue scheduling. (U/6)
6. Consider the four processes P1, P2, P3 and P4 with length of CPU burst time. Find
out average waiting time average turnaround time for the following algorithms.
(i) FCFS
(ii) RR (Slice-4ms)
(iii) SJF
Process Arrival Time Burst time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Ans:
(a) First Come First Serve (FCFS)
Gant Chart

Turnaround Time(TAT) = Completion Time (CT) – Arrival Time(AT)


P1TAT = 8 - 0 = 8
P2TAT = 12 – 1 = 11
P3TAT = 21 – 2 = 19
P4TAT = 26 – 3 = 23
(𝟖+ 𝟏𝟏+𝟏𝟗+𝟐𝟑) 𝟔𝟏
AverageTAT = = = 15.25 ms
𝟒 𝟒
Waiting Time (WT) = Turnaround Time(TAT) – Burst Time (BT)
P1WT = 8 - 8 = 0
P2WT = 11 – 4 = 7
P3WT = 19 – 9 = 10
P4WT = 23 – 5 = 18
(𝟎+ 𝟕+𝟏𝟎+𝟏𝟖) 𝟑𝟓
AverageWT = = = 08.75 ms
𝟒 𝟒
(b) Round Robin (Time Slice=4 ms)
Gant Chart

Turnaround Time(TAT) = Completion Time (CT) – Arrival Time(AT)


P1TAT = 20 - 0 = 20
P2TAT = 8 – 1 = 7
P3TAT = 26 – 2 = 24
P14TAT = 25 – 3 = 22

Mr. V.N.Kukre Page 32 of 33


Unit IV – CPU Scheduling and Algorithm

(𝟐𝟎+𝟕+𝟐𝟒+𝟐𝟐) 𝟕𝟑
AverageTAT = = = 18.25 ms
𝟒 𝟒
Waiting Time (WT) = Turnaround Time(TAT) – Burst Time (BT)
P1WT = 20 - 8 = 12
P2WT = 7 – 4 = 3
P3WT = 24 – 9 = 15
P4WT = 22 – 5 = 17
(𝟏𝟐+𝟑+𝟏𝟓+𝟏𝟕) 𝟒𝟕
AverageTAT = = = 11.75 ms
𝟒 𝟒
(a) Shortest Job First (SJF)
Gant Chart:

Turnaround Time(TAT) = Completion Time (CT) – Arrival Time(AT)


P1TAT = 8 - 0 = 8
P2TAT = 12 – 1 = 11
P3TAT = 26 – 2 = 24
P4TAT = 17 – 3 = 14
(𝟖+ 𝟏𝟏+𝟐𝟒+𝟏𝟒) 𝟓𝟕
AverageTAT = = = 14.25 ms
𝟒 𝟒
Waiting Time (WT) = Turnaround Time(TAT) – Burst Time (BT)
P1WT = 8 - 8 = 0
P2WT = 11 – 4 = 7
P3WT = 24 – 9 = 15
P4WT = 14 – 5= 9
(𝟎+ 𝟕+𝟏𝟓+𝟗) 𝟑𝟏
AverageTAT = = = 07.75 ms
𝟒 𝟒

Mr. V.N.Kukre Page 33 of 33

You might also like