0% found this document useful (0 votes)
251 views31 pages

4.CPU Scheduling and Algorithm-Notes

This document discusses scheduling algorithms and their types. It begins by defining scheduling as the process by which a CPU allocates time to different processes. There are two main types of scheduling: preemptive and non-preemptive. It then discusses important scheduling criteria like CPU utilization, throughput, turnaround time, and waiting time. Several common scheduling algorithms are introduced, including First Come First Served (FCFS), Shortest Job First (SJF), Priority Scheduling, and Round Robin. Examples are provided to demonstrate how to calculate average waiting time and turnaround time using FCFS scheduling. Key scheduling concepts like arrival time, burst time, completion time, and Gantt charts are also overviewed.

Uploaded by

Sp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
251 views31 pages

4.CPU Scheduling and Algorithm-Notes

This document discusses scheduling algorithms and their types. It begins by defining scheduling as the process by which a CPU allocates time to different processes. There are two main types of scheduling: preemptive and non-preemptive. It then discusses important scheduling criteria like CPU utilization, throughput, turnaround time, and waiting time. Several common scheduling algorithms are introduced, including First Come First Served (FCFS), Shortest Job First (SJF), Priority Scheduling, and Round Robin. Examples are provided to demonstrate how to calculate average waiting time and turnaround time using FCFS scheduling. Key scheduling concepts like arrival time, burst time, completion time, and Gantt charts are also overviewed.

Uploaded by

Sp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 31

Chapter 04

SCHEDULING AND ALGORITHM


14 Marks

Course Outcome: Student will able to understand the process


Scheduling and algorithm.
(C 22516.4)

Content:
 Scheduling types - scheduling
 Objectives, CPU and I/O burst cycles,
 Pre-emptive, Non- Pre-emptive Scheduling,
 Scheduling criteria.
 Types of Scheduling algorithms – First come first served (FCFS),
 Types of Scheduling algorithms – Shortest Job First (SJF),
 Types of Scheduling algorithms –Shortest Remaining Time(SRTN)
 Types of Scheduling algorithms – Round Robin (RR)
 Types of Scheduling algorithms – Priority scheduling
 Deadlock - System Models,
 Necessary Conditions leading to Deadlocks,
 Deadlock Handling - Preventions, avoidance.

Prof.S.V.Gunjal, Lecturer, Cloud Computing & BD, Polytechnic, Loni


Operating Systems

Scheduling Algorithm

A Scheduling Algorithm is the algorithm which tells us how much CPU time we can allocate to
the processes.

These scheduling algorithms are either preemptive or non-preemptive. Preemptive Scheduling


Algorithms are those which are based on the priority of the processes.

Introduction

 One of the important functions of operating system.

 Operating system tries to utilize the devices maximum.

 In multiprogramming system CPU scheduling is done.

 Various algorithms are used for scheduling.

 Scheduling is required for the CPU as well as I/O devices.

CPU Burst and I/O Burst cycle


Almost all processes alternate between two states in a continuing cycle, as shown in Figure below :

 A CPU burst of performing calculations, and


 An I/O burst, waiting for data transfer in or out of the system.

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


1
Operating Systems

CPU Scheduling Criteria

Different CPU scheduling algorithms have different properties and the choice of a particular
algorithm depends on the various factors. Many criteria have been suggested for comparing CPU
scheduling algorithms.
The criteria include the following:

1. CPU utilisation

The main objective of any CPU scheduling algorithm is to keep the CPU as busy as possible.
Theoretically, CPU utilisation can range from 0 to 100 but in a real-time system, it varies from 40 to 90
percent depending on the load upon the system.

2. Throughput

A measure of the work done by CPU is the number of processes being executed and completed
per unit time. This is called throughput. The throughput may vary depending upon the length or
duration of processes.

3. Turnaround time

For a particular process, an important criteria is how long it takes to execute that process. The
time elapsed from the time of submission of a process to the time of completion is known as the
turnaround time. Turn-around time is the sum of times spent waiting to get into memory, waiting in
ready queue, executing in CPU, and waiting for I/O.

4. Waiting time

A scheduling algorithm does not affect the time required to complete the process once it starts
execution. It only affects the waiting time of a process i.e. time spent by a process waiting in the ready
queue.

5. Response time

In an interactive system, turn-around time is not the best criteria. A process may produce some
output fairly early and continue computing new results while previous results are being output to the
user. Thus another criteria is the time taken from submission of the process of request until the first
response is produced. This measure is called response time.

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


