操作系统实验报告.docx
- 文档编号:12712162
- 上传时间:2023-04-21
- 格式:DOCX
- 页数:11
- 大小:77.13KB
操作系统实验报告.docx
《操作系统实验报告.docx》由会员分享,可在线阅读,更多相关《操作系统实验报告.docx(11页珍藏版)》请在冰豆网上搜索。
操作系统实验报告
实验目的
1、增强学生对计算机操作系统基本原理、基本理论、基本算法的理解
2、提高和培养学生的动手能力
银行家算法
实验内容:
利用银行家算法避免死锁
实验要求:
通过编写一个简单的银行家算法程序,加深了解有关资源申请、避免死锁等概念,并体会和了解死锁和避免死锁的具体实施方法。
设计方案:
1)银行家算法中的数据结构:
(1)可利用资源向量Available
(2)最大需求矩阵Max
(3)分配矩阵Allocation
(4)需求矩阵Need
Need[i,j]=Max[i,j]-Allocation[i,j]
2)银行家算法步骤:
(1)如果Requesti<or=Need,则转向步骤
(2);否则,认为出错,因为它所需要的资源数已超过它所宣布的最大值。
(2)如果Request<or=Available,则转向步骤(3);否则,表示系统中尚无足够的资源,进程必须等待。
(3)系统试探把要求的资源分配给进程Pi,并修改下面数据结构中的数值:
Available[j]:
=Available[j]-Request[j];
Allocation[i,j]=Allocation[i,j]+Request[j];
Need[i,j]=Need[i,j]-Request[j];
(4)系统执行安全性算法,检查此次资源分配后,系统是否处于安全状态。
3)安全性算法步骤:
(1)设置两个向量
①工作向量Work。
它表示系统可提供进程继续运行所需要的各类资源数目,执行安全算法开始时,Work=Allocation;
②布尔向量Finish。
它表示系统是否有足够的资源分配给进程,使之运行完成,开始时先做Finish[i]=false,当有足够资源分配给进程时,令Finish[i]=true。
(2)从进程集合中找到一个能满足下述条件的进程:
①Finish[i]=false
②Need 如找到,执行步骤(3);否则,执行步骤(4) (3)当进程P获得资源后,可顺利执行,直至完成,并释放出分配给它的资源,故应执行: Work=Work+Allocation; Finish[i]=true; 转向步骤 (2)。 (4)如果所有进程的Finish[i]=true,则表示系统处于安全状态;否则,系统处于不安全状态。 流程图: 运行结果: 1)初始化 资源 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 2)P1发送请求 P1发出请求向量Request(1,0,2),系统按照银行家算法进行检查: Request(1,0,2) Request(1,0,2) 安全性检查 资源 Work Need Allocation Work+Allocation Finish 进程 A B C A B C A B C A B C P0 2 3 0 0 2 0 3 0 2 5 3 2 true P1 5 3 2 0 1 1 2 1 1 7 4 3 true P2 7 4 3 4 3 1 0 0 2 7 4 5 true P3 7 4 5 7 4 3 0 1 0 7 5 5 true P4 7 5 5 6 0 0 3 0 2 10 5 7 true 根据安全性检查,可以得到一组安全序列{P1,P3,P0,P2,P4},因此系统安全。 3)P4请求资源 Request(3,3,0),同理可得P4必须等待 Request(3,3,0) Request(3,3,0) 4)程序截图 可得实际结果同理论结果相同 源代码: packagebanker; importjava.util.Scanner; publicclassBanker { intavailable[]=newint[]{3,3,2};//可得到的资源 intmax[][]=newint[][]{{7,5,3},{3,2,2},{9,0,2}, {2,2,2},{4,3,3}};//每个进程最大资源数 intallocation[][]=newint[][]{{0,1,0},{2,0,0},{3,0,2}, {2,1,1},{0,0,2}};//每个进程目前拥有的资源数 intneed[][]=newint[][]{{7,4,3},{1,2,2},{6,0,0}, {0,1,1},{4,3,1}};//每个进程需要的资源数 voidshowData()//展示数据输出每个进程的相关数据 { System.out.println("资源情况MaxAllocationNeedAvailable"); System.out.println("进程号ABCABCABCABC"); for(inti=0;i<5;i++) { System.out.print(i+""); for(intm=0;m<3;m++){ System.out.print(max[i][m]+""); } System.out.print(""); for(intm=0;m<3;m++){ System.out.print(allocation[i][m]+""); } System.out.print(""); for(intm=0;m<3;m++){ System.out.print(need[i][m]+""); } if(i==0){ System.out.print(""); for(intm=0;m<3;m++) System.out.print(available[m]+""); } System.out.println(); } } booleanchange(intinRequestNum,intinRequest[])//分配数据 { intrequestNum=inRequestNum; intrequest[]=inRequest; if(! (request[0]<=need[requestNum][0] &&request[1]<=need[requestNum][1]&&request[2]<=need[requestNum][2])) { System.out.println("请求的资源数超过了所需要的最大值,分配错误"); returnfalse; } if((request[0]<=available[0]&&request[1]<=available[1]&&request[2]<=available[2])==false) { System.out.println("尚无足够资源分配,必须等待"); returnfalse; } for(inti=0;i<3;i++)//试分配数据给请求的线程 { available[i]=available[i]-request[i]; allocation[requestNum][i]=allocation[requestNum][i]+request[i]; need[requestNum][i]=need[requestNum][i]-request[i]; } booleanflag=checkSafe(available[0],available[1],available[2]);//进行安全性检查并返回是否安全 if(flag==true) { System.out.println("能够安全分配"); returntrue; } else//不能通过安全性检查恢复到未分配前的数据 { System.out.println("不能够安全分配"); for(inti=0;i<3;i++) { available[i]=available[i]+request[i]; allocation[requestNum][i]=allocation[requestNum][i] -request[i]; need[requestNum][i]=need[requestNum][i]+request[i]; } returnfalse; } } booleancheckSafe(inta,intb,intc)//安全性检查 { intwork[]=newint[3]; work[0]=a; work[1]=b; work[2]=c; inti=0; booleanfinish[]=newboolean[5]; while(i<5)//寻找一个能够满足的认为完成后才去执行下一进程 { if(finish[i]==false&&need[i][0]<=work[0] &&need[i][1]<=work[1]&&need[i][2]<=work[2]) {//找到满足的修改work值,然后i=0,重新从开始的为分配的中寻找 System.out.println("分配成功的是"+i); for(intm=0;m<3;m++) work[m]=work[m]+allocation[i][m]; finish[i]=true; i=0; } else //如果没有找到直接i++ i++; } for(i=0;i<5;i++)//通过finish数组判断是否都可以分配 { if(finish[i]==false) returnfalse; } returntrue; } publicstaticvoidmain(String[]args) { Bankerbank=newBanker(); intrequest[]=newint[3]; intrequestNum; Stringsource[]=newString[]{"A","B","C"}; Scanners=newScanner(System.in); Stringchoice; while(true)//循环进行分配 { bank.showData(); System.out.println("请输入要请求的进程号(0--4): "); requestNum=s.nextInt(); System.out.println("请输入请求的资源数目"); for(inti=0;i<3;i++) { System.out.println(source[i]+"资源的数目: "); request[i]=s.nextInt(); } bank.change(requestNum,request); System.out.println("是否再请求分配(y/n)"); choice=s.next(); if("n".equals(choice)) break; } } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 操作系统 实验 报告
![提示](https://static.bdocx.com/images/bang_tan.gif)