0% found this document useful (0 votes)
58 views3 pages

Bankers Algo

The document describes the Banker's Algorithm for deadlock avoidance. It involves testing for safety by simulating maximum resource allocation and making an "s-state" check before allocation. The algorithm gets process and resource data, calculates the need matrix, and flags processes that can safely get resources without causing deadlock. It then outputs whether the system is in a safe state or not based on the flag values. The code provided implements this algorithm in C - it takes input for processes, resources, allocation and max matrices, calculates need, and simulates allocation to determine the safe process sequence or detect an unsafe system.

Uploaded by

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

Bankers Algo

The document describes the Banker's Algorithm for deadlock avoidance. It involves testing for safety by simulating maximum resource allocation and making an "s-state" check before allocation. The algorithm gets process and resource data, calculates the need matrix, and flags processes that can safely get resources without causing deadlock. It then outputs whether the system is in a safe state or not based on the flag values. The code provided implements this algorithm in C - it takes input for processes, resources, allocation and max matrices, calculates need, and simulates allocation to determine the safe process sequence or detect an unsafe system.

Uploaded by

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

Ex.

No:7 Bankers Algorithm for Dead Lock


Avoidance

Aim:
To execute a C program implementing Bankers Algorithm for dead lock avoidance

Bankers Algorithm:
The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that
tests for safety by simulating the allocation for the predetermined maximum possible
amounts of all resources, then makes an “s-state” check to test for possible activities,
before deciding whether allocation should be allowed to continue.

Algorithm:
1. Start
2. Get the no.of processes,resources,max matrix,allocation matrix and the available
sequence.
3. Flag[i]=0 for all process and find need matrix = max-allocation.
4. Find a process Pi such that flag[I]=0 and need i <=available
5. If such i exists then flag[i]=1,available=available+allocate, goto step 4, otherwise
go to step 6.
6. If flag[i]=0,for all i then system is in safe state otherwise unsafe state.
7. Stop.

Code:
// Banker's Algorithm
#include <stdio.h>
int main()
{
// P0, P1, P2, P3, P4 are the Process names here

int n, m, i, j, k,alloc[20][20],max[20][20],avail[20];
printf("Enter the no.of processes:");
scanf("%d",&n);
printf("Enter the no.of resources:");
scanf("%d",&m);
printf("Enter the allocation matrix:\n");
for(i=0;i<n;i++)
{
for (j=0;j<m;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("\nEnter the max matrix:\n");
for(i=0;i<n;i++)
{
for (j=0;j<m;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("\nEnter the available sequence:");
for(i=0;i<m;i++)
{
scanf("%d",&avail[i]);
}

int f[n], ans[n], ind = 0;


for (k = 0; k < n; k++) {
f[k] = 0;
}
int need[n][m];
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}
int y = 0;
for (k = 0; k < 5; k++) {
for (i = 0; i < n; i++) {
if (f[i] == 0) {

int flag = 0;
for (j = 0; j < m; j++) {
if (need[i][j] > avail[j]){
flag = 1;
break;
}
}

if (flag == 0) {
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}

int flag = 1;

for(int i=0;i<n;i++)
{
if(f[i]==0)
{
flag=0;
printf("The following system is not safe");
break;
}
}

if(flag==1)
{
printf("Following is the SAFE Sequence\n");
for (i = 0; i < n - 1; i++)
printf(" P%d ->", ans[i]);
printf(" P%d", ans[n - 1]);
}
return (0);
}

Output:

Result:
Thus the C program for the implementation of banker’s algorithm for dead lock
avoidance is written,executed and the outputs are verified.

You might also like