2
Operating Systems

Types of Scheduling

There are two types of scheduling-

1. Preemptive Scheduling-

2. Non Preemptive Scheduling-

1. Preemptive Scheduling:

Preemptive scheduling is used when a process switches from running state to ready state or
from the waiting state to ready state.

The resources (mainly CPU cycles) are allocated to the process for a limited amount of time and
then 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 the ready queue till it gets its next chance to execute.

2. Non-Preemptive Scheduling:

Non-preemptive Scheduling is used when a process terminates, or a process switches from


running to the waiting state.

In this scheduling, once the resources (CPU cycles) are allocated to a process, the process holds
the CPU till it gets terminated or reaches a waiting state. In the case of non-preemptive scheduling does
not interrupt a process running CPU in the middle of the execution. Instead, it waits till the process
completes its CPU burst time, and then it can allocate the CPU to another process.

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


3
Operating Systems

Important Concepts

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 turn around time and burst time.

Waiting Time = Turn Around Time – Burst Time

Gantt Chart-
 It is a bar type chart derived by scientist Henry Gantt in 1910.
 It is used to illustrate the project schedule.

Ex.

Job 1 Job 2 Job 3 Job 4

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


4
Operating Systems

SchedulingAlgorithm

 Scheduling algorithm can be preemptive or non preemptive.


 There are different CPU scheduling algorithms.

1. First Come First Serve (FCFS)


2. Shortest Job First (SJF)
3. Priority Scheduling
4. Round Robin Algorithm

1. First Come First Serve (FCFS)-

 FCFS is a Non Preemptive algorithm.

 It is easy to implement for CPU scheduling.

 The process which request first is allocated to CPU first.

 When Process enters for processing its PCB is connected to ready queue.

 Jobs are executed on first come, first serve basis.

 Easy to understand and implement.

 Its implementation is based on FIFO queue.

 Poor in performance as average wait time is high.

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


5
Operating Systems

Ex.1. Consider the following example. Calculate the average waiting time and turn around time.

JOBs CPU Burst Time


JOB 1 24

JOB 2 3

JOB 3 5

Solution :

JOB 1 JOB 2 JOB 3

0 24 27 32

1. Average waiting time

Waiting time of JOB 1 = 0


Waiting time of JOB 2 = 24
Waiting time of JOB 3 = 27

Average waiting time = (0+24+27)/3


= 51/3
= 17

Average waiting time is = 17

2. Turnaround time

Turnaround time of job 1 = 24


Turnaround time of job 1 = 27
Turnaround time of job 1 = 32

Average turnaround time = (24+27+32)/3


= 83/3
= 27.6

Average turnaround time is = 27.6

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


6
Operating Systems

Ex.2. Consider the following example. Calculate the average waiting time and turn around time.

Process CPU Burst Time

P1 8

P2 4

P3 9

P4 5
Solution

P1 P2 P3 P4

0 8 12 21 26

1. Average waiting time

Waiting time of P1 = 0
Waiting time of P2 = 8
Waiting time of P3 = 12
Waiting time of P4 = 21

Average waiting time = (0+8+12+21)/4


= 41/4
= 10.25

Average waiting time is = 10.25

2. Turnaround time

Turnaround time of P1 = 8
Turnaround time of P2 = 12
Turnaround time of P3 = 21
Turnaround time of P4 = 26

Average turnaround time = (8+12+21+26)/4


= 67/4
= 16.75

Average turnaround time is = 16.75


Ex.3. Consider the following example. Calculate the average waiting time and turn around time.

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


7
Operating Systems

Process Arrival Time Execute Time Service Time


P0 0 5 0
P1 1 3 5
P2 2 8 8
P3 3 6 16

Solution

P0 P1 P2 P3

0 5 8 16 22

1. Average waiting time

Waiting time of P0 = 0-0 = 0


Waiting time of P1 = 5-1 = 4
Waiting time of P2 = 8-2 = 6
Waiting time of P3 = 16-3 = 13

Average waiting time = (0+4+6+13)/4


= 23/4
= 5.75

Average waiting time is = 5.75

2. Turnaround time

Turnaround time of P0 = 5-0 = 5


Turnaround time of P1 = 8-1 = 7
Turnaround time of P2 = 16-2 = 14
Turnaround time of P3 = 22-3 = 19

Average turnaround time = (5+7+14+19)/4


= 45/4
= 11.25

Average turnaround time is = 11.25

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


