1+ #include < bits/stdc++.h>
2+ using namespace std ;
3+ void Calculate ();
4+ void IMP ();
5+ int allocation[10 ][3 ],need[10 ][3 ],Max[10 ][3 ],available[10 ][3 ];
6+ int p,current[3 ];
7+ bool executed[10 ],come;
8+ int main (){
9+ int keepon = 1 ;
10+ cout<<" Enter No. of processes: " ;
11+ cin>>p;
12+ cout<<" \n " ;
13+ cout<<" Enter the current resources: " ;
14+ cin>>current[0 ]>>current[1 ]>>current[2 ];
15+ for (int i = 0 ; i < p; ++i)
16+ {
17+ cout<<" \n\n\t\t\t Process P" <<i+1 <<" Details\n " ;
18+ cout<<" Enter Allocation : " ;
19+ cin>>allocation[i][0 ]>>allocation[i][1 ]>>allocation[i][2 ];
20+ cout<<" Enter Max :" ;
21+ cin>>Max[i][0 ]>>Max[i][1 ]>>Max[i][2 ];
22+ need[i][0 ]=Max[i][0 ]-allocation[i][0 ];need[i][1 ]=Max[i][1 ]-allocation[i][1 ];need[i][2 ]=Max[i][2 ]-allocation[i][2 ];
23+ }
24+ cout<<" \n\n\t\t\t Table for Bankers Algo\n\n " ;
25+ cout<<" Initial Resources: " <<current[0 ]<<" " <<current[1 ]<<" " <<current[2 ]<<" \n\n " ;
26+ cout<<" Process Max Allocation Need\n " ;
27+ for (int i = 0 ; i < p; ++i)
28+ {
29+ cout<<" P" <<i+1 <<" " ;
30+ cout<<" " <<Max[i][0 ]<<" " <<Max[i][1 ]<<" " <<Max[i][2 ]<<" " ;
31+ cout<<" " <<allocation[i][0 ]<<" " <<allocation[i][1 ]<<" " <<allocation[i][2 ]<<" " ;
32+ cout<<" " <<need[i][0 ]<<" " <<need[i][1 ]<<" " <<need[i][2 ];
33+ cout<<" \n " ;
34+ }
35+ cout<<" \n\n " ;
36+ Calculate ();
37+ while (keepon){
38+ int val,pro;
39+ cout<<" \n\n Select Below oprations:\n\n " ;
40+ cout<<" 1.Change Max of process: \n " ;
41+ cout<<" 2.Change Allocation of process\n " ;
42+ cout<<" 3.Change Initial Resources\n " ;
43+ cout<<" 4.Exit\n\n " ;
44+ cin>>val;
45+ if (val==1 )
46+ {
47+ cout<<" \n\n Enter Process No: " ;
48+ cin>>pro;
49+ cout<<" \n Enter New Max: " ;
50+ cin>>Max[pro-1 ][0 ]>>Max[pro-1 ][1 ]>>Max[pro-1 ][2 ];
51+ }
52+ else if (val==2 )
53+ {
54+ cout<<" \n\n Enter Process No: " ;
55+ cin>>pro;
56+ cout<<" \n Enter New Allocation: " ;
57+ cin>>allocation[pro-1 ][0 ]>>allocation[pro-1 ][1 ]>>allocation[pro-1 ][2 ];
58+ }
59+ else if (val==3 )
60+ {
61+ cout<<" \n Enter Initial Resources: " ;
62+ cin>>current[0 ]>>current[1 ]>>current[2 ];
63+ }
64+ else {
65+ break ;
66+ }
67+ Calculate ();
68+ }
69+ return 0 ;
70+ }
71+
72+ void Calculate (){
73+ IMP ();
74+ int i,j;
75+ for (i = 0 ; i < p; ++i)
76+ {
77+ for (j = 0 ; j < p; ++j)
78+ {
79+ while (executed[j] && j<p-1 ){
80+ j++;
81+ }
82+ if (need[j][0 ]<=current[0 ]&&need[j][1 ]<=current[1 ]&&need[j][2 ]<=current[2 ])
83+ {
84+ if (!executed[j])
85+ {
86+ executed[j]=true ;
87+ current[0 ]+=allocation[j][0 ];current[1 ]+=allocation[j][1 ];current[2 ]+=allocation[j][2 ];
88+ cout<<" \n Process P" <<j+1 ;
89+ cout<<" \n Current: " <<current[0 ]<<" " <<current[1 ]<<" " <<current[2 ]<<" \n " ;
90+ cout<<" \n Process executed without deadlock" ;
91+ come=true ;
92+ break ;
93+ }
94+ }
95+ }
96+ if (!come)
97+ {
98+ cout<<" \n\t\t\t Dead lock\n\n " ;
99+ break ;
100+ }else {
101+ come=false ;
102+ }
103+ }
104+ }
105+
106+ void IMP (){
107+ come=false ;
108+ for (int i = 0 ; i < 10 ; ++i)
109+ {
110+ executed[i]=false ;
111+ }
112+ }
0 commit comments