forked from wbhima/CodeforALL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bankersalgo.cpp
146 lines (142 loc) · 2.36 KB
/
bankersalgo.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
Banker’s Algorithm
#include<stdio.h>
#include<conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
printf("********** Banker's Algo ************\n");
input();
show();
cal();
getch();
return 0;
}
void input()
{
int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resources instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)< span="" style="box-sizing: border-box;">
{
for(j=0;j<r;j++)< span="" style="box-sizing: border-box;">
{
scanf("%d",&max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)< span="" style="box-sizing: border-box;">
{
for(j=0;j<r;j++)< span="" style="box-sizing: border-box;">
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)< span="" style="box-sizing: border-box;">
{
scanf("%d",&avail[j]);
}
}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t");
for(i=0;i<n;i++)< span="" style="box-sizing: border-box;">
{
printf("\nP%d\t ",i+1);
for(j=0;j<r;j++)< span="" style="box-sizing: border-box;">
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)< span="" style="box-sizing: border-box;">
{
printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)< span="" style="box-sizing: border-box;">
printf("%d ",avail[j]);
}
}
}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int safe[100];
int i,j;
for(i=0;i<n;i++)< span="" style="box-sizing: border-box;">
{
finish[i]=0;
}
//find need matrix
for(i=0;i<n;i++)< span="" style="box-sizing: border-box;">
{
for(j=0;j<r;j++)< span="" style="box-sizing: border-box;">
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
printf("\n");
while(flag)
{
flag=0;
for(i=0;i<n;i++)< span="" style="box-sizing: border-box;">
{
int c=0;
for(j=0;j<r;j++)< span="" style="box-sizing: border-box;">
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)< span="" style="box-sizing: border-box;">
{
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}
printf("P%d->",i);
if(finish[i]==1)
{
i=n;
}
}
}
}
}
}
for(i=0;i<n;i++)< span="" style="box-sizing: border-box;">
{
if(finish[i]==1)
{
c1++;
}
else
{
printf("P%d->",i);
}
}
if(c1==n)
{
printf("\n The system is in safe state");
}
else
{
printf("\n Process are in dead lock");
printf("\n System is in unsafe state");
}
}