1.设计五个进程{P0,P1,P2,P3,P4}共享三类资源{A,B,C}的系统,{A,B,C}的资源数量分别为10,5,7。进程可动态地申请资源和释放资源,系统按各进程的申请动态地分配资源。在T0时刻的资源分配情况如下图所示:
(1)若进程P1请求资源,发出请求向量Request1(1,0,2),编写程序用银行家算法判断系统能否将资源分配给它;
(2)若进程P2提出请求Request2(0,1,0),用银行家算法程序验证系统能否将资源分配给它;
(3)若进程P0提出请求Request2(0,0,3),用银行家算法程序验证系统能否将资源分配给它。
要求程序具有显示和打印各进程的某一时刻的资源分配表和安全序列。
2. 实验代码
#include<iostream>
using namespace std;
int Available[3]={3,3,2};
int Max[5][3]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
int Allocation[5][3]={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
int Need[5][3]={{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}};
int Work[3];
int Finish[5]={0,0,0,0,0};
int SafeArray[5];
int Request[3];
void ShowSafe(int i);
void Show(){
cout<<"T0时刻的分配情况"<<endl;
cout<<"进程\tMax\t\tAllocation\tNeed\t\tAvailable"<<endl;
for(int i=0;i<5;i++){
cout<<"P"<<i<<"\t";
for(int j=0;j<3;j++){
cout<<Max[i][j]<<" ";
}
cout<<"\t\t";
for(int j=0;j<3;j++){
cout<<Allocation[i][j]<<" ";
}
cout<<"\t\t";
for(int j=0;j<3;j++){
cout<<Need[i][j]<<" ";
}
cout<<"\t\t";
if(i==0){
for(int j=0;j<3;j++){
cout<<Available[j]<<" ";
}
}
cout<<endl;
}
}
int NeedLessWork(int i){
for(int j=0;j<3;j++){
if(Need[i][j]>Work[j]){
return 0;
}
}
return 1;
}
void SafeLine(){
cout<<"当前系统安全"<<endl;
cout<<"安全序列是:" ;
for(int i=0;i<5;i++){
cout<<SafeArray[i]<<" ";
}
cout<<endl;
for(int i=0;i<5;i++){
Finish[i]=0;
}
}
void IsSafe(int index){
for(int i=0;i<5;i++){
int temp=NeedLessWork(i);
if(Finish[i]==0&&temp){
SafeArray[index]=i;
index++;
Finish[i]=1;
ShowSafe(i);
for(int j=0;j<3;j++){
Work[j]=Work[j]+Allocation[i][j];
}
break;
}
}
int mult=1;
for(int k=0;k<5;k++){
mult*=Finish[k];
}
if(mult==0){
IsSafe(index);
}else{
SafeLine();
}
}
void ShowSafe(int i){
cout<<"P"<<i<<"\t";
for(int j=0;j<3;j++){
cout<<Work[j]<<" ";
}
cout<<"\t\t";
for(int j=0;j<3;j++){
cout<<Need[i][j]<<" ";
}
cout<<"\t\t";
for(int j=0;j<3;j++){
cout<<Allocation[i][j]<<" ";
}
cout<<"\t\t";
for(int j=0;j<3;j++){
cout<<Work[j]+Allocation[i][j]<<" ";
}
cout<<"\t\t";
cout<<Finish[i];
cout<<endl;
}
void SafeCheck(){
cout<<"系统安全情况分析"<<endl;
cout<<"进程\tWork\t\tNeed\t\tAllocation\tWork+Allocation\tFinish"<<endl;
for(int i=0;i<3;i++){
Work[i]=Available[i];
}
IsSafe(0);
}
int RequestLessNeed(int i){
for(int j=0;j<3;j++){
if(Request[j]>Need[i][j]){
return 0;
}
}
return 1;
}
int RequestLessAvailable(int i){
for(int j=0;j<3;j++){
if(Request[j]>Available[j]){
return 0;
}
}
return 1;
}
void RequestResourse(){
cout<<"请输入请求资源的进程"<<endl;
int n;
cin>>n;
cout<<"请输入需要的资源数量"<<endl;
for(int i=0;i<3;i++){
cin>>Request[i];
}
if(RequestLessNeed(n)&&RequestLessAvailable(n)){
for(int j=0;j<3;j++){
Available[j]=Available[j]-Request[j];
Allocation[n][j]=Allocation[n][j]+Request[j];
Need[n][j]=Need[n][j]-Request[j];
}
SafeCheck();
}else{
cout<<"资源不足,请求失败,p"<<n<<"等待"<<endl;
return ;
}
}
int main(){
while(true){
cout<<"------------------------------------"<<endl;
cout<<" 1 系统安全情况分析"<<endl;
cout<<" 2 请求资源"<<endl;
cout<<" 3 显示有关资源数据"<<endl;
cout<<" 4 退出系统"<<endl;
int choose;
cin>>choose;
switch (choose){
case 1 :
SafeCheck();
break;
case 2:
RequestResourse();
break;
case 3:
Show();
break;
case 4:
cout<<"退出系统"<<endl;
return 0;
default:
cout<<"输入错误,请重新输入"<<endl;
continue;
}
}
}
存在问题:
1
请求资源时,如果满足
Request<=Need&&Request<=Available
但无法通过安全性检查的情况没有完善
2
无法通过安全性检查的情况没有完善