-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.cpp
126 lines (125 loc) · 2.37 KB
/
project.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
#include<iostream>
#include<stdlib.h>
#include<stdbool.h>
using namespace std;
void display(int **a,int n,int m)
{
for(int i=0;i<n;i++)
{
cout<<endl;
for(int j=0;j<m;j++)
cout<<a[i][j]<<" ";
}
}
bool isSmall(int *a,int *b,int n)
{
int flag=0;
for(int i=0;i<n;i++)
{
if(a[i]>b[i])
{
flag=1;
break;
}
}
if(flag==0)
return true;
else
return false;
}
int main()
{
cout<<"\nEnter no of variety of resources : ";
int nr;
cin>>nr;
cout<<"\nEnter the instances of resources : \n";
int *r=new int[nr];
for(int i=0;i<nr;i++)
cin>>r[i];
cout<<"Enter the no of processes : ";
int p;
cin>>p;
cout<<"Enter the Allocation matrix : \n";
int **am=new int*[p];
for(int i=0;i<p;i++)
am[i]=new int[nr];
for(int i=0;i<p;i++)
for(int j=0;j<nr;j++)
cin>>am[i][j];
cout<<"Enter the Max matrix : \n";
int **mm=new int*[p];
for(int i=0;i<p;i++)
mm[i]=new int[nr];
for(int i=0;i<p;i++)
for(int j=0;j<nr;j++)
cin>>mm[i][j];
int **nm=new int*[p];
for(int i=0;i<p;i++)
nm[i]=new int[nr];
for(int i=0;i<p;i++)
for(int j=0;j<nr;j++)
nm[i][j]=mm[i][j]-am[i][j];
cout<<"Need matrix is: \n";
display(nm,p,nr);
int *avai=new int[nr];
int *temp=new int[nr];
cout<<"\nenter the process that is requesting :";
int rp;
cin>>rp;
cout<<"\nenter the request : ";
int *request=new int[nr];
for(int i=0;i<nr;i++)
cin>>request[i];
if(isSmall(request,nm[rp],nr)&&isSmall(request,avai,nr))
{
for(int i=0;i<nr;i++)
{
avai[i]-=request[i];
nm[rp][i]-=request[i];
am[rp][i]+=request[i];
}
}
for(int i=0;i<nr;i++)
{
int sum=0;
for(int j=0;j<p;j++)
sum+=am[j][i];
temp[i]=sum;
}
int *work=new int[nr];
for(int i=0;i<nr;i++)
{
avai[i]=r[i]-temp[i];
work[i]=avai[i];
}
bool *finish=new bool[p];
for(int i=0;i<p;i++)
finish[i]=false;
int trap=0;
int count=0;
int disp=1;
//cout<<"\nsafe sequence :";
while(trap<p)
{
if(count>p*p)
{
cout<<"unsafe";
break;
}
for(int i=0;i<p;i++)
{
count++;
if(finish[i]==false&&isSmall(nm[i],work,nr))
{
if(disp==1)
cout<<"\nsafe sequence :";
disp++;
for(int j=0;j<nr;j++)
work[j]=work[j]+am[i][j];
trap++;
finish[i]=true;
cout<<i<<" ";
}
}
}
}