forked from anoop-singh-dev/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Banker's algorithm
151 lines (134 loc) · 3.35 KB
/
Banker's algorithm
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
147
148
149
150
151
#include<iostream.h>
void main()
{
int instance[5],count,sequence[10],safe,s=0,j,completed;
int available[5],allocation[10][5],max[10][5];
int need[10][5],process,P[10],countofr,countofp,running[10];
clrscr();
cout<<"\n Enter the number of resources (<=5): ";
cin>> countofr;
for(int i=0;i<countofr;i++)
{ cout<<"\n enter the max instances of resource R["<<i<<"] :";
cin>>instance[i];
available[i]=instance[i];
}
cout<<"\n Enter the number of processes (<=10): ";
cin>> countofp;
cout<<"\n Enter the allocation matrix \n ";
for(i=0;i<countofp;i++)
{ cout<<"FOR THE PROCESS :P["<<i<<"]"<<endl;
for(int j=0;j<countofr;j++)
{ cout<<"allocation of resource R["<<j<<"] is : " ;
cin>>allocation[i][j];
available[j]-=allocation[i][j];
}
}
cout<<"\nEnter the MAX matrix \n\n";
for(i=0;i<countofp;i++)
{ cout<<"FOR THE PROCESS P["<<i<<"]"<<endl;
for(int j=0;j<countofr;j++)
{ cout<<"max demand of resource R["<<j<<"] is : ";
cin>>max[i][j];
}
}
clrscr();
cout<<"\n the given data are : \n";
cout<<endl<<"\nTotal resources in system : \n\n ";
for(i=0;i<countofr;i++)
cout<<" R["<<i<<"] ";
cout<<endl;
for(i=0;i<countofr;i++)
cout<<" "<<instance[i];
cout<<"\n\n ALLOCATION matrix \n\n\t";
for(j=0;j<countofr;j++)
cout<<"R["<<j<<"] ";
cout<<endl;
for(i=0;i<countofp;i++)
{ cout<<"P["<<i<<"] ";
for(j=0;j<countofr;j++)
cout<<" "<<allocation[i][j];
cout<<endl;
}
cout<<"\n\n MAX matrix \n\n\t";
for(j=0;j<countofr;j++)
cout<<"R["<<j<<"] ";
cout<<endl;
for(i=0;i<countofp;i++)
{ cout<<"P["<<i<<"] ";
for(j=0;j<countofr;j++)
cout<<" "<<max[i][j];
cout<<endl;
}
for(i=0;i<countofp;i++)
{
for(j=0;j<countofr;j++)
{
need[i][j]=max[i][j]-allocation[i][j];
}
}
cout<<"\n\n NEED matrix \n\n\t";
for(j=0;j<countofr;j++)
cout<<"R["<<j<<"] ";
cout<<endl;
for(i=0;i<countofp;i++)
{ cout<<"P["<<i<<"] ";
for(j=0;j<countofr;j++)
cout<<" "<<need[i][j];
cout<<endl;
}
cout<<"\n NOW to check whether above state is safe";
cout<<"\n sequence in which above requests can be fulfilled";
cout<<"\n press any key to continue";
getch();
count=countofp;
for(i=0;i<countofp;i++)
{ running[i]=1;}
while(count)
{ safe=0;
for(i=0;i<countofp;i++)
{ if(running[i])
{ completed=1;
for(j=0;j<countofr;j++)
{ if(need[i][j]> available[j])
{ completed=0;
break;
}
}
if(completed)
{
running[i]=0;
count--;
safe=1;
for(j=0;j<countofr;j++)
{
available[j]+=allocation[i][j];
}
sequence[s++]=i;
cout<<"\n\n Running process P["<<i<<"]";
cout<<endl<<"\n\nTotal resources now available:\n\n";
for(i=0;i<countofr;i++)
cout<<" R["<<i<<"] ";
cout<<endl;
for(i=0;i<countofr;i++)
cout<<" "<<available[i];
break;
}
}
}
if(!safe)
break;
}
if(safe)
{
cout<<"\nThe System is in safe state";
cout<<"\nSafe sequence is :";
for(i=0;i<countofp;i++)
{
cout<<"\t"<<"P["<<sequence[i]<<"]";
}
}
else
{
cout<<"\nThe System is in unsafe state";
}
}