实验二银行家算法.docx
- 文档编号:24167105
- 上传时间:2023-05-24
- 格式:DOCX
- 页数:36
- 大小:24.14KB
实验二银行家算法.docx
《实验二银行家算法.docx》由会员分享,可在线阅读,更多相关《实验二银行家算法.docx(36页珍藏版)》请在冰豆网上搜索。
实验二银行家算法
实验二银行家算法
一、实验目的
死锁会引起计算机工作僵死,因此操作系统中必须防止。
本实验的目的在于让学生独立的使用高级语言编写和调试一个系统动态分配资源的简单模拟程序,了解死锁产生的条件和原因,并采用银行家算法有效地防止死锁的发生,以加深对课堂上所讲授的知识的理解。
二、实验要求
设计有n个进程共享m个系统资源的系统,进程可动态的申请和释放资源,系统按各进程的申请动态的分配资源。
系统能显示各个进程申请和释放资源,以及系统动态分配资源的过程,便于用户观察和分析;
三、数据结构
1.可利用资源向量Available,它是一个含有m个元素的数组,其中的每一个元素代表一类可利用的资源的数目,其初始值是系统中所配置的该类全部可用资源数目。
其数值随该类资源的分配和回收而动态地改变。
如果Available(j)=k,标是系统中现有Rj类资源k个。
2.最大需求矩阵Max,这是一个n×m的矩阵,它定义了系统中n个进程中的每一个进程对m类资源的最大需求。
如果Max(i,j)=k,表示进程i需要Rj类资源的最大数目为k。
3.分配矩阵Allocation,这是一个n×m的矩阵,它定义了系统中的每类资源当前一分配到每一个进程的资源数。
如果Allocation(i,j)=k,表示进程i当前已经分到Rj类资源的数目为k。
Allocationi表示进程i的分配向量,有矩阵Allocation的第i行构成。
4.需求矩阵Need,这是一个n×m的矩阵,用以表示每个进程还需要的各类资源的数目。
如果Need(i,j)=k,表示进程i还需要Rj类资源k个,才能完成其任务。
Needi表示进程i的需求向量,由矩阵Need的第i行构成。
上述三个矩阵间存在关系:
Need(i,j)=Max(i,j)-Allocation(i,j);
四、银行家算法
参考教材P96
五、安全性算法
1.设置两个向量。
Work:
它表示系统可提供给进程继续运行的各类资源数目,它包含m个元素,开始执行安全性算法时,Work=Available。
Finish:
它表示系统是否有足够的资源分配给进程,使之运行完成,开始Finish(I)=false;当有足够资源分配给进程Pi时,令Finish(i)=true;
2.从进程集合中找到一个能满足下述条件的进程。
Finish(i)==false;
Needi≤work;
如找到则执行步骤3;否则,执行步骤4;
3.当进程Pi获得资源后,可顺利执行直到完成,并释放出分配给它的资源,故应执行
Work=work+Allocationi
Finish(i)=true;转向步骤2;
4.若所有进程的Finish(i)都为true,则表示系统处于安全状态;否则,系统处于不安全状态。
六、流程图
七、源程序代码
1.银行家算法类
packageBankerAlgorithm;
publicclassBanker{
privateint[]Available;//可利用资源向量
privateint[]AllSource;//总资源数
privateint[][]Max;//最大需求矩阵
privateint[][]Allocation;//已分配资源矩阵
privateint[][]Need;//需求矩阵
privateint[]Work;//工作向量
privateint[]Request;//资源请求向量
privateint[]Temp;//交换向量
//privateboolean[]Finish;//判断进程是否可以得到资源
privateintprocessCount;//进程数
privateintsourceCount;//资源数
privateStringsafeList=newString();
privateintcount;
privateString[][]dataStrings;
privateintflag;
publicvoidinitBanker(intprocessCount,intsourceCount){
this.processCount=processCount;
this.sourceCount=sourceCount;
this.Max=newint[processCount][sourceCount];
this.Allocation=newint[processCount][sourceCount];
this.Need=newint[processCount][sourceCount];
this.Work=newint[sourceCount];
this.Request=newint[sourceCount];
//this.Finish=newboolean[processCount];
this.dataStrings=newString[processCount][6];
this.Temp=newint[processCount];
for(inti=0;i for(intj=0;j<6;j++){ dataStrings[i][j]=newString(); } } } publicvoidinitMaxAllocationNeed(int[][]max,int[][]allocation,int[][]need,introw){//初始化其余矩阵 for(inti=0;i this.Max[row-1][i]=max[row-1][i]; this.Allocation[row-1][i]=allocation[row-1][i]; this.Need[row-1][i]=need[row-1][i]; } } publicvoidinitAvailable(intsourceCount,int[]available){//初始化可利用资源矩阵 this.Available=newint[sourceCount]; for(inti=0;i this.Available[i]=available[i]; } } publicvoidinitAllSource(intsourceCount,int[]available){//初始化资源总数矩阵 this.AllSource=newint[sourceCount]; for(inti=0;i this.AllSource[i]=available[i]; } } publicvoidinitTemp(int[]temp,introw){//初始化临时矩阵 this.Temp[row-1]=temp[row-1]; } publicvoidinitRequest(int[]request){//初始化请求矩阵 this.Request=newint[sourceCount]; for(inti=0;i this.Request[i]=request[i]; } } publicbooleanisEnoughAll(int[]max){//检查最大资源矩阵是否小于资源总数 for(inti=0;i if(max[i]>AllSource[i]){ returnfalse; } } returntrue; } publicbooleanisMaxAllocation(int[]max,int[]allocation){//检查已分配资源数是否小于最大资源数 for(inti=0;i if(allocation[i]>max[i]){ returnfalse; } } returntrue; } publicbooleanisEnoughNeed(introw){//工作向量是否满足需求矩阵 for(inti=0;i if(Need[row][i]>Work[i]){ this.flag=0; returnfalse; } } this.flag=1; returntrue; } publicbooleanisEnoughReq(introw){//请求矩阵是否满足需求矩阵 for(inti=0;i if(Need[row][i] this.flag=0; returnfalse; } } this.flag=1; returntrue; } publicbooleanisEnoughAva(introw){//请求矩阵是否满足需求矩阵 for(inti=0;i if(Request[i]>Available[i]){ this.flag=0; returnfalse; } } this.flag=1; returntrue; } publicbooleanisAvailable(int[]available){ for(inti=0;i if(available[i]<0){ returnfalse; } } returntrue; } publicbooleancheckNeed(int[]need){//检查需求矩阵中资源是否都为0 for(inti=0;i if(need[i]! =0){ returnfalse; } } returntrue; } publicbooleanisSafe(){//检查是否安全 inti; for(i=0;i if(isEnoughNeed(Temp[i]-1)){ CountWork(Temp[i]-1); continue; } elsebreak; } if(i==processCount)returntrue; elsereturnfalse; } publicvoidCountWork(introw){//计算工作向量 for(inti=0;i Work[i]=Work[i]+Allocation[row][i]; } } publicvoidgetSafeList(intk,intn){//递归求安全序列 intm,a; if(k==n){ for(intj=0;j Work[j]=Available[j]; if(isSafe()){ for(a=0;a safeList+=Temp[a]+"->"; safeList+=Temp[a]+""+"\n"; count++; for(intj=0;j Work[j]=Available[j]; } }else{ for(inti=k;i<=n;i++){//排列 m=Temp[k]; Temp[k]=Temp[i]; Temp[i]=m; getSafeList(k+1,n); m=Temp[k]; Temp[k]=Temp[i]; Temp[i]=m; } } } publicvoidchangeDataString(){//设置数据 for(inti=0;i Strings_available=newString(); Strings_allocation=newString(); Strings_need=newString(); Strings_max=newString(); for(intj=0;j s_available+=this.Available[j]+""; s_allocation+=this.Allocation[i][j]+""; s_need+=this.Need[i][j]+""; s_max+=this.Max[i][j]+""; } this.dataStrings[i][1]=i+1+""; this.dataStrings[i][2]=s_max; this.dataStrings[i][3]=s_allocation; this.dataStrings[i][4]=s_need; this.dataStrings[0][5]=s_available; } } publicvoidallocateSource(introw){ for(inti=0;i if(checkNeed(Need[i])){ for(intj=0;j this.Available[j]+=this.Allocation[i][j]; this.Allocation[i][j]=0; this.Max[i][j]=0; } } } for(inti=0;i this.Available[i]=this.Available[i]-this.Request[i]; this.Allocation[row-1][i]=this.Allocation[row-1][i]+this.Request[i]; this.Need[row-1][i]=this.Need[row-1][i]-this.Request[i]; } changeDataString(); } publicbooleanrequestSource(introw){ count=0; if(isEnoughReq(row-1)&&isEnoughAva(row-1)){ allocateSource(row); returntrue; }else{ returnfalse; } } publicint[]getAvailable(){ returnAvailable; } publicvoidsetAvailable(int[]available){ Available=available; } publicStringgetSafeList(){ returnsafeList; } publicvoidsetSafeList(StringsafeList){ this.safeList=safeList; } publicintgetCount(){ returncount; } publicvoidsetCount(intcount){ this.count=count; } publicString[][]getDataStrings(){ returndataStrings; } publicvoidsetDataStrings(String[][]dataStrings){ this.dataStrings=dataStrings; } publicintgetFlag(){ returnflag; } publicvoidsetFlag(intflag){ this.flag=flag; } } 2.界面类 packageBankerAlgorithm; importorg.eclipse.swt.widgets.Display; importorg.eclipse.swt.widgets.Shell; importorg.eclipse.swt.SWT; importorg.eclipse.swt.widgets.Label; importorg.eclipse.swt.widgets.MessageBox; importorg.eclipse.swt.widgets.TableItem; importorg.eclipse.swt.widgets.Text; importorg.eclipse.swt.widgets.Button; importorg.eclipse.swt.widgets.Table; importorg.eclipse.swt.widgets.TableColumn; importorg.eclipse.swt.events.SelectionAdapter; importorg.eclipse.swt.events.SelectionEvent; importorg.eclipse.swt.custom.ScrolledComposite; importorg.eclipse.wb.swt.SWTResourceManager; publicclassBankerSWT{ protectedShellshell; privateTextsourceCountText; privateTextproCountText; privateTextcountText; privateText[]sourceTexts=newText[10]; privateText[]maxTexts=newText[10]; privateText[]allocationTexts=newText[10]; privateText[]requestTexts=newText[10]; privateButtonbutton; privateLabellabel_2; privateButtonbutton_1; privateLabellabel_3; privateLabellabel_4; privateButtonbutton_2; privateLabellblNewLabel; privateTabletable; privateTextrequsetText; privateString[][]dataStrings; privateTextsecurityText; privateintprocessCount; privateintsourceCount; privateintrow; privateBankerbanker=newBanker(); /** *Launchtheapplication. *@paramargs */ publicstaticvoidmain(String[]args){ try{ BankerSWTwindow=newBankerSWT(); window.open(); }catch(Exceptione){ e.printStackTrace(); } } /** *Openthewindow. */ publicvoidopen(){ Displaydisplay=Display.getDefault(); createContents(); shell.open(); shell.layout(); while(! shell.isDisposed()){ if(! display.readAndDispatch()){ display.sleep(); } } } /** *Createcontentsofthewindow. */ protectedvoidcreateContents(){ shell=newShell(); shell.setSize(550,475); shell.setText("银行家算法"); Labellabel=newLabel(shell,SWT.NONE); label.setBounds(10,10,84,17); label.setText("\u8BF7\u8F93\u5165\u8D44\u6E90\u6570\uFF1A"); sourceCountText=newText(shell,SWT.BORDER); sourceCountText.setBounds(100,10,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验二 银行家算法 实验 银行家 算法
![提示](https://static.bdocx.com/images/bang_tan.gif)