8
Operating Systems

Ex.4. Consider the following example. Calculate the average waiting time and turn around time.

Process Burst Time Arrival Time


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

Solution

P4 P3 P1 P5 P2

0 3 11 17 21 23

1. Average waiting time

Waiting time of P1 = 11-2 =9


Waiting time of P2 = 21-5 = 16
Waiting time of P3 = 3-1 =2
Waiting time of P4 = 0-0 =0
Waiting time of P5 = 17-4 = 13

Average waiting time = (9+16+2+0+13)/5


= 40/5
= 5

Average waiting time is = 5

2. Turnaround time

Turnaround time of P0 = 5-0 = 5


Turnaround time of P1 = 8-1 = 7
Turnaround time of P2 = 16-2 = 14
Turnaround time of P3 = 22-3 = 19

Average turnaround time = (5+7+14+19)/4


= 45/4
= 11.25

Average turnaround time is = 11.25

Advantages of FCFS
Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni
9
Operating Systems

1. The simplest form of a CPU scheduling algorithm


2. Easy to program
3. First come first served

Disadvantages of FCFS

1. It is a Non-Preemptive CPU scheduling algorithm, so after the process has been allocated to the
CPU, it will never release the CPU until it finishes executing.
2. The Average Waiting Time is high.
3. Short processes that are at the back of the queue have to wait for the long process at the front to
finish.
4. Not an ideal technique for time-sharing systems.
5. Because of its simplicity, FCFS is not very efficient.

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


10
Operating Systems

2. Shortest Job First (SJF)


Shortest Job First (SJF) is an algorithm in which the process having the
smallest execution time is chosen for the next execution. This scheduling method can be preemptive or
non-preemptive. It significantly reduces the average waiting time for other processes awaiting
execution. The full form of SJF is Shortest Job First.

Characteristics of SJF Scheduling

 It is associated with each job as a unit of time to complete.


 This algorithm method is helpful for batch-type processing, where waiting for jobs to complete is
not critical.
 It can improve process throughput by making sure that shorter jobs are executed first, hence
possibly have a short turnaround time.
 It improves job output by offering shorter jobs, which should be executed first, which mostly
have a shorter turnaround time.

It has two types as below.

1. Non Preemptive SJF


2. Preemptive SJF

1. Non-Preemptive SJF: –
In Non-Preemptive Scheduling, if a CPU is located to the process, then the process will hold the
CPU until the process enters into the waiting state or terminated.

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


11
Operating Systems

Ex.12. Consider the following example. Calculate the average waiting time and turn around

Process Burst Time


P1 21
P2 3
P3 6
P4 2

Solution

P4 P2 P3 P1

0 2 5 11 32

Average waiting time

Waiting time of P1 = 11
Waiting time of P2 = 2
Waiting time of P3 = 5
Waiting time of P4 = 0

Average waiting time = (11+2+5+0)/4


= 18/4
= 4.50

Average waiting time is = 4.5

Turnaround time

Turnaround time of P1 = 32
Turnaround time of P2 = 5
Turnaround time of P3 = 11
Turnaround time of P4 = 2

Average turnaround time = (32+5+11+2)/4


= 50/4
= 12.5

Average turnaround time is = 12.5

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


12
Operating Systems

Ex.2. Consider the following example. Calculate the average waiting time and turn around time.

Process Burst Time Arrival Time


P0 8 5
P1 5 0
P2 9 4
P3 2 1

Solution

P1 P3 P2 P0

0 5 7 16 24

Turnaround time = completion time – Arrival Time

Turnaround time of P0 = 24-5 = 19


Turnaround time of P1 = 5-0 = 5
Turnaround time of P2 = 16-4 =12
Turnaround time of P3 = 7-1 = 6

Average turnaround time = (19+5+12+6)/4


= 42/4
= 10.5

Average turnaround time is = 10.5

Average waiting time = Turnaround Time – Burst Time

Waiting time of P0 = 19-8 = 11


Waiting time of P1 = 5-5 = 0
Waiting time of P2 = 12-9 = 3
Waiting time of P3= 6-2 = 4

Average waiting time = (11+0+3+4)/4


= 18/4
= 4.5

Average waiting time is = 4.5

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


13
Operating Systems

2. Preemptive SJF

In non-preemptive scheduling, once the CPU cycle is allocated to process, the process holds it
till it reaches a waiting state or terminated.

Consider the following five processes each having its own unique burst time and arrival time.

