#include<iostream> |
using namespace std; |
int avaResour[3]={3,3,2}; |
int allocation[5][3]={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}}; |
int maxRequest[5][3]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}}; |
int nneed[5][3]={{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}}; |
void rrequest(); |
bool safeTest(); |
int main() |
{ |
rrequest(); |
return 0; |
} |
void rrequest() |
{ |
int sqjc=0,i=0,l=0,a=0,b=0,c=0; |
char contn=0; |
cout<<endl<< "进程个数:5 资源个数:3" <<endl<< "可用资源向量Available:" <<endl; |
for (i=0;i<3;i++) |
cout<<avaResour[i]<< " " ; |
cout<<endl<< "最大需求矩阵Max:" <<endl; |
for (i=0;i<5;i++) |
{ |
for (l=0;l<3;l++) |
cout<<maxRequest[i][l]<< " " ; |
cout<<endl; |
} |
cout<< "已分配矩阵Allocation:" <<endl; |
for (i=0;i<5;i++) |
{ |
for (l=0;l<3;l++) |
cout<<allocation[i][l]<< " " ; |
cout<<endl; |
} |
cout<< "需求矩阵Need:" <<endl; |
for (i=0;i<5;i++) |
{ |
for (l=0;l<3;l++) |
cout<<nneed[i][l]<< " " ; |
cout<<endl; |
} |
cout<< "输入发起请求的进程(0-4): " ; |
cin>>sqjc; |
while (sqjc>4||sqjc<0) |
{ |
cout<<endl<< "不要乱输! ^_^" <<endl; |
cout<<endl<< "输入发起请求的进程(0-4): " ; |
cin>>sqjc; |
} |
cout<< "输入请求资源的数目,按照这样的格式输入 x x x: " ; |
cin>>a>>b>>c; |
if (a<=nneed[sqjc][0] && b<=nneed[sqjc][1] && c<=nneed[sqjc][2]) |
{ |
cout<<endl<< "开始执行银行家算法..." <<endl; |
if (a<=avaResour[0] && b<=avaResour[1] && c<=avaResour[2]) |
{ |
avaResour[0]=avaResour[0]-a; avaResour[1]=avaResour[1]-b; |
avaResour[2]=avaResour[2]-c; |
allocation[sqjc][0]=allocation[sqjc][0]+a; |
allocation[sqjc][1]=allocation[sqjc][1]+b; |
allocation[sqjc][2]=allocation[sqjc][2]+c; |
nneed[sqjc][0]=nneed[sqjc][0]-a; |
nneed[sqjc][1]=nneed[sqjc][1]-b; |
nneed[sqjc][2]=nneed[sqjc][2]-c; |
cout<<endl<< "试分配完成..." <<endl; |
if (safeTest()) |
{ |
cout<<endl<< "开始给第" <<sqjc<< "个进程分配资源..." <<endl |
<< "分配完成,已更新Allocation_list.txt" <<endl; |
for (i=0;i<5;i++) |
if (nneed[i][0]==0 && nneed[i][1]==0 && nneed[i][2]==0) |
for (l=0;l<3;l++) |
{ |
avaResour[l]=avaResour[l]+allocation[i][l]; |
allocation[i][l]=0; |
} |
} |
else |
{ |
cout<<endl<< "系统已恢复试分配前状态。" <<endl; |
avaResour[0]=avaResour[0]+a; avaResour[1]=avaResour[1]+b; |
avaResour[2]=avaResour[2]+c; |
allocation[sqjc][0]=allocation[sqjc][0]-a; |
allocation[sqjc][1]=allocation[sqjc][1]-b; |
allocation[sqjc][2]=allocation[sqjc][2]-c; |
nneed[sqjc][0]=nneed[sqjc][0]+a; |
nneed[sqjc][1]=nneed[sqjc][1]+b; |
nneed[sqjc][2]=nneed[sqjc][2]+c; |
} |
} |
else |
cout<<endl<< "试分配失败,系统无足够资源" <<endl; |
} |
else |
cout<<endl<< "Error!申请的资源大于需求值" <<endl; |
xz: |
cout<< "\n\n" << "要继续吗?(y-继续;n-终止):" ; |
cin>>contn; |
switch (contn) |
{ |
case 'y' :rrequest(); |
case 'n' : break ; |
default :cout<< "亲,不要乱输哦" <<endl; |
goto xz; break ; |
} |
} |
bool safeTest() |
{ |
int s=0,m=0,z=0,r[5]={0},y=0,wwork[3]={0}; |
bool ffinish[5]={0}; |
cout<<endl<< "进入安全性测试!" <<endl; |
for (s=0;s<3;s++) |
wwork[s]=avaResour[s]; |
for (z=0;z<5;z++) |
{ |
for (s=0;s<5;s++) |
if (ffinish[s]==0 && nneed[s][0]<=wwork[0] && nneed[s][1]<=wwork[1] |
&& nneed[s][2]<=wwork[2]) |
{ |
for (m=0;m<3;m++) |
wwork[m]=wwork[m]+allocation[s][m]; |
ffinish[s]=1; |
r[y]=s; |
y++; |
} |
} |
if (y==5) |
{ |
cout<<endl<< "找到一个安全序列:" ; |
for (s=0;s<2;s++) |
cout<< "P" <<r[2*s]<< "--->" << "P" <<r[2*s+1]<< "--->" ; |
cout<< "P" <<r[4]<<endl<< "已通过安全检测!" <<endl; |
return 1; |
} |
cout<<endl<< "没有找到安全序列!" <<endl<< "安全测试失败。" <<endl; |
return 0; |
} |