SlideShare a Scribd company logo
1 of 6
Download to read offline
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

What's hot

Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsiqbalphy1
 
Scalable Nonmonotonic Reasoning over RDF Data Using MapReduce
Scalable Nonmonotonic Reasoning over RDF Data Using MapReduceScalable Nonmonotonic Reasoning over RDF Data Using MapReduce
Scalable Nonmonotonic Reasoning over RDF Data Using MapReducePlanetData Network of Excellence
 
Graph Gurus Episode 7: Connecting the Dots in Real-Time: Deep Link Analysis w...
Graph Gurus Episode 7: Connecting the Dots in Real-Time: Deep Link Analysis w...Graph Gurus Episode 7: Connecting the Dots in Real-Time: Deep Link Analysis w...
Graph Gurus Episode 7: Connecting the Dots in Real-Time: Deep Link Analysis w...TigerGraph
 
CS-141 Java programming II ASSIGNMENT 2
CS-141 Java programming II ASSIGNMENT 2CS-141 Java programming II ASSIGNMENT 2
CS-141 Java programming II ASSIGNMENT 2Voffelarin
 
Basics in algorithms and data structure
Basics in algorithms and data structure Basics in algorithms and data structure
Basics in algorithms and data structure Eman magdy
 
A PREFIXED-ITEMSET-BASED IMPROVEMENT FOR APRIORI ALGORITHM
A PREFIXED-ITEMSET-BASED IMPROVEMENT FOR APRIORI ALGORITHMA PREFIXED-ITEMSET-BASED IMPROVEMENT FOR APRIORI ALGORITHM
A PREFIXED-ITEMSET-BASED IMPROVEMENT FOR APRIORI ALGORITHMcsandit
 
Chapter 6.5
Chapter 6.5Chapter 6.5
Chapter 6.5sotlsoc
 

What's hot (9)

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

Similar to Bankers algorithm

12th CBSE Practical File
12th CBSE Practical File12th CBSE Practical File
12th CBSE Practical FileAshwin Francis
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)Make Mannan
 
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnNumerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnArnaud Joly
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Saket Pathak
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10Vince Vo
 
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...GeeksLab Odessa
 
Lo Mejor Del Pdc2008 El Futrode C#
Lo Mejor Del Pdc2008 El Futrode C#Lo Mejor Del Pdc2008 El Futrode C#
Lo Mejor Del Pdc2008 El Futrode C#Juan Pablo
 
Methods for handling deadlock
Methods for handling deadlockMethods for handling deadlock
Methods for handling deadlocksangrampatil81
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithmK Hari Shankar
 
Assignment Java Programming 2
Assignment Java Programming 2Assignment Java Programming 2
Assignment Java Programming 2Kaela Johnson
 

Similar to Bankers algorithm (20)

12th CBSE Practical File
12th CBSE Practical File12th CBSE Practical File
12th CBSE Practical File
 
Linux_C_LabBasics.ppt
Linux_C_LabBasics.pptLinux_C_LabBasics.ppt
Linux_C_LabBasics.ppt
 
Deadlock
DeadlockDeadlock
Deadlock
 
C important questions
C important questionsC important questions
C important questions
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnNumerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
 
Getting Input from User
Getting Input from UserGetting Input from User
Getting Input from User
 
Unit-2 Getting Input from User.pptx
Unit-2 Getting Input from User.pptxUnit-2 Getting Input from User.pptx
Unit-2 Getting Input from User.pptx
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
 
CH07.pdf
CH07.pdfCH07.pdf
CH07.pdf
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
 
Lo Mejor Del Pdc2008 El Futrode C#
Lo Mejor Del Pdc2008 El Futrode C#Lo Mejor Del Pdc2008 El Futrode C#
Lo Mejor Del Pdc2008 El Futrode C#
 
Methods for handling deadlock
Methods for handling deadlockMethods for handling deadlock
Methods for handling deadlock
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
 
Assignment Java Programming 2
Assignment Java Programming 2Assignment Java Programming 2
Assignment Java Programming 2
 

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

2D Transformation in Computer Graphics
2D Transformation in Computer Graphics2D Transformation in Computer Graphics
2D Transformation in Computer Graphics
 
3D Transformation in Computer Graphics
3D Transformation in Computer Graphics3D Transformation in Computer Graphics
3D Transformation in Computer Graphics
 
Projection
ProjectionProjection
Projection
 
2D Transformation
2D Transformation2D Transformation
2D Transformation
 
Line drawing algorithm
Line drawing algorithmLine drawing algorithm
Line drawing algorithm
 
Fragmentation
FragmentationFragmentation
Fragmentation
 
File organization
File organizationFile organization
File organization
 
RR and priority scheduling
RR and priority schedulingRR and priority scheduling
RR and priority scheduling
 
Fcfs and sjf
Fcfs and sjfFcfs and sjf
Fcfs and sjf
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
1D Array
1D Array1D Array
1D Array
 
2D array
2D array2D array
2D array
 
Stack push pop
Stack push popStack push pop
Stack push pop
 
Queue
QueueQueue
Queue
 
Searching
SearchingSearching
Searching
 
Sorting
SortingSorting
Sorting
 
Linked list
Linked listLinked list
Linked list
 
Sum of subset problem
Sum of subset problemSum of subset problem
Sum of subset problem
 
Quick sort
Quick sortQuick sort
Quick sort
 
N queens problem
N queens problemN queens problem
N queens problem
 

Recently uploaded

Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 

Recently uploaded (20)

Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 

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