SlideShare a Scribd company logo
2
Most read
3
Most read
5
Most read
Bankers Algorithm
The banker’s algorithm which is also known as avoidance algorithm is a deadlock detection
algorithm. It was developed by Edsger Dijkstra. It is designed to check the safe state whenever a
resource is requested. It takes analogy of bank, where customer request to withdraw cash. Based
on some data the cash is lent to the customer. The banker can’t give more cash than what the
customer has requested for, and the total available cash. As this algorithm uses bank analogy so
named as banker’s algorithm
Following data structures are used to implement the Banker’s Algorithm:
Let ‘n’ be the number of processes in the system and ‘m’ be the number of resources types.
Available:
 It is a 1-d array of size ‘m’ indicating the number of available resources of each type.
 Available[ j ] = k means there are ‘k’ instances of resource type Rj
Max:
 It is a 2-d array of size ‘n*m’ that defines the maximum demand of each process in a
system.
 Max[ i, j ] = k means process Pi may request at most ‘k’ instances of resource type Rj.
Allocation:
 It is a 2-d array of size ‘n*m’ that defines the number of resources of each type currently
allocated to each process.
 Allocation[ i, j ] = k means process Pi is currently allocated ‘k’ instances of resource
type Rj
Need:
 It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each process.
 Need [ i, j ] = k means process Pi currently allocated ‘k’ instances of resource type Rj
 Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ]
Banker’s Algorithm
1. Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
Initialize: Work = Available
Finish [i] = false; for i=1,2,……,n
2. Find an i such that both
a) Finish [i] = false
b) Need_i <= work
if no such i exists goto step (4)
3. Work = Work + Allocation_i
Finish[i] = true
goto step(2)
4. If Finish[i] = true for all i, then the system is in safe state.
Sample Code
#include<stdio.h>
int main()
{
int allocation[20][20],max[20][20],available[20];
int i,j,process,flag = 1,sq[20],index = 0,flag2,
resource,need[20][20],work[20],finish[20];
printf("Enter the number of process:");
scanf("%d",&process);
printf("Enter the number of resource:");
scanf("%d",&resource);
printf("Enter the allocation matrix:");
for(i = 0; i<process; i++)
for(j = 0; j<resource; j++)
scanf("%d",&allocation[i][j]);
printf("Enter the max matrix:");
for(i = 0; i<process; i++)
for(j = 0; j<resource; j++)
scanf("%d",&max[i][j]);
printf("Enter available:");
for(j = 0; j<resource; j++)
scanf("%d",&available[j]);
for(i = 0; i<process; i++)
for(j = 0; j<resource; j++)
need[i][j] = max[i][j]-allocation[i][j];
for(i = 0; i<resource; i++)
work[i] = available[i];
for(i = 0; i<process; i++)
finish[i] = 0;
while(flag)
{
flag = 0;
for(i = 0; i<process; i++)
{
if(!finish[i])
{
flag2 = 1;
for(j = 0; j<resource; j++)
if(need[i][j]>work[j])
flag2 = 0;
if(flag2)
{
flag = 1;
for(j = 0; j<resource; j++)
work[j]+=allocation[i][j];
finish[i] = 1;
sq[index++] = i;
}
}
}
}
flag = 1;
for(i = 0; i<process;i++)
{
if(!finish[i])
flag = 0;
}
if(flag)
{
printf("ntTHE SQUENCE OF SAFE STATE IS :n");
for(i = 0; i<index; i++)
printf("t%d",sq[i]);
}
else
printf("ntIT IS IN UNSAFE STATE .");
}
Sample Output
Case 1:
Enter the number of process:5
Enter the number of resource:3
Enter the allocation matrix:
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Enter the max matrix:
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
Enter available:
3 3 2
THE SQUENCE OF SAFE STATE IS :
1 3 4 0 2
Case 2:
Enter the number of process:3
Enter the number of resource:4
Enter the allocation matrix:
1 2 5 1
1 1 3 3
1 2 1 0
Enter the max matrix:
3 3 2 2
1 2 3 4
1 3 5 0
Enter available:
3 0 1 2
IT IS IN UNSAFE STATE .
Explanation
Process
Allocation Max Available Need
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3
P1 2 0 0 3 2 2 5 3 2 1 2 2
P2 3 0 2 9 0 2 5 3 2 6 0 0
P3 2 1 1 2 2 2 7 4 3 0 1 1
P4 0 0 2 4 3 3 7 4 5 4 3 1
7 5 5
10 5 7

More Related Content

PPTX
Bankers algorithm
PPTX
K-Means Algorithm Implementation In python
PPTX
9 big o-notation
PPT
PDF
Representative basedclustering
PDF
Graph Gurus Episode 4: Detecting Fraud and Money Laudering in Real-Time Part 2
PPTX
Clustering: A Scikit Learn Tutorial
Bankers algorithm
K-Means Algorithm Implementation In python
9 big o-notation
Representative basedclustering
Graph Gurus Episode 4: Detecting Fraud and Money Laudering in Real-Time Part 2
Clustering: A Scikit Learn Tutorial

What's hot (9)

