1、亲测银行家和随机算法在一个主函数里#include string.h #include iostreamusing namespace std;#define FALSE 0 #define TRUE 1 #define W 10#define R 20int M ; /总进程数int N ; /资源种类 int ALL_RESOURCEW;/各种资源的数目总和int MAXWR; /M个进程对N类资源最大资源需求量int AVAILABLER; /系统可用资源数int ALLOCATIONWR; /M个进程已经得到N类资源的资源量int NEEDWR; /M个进程还需要N类资源的资源量int
2、 RequestR; /请求资源个数void showdata() /函数showdata,输出资源分配情况 int i,j; cout各种资源的总数量(all):endl; cout ; for (j=0;jN;j+)cout 资源j: ALL_RESOURCEj; coutendlendl; cout系统目前各种资源可用的数为(available):endl; cout ; for (j=0;jN;j+)cout 资源j: AVAILABLEj; coutendlendl; cout 各进程还需要的资源量(need):endlendl; cout 资源 资源 资源endl;for (i=0
3、;iM;i+) for (i=0;iM;i+) cout进程pi: ; for (j=0;jN;j+)coutNEEDij ; coutendl; coutendl; cout 各进程已经得到的资源量(allocation): endlendl; cout 资源 资源 资源endl;for (i=0;iM;i+) cout进程pi: ; for (j=0;jN;j+)coutALLOCATIONij ; coutendl; coutendl; ; void changdata(int k) /函数changdata,改变可用资源和已经拿到资源和还需要的资源的值 int j; for (j=0;
4、jN;j+) AVAILABLEj=AVAILABLEj-Requestj; ALLOCATIONkj=ALLOCATIONkj+Requestj; NEEDkj=NEEDkj-Requestj; ; void rstordata(int k) /函数rstordata,恢复可用资源和已经拿到资源和还需要的资源的值 int j; for (j=0;jN;j+) AVAILABLEj=AVAILABLEj+Requestj; ALLOCATIONkj=ALLOCATIONkj-Requestj; NEEDkj=NEEDkj+Requestj; ; int chkerr(int s) /函数chk
5、err,检查是否安全 int WORK,FINISHW,tempR; int i,j,k=0; for(i=0;iM;i+)FINISHi=FALSE; for(j=0;jN;j+) WORK=AVAILABLEj; i=s; while(iM) if (FINISHi=FALSE&NEEDij=WORK) WORK=WORK+ALLOCATIONij; FINISHi=TRUE; tempk=i; k+; i=0; else i+; for(i=0;iM;i+) if(FINISHi=FALSE) coutendl; cout 系统不安全! 本次资源申请不成功!endl; coutendl;
6、 return 1; coutendl; cout 经安全性检查,系统安全,本次分配成功。endl; coutendl; cout 本次安全序列:; for(i=0;iM;i+)coutptempi; coutendlendl;return 0; int chkerr2(int s) /函数chkerr2,检查是否安全 int WORK,FINISHW,tempR; int i,j,k=0; for(i=0;iM;i+)FINISHi=FALSE; for(j=0;jN;j+) WORK=AVAILABLEj; i=s; while(iM) if (FINISHi=FALSE&NEEDij=W
7、ORK) WORK=WORK+ALLOCATIONij; FINISHi=TRUE; tempk=i; k+; i=0; else i+; for(i=0;iM;i+) if(FINISHi=FALSE) coutendl; cout 本次资源申请成功!但系统不安全! endl; coutendl; return 1; coutendl; cout 经安全性检查,系统安全,本次分配成功。endl; coutendl; cout 本次安全序列:; for(i=0;iM;i+)coutptempi; coutendlendl;return 0; void bank() /银行家算法 int i=0
8、,j=0; char flag=Y; while(flag=Y|flag=y) i=-1; while(i=M) cout 请输入需申请资源的进程号(从P0到PM-1,否则重输入!):; couti; if(i=M)cout 输入的进程号不存在,重新输入!endl; cout 请输入进程Pi申请的资源数:endl; for (j=0;jN;j+) cout 资源jRequestj; if(RequestjNEEDij) cout 进程Pi申请的资源数大于进程Pi还需要j类资源的资源量!; cout申请不合理,出错!请重新选择!endlAVAILABLEj) cout 进程Pi申请的资源数大于系
9、统可用j类资源的资源量!; cout申请不合理,出错!请重新选择!endlendl; flag=N; break; if(flag=Y|flag=y) changdata(i); if(chkerr(i) rstordata(i); showdata(); else showdata(); else showdata(); coutendl; coutflag; void randomallocation() /随机分配算法int i=0,j=0; char flag=Y; while(flag=Y|flag=y) i=-1; while(i=M) cout 请输入需申请资源的进程号(从P0到
10、PM-1,否则重输入!):; couti; if(i=M)cout 输入的进程号不存在,重新输入!endl; cout 请输入进程Pi申请的资源数:endl; for (j=0;jN;j+) cout 资源jRequestj; if(RequestjAVAILABLEj) cout 进程Pi申请的资源数大于系统可用j类资源的资源量!; cout申请不合理,出错!请重新选择!endlendl; flag=N; break; if(flag=Y|flag=y) changdata(i); showdata(); chkerr2(i); coutflag; void main() /主函数 cout
11、endl;cout 欢迎使用本系统! endl;cout endl;cout=endl;int i=0,j=0,p; cout输入总进程数:M;cout输入总资源种类:N;cout请输入各类总资源数(all_resource):endl;for(i=0;iALL_RESOURCEi;cout依次输入各进程所需要的最大资源数量(max):endl;for (i=0;iM;i+) for (j=0;jMAXij; if (MAXijALL_RESOURCEj) coutendl占有资源超过了声明的该资源总数,请重新输入ALL_RESOURCEj); cout依次输入各进程已经占据的资源数量(all
12、ocation):endl;for (i=0;iM;i+) for (j=0;jALLOCATIONij; if (ALLOCATIONijMAXij) coutendl占有资源超过了声明的最大资源,请重新输入MAXij); /初始化资源数量 for (i=0;iN;i+) p=ALL_RESOURCEi; for (j=0;jM;j+) p-=ALLOCATIONji;/减去已经被占据的资源 AVAILABLEi=p; if(AVAILABLEi0) AVAILABLEi=0; for (i=0;iM;i+) for(j=0;jN;j+) NEEDij=MAXij-ALLOCATIONij;showdata();docoutendl1-随机分配算法 2-银行家算法endl;coutendli;if(i=2) /m=2表赋值,m=2表判断bank();else randomallocation();while(!(i=1)|(i=2);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1