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)

PDF
Zoro123456789123456789123456789123456789
 
PDF
labb123456789123456789123456789123456789
 
PPTX
Banker Algorithm in operating system.pptx
DOCX
12th CBSE Practical File
PPT
Linux_C_LabBasics.ppt
PDF
Deadlock
PPT
topic27_classes_objects_1.ppt for beginners and idiots
DOC
C important questions
DOCX
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
PDF
DSC program.pdf
PDF
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
PPTX
Getting Input from User
PPTX
Unit-2 Getting Input from User.pptx
DOCX
Data Structure in C (Lab Programs)
PDF
CH07.pdf
PPT
Java căn bản - Chapter10
PDF
Sujal Shripat Shirole ds practical book
PPTX
Loops in Python
PDF
C++ manual Report Full
PDF
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Zoro123456789123456789123456789123456789
 
labb123456789123456789123456789123456789
 
Banker Algorithm in operating system.pptx
12th CBSE Practical File
Linux_C_LabBasics.ppt
Deadlock
topic27_classes_objects_1.ppt for beginners and idiots
C important questions
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
DSC program.pdf
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Getting Input from User
Unit-2 Getting Input from User.pptx
Data Structure in C (Lab Programs)
CH07.pdf
Java căn bản - Chapter10
Sujal Shripat Shirole ds practical book
Loops in Python
C++ manual Report Full
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
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)

PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
International_Financial_Reporting_Standa.pdf
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
Empowerment Technology for Senior High School Guide
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PPTX
Introduction to pro and eukaryotes and differences.pptx
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
IGGE1 Understanding the Self1234567891011
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
Unit 4 Computer Architecture Multicore Processor.pptx
A powerpoint presentation on the Revised K-10 Science Shaping Paper
International_Financial_Reporting_Standa.pdf
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
Uderstanding digital marketing and marketing stratergie for engaging the digi...
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
Practical Manual AGRO-233 Principles and Practices of Natural Farming
Empowerment Technology for Senior High School Guide
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
Introduction to pro and eukaryotes and differences.pptx
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
TNA_Presentation-1-Final(SAVE)) (1).pptx
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
B.Sc. DS Unit 2 Software Engineering.pptx
History, Philosophy and sociology of education (1).pptx
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
IGGE1 Understanding the Self1234567891011
LDMMIA Reiki Yoga Finals Review Spring Summer

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