银行家算法问题
来源:互联网 发布:淘宝内部优惠卷网站 编辑:程序博客网 时间:2024/06/11 01:28
—4月28日更新,减少内存需要——–
现代操作系统中,老师给我们布置了银行家算法问题。
源码:
//一维数组的比较,返回boolbool victorCompare(int*& oneNeedted, int* available, int typeCount){ for(int i=0;i<typeCount;i++) { if(oneNeedted[i]>available[i]) { return false; } } return true;}//验证安全时的回收工作void release(int*& oneAllocated, int*& oneNeeded, int* available, int typeCount){ for(int i=0;i<typeCount;i++) { available[i] += oneAllocated[i]; }}//实际进程中的回收工作void realrelease(int*& oneAllocated, int*& oneNeeded, int* available, int typeCount){ for(int i=0;i<typeCount;i++) { available[i] += oneAllocated[i]; oneAllocated[i] = oneNeeded[i] = 0; //对所要请求的资源数置0 }}//检查提出请求是否安全,allocated 已经分配资源的矩阵,available 表示资源的当前可用数,needed 还需要多少资源//processCount 进程数, typeCount 申请的种类数, //二维矩阵allocated, needed = processCount * typeCount;//一维矩阵available = 1×typeCount;bool isSafe(int**& allocated, int**& needed, int*& available, int processCount, int typeCount){ bool* isSafe = new bool[processCount]; //记录判断每次比较时各个进程是否安全 for(int i=0;i<processCount;i++) { isSafe[i] = false; } int index = 0; int i = 0;//记录连续判断为不安全的次数 int safe = 0;//记录已经安全的进程,safe+i==5,则返回false while(i+safe < 5) { isSafe[index] = victorCompare(needed[index], available, typeCount);//判断当前进程是否安全 if(!isSafe[index]) i++; else //安全,回收资源进入下一轮判断 { release(allocated[index], needed[index], available, typeCount); i = 0; safe++; } if(safe==5) { delete[] isSafe; return true; } //寻找下一个进程判断 for(int j=1;j<processCount;j++) { index++; index %= processCount; if(!isSafe[index]) break; } } delete[] isSafe; return false;}//银行家算法void banker(){ int processCount; int typeCount; cout<<"请按照提示输入数据"<<endl; cout<<"请输入进程数x 与资源种类y :"<<endl; cout<<"x y : ";cin>>processCount>>typeCount; int** allocated = new int*[processCount]; int** needed = new int*[processCount]; int* available = new int[typeCount]; int* availableCompute = new int[typeCount]; //对资源可用数进行复制的矩阵 for(int i=0;i<processCount;i++) { allocated[i] = new int[typeCount]; needed[i] = new int[typeCount]; } cout<<"请输入"<<typeCount<<" 种进程的可用数: "; for(int i=0;i<typeCount;i++) { cin>>available[i]; availableCompute[i] = available[i]; } for(int i=0;i<processCount;i++) { cout<<"请输入第"<<i+1<<" 个进程所请求的"<<typeCount<<" 个资源数: "; for(int j=0;j<typeCount;j++) { cin>>needed[i][j]; allocated[i][j] = 0; } } int x; int y; int z; while(true) { cout<<"请输入第x 个进程请求第y 种资源的数量z:"; cin>>x>>y>>z; if(z>available[y-1]||z>needed[x-1][y-1]) { cout<<"不安全,银行家不分配资源"<<endl; continue; } allocated[x-1][y-1] += z; needed[x-1][y-1] -= z; availableCompute[y-1] -= z; if(isSafe(allocated, needed, availableCompute, processCount, typeCount)) { cout<<"请求成功!"<<endl; available[y-1] -= z; } else { allocated[x-1][y-1] -=z; needed[x-1][y-1] += z; cout<<"不安全,银行家不分配资源"<<endl; } bool isend = true; cout<<"请求资源数的矩阵: "<<endl; for(int i=0;i<processCount;i++) { bool iscomplete = true; //判断是否该进程全部获得所需资源数 for(int j=0;j<typeCount;j++) { cout<<needed[i][j]<<" "; if(isend&&needed[i][j]!=0) { isend=false; } if(iscomplete&&needed[i][j]!=0) { iscomplete = false; } } if(iscomplete)//如果该进程全部获得所需资源数,则开始release { realrelease(allocated[i], needed[i], available, typeCount); } cout<<endl; } cout<<"资源可用数的矩阵:"<<endl; for(int i=0;i<typeCount;i++) cout<<available[i]<<" "; cout<<endl; if(isend) break; for(int i=0;i<typeCount;i++) availableCompute[i] = available[i]; } cout<<"所有进程已经运行结束!"<<endl; for(int i=0;i<processCount;i++) { delete[] allocated[i]; delete[] needed[i]; } delete[] available; delete[] availableCompute; delete[] allocated; delete[] needed;}
欢迎拍砖。
0 0
- 银行家算法问题
- 死锁问题及银行家算法
- 银行家算法解决死锁问题
- 银行家算法避免死锁问题
- 银行家算法解决死锁问题
- 数据库死锁问题及银行家算法
- 《操作系统》 死锁问题以及银行家算法解决
- 银行家算法
- 银行家算法
- 银行家算法
- 银行家算法
- 银行家算法
- 银行家算法
- 银行家算法
- 银行家算法
- 银行家算法
- 银行家算法
- 银行家算法
- jstl取纯数字需要加单引号
- C#6.0特性(快来围观)
- Ext JS 5(三)组件
- 设计模式(创建型)之工厂方法模式(Factory Method Pattern)
- Socket实现手机客户端和PC机服务端通信
- 银行家算法问题
- Windows Phone 图片控件
- 新浪微博新增评论带图功能
- 隔行变色
- 北漂人:为什么还要坚守北京
- 冒泡排序总结
- How to Enable EPEL Repository for RHEL/CentOS 7.x/6.x/5.x
- js封装,一个JS文件引用多个JS文件
- MVP模式在Android中的使用