Ex.3. Consider the following example. Calculate the average waiting time.

Process Burst Time Arrival Time


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

Solution

P4 P1 P5 P2 P5 P1 P3

0 3 4 5 7 10 15 23

Average waiting time

Waiting time of P1 = 3-2+6 = 7


Waiting time of P2 = 5-5 = 0
Waiting time of P3 = 15-1 = 14
Waiting time of P4 = 0-0 = 0
Waiting time of P5 = 4-4+2 = 2

Average waiting time = (7+0+14+0+2)/5


= 23/4
= 4.6

Average waiting time is = 4.6

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


14
Operating Systems

Ex.4. Consider the following example. Calculate the average waiting time.

Process Arrival Time Burst Time


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

Solution

P1 P2 P3 P2 P4 P1

0 1 2 4 7 10 15

Average waiting time

Waiting time of P1 = 0-0+9 =9


Waiting time of P2 = 1-1+2 =2
Waiting time of P3 = 2-2 =0
Waiting time of P4 = 7-3 =4

Average waiting time = (9+2+0+4)/4


= 15/4
= 3.75

Average waiting time is = 3.75

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


15
Operating Systems

Advantages of SJF

 SJF is frequently used for long term scheduling.

 It reduces the average waiting time over FIFO (First in First Out) algorithm.

 SJF method gives the lowest average waiting time for a specific set of processes.

 It is appropriate for the jobs running in batch, where run times are known in advance.

 For the batch system of long-term scheduling, a burst time estimate can be obtained from the

job description.

 For Short-Term Scheduling, we need to predict the value of the next burst time.

 Probably optimal with regard to average turnaround time.

Disadvantages/Cons of SJF

 Job completion time must be known earlier, but it is hard to predict.

 It is often used in a batch system for long term scheduling.

 SJF can’t be implemented for CPU scheduling for the short term. It is because there is no specific

method to predict the length of the upcoming CPU burst.

 This algorithm may cause very long turnaround times or starvation.

 Requires knowledge of how long a process or job will run. 

 It leads to the starvation that does not reduce average turnaround time. 

 It is hard to know the length of the upcoming CPU request.

 Elapsed time should be recorded, that results in more overhead on the processor.

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


16
Operating Systems

3. Priority Algorithm
 Priority scheduling is a non-preemptive algorithm and one of the most common scheduling
algorithms in batch systems.

 Each process is assigned a priority. Process with highest priority is to be executed first and so on.

 Processes with same priority are executed on first come first served basis.

 Priority can be decided based on memory requirements, time requirements or any other
resource requirement.

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


17
Operating Systems

Ex.18. Consider the following example. Calculate the average waiting

Process Burst Time Priority


P0 5 1
P1 3 2
P2 8 1
P3 6 3

Solution

P3 P1 P0 P2

0 6 9 14 22

Average waiting time

Waiting time of P0 = 9
Waiting time of P1 = 6
Waiting time of P2 = 14
Waiting time of P3 = 0

Average waiting time = (9+6+14+0)/4


= 29/4
= 7.25

Average waiting time is = 7.25

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


18
Operating Systems

Ex.2. Consider the following example. Calculate the average waiting time.

Process Burst Time Priority


P1 4 1
P2 3 2
P3 7 1
P4 4 3
P5 2 2

Solution

P4 P5 P2 P1 P3

0 4 6 9 13 20

Average waiting time

Waiting time of P1 = 9
Waiting time of P2 = 6
Waiting time of P3 = 13
Waiting time of P4 = 0
Waiting time of P5 = 4

Average waiting time = (9+6+13+0+4)/5


= 32/5
= 6.4

Average waiting time is = 6.4

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


19
Operating Systems

Advantages of priority scheduling

1. Easy to use scheduling method

2. Processes are executed on the basis of priority so high priority does not need to wait for long

which saves time

3. This method provides a good mechanism where the relative important of each process may be

precisely defined.

4. Suitable for applications with fluctuating time and resource requirements.

Disadvantages of priority scheduling

1. If the system eventually crashes, all low priority processes get lost.

2. If high priority processes take lots of CPU time, then the lower priority processes may starve and

will be postponed for an indefinite time.

3. This scheduling algorithm may leave some low priority processes waiting indefinitely.

4. A process will be blocked when it is ready to run but has to wait for the CPU because some other

process is running currently.

5. If a new higher priority process keeps on coming in the ready queue, then the process which is in

the waiting state may need to wait for a long duration of time.

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


