forked from amanss00/ForNewbies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bankers.cpp
100 lines (94 loc) · 1.38 KB
/
bankers.cpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include<stdio.h>
int main()
{
int r,c,min=-1,s,ct;
printf("Enter number of processes:");
scanf("%d",&r);
int p[r];
for(int i=1;i<=r;i++)
{
p[i]=0;
}
printf("Enter number of resources:");
scanf("%d",&c);
int max[r][c];
printf("Enter max.number of resources required for each process:\n");
for( int i=1;i<=r;i++)
{
printf("P[%d]:\n",i);
for(int j=1;j<=c;j++)
{
printf("\tR[%d]:",j);
scanf("%d",&max[i][j]);
}
}
int allo[r][c];
printf("Enter number of resources allocated for each process:\n");
for( int i=1;i<=r;i++)
{
printf("P[%d]:\n",i);
for(int j=1;j<=c;j++)
{
printf("\tR[%d]:",j);
scanf("%d",&allo[i][j]);
}
}
int need[r][c];
for( int i=1;i<=r;i++)
{
for(int j=1;j<=c;j++)
{
need[i][j]=max[i][j]-allo[i][j];
}
}
int avl[1][c];
printf("Enter number of available resources:\n");
{
for( int j=1;j<=c;j++)
{
scanf("%d",&avl[1][j]);
}
}
for(int i=1;i<=r;i++)
{
if(p[i]!=1)
{
ct=0;
for(int j=1;j<=c;j++)
{
if(need[i][j]<=avl[1][j])
{
ct=ct+1;
}
else
{
break;
}
}
if(ct==c)
{
p[i]=1;
printf("P[%d]\t",i);
for(int j=1;j<=c;j++)
{
avl[1][j]+=allo[i][j];
}
i=0;
}
}
}
for(int i=1;i<=r;i++)
{
if(p[i]==1)
{
continue;
}
else
{
printf("\n Not safe");
return 0;
}
}
printf("\nSafe");
return 0;
}