《银行家算法与安全检测》实验报告.docx
- 文档编号:3812825
- 上传时间:2023-05-06
- 格式:DOCX
- 页数:14
- 大小:245.93KB
《银行家算法与安全检测》实验报告.docx
《《银行家算法与安全检测》实验报告.docx》由会员分享,可在线阅读,更多相关《《银行家算法与安全检测》实验报告.docx(14页珍藏版)》请在冰点文库上搜索。
《银行家算法与安全检测》实验报告
0838110205
胡乘龙
08计算机科学与技术2班
专业班级
姓名
学号
《银行家算法与安全检测》实验报告
一、实验名称
银行家算法与安全检测实验
二、实验目的
1.了解进程产生死锁原因,了解为什么要避免死锁。
2.掌握银行家算法的数据结构,了解算法的执行过程,加深对银行家算法的理解。
三、实验内容
采用银行家算法来实现一个n个并发进程共享m个系统资源的系统。
进程可以申请和释放资源,系统可以按照各进程的申请计算是否可以分配给其资源。
四、实验环境
1、WindowsXP/7
2、Java1.6.0_14
3、UltraEdit
五、实验思路
系统中的所有进程放入进程集合,在安全状态下系统受到进程的请求后试探性的把资源分配给他,现在系统将剩下的资源和进程集合中其他进程还需要的资源数做比较,找出剩余资源能满足最大需求量的进程,从而保证进程运行完成后还回全部资源。
这时系统将该进程从进程集合中将其清除。
此时系统中的资源就更多了。
反复执行上面的步骤,最后检查进程的集合为空时就表明本次申请可行,系统处于安全状态,可以实施本次分配,否则,只要进程集合非空,系统便处于不安全状态,本次不能分配给他。
请进程等待。
六、实验数据结构
1.可利用资源向量AVAILABLE。
这是一个含有M个元素的数组,其中的每一个元素代表一类可利用的资源数目,其3初始值是系统中所配置的该类全部可哦那个资源的数目,其数值随该类资源的分配和回收而动态的改变。
2.最大需求矩阵MAX。
这是一个M*N的矩阵,它定义了系统中N个进程中的每一个进程对M类资源的最大需求。
3.分配矩阵ALLOCATION。
这也是一个M*N的矩阵,它定义了系统中每一类资源当前已分配给每一进程的资源数。
4.需求矩阵NEED。
这也是一个M*N的矩阵,用以表示每一个进程尚需的各类资源数。
5.NEED[R,W]=MAX[R,W]-ALLOCATION[R,W]
七、程序流程图
八、实验结果
九、源代码
importjava.io.*;
publicclassanquan{
publicstaticvoidmain(Stringargs[]){
Testts=newTest();
ts.Putout();
System.out.println();
for(;;){if(ts.Safe()==1){
ts.Safe2();
ts.Show();
ts.Request();}}}}
classTest{finalintP=10;//最大进程数
finalintR=5;//最大资源种类数
inti,j,dd,tt;
intAvailable[];//可利用资源向量
intMax[][];//最大需求矩阵
intAllocation[][];//分配矩阵
intNeed[][];//需求矩阵
intRequest1[];//进程请求向量
intAllcount[];//各资源总数
intC[];//安全序列
publicvoidPutout(){try{do{System.out.print("请输入进程数=");
BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
Strings=br.readLine();
i=Integer.parseInt(s);
if(i<=0||i>P){System.out.println("输入错误!
请输入10以内的整数!
");}}while(i<=0||i>P);}catch(IOExceptione){}
System.out.println();try{do{
System.out.print("请输入资源种类数=");
BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
Strings=br.readLine();
j=Integer.parseInt(s);
if(j<=0||j>R){System.out.println("输入错误!
请输入5以内的整数!
");}}while(j<=0||j>R);}catch(IOExceptione){}
System.out.println();
System.out.println("请依次输入各种资源总数量:
");
System.out.println();
Allcount=newint[j];
for(intt=0;t "); BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in)); Strings=br.readLine(); Allcount[t]=Integer.parseInt(s); System.out.println();}catch(IOExceptione){}} System.out.println("请依次输入各进程所需要最大资源数量,即设置最大需求矩阵Max: "); System.out.println(); Max=newint[i][j]; for(intt=0;t "); System.out.println(); for(intt1=0;t1 try{do{System.out.print("资源"+t1+": "); BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in)); Strings=br.readLine(); Max[t][t1]=Integer.parseInt(s); if(Max[t][t1]>Allcount[t1]){ System.out.println("输入的最大资源数超过了声明的该资源总数! 请重新输入! ");} System.out.println(); }while(Max[t][t1]>Allcount[t1]); }catch(IOExceptione){}}} System.out.println();System.out.println("请依次输入各进程已经占据各类资源数量,即设置分配矩阵Allocation: "); System.out.println(); Allocation=newint[i][j]; for(intt=0;t "); System.out.println(); for(intt1=0;t1 try{do{System.out.print("资源"+t1+": "); BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in)); Strings=br.readLine(); Allocation[t][t1]=Integer.parseInt(s); if(Allocation[t][t1]>Max[t][t1]){ System.out.println("输入的已占有资源数超过了声明的最大资源数! 请重新输入! ");} System.out.println();}while(Allocation[t][t1]>Max[t][t1]);}catch(IOExceptione){}}} Need=newint[i][j]; for(intt=0;t for(intt1=0;t1 Need[t][t1]=Max[t][t1]-Allocation[t][t1]; Available=newint[j]; for(intt1=0;t1 {intp1=Allcount[t1]; for(intt=0;t {p1=p1-Allocation[t][t1]; Available[t1]=p1; if(Available[t1]<0) Available[t1]=0;}}} publicintSafe()//安全算法 {intWork[]=newint[j]; for(intt=0;t {Work[t]=Available[t];} intFinish[]=newint[i]; for(intt=0;t {Finish[t]=0;} intC[]=newint[i]; intaa=0; for(intk=0;k {for(intt1=0;t1 {intcount1=0; if(Finish[t1]==0) {for(intt2=0;t2 {if(Need[t1][t2]>Work[t2]) {break;} else{count1=count1+1;}} if(count1==j) {Finish[t1]=1; for(intt3=0;t3 Work[t3]=Work[t3]+Allocation[t1][t3]; aa++;}}}} if(aa==i){return1;} else{return0;}} publicvoidSafe2()//安全序列输出 {intWork[]=newint[j]; for(intt=0;t {Work[t]=Available[t];} intFinish[]=newint[i]; for(intt=0;t {Finish[t]=0;} intC[]=newint[i]; inta=0; for(intk=0;k {for(intt1=0;t1 {intcount1=0; if(Finish[t1]==0) {for(intt2=0;t2 {if(Need[t1][t2]>Work[t2]) {break;} else {count1=count1+1;}} if(count1==j) {Finish[t1]=1; for(intt3=0;t3 Work[t3]=Work[t3]+Allocation[t1][t3]; C[a++]=t1;}}}} if(a==i) {System.out.println(); System.out.print("系统安全,存在安全序列: "); for(intt1=0;t1 {System.out.print("P"+C[t1]+"");} System.out.println();}else {System.out.println("系统不安全"); System.out.println();}} publicvoidShow() {System.out.println(); System.out.println("当前各种资源的总数量为: "); for(intt=0;t System.out.print(""+"资源"+t+": "+Allcount[t]+""); System.out.println(); System.out.println(); System.out.println("当前系统中各种资源可用数量,即Available为: "); for(intt=0;t System.out.print(""+"资源"+t+": "+Available[t]+""); System.out.println(); System.out.println(); System.out.println("资源"+"Max"+"Allocation"+"Need"); System.out.println("进程"); System.out.println("===================================================="); for(intt=0;t {System.out.print("P"+t+": "); for(intt1=0;t1 {System.out.print(Max[t][t1]+"");} System.out.print(""); for(intt1=0;t1 {System.out.print(Allocation[t][t1]+"");} System.out.print(""); for(intt1=0;t1 {System.out.print(Need[t][t1]+"");} System.out.println();} System.out.println("===================================================="); System.out.println();} publicvoidWait(intss) {intsss=ss; System.out.println("进程P"+sss+"等待,返回");} publicvoidRequest() {System.out.println("是否需要请求资源: 是 (1)or否(0)"); do {try{BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in)); tt=Integer.parseInt(br.readLine()); if(tt<0||tt>1) {System.out.println("输入错误,重新输入: ");}}catch(IOExceptione){}}while(tt<0||tt>1); if(tt==1) {intRequest1[]=newint[j]; System.out.print("请输入需要修改的进程P="); do {try{BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in)); dd=Integer.parseInt(br.readLine()); if(dd<0||dd>i) {System.out.println("输入错误,重新输入: ");} }catch(IOExceptione){} }while(dd<0||dd>i); System.out.println("请输入依次输入请求资源数量: "); intcount2=0; for(intt1=0;t1 {System.out.print("资源"+t1+"="); do {try{BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in)); Request1[t1]=Integer.parseInt(br.readLine()); if(Request1[t1]>Need[dd][t1]) {System.out.print("输入错误,重新输入: ");} else {if(Request1[t1]>Available[t1]) {count2=count2+1;}}}catch(IOExceptione){} }while(Request1[t1]>Need[dd][t1]); if(count2==0){Available[t1]=Available[t1]-Request1[t1]; Allocation[dd][t1]=Allocation[dd][t1]+Request1[t1]; Need[dd][t1]=Need[dd][t1]-Request1[t1];} }if(count2>0){Wait(dd);} if(count2==0){if(Safe()==0){ System.out.print("不予分配资源,返回"); System.out.println(""); for(intt1=0;t1 Allocation[dd][t1]=Allocation[dd][t1]-Request1[t1]; Need[dd][t1]=Need[dd][t1]+Request1[t1];}}}} if(tt==0){System.out.println("Bye"); System.exit(0);}}}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 银行家算法与安全检测 银行家 算法 安全 检测 实验 报告