操作系统
来源:互联网 发布:淘宝达人直播的入口 编辑:程序博客网 时间:2024/06/06 02:47
/* 实验以3.7.4为例 N=5个进程(p0,p1,p2,p3,p4) M=3类资源(A=10,B=5,C=7) 在T0时刻资源分配如下 Max Allocation Need Available A B C A B C A B C A B C p0 7 5 3 0 1 0 7 4 3 3 3 2 p1 3 2 2 2 0 0 1 2 2 p2 9 0 2 3 0 2 6 0 0 p3 2 2 2 2 1 1 0 1 1 p4 4 3 3 0 0 2 4 3 1*/#include <iostream>using namespace std;#define N 5//进程数#define M 3//资源数int Available[M]={3,3,2};//可用资源向量//最大需求矩阵int Max[N][M]={ {7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3} };//分配矩阵int Allocation[N][M]={ {0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2} };//需求矩阵int Need[N][M] = { {7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1} };//请求向量int Request[M];bool savety(int allocation[N][M],int need[N][M],int ava[M]);int check(int a,int request[]);void execute(int a,int request[M]);void print();void result(int res);int main(){ //安全性检测 int allocation[N][M]; int need[N][M]; int ava[M]; for(int i=0;i<M;i++) ava[i]=Available[i]; for(int i=0;i<N;i++){ for(int j=0;j<M;j++){ allocation[i][j]=Allocation[i][j]; need[i][j]=Need[i][j]; } } if(savety(allocation,need,ava)) { cout<<"系统目前处于安全状态"<<endl<<endl; //p1发出请求向量 Request[0]=1,Request[1]=0,Request[2]=2; cout<<"P1发出请求向量Request(1,0,2)"<<endl; int res=check(1,Request); result(res); cout<<endl<<endl; //p4发出请求向量 Request[0]=3,Request[1]=3,Request[2]=0; cout<<"P4发出请求向量Request(3,3,0)"<<endl; res=check(4,Request); result(res); cout<<endl<<endl; //p0发出请求向量 Request[0]=0,Request[1]=2,Request[2]=0; cout<<"P0发出请求向量Request(0,2,0)"<<endl; res=check(0,Request); result(res); }}//处理结果void result(int res){ if(res==1){ cout<<"系统是安全的,同意请求"<<endl; //执行 execute(1,Request); //打印 print(); } else if(res==3) { cout<<"分配后系统是不安全的,拒绝请求!"<<endl; } else if(res==2) { cout<<"系统资源不足以分配,请等待"<<endl; }}bool savety(int allocation[N][M],int need[N][M],int ava[M]){ int count=0; int count2=0; //安全序列 int SaveList[N]={-1,-1,-1,-1,-1}; cout<<" Work Need Allocation Work+Allocation Finish"<<endl; cout<<" A B C A B C A B C A B C"<<endl; //虚拟分配 while(count<5&&count2<5){ for(int i=0;i<N;i++){ //排除已经执行完的进程 if(SaveList[0]==i)continue; if(SaveList[1]==i)continue; if(SaveList[2]==i)continue; if(SaveList[3]==i)continue; if(SaveList[4]==i)continue; //寻找可以执行完毕的进程 if(need[i][0]<=ava[0]&&need[i][1]<=ava[1]&&need[i][2]<=ava[2]){ SaveList[count++]=i; //回收资源 for(int j=0;j<M;j++) ava[j]=ava[j]+allocation[i][j]; cout<<"p"<<i<<" "<<ava[0]<<" "<<ava[1]<<" "<<ava[2]<<" "; cout<<need[i][0]<<" "<<need[i][1]<<" "<<need[i][2]<<" "; cout<<allocation[i][0]<<" "<<allocation[i][1]<<" "<<allocation[i][2]<<" "; cout<<ava[0]+allocation[i][0]<<" "<<ava[1]+allocation[i][1]<<" "<<ava[2]+allocation[i][2]<<" true"<<endl; } } count2++; } if(count==5){ cout<<"存在安全序列:"; for(int i=0;i<N;i++) cout<<SaveList[i]<<" "; cout<<endl; return true; } else { cout<<"不存在安全序列 "<<endl; return false; }}//a为线程号,request为请求表,返回结果:1.通过 2,等待 3.不通过int check(int a,int request[]){ //检查是否小于需求 if(!(request[0]<=Need[a][0]&&request[1]<=Need[a][1]&&request[2]<=Need[a][2])){ return 3; } //检查是否小于可用资源 if(!(request[0]<=Available[0]&&request[1]<=Available[1]&&request[2]<=Available[2])){ return 2; } //试探分配 int ava[M]; for(int i=0;i<M;i++)ava[i]=Available[i]; int allocation[N][M]; int need[N][M]; for(int i=0;i<N;i++){ for(int j=0;j<M;j++){ allocation[i][j]=Allocation[i][j]; need[i][j]=Need[i][j]; } } //分配并修改 for(int i=0;i<M;i++){ ava[i]-=request[i]; need[a][i]-=request[i]; allocation[a][i]+=request[i]; } //安全性检查 if(savety(allocation,need,ava)) return 1; else return 3;}//执行函数,a为修改进程号,request为请求表void execute(int a,int request[M]){ for(int i=0;i<M;i++){ Allocation[a][i]+=request[i]; Need[a][i]-=request[i]; Available[i]-=request[i]; }}void print(){ cout<<"执行后:"<<endl; cout<<" MAX Allocation Need"<<endl; cout<<" A B C A B C A B C "<<endl; for(int i=0;i<N;i++) cout<<"p"<<i<<" "<<Max[i][0]<<" "<<Max[i][1]<<" "<<Max[i][2]<<" "<<Allocation[i][0]<<" "<<Allocation[i][1]<<" "<<Allocation[i][2]<<" "<<Need[i][0]<<" "<<Need[i][1]<<" "<<Need[i][2]<<endl; cout<<"Available:A("<<Available[0]<<") B("<<Available[1]<<") C("<<Available[2]<<")"<<endl;}
阅读全文
0 0
- 操作系统~
- 操作系统
- 操作系统
- 操作系统
- 操作系统
- 操作系统
- 操作系统
- 操作系统
- 操作系统
- 操作系统
- 操作系统
- 操作系统
- 操作系统
- 操作系统
- 操作系统
- 操作系统
- 操作系统
- 操作系统
- 三数之和
- java语言基础(67)——集合框架之数据结构
- 短地址(ShortUrl)实例
- JS学习笔记(二)
- C++跨文件共享全局变量
- 操作系统
- pyenv & pyenv-virtualenv
- 欢迎使用CSDN-markdown编辑器
- mac 安装 golang 1.8
- docker学习
- ReetrantLock源码解析(一):获得公平锁和非公平锁lock()
- Apple Mach-O Linker Error 一个粗心造成的问题
- 《Effective Java》 第二讲:对于所有对象都通用的方法
- Kotlin详解:第一章,基本语法