PPTX
Analysis of algorithms
PDF
Scalable Nonmonotonic Reasoning over RDF Data Using MapReduce
PDF
Graph Gurus Episode 7: Connecting the Dots in Real-Time: Deep Link Analysis w...
PDF
CS-141 Java programming II ASSIGNMENT 2
PDF
Basics in algorithms and data structure
PDF
PDF
A PREFIXED-ITEMSET-BASED IMPROVEMENT FOR APRIORI ALGORITHM
PPTX
Chapter 6.5
Analysis of algorithms
Scalable Nonmonotonic Reasoning over RDF Data Using MapReduce
Graph Gurus Episode 7: Connecting the Dots in Real-Time: Deep Link Analysis w...
CS-141 Java programming II ASSIGNMENT 2
Basics in algorithms and data structure
A PREFIXED-ITEMSET-BASED IMPROVEMENT FOR APRIORI ALGORITHM
Chapter 6.5
Ad

Similar to Bankers algorithm (20)

PPTX
bankers algorithm in operating system.pptx
PDF
PPTX
OS - Unit 3 Deadlock (Bankers Algorithm).pptx
DOCX
BANKER'S ALGORITHM
PPTX
bankers-algorithm2.pptx
PDF
Deadlock
PPTX
Deadlock Algorithms 3.pptx
PDF
The implementation of Banker's algorithm, data structure and its parser
PPTX
Banker Algorithm in operating system.pptx
PDF
Deadlocks Part- II.pdf
PDF
A Dynamic and Improved Implementation of Banker’s Algorithm
PPT
Deadlock principles in operating systems
PPT
26 to 27deadlockavoidance
PPTX
Deadlock and Banking Algorithm
PDF
Implementation of Banker’s Algorithm Using Dynamic Modified Approach
PDF
Implementation of Banker’s Algorithm Using Dynamic Modified Approach
PPTX
A petri-net
PPT
DeadlockMar21.ppt
PPTX
Deadlock avoidance and prevention .. computer networking
PPTX
Bankers algorithm pbl project based ppt.
bankers algorithm in operating system.pptx
OS - Unit 3 Deadlock (Bankers Algorithm).pptx
BANKER'S ALGORITHM
bankers-algorithm2.pptx
Deadlock
Deadlock Algorithms 3.pptx
The implementation of Banker's algorithm, data structure and its parser
Banker Algorithm in operating system.pptx
Deadlocks Part- II.pdf
A Dynamic and Improved Implementation of Banker’s Algorithm
Deadlock principles in operating systems
26 to 27deadlockavoidance
Deadlock and Banking Algorithm
Implementation of Banker’s Algorithm Using Dynamic Modified Approach
Implementation of Banker’s Algorithm Using Dynamic Modified Approach
A petri-net
DeadlockMar21.ppt
Deadlock avoidance and prevention .. computer networking
Bankers algorithm pbl project based ppt.
Ad

More from A. S. M. Shafi (20)

DOCX
Data Warehouse Schema (Star, Snowflake).docx
PDF
Correlation Analysis in Machine Learning.pdf
PDF
Naive Bayes and Decision Tree Algorithm.pdf
PDF
Frequent Pattern Growth Mining Algorithm.pdf
PDF
Direct Hashing and Pruning Algorithm in Data MIning.pdf
PDF
Association Rule Mining with Apriori Algorithm.pdf
PDF
HITS Algorithm in Data and Web MIning.pdf
PDF
Page Rank Algorithm in Data Mining and Web Application.pdf
PDF
K Nearest Neighbor Classifier in Machine Learning.pdf
PDF
K Means Clustering Algorithm in Machine Learning.pdf
PDF
2D Transformation in Computer Graphics
PDF
3D Transformation in Computer Graphics
PDF
Projection
PDF
2D Transformation
PDF
Line drawing algorithm
PDF
Fragmentation
PDF
File organization
PDF
RR and priority scheduling
PDF
Fcfs and sjf
PDF
Applications of stack
Data Warehouse Schema (Star, Snowflake).docx
Correlation Analysis in Machine Learning.pdf
Naive Bayes and Decision Tree Algorithm.pdf
Frequent Pattern Growth Mining Algorithm.pdf
Direct Hashing and Pruning Algorithm in Data MIning.pdf
Association Rule Mining with Apriori Algorithm.pdf
HITS Algorithm in Data and Web MIning.pdf
Page Rank Algorithm in Data Mining and Web Application.pdf
K Nearest Neighbor Classifier in Machine Learning.pdf
K Means Clustering Algorithm in Machine Learning.pdf
2D Transformation in Computer Graphics
3D Transformation in Computer Graphics
Projection
2D Transformation
Line drawing algorithm
Fragmentation
File organization
RR and priority scheduling
Fcfs and sjf
Applications of stack

Recently uploaded (20)