20
Operating Systems

4. Round-Robin Algorithm

The name of this algorithm comes from the round-robin principle, where each person gets an
equal share of something in turns. It is the oldest, simplest scheduling algorithm, which is mostly used
for multitasking.

In Round-robin scheduling, each ready task runs turn by turn only in a cyclic queue for a limited
time slice. This algorithm also offers starvation free execution of processes.

Characteristics of Round-Robin Scheduling

1. Round robin is a pre-emptive algorithm

2. The CPU is shifted to the next process after fixed interval time, which is called time

quantum/time slice.

3. The process that is preempted is added to the end of the queue.

4. Round robin is a hybrid model which is clock-driven

5. Time slice should be minimum, which is assigned for a specific task that needs to be processed.

However, it may differ OS to OS.

6. It is a real time algorithm which responds to the event within a specific time limit.

7. Round robin is one of the oldest, fairest, and easiest algorithm.

8. Widely used scheduling method in traditional OS.

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


21
Operating Systems

Ex.22. Consider the following example. Calculate the average waiting

Process Burst Time


P1 4
P2 3
P3 5

Solution

P1 P2 P3 P1 P2 P3 P3

0 2 4 6 8 9 11 12

Average waiting time

Waiting time of P1 = 0+4 = 4


Waiting time of P2 = 2+4 = 6
Waiting time of P3 = 4+3 = 7

Average waiting time = (4+6+7)/3


= 17/3
= 5.66

Average waiting time is = 6.66

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


22
Operating Systems

Ex.2. Consider the following example. Calculate the average waiting time.

Process Burst Time Arrival Time


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

Solution

P1 P2 P3 P4 P5 P6 P1 P2 P5

0 4 8 11 12 16 20 21 23 24

Average waiting time

Waiting time of P1 = 0+16-0 = 16


Waiting time of P2 = 4+13-1 = 16
Waiting time of P3 = 8-2 =6
Waiting time of P4 = 11-3 =8
Waiting time of P5 = 12+7-4 = 15
Waiting time of P6 = 16-6 = 10

Average waiting time = (16+16+6+8+15+10)/6


= 71/6
= 11.83

Average waiting time is = 11.83

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


23
Operating Systems

Advantage of Round-robin Scheduling

 It doesn’t face the issues of starvation or convoy effect.

 All the jobs get a fair allocation of CPU.

 It deals with all process without any priority

 If you know the total number of processes on the run queue, then you can also assume the

worst-case response time for the same process.

 This scheduling method does not depend upon burst time. That’s why it is easily implementable

on the system.

 Once a process is executed for a specific set of the period, the process is preempted, and another

process executes for that given time period.

 Allows OS to use the Context switching method to save states of preempted processes.

 It gives the best performance in terms of average response time.

Disadvantages of Round-robin Scheduling

 If slicing time of OS is low, the processor output will be reduced.

 This method spends more time on context switching

 Its performance heavily depends on time quantum.

 Priorities cannot be set for the processes.

 Round-robin scheduling doesn’t give special priority to more important tasks.

 Decreases comprehension

 Lower time quantum results in higher the context switching overhead in the system.

 Finding a correct time quantum is a quite difficult task in this system.

Deadlock

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


24
Operating Systems

Deadlock happens in operating system when two or more processes need some resource to
complete their execution that is held by the other process.

In the above diagram, the process 1 has resource 1 and needs to acquire resource 2. Similarly
process 2 has resource 2 and needs to acquire resource 1. Process 1 and process 2 are in deadlock as
each of them needs the other’s resource to complete their execution but neither of them is willing to
relinquish their resources.

Deadlock condition
1. Mutual Exclusion

2. Hold and Wait

3. No Preemption

4. Circular Wait

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


25
Operating Systems

1. Mutual Exclusion
There should be a resource that can only be held by one process at a time. In the diagram
below, there is a single instance of Resource 1 and it is held by Process 1 only.

2. Hold and Wait


A process can hold multiple resources and still request more resources from other processes
which are holding them. In the diagram given below, Process 2 holds Resource 2 and Resource 3 and is
requesting the Resource 1 which is held by Process 1.

3. No Preemption
A resource cannot be preempted from a process by force. A process can only release a resource
voluntarily. In the diagram below, Process 2 cannot preempt Resource 1 from Process 1. It will only be
released when Process 1 relinquishes it voluntarily after its execution is complete.

4. Circular Wait

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


26
Operating Systems

