别人的银行家算法

本文介绍了一个基于银行家算法的资源管理系统实现。该系统通过定义进程所需的资源最大需求、当前可用资源等参数,并使用安全算法确保系统在资源分配后仍处于安全状态。文章详细展示了资源分配、请求处理及安全性检查的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


// 课程设计2_0.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include<iostream> #include<string> using namespace std; #define False 0 #define True 1 int Max[100][3]={0};//各进程所需各类资源的最大需求 int Avaliable[3]={0};//系统可用资源 char name[100]={0};//资源的名称 int Allocation[100][3]={0};//系统已分配资源 int Need[100][3]={0};//还需要资源 int Request[3]={0};//请求资源向量 int temp[100]={0};//存放安全序列 int Work[3]={0};//存放系统可提供资源 int M=100;//作业的最大数为100 int N=3;//资源数为3 //1.显示资源矩阵 void showdata() { int i,j; cout<<" System is currently available resources [Avaliable]:"<<endl; for(i=0;i<N;i++) cout<<name[i]<<" "; cout<<endl; for (j=0;j<N;j++) cout<<Avaliable[j]<<" ";//输出分配资源 cout<<endl; cout<<" Max Allocation Need"<<endl; cout<<" Process Name "; for(j=0;j<3;j++){ for(i=0;i<N;i++) cout<<name[i]<<" "; cout<<" "; } cout<<endl; for(i=0;i<M;i++){ cout<<" "<<i<<" "; for(j=0;j<N;j++) cout<<Max[i][j]<<" "; cout<<" "; for(j=0;j<N;j++) cout<<Allocation[i][j]<<" "; cout<<" "; for(j=0;j<N;j++) cout<<Need[i][j]<<" "; cout<<endl; } } //2.进行资源分配 int changdata(int i) { int j; for (j=0;j<M;j++) { Avaliable[j]=Avaliable[j]-Request[j]; Allocation[i][j]=Allocation[i][j]+Request[j]; Need[i][j]=Need[i][j]-Request[j]; } return 1; } //2.安全性算法 int safe() { int i,k=0,m,apply,Finish[100]={0}; int j; int flag=0; Work[0]=Avaliable[0]; Work[1]=Avaliable[1]; Work[2]=Avaliable[2]; for(i=0;i<M;i++){ apply=0; for(j=0;j<N;j++){ if (Finish[i]==False&&Need[i][j]<=Work[j]){ apply++; if(apply==N){ for(m=0;m<N;m++) Work[m]=Work[m]+Allocation[i][m];//变分配数 Finish[i]=True; temp[k]=i; i=-1; k++; flag++; } } } } for(i=0;i<M;i++){ if(Finish[i]==False){ cout<<" System is unsafe! "<<endl;//不成功系统不安全 return -1; } } cout<<"System is safe!"<<endl;//如果安全,输出成功 cout<<" The distribution of sequence:"; for(i=0;i<M;i++){//输出运行进程数组 cout<<temp[i]; if(i<M-1) cout<<"->"; } cout<<endl; return 0; } //4利用银行家算法对申请资源对进行判定 void share() { char ch; int i=0,j=0; ch='y'; cout<<" Please enter the requested allocation of resources to process ID (0-"<<M-1<<"):"; cin>>i;//输入须申请的资源号 cout<<"Please enter the resource number to apply for the application of resources "<<i<<endl; for(j=0;j<N;j++) { cout<<name[j]<<":"; cin>>Request[j];//输入需要申请的资源 } for (j=0;j<N;j++){ if(Request[j]>Need[i][j])//判断申请是否大于需求,若大于则出错 { cout<<" The application of resources is greater than the resources it needs to process "<<i<<endl; cout<<" Distribution of irrational, non-distribution!"<<endl; ch='n'; break; } else { if(Request[j]>Avaliable[j])//判断申请是否大于当前资源,若大于则 { //出错 cout<<" the resource is greater than the system can now make use of resources for the process"<<i<<endl; cout<<" Distribution of irrational, non-distribution!"<<endl; ch='n'; break; } } } if(ch=='y') { changdata(i);//根据进程需求量变换资源 showdata();//根据进程需求量显示变换后的资源 safe();//根据进程需求量进行银行家算法判断 } } //添加作业 void addprocess(){ int flag=M; M=M+1; cout<<"请输入该作业的最大需求量[Max]"<<endl; for(int i=0;i<N;i++){ cout<<name[i]<<":"; cin>>Max[flag][i]; Need[flag][i]=Max[flag][i]-Allocation[flag][i]; } showdata(); safe(); } int main()//主函数 { int i,j,number,choice,m,n,flag; char ming; for(i=0;i<N;i++) { cout<<" The name of resources "<<i+1<<":"; cin>>ming; name[i]=ming; cout<<" The amount of resources:"; cin>>number; Avaliable[i]=number; } cout<<endl; cout<<" Please enter the number of operations:"; cin>>m; M=m; cout<<" Please enter the maximum demand of the process ("<<m<<"*"<<n<<"矩阵)[Max]:"<<endl; for(i=0;i<m;i++) for(j=0;j<3;j++) cin>>Max[i][j]; do{ flag=0; cout<<" Please enter the amount of resources the process has applied ("<<m<<"*"<<n<<"矩阵)[Allocation]:"<<endl; for(i=0;i<m;i++) for(j=0;j<3;j++){ cin>>Allocation[i][j]; if(Allocation[i][j]>Max[i][j]) flag=1; Need[i][j]=Max[i][j]-Allocation[i][j]; } if(flag) cout<<" The application of resources is greater than the greatest demand, please re-enter !\n"; } while(flag); showdata();//显示各种资源 safe();//用银行家算法判定系统是否安全 while(choice) { cout<<"************** Banker's algorithm Demo ***************"<<endl; cout<<" 1: Edit resource "<<endl; cout<<" 2: resource allocation "<<endl; cout<<" 3: Increase operational "<<endl; cout<<" 0: End "<<endl; cout<<"*******************************************"<<endl; cout<<" Please select No.:"; cin>>choice; switch(choice) { // case 1: changdata();break; case 1: changdata(number);break; case 2: share();break; case 3: addprocess();break; case 0: choice=0;break; default: cout<<" Please select the correct number from 0 to 3!"<<endl;break; } } return 1; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值