Skip to content

Commit b2c85d3

Browse files
committed
Added Source Code and Output Image
1 parent 4a86628 commit b2c85d3

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

OS.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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\tProcess 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\tTable 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\nSelect 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\nEnter Process No: ";
48+
cin>>pro;
49+
cout<<"\nEnter 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\nEnter Process No: ";
55+
cin>>pro;
56+
cout<<"\nEnter 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<<"\nEnter 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<<"\nProcess P"<<j+1;
89+
cout<<"\nCurrent: "<<current[0]<<" "<<current[1]<<" "<<current[2]<<"\n";
90+
cout<<"\nProcess executed without deadlock";
91+
come=true;
92+
break;
93+
}
94+
}
95+
}
96+
if (!come)
97+
{
98+
cout<<"\n\t\t\tDead 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+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
We have made a program using C++ which can be executed for different process to find out if deadlock happens or not.
44
User can also try to change constraints like initial resources, allocated or max resources of a process.
55

6+
## Output
67
<center>
78
<img src="images/output.png" align="center">
89
</center>

images/output.png

333 KB
Loading

0 commit comments

Comments
 (0)