0% found this document useful (0 votes)
4 views2 pages

Bankers Algorithm

The document contains a C program implementing the Banker's Algorithm for deadlock avoidance in operating systems. It initializes process allocation and maximum resource matrices, calculates the need matrix, and checks for a safe sequence of process execution. If a safe sequence exists, it prints the sequence; otherwise, it indicates that the system is not safe.

Uploaded by

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

Bankers Algorithm

The document contains a C program implementing the Banker's Algorithm for deadlock avoidance in operating systems. It initializes process allocation and maximum resource matrices, calculates the need matrix, and checks for a safe sequence of process execution. If a safe sequence exists, it prints the sequence; otherwise, it indicates that the system is not safe.

Uploaded by

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

//Banker's Algorithm

#include<stdio.h>
#define n 5 //no of processes
#define m 3 //no of resources
void main(){
int i,j,k,flag;
int alloc[n][m] = { {0,1,0},
{2,0,0},
{3,0,2},
{2,1,1},
{0,0,2}
};
int max[n][m] = { {7,5,3},
{3,2,2},
{9,0,2},
{4,2,2},
{5,3,3}
};
int avail[3] = {3,3,2};
int f[n]={0},ans[n],ind=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){
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;
}
}
}
}
flag = 1;
for(i = 0;i<n;++i){
if(f[i]==0){
flag = 0;
printf("The following system is not safe.");
break;
}
}
if(flag ==1){
printf("\nFollowing is the safe sequence\n");
for(i=0;i<n;++i){
printf("P%d, ",ans[i]+1);
}
}
getch();
}

You might also like