操作系统银行家算法的实现文档格式.docx
- 文档编号:16965928
- 上传时间:2022-11-27
- 格式:DOCX
- 页数:11
- 大小:170.13KB
操作系统银行家算法的实现文档格式.docx
《操作系统银行家算法的实现文档格式.docx》由会员分享,可在线阅读,更多相关《操作系统银行家算法的实现文档格式.docx(11页珍藏版)》请在冰豆网上搜索。
(一)任务分析:
实现银行家算法,首先需要构造四张链表表,如下:
进程最大需求数量表:
Max[m][n]
进程以获取资源量表Allocation[m][n]
需求表Need[m][n]
可用资资源Available[n]
其中,m表示进程数目。
n表示资源数目。
对于银行家算的实现,我们可以先初始化一部分数据,模拟出某一状态下的资源分配情况。
并发出资源请求,然后判断请求是否可行,并寻找安全序列。
可以看出,本次试验会设计到大量的数据,所以为了简化步骤,并且能直观的得到算法运行结果,需要用到窗口来呈现数据变化情况。
(二)程序设计:
(1)总体设计:
本次试验语言为java。
程序分两大部分,一部分是核心的银行家算法,用来处理资源请求。
另一部分是界面,用来发出资源请求并显示处理结果。
利用java的swing编程和相关IDE,很容易初始化资源分布情况,下面是其截图:
(2)具体实现:
核心部分,银行家算法:
算法的实现完全按照教材中的步骤来进行,具体实现如下:
/**
*
*@paramprocessID进程号
*@paramra请求A类资源数量
*@paramrb请求A类资源数量
*@paramrc请求C类资源数量
*/
publicstaticList<
Integer>
checkEnable(intprocessID,intra,intrb,intrc){
//得等到该进程对各类资源的需求量数组
Integerneed[]=Need.get(processID);
//得等到该进程的各类资源的就绪量数组
Integerallocation[]=Allocation.get(processID);
//检测请求数量是否大于需求量
if(ra>
need[0]||rb>
need[1]||rc>
need[2]){
returnnull;
}
//检测请求量是否大于可用量
Available[0]||rb>
Available[1]||rc>
Available[2]){
//先根据需求修改各类数据,如果后来检测到不存在安全序列,这些数据还会复原
Available[0]-=ra;
Available[1]-=rb;
Available[2]-=rc;
need[0]-=ra;
need[1]-=rb;
need[2]-=rc;
allocation[0]+=ra;
allocation[1]+=rb;
allocation[2]+=rc;
//获取安全序列
List<
list=findSaftyLine(processID);
//如果安全序列为空,则恢复刚才修改的数据
if(list==null){
Available[0]+=ra;
Available[1]+=rb;
Available[2]+=rc;
need[0]+=ra;
need[1]+=rb;
need[2]+=rc;
allocation[0]-=ra;
allocation[1]-=rb;
allocation[2]-=rc;
returnlist;
}
*寻找安全序列
*@paramproceeeId
*@return
privatestaticList<
findSaftyLine(intproceeeId){
//得到进程数目
intcount=Max.size();
//标示进程是否安全的数组
boolean[]finish=newboolean[count];
for(inti=0;
i<
count;
i++){
finish[i]=false;
//得到现有各类资源数目
Integerwork[]=newInteger[]{Available[0],Available[1],Available[2]};
intcurr=proceeeId;
//安全序列
saft=newArrayList();
//寻找满足条件”finish[i]=falseAndNeed[i][j]<
work[j]“(摘自课本)的进程
while(curr<
count){
if(finish[curr]==false){
Integerneed[]=Need.get(curr);
//检测需求量是否符合要求,如果符合,需要修改相应数值
if((need[0]<
=work[0])&
&
(need[1]<
=work[1])&
(need[2]<
=work[2])){
Integerallocation[]=Allocation.get(curr);
work[0]+=allocation[0];
work[1]+=allocation[1];
work[2]+=allocation[2];
saft.add(curr);
//线程号加入安全序列
finish[curr]=true;
//标示完成
curr=0;
//从头搜索数据
}else{
curr++;
}
}else{
curr++;
}
//检测是否有进程不安全
if(finish[i]==false){
returnnull;
returnsaft;
界面部分:
虽然界面不是实验的关键,但在本次实验中,界面担当了很重要作用,银行家算法有可能涉及大量数据,所以这需要一个良好的界面来呈现数据。
在该程序中,只需要在表格中输入各类请求资源的数目,并指定相关进程,点击按钮就可的出计算结果,具体实现如下:
*初始化数据
privatevoidinitData(){
introws=this.jTable1.getRowCount();
this.jComboBox1.removeAllItems();
rows;
this.jComboBox1.addItem(this.jTable1.getValueAt(i,0));
//最大需求表
Integermax[]={Integer.parseInt(this.jTable1.getValueAt(i,1).toString()),Integer.parseInt(this.jTable1.getValueAt(i,2).toString()),Integer.parseInt(this.jTable1.getValueAt(i,3).toString())};
DoMethod.Max.add(max);
//已获得资源表
Integerallocation[]={Integer.parseInt(this.jTable1.getValueAt(i,4).toString()),Integer.parseInt(this.jTable1.getValueAt(i,5).toString()),Integer.parseInt(this.jTable1.getValueAt(i,6).toString())};
DoMethod.Allocation.add(allocation);
//需求表
Integerneed[]={Integer.parseInt(this.jTable1.getValueAt(i,7).toString()),Integer.parseInt(this.jTable1.getValueAt(i,8).toString()),Integer.parseInt(this.jTable1.getValueAt(i,9).toString())};
DoMethod.Need.add(need);
//各类资源总量
DoMethod.Available=newInteger[]{Integer.parseInt(this.a.getText()),Integer.parseInt(this.b.getText()),Integer.parseInt(this.c.getText())};
}
privatevoidjButton1ActionPerformed(java.awt.event.ActionEventevt){
//获取请求参数,分别为三类资源的数目
intra_=Integer.parseInt(this.ra.getText());
intrb_=Integer.parseInt(this.rb.getText());
intrc_=Integer.parseInt(this.rc.getText());
//得到请求数据的进程号
intprocessId=this.jComboBox1.getSelectedIndex();
list=DoMethod.checkEnable(processId,ra_,rb_,rc_);
if(list!
=null){
//显示处理情况
showResult(list);
}else{
JOptionPane.showMessageDialog(null,"
此请求无法获得安全序列!
"
);
//TODOaddyourhandlingcodehere:
*显示安全序列
*@paramlist
privatevoidshowResult(List<
list){
DefaultTableModelnewtable1=(DefaultTableModel)this.jTable1.getModel();
newtable1.setRowCount(0);
//清空表格1
intcount=DoMethod.Max.size();
//显示各进程的资源情况
Integermax[]=DoMethod.Max.get(i);
Integerneed[]=DoMethod.Need.get(i);
Integerallocation[]=DoMethod.Allocation.get(i);
Objecto[]=newObject[]{"
P"
+i,max[0],max[1],max[2],allocation[0],allocation[1],allocation[2],need[0],need[1],need[2]};
newtable1.addRow(o);
this.jTable1.updateUI();
//显示可用资源情况
this.a.setText(DoMethod.Available[0]+"
this.b.setText(DoMethod.Available[1]+"
this.c.setText(DoMethod.Available[2]+"
//请求一次资源后,就禁止修改现有资源数目
this.a.setEnabled(false);
this.b.setEnabled(false);
this.c.setEnabled(false);
//根据安全序列显示数据
//得到可用资源的一个副本
Integerwork[]={DoMethod.Available[0],DoMethod.Available[1],DoMethod.Available[2]};
DefaultTableModelnewtable2=(DefaultTableModel)this.jTable2.getModel();
newtable2.setRowCount(0);
//清空表格
for(intget:
Integerneed[]=DoMethod.Need.get(get);
Integerallocation[]=DoMethod.Allocation.get(get);
+get,work[0],work[1],work[2],need[0],need[1],need[2],allocation[0],allocation[1],allocation[2],work[0]+allocation[0],work[1]+allocation[1],work[2]+allocation[2],true};
work[0]+=allocation[0];
work[1]+=allocation[1];
work[2]+=allocation[2];
newtable2.addRow(o);
this.jTable2.updateUI();
(三)程序结果:
1-主界面
2-进程P1请求资源
3-不安全的请求
六.实验总结
由于教材中已经明确给出了算法实现的详细步骤,这为本次试验提供了很大的方便。
银行家算法是一种很好的避免线程死锁的方法,希望今后能学习到更多高效的算法。
七.附录
试验源代码已在详细设计中给出,此处不再附录。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 操作系统 银行家 算法 实现