A process is waiting for the resource held by the second process, which is waiting for the
resource held by the third process and so on, till the last process is waiting for a resource held by the
first process. This forms a circular chain. For example: Process 1 is allocated Resource2 and it is
requesting Resource 1. Similarly, Process 2 is allocated Resource 1 and it is requesting Resource 2. This
forms a circular wait loop.

Advantages of Deadlock
 This situation works well for processes which perform a single burst of activity
 No preemption needed for Deadlock.
 Convenient method when applied to resources whose state can be saved and restored easily
 Feasible to enforce via compile-time checks
 Needs no run-time computation since the problem is solved in system design

Disadvantages of Deadlock method

 Delays process initiation


 Processes must know future resource need
 Pre-empts more often than necessary
 Dis-allows incremental resource requests
 Inherent preemption losses.

Deadlock Prevention

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


27
Operating Systems

It’s important to prevent a deadlock before it can occur. The system checks every transaction
before it is executed to make sure it doesn’t lead the deadlock situations. Such that even a small
change to occur dead that an operation which can lead to Deadlock in the future it also never allowed
process to execute.

It is a set of methods for ensuring that at least one of the conditions cannot hold.

1. No Mutual Exclusion

2. No Hold and Wait

3. Removal of No Preemption

4. Removal of Circular Wait

1. No Mutual Exclusion

It means more than one process can have access to a single resource at the same time. It’s
impossible because if multiple processes access the same resource simultaneously, there will be chaos.
Additionally, no process will be completed. So this is not feasible. Hence, the OS can’t avoid mutual
exclusion.

2. No Hold and Wait

To avoid the hold and wait, there are many ways to acquire all the required resources before
starting the execution. But this is also not feasible because a process will use a single resource at a
time. Here, the resource utilization will be very less.

Before starting the execution, the process does not know how many resources would be
required to complete it. In addition to that, the bus time, in which a process will complete and free the
resource, is also unknown.

Another way is if a process is holding a resource and wants to have additional resources, then it
must free the acquired resources. This way, we can avoid the hold and wait condition, but it can result
in starvation.

3. Removal of No Preemption

One of the reasons that cause the deadlock is the no preemption. It means the CPU can’t take
acquired resources from any process forcefully even though that process is in a waiting state. If we can
remove the no preemption and forcefully take resources from a waiting process, we can avoid the
deadlock. This is an implementable logic to avoid deadlock.

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


28
Operating Systems

For example, it’s like taking the bowl from Jones and give it to Jack when he comes to have
soup. Let’s assume Jones came first and acquired a resource and went into the waiting state. Now
when Jack came, the caterer took the bowl from Jones forcefully and told him not to hold the bowl if
you are in a waiting state.

4. Removal of Circular Wait

In the circular wait, two processes are stuck in the waiting state for the resources which have
been held by each other. To avoid the circular wait, we assign a numerical integer value to all
resources, and a process has to access the resource in increasing or decreasing order.

If the process acquires resources in increasing order, it’ll only have access to the new additional
resource if that resource has a higher integer value. And if that resource has a lesser integer value, it
must free the acquired resource before taking the new resource and vice-versa for decreasing order.

Banker's Algorithm
Banker's algorithm is a deadlock avoidance algorithm. It is named so because this algorithm is
used in banking systems to determine whether a loan can be granted or not.

Consider there are n account holders in a bank and the sum of the money in all of their
accounts is S. Every time a loan has to be granted by the bank, it subtracts the loan amount from
the total money the bank has. Then it checks if that difference is greater than S. It is done because,
only then, the bank would have enough money even if all the n account holders draw all their money
at once.

Banker's algorithm works in a similar way in computers.

Characteristics of Banker's Algorithm


 If any process requests for a resource, then it has to wait.
 This algorithm consists of advanced features for maximum resource allocation.
 There are limited resources in the system we have.
 In this algorithm, if any process gets all the needed resources, then it is that it should return the
resources in a restricted period.
 Various resources are maintained in this algorithm that can fulfill the needs of at least one client.
 Let us assume that there are n processes and m resource types.

 Data Structures used to implement the Banker’s Algorithm

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


29
Operating Systems

Some data structures that are used to implement the banker's algorithm are:

1. 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.

2. 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.

3. 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.

4. 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.

Banker’s algorithm comprises of two algorithms:

1. Safety algorithm
2. Resource request algorithm

Prof.S.V.Gunjal, Lecturer, BD Dept., Polytechnic, Loni


30

You might also like