-
Notifications
You must be signed in to change notification settings - Fork 0
/
bankers.c
77 lines (77 loc) · 1.27 KB
/
bankers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<stdio.h>
int flagging(int flag[],int n)
{int i;
for(i=0;i<n;i++)
{
if(flag[i]==0)
return 1;
}
return 0;
}
int main(){
printf("\nEnter number of processes and resources: ");
int n,m,i,j,t=0,counter=0;
scanf("%d %d",&n,&m);
int need[n][m],alloc[n][m],max[n][m],avail[m],flag[n];
printf("\nEnter available resorces: ");
for(i=0;i<m;i++)
{
scanf("%d",&avail[i]);
}
printf("\nEnter maximum resources needed for ");
for(i=0;i<n;i++)
{
printf("Process %d: ",i+1);
for(j=0;j<m;j++)
{
scanf("%d",&max[i][j]);
}
flag[i]=0;
}
printf("\nEnter allocated resources needed for ");
for(i=0;i<n;i++)
{
printf("\nProcess %d: ",i+1);
for(j=0;j<m;j++)
{
scanf("%d",&alloc[i][j]);
need[i][j]=max[i][j]-alloc[i][j];
}
}
while(flagging(flag,n))
{
for(i=0;i<n;i++)
{
if(flag[i]!=1)
{
for(j=0;j<m;j++)
{
if(need[i][j]<=avail[j])
t+=1;
}
}
else
continue;
if(t==m)
break;
t=0;
counter++;
if(counter==n-1)
{
printf("\nUnsafe state\n");
return 0;
}
}
printf("---%d---",i+1);
flag[i]=1;
for(j=0;j<m;j++)
{
avail[j]+=alloc[i][j];
alloc[i][j]=0;
need[i][j]=0;
}
t=0;
counter=0;
}
return 0;
}