PDF
Computing-Curriculum for Schools in Ghana
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Pre independence Education in Inndia.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Lesson notes of climatology university.
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Cell Structure & Organelles in detailed.
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Insiders guide to clinical Medicine.pdf
PPTX
master seminar digital applications in india
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Cell Types and Its function , kingdom of life
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Classroom Observation Tools for Teachers
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
Computing-Curriculum for Schools in Ghana
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Pre independence Education in Inndia.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Lesson notes of climatology university.
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Cell Structure & Organelles in detailed.
STATICS OF THE RIGID BODIES Hibbelers.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Insiders guide to clinical Medicine.pdf
master seminar digital applications in india
GDM (1) (1).pptx small presentation for students
Renaissance Architecture: A Journey from Faith to Humanism
Cell Types and Its function , kingdom of life
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Classroom Observation Tools for Teachers
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Anesthesia in Laparoscopic Surgery in India

Bankers algorithm

  • 1. Bankers Algorithm The banker’s algorithm which is also known as avoidance algorithm is a deadlock detection algorithm. It was developed by Edsger Dijkstra. It is designed to check the safe state whenever a resource is requested. It takes analogy of bank, where customer request to withdraw cash. Based on some data the cash is lent to the customer. The banker can’t give more cash than what the customer has requested for, and the total available cash. As this algorithm uses bank analogy so named as banker’s algorithm Following data structures are used to implement the Banker’s Algorithm: Let ‘n’ be the number of processes in the system and ‘m’ be the number of resources types. Available:  It is a 1-d array of size ‘m’ indicating the number of available resources of each type.  Available[ j ] = k means there are ‘k’ instances of resource type Rj Max:  It is a 2-d array of size ‘n*m’ that defines the maximum demand of each process in a system.  Max[ i, j ] = k means process Pi may request at most ‘k’ instances of resource type Rj. Allocation:  It is a 2-d array of size ‘n*m’ that defines the number of resources of each type currently allocated to each process.  Allocation[ i, j ] = k means process Pi is currently allocated ‘k’ instances of resource type Rj Need:  It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each process.  Need [ i, j ] = k means process Pi currently allocated ‘k’ instances of resource type Rj  Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ] Banker’s Algorithm 1. Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively. Initialize: Work = Available Finish [i] = false; for i=1,2,……,n 2. Find an i such that both a) Finish [i] = false b) Need_i <= work if no such i exists goto step (4) 3. Work = Work + Allocation_i Finish[i] = true goto step(2) 4. If Finish[i] = true for all i, then the system is in safe state.
  • 2. Sample Code #include<stdio.h> int main() { int allocation[20][20],max[20][20],available[20]; int i,j,process,flag = 1,sq[20],index = 0,flag2, resource,need[20][20],work[20],finish[20]; printf("Enter the number of process:"); scanf("%d",&process); printf("Enter the number of resource:"); scanf("%d",&resource); printf("Enter the allocation matrix:"); for(i = 0; i<process; i++) for(j = 0; j<resource; j++) scanf("%d",&allocation[i][j]); printf("Enter the max matrix:"); for(i = 0; i<process; i++) for(j = 0; j<resource; j++) scanf("%d",&max[i][j]); printf("Enter available:"); for(j = 0; j<resource; j++) scanf("%d",&available[j]); for(i = 0; i<process; i++)
  • 3. for(j = 0; j<resource; j++) need[i][j] = max[i][j]-allocation[i][j]; for(i = 0; i<resource; i++) work[i] = available[i]; for(i = 0; i<process; i++) finish[i] = 0; while(flag) { flag = 0; for(i = 0; i<process; i++) { if(!finish[i]) { flag2 = 1; for(j = 0; j<resource; j++) if(need[i][j]>work[j]) flag2 = 0; if(flag2) { flag = 1; for(j = 0; j<resource; j++) work[j]+=allocation[i][j]; finish[i] = 1; sq[index++] = i; }
  • 4. } } } flag = 1; for(i = 0; i<process;i++) { if(!finish[i]) flag = 0; } if(flag) { printf("ntTHE SQUENCE OF SAFE STATE IS :n"); for(i = 0; i<index; i++) printf("t%d",sq[i]); } else printf("ntIT IS IN UNSAFE STATE ."); } Sample Output Case 1: Enter the number of process:5 Enter the number of resource:3 Enter the allocation matrix: 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2
  • 5. Enter the max matrix: 7 5 3 3 2 2 9 0 2 2 2 2 4 3 3 Enter available: 3 3 2 THE SQUENCE OF SAFE STATE IS : 1 3 4 0 2 Case 2: Enter the number of process:3 Enter the number of resource:4 Enter the allocation matrix: 1 2 5 1 1 1 3 3 1 2 1 0 Enter the max matrix: 3 3 2 2 1 2 3 4 1 3 5 0 Enter available: 3 0 1 2 IT IS IN UNSAFE STATE .
  • 6. Explanation Process Allocation Max Available Need A B C A B C A B C A B C P0 0 1 0 7 5 3 3 3 2 7 4 3 P1 2 0 0 3 2 2 5 3 2 1 2 2 P2 3 0 2 9 0 2 5 3 2 6 0 0 P3 2 1 1 2 2 2 7 4 3 0 1 1 P4 0 0 2 4 3 3 7 4 5 4 3 1 7 5 5 10 5 7