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

Lecture 19 (3)

The document discusses the Banker's Algorithm for deadlock avoidance in operating systems, emphasizing its role in determining safe and unsafe states for resource allocation. It includes an overview of resource allocation graphs, data structures used in the algorithm, and step-by-step procedures for safety and resource-request algorithms. The session aims to help students apply the Banker's Algorithm to numerical problems and understand deadlock-free resource allocation.

Uploaded by

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

Lecture 19 (3)

The document discusses the Banker's Algorithm for deadlock avoidance in operating systems, emphasizing its role in determining safe and unsafe states for resource allocation. It includes an overview of resource allocation graphs, data structures used in the algorithm, and step-by-step procedures for safety and resource-request algorithms. The session aims to help students apply the Banker's Algorithm to numerical problems and understand deadlock-free resource allocation.

Uploaded by

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

Banker's Algorithm – Deadlock Avoidance

Session No.:19
Course Name: Operating System
Course Code: R1UC403B
Instructor Name: Chaya Rawal
Duration: 50 Minutes
Date of Conduction of Class:

Galgotias University 1
Recap : Resource Allocation Graph
• RAG Overview:
• Nodes represent processes (P) and resources (R)
• Edges:
• P → R: request edge
• R → P: assignment edge
• Key Concept:
• If a cycle exists and each resource has only one instance, deadlock may occur

Galgotias University 2
Question based on previous lecture

• Q1: Can a cycle always guarantee a deadlock? Why or why not?

• Q2: How does the Resource Allocation Graph help in deadlock


detection?

Galgotias University 3
Opening Question

How can a system prevent entering a deadlock state


rather than detecting it after it happens?

Galgotias University 4
At the end of this session students will be able to

Learning Outcome 1: Determine whether a system


is in a safe or unsafe state using the Banker's
Algorithm

Learning Outcome 2: Apply the Banker's


Algorithm to solve numerical problems and
check for deadlock-free resource allocation.

Galgotias University 5
1 Introduction to Banker's Algorithm

2 Data Structures & Algorithm Steps

Session 3 Learning Activity 1


Outline 4 Numerical Example

5 Learning Activity 2

Galgotias University 6
Avoidance Algorithms
• Single instance of a resource type
• Use a resource-allocation graph

• Multiple instances of a resource type


• Use the banker’s algorithm
Resource-Allocation Graph Scheme
• Claim edge Pi  Rj indicated that process Pj
may request resource Rj; represented by a
dashed line
• Claim edge converts to request edge when a
process requests a resource
• Request edge converted to an assignment
edge when the resource is allocated to the
process
• When a resource is released by a process,
assignment edge reconverts to a claim edge
• Resources must be claimed a priori in the
system
Resource-Allocation Graph
Unsafe State In Resource-Allocation Graph
Resource-Allocation Graph Algorithm

• Suppose that process Pi requests


a resource Rj
• The request can be granted only
if converting the request edge to
an assignment edge does not
result in the formation of a cycle
in the resource allocation graph
Avoidance Algorithms
• Single instance of a resource type
• Use a resource-allocation graph

• Multiple instances of a resource type


• Use the banker’s algorithm
Resource-Allocation Graph Scheme
• Claim edge Pi  Rj indicated that process Pj
may request resource Rj; represented by a
dashed line
• Claim edge converts to request edge when a
process requests a resource
• Request edge converted to an assignment
edge when the resource is allocated to the
process
• When a resource is released by a process,
assignment edge reconverts to a claim edge
• Resources must be claimed a priori in the
system
Banker’s Algorithms: Introduction

•Introduced by Edsger Dijkstra


•A deadlock avoidance algorithm
•Works like a bank deciding whether to approve a
loan based on available funds
•Ensures the system is always in a safe state before
allocating resources

Real-World Analogy: A banker won’t give a loan unless they’re sure


the customer (process) can repay it without bankrupting the bank
(system).
Galgotias University 14
Data Structures for the Banker’s Algorithm
Let n = number of processes, and m = number of resources types.
● Available: Vector of length m. If available [j] = k, there are k instances of resource type Rj
available

● Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances of
resource type Rj

● Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj

● Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete


its task
Need [i,j] = Max[i,j] – Allocation [i,j]

Galgotias University 15
Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1
2. Find an i such that both:
(a) Finish [i] = false
(b) Needi ≤ Work
If no such i exists, go to step 4
3. Work = Work + Allocationi
Finish[i] = true
go to step 2
4. If Finish [i] == true for all i, then the system is in a safe state

Galgotias University 16
Resource-Request Algorithm for Process Pi
Requesti = request vector for process Pi. If Requesti [j] = k then process Pi wants k instances of
resource type Rj
1.If Requesti ≤ Needi go to step 2. Otherwise, raise error condition, since process has
exceeded its maximum claim
2.If Requesti ≤ Available, go to step 3. Otherwise Pi must wait, since resources are not
available
3.Pretend to allocate requested resources to Pi by modifying the state as follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
● If safe ⇒ the resources are allocated to Pi
● If unsafe ⇒ Pi must wait, and the old resource-allocation state is restored
Galgotias University 17
Activity 1 – Role Play
•Assign roles: Each student (or small group) becomes a process (P0–P4)
•Give each group their Max, Allocated, and Need values
•Write initial Available resources on the board
How it works:
•Each group evaluates: “Can we complete with current Available?”
•If yes: "Complete" the process, release resources to Available
•Continue until all processes are complete or stuck

Galgotias University 18
Numerical Example
Considering a system with five processes P0 through P4 and three resources of type A, B, C.
Resource type A has 10 instances, B has 5 instances and type C has 7 instances. Suppose at time
t0 following snapshot of the system has been taken:

Galgotias University 19
Numerical Example
What will be the content of the Need matrix?
• Need [i, j] = Max [i, j] – Allocation [i, j]
So, the content of Need Matrix is:

Galgotias University 20
Numerical Example
Is the system in a safe state? If Yes, then what is the safe sequence?
• Applying the Safety algorithm on the given system,

Galgotias University 21
Numerical Example

Galgotias University 22
Numerical Example

Galgotias University 23
Numerical Example

Galgotias University 24
Numerical Example

Galgotias University 25
Numerical Example

Galgotias University 26
Numerical Example
What will happen if process P1 requests one additional instance of resource type A and two
instances of resource type C?

We must determine
whether this new
system state is safe.
To do so, we will
again execute Safety
algorithm on the new
data structure.

Galgotias University 27
Activity 2 – Pen & Paper
A single processor system has three resource types X, Alloc Request
Need
Y and Z, which are shared by three processes. There
are 5 units of each resource type. Consider the X Y Z X Y Z
following scenario, where the column Alloc denotes
the number of units of each resource type allocated to
each process, and the column request denotes the P0 1 2 1 1 0 3
number of units of each resource type requested by a
process in order to complete execution. Which of P1 2 0 1 0 1 2
these processes will finish LAST?
1.P0 P2 2 2 1 1 2 0
2.P1
3.P2
4.None of the above since the system is in a deadlock
Galgotias University 28
Conclusion with attainment of LOs in alignment to
the learning activities:

Learning Outcome 1: Determine whether a system


is in a safe or unsafe state using the Banker's
Algorithm

Learning Outcome 2: Apply the Banker's


Algorithm to solve numerical problems and
check for deadlock-free resource allocation.

Galgotias University 29
Any Query ?

GSCALE full form and date 30


Thanks

GSCALE full form and date 31

You might also like