操作系统实验报告-利用银行家算法避免死锁Word文档下载推荐.docx
- 文档编号:13005411
- 上传时间:2022-10-02
- 格式:DOCX
- 页数:20
- 大小:523.74KB
操作系统实验报告-利用银行家算法避免死锁Word文档下载推荐.docx
《操作系统实验报告-利用银行家算法避免死锁Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《操作系统实验报告-利用银行家算法避免死锁Word文档下载推荐.docx(20页珍藏版)》请在冰豆网上搜索。
若安全,则分配;
若不安全,则拒绝申请,恢复到原来的状态,拒绝申请。
2、银行家算法步骤:
(1)如果Requesti<or=Need,则转向步骤
(2);
否则,认为出错,因为它所需要的资源数已超过它所宣布的最大值。
(2)如果Request<or=Available,则转向步骤(3);
否则,表示系统中尚无足够的资源,进程必须等待。
(3)系统试探把要求的资源分配给进程Pi,并修改下面数据结构中的数值:
Available=Available-Request[i];
Allocation=Allocation+Request;
Need=Need-Request;
(4) 系统执行安全性算法,检查此次资源分配后,系统是否处于安全状态。
3、安全性算法步骤:
(1)设置两个向量
①工作向量Work。
它表示系统可提供进程继续运行所需要的各类资源数目,执行安全算法开始时,Work=Allocation;
②布尔向量Finish。
它表示系统是否有足够的资源分配给进程,使之运行完成,开始时先做Finish[i]=false,当有足够资源分配给进程时,令Finish[i]=true。
(2)从进程集合中找到一个能满足下述条件的进程:
①Finish[i]=false
②Need<
or=Work
如找到,执行步骤(3);
否则,执行步骤(4)。
(3)当进程P获得资源后,可顺利执行,直至完成,并释放出分配给它的资源,故应执行:
Work=Work+Allocation;
Finish[i]=true;
转向步骤
(2)。
(4)如果所有进程的Finish[i]=true,则表示系统处于安全状态;
否则,系统处于不安全状态。
4、流程图:
系统主要过程流程图
银行家算法流程图
安全性算法流程图
5、主要数据结构
假设有M个进程N类资源,则有如下数据结构:
intmax[M*N]M个进程对N类资源的最大需求量
intavailable[N]系统可用资源数
intallocated[M*N]M个进程已经得到N类资源的资源量
intneed[M*N]M个进程还需要N类资源的资源量
intworked[]系统提供给进程继续运行所需的各类资源数目
四、源代码
importjava.awt.*;
importjavax.swing.*;
importjava.util.*;
importjava.awt.event.*;
importjavax.swing.border.*;
publicclassOsBankerextendsJFrame{//界面设计JLabellabelInfo;
JLabellabelInfo1;
intresourceNum,processNum;
intcount=0;
JButtonbuttonRequest,buttonSetInit,button,button1,buttonsearch,button2;
JTextFieldtf1,tf2;
JTextField[]textAvailable;
JTextField[][]textAllocation;
JTextField[][]textNeed;
JTextFieldtextProcessName;
JTextField[]textRequest;
intavailable[];
intmax[][];
intneed[][];
intallocated[][];
intSafeSequence[];
intrequest[];
booleanFinish[];
intworked[];
booleanflag=false;
JFramef1;
JFramef2;
JFramef3;
JTextAreajt;
voiddisplay(){
Borderborder=BorderFactory.createLoweredBevelBorder();
BorderborderTitled=BorderFactory.createTitledBorder(border,"
按钮区"
);
textAvailable=newJTextField[5];
textAllocation=newJTextField[6][5];
textNeed=newJTextField[6][5];
textProcessName=newJTextField("
"
textProcessName.setEnabled(false);
textRequest=newJTextField[5];
tf1=newJTextField(20);
tf2=newJTextField(20);
labelInfo=newJLabel("
请先输入资源个数和进程个数(1~6),后单击确定"
JPanelcontentPane;
contentPane=(JPanel)this.getContentPane();
contentPane.setLayout(null);
contentPane.setBackground(Color.pink);
labelInfo.setBounds(50,10,300,40);
labelInfo.setOpaque(true);
labelInfo.setForeground(Color.red);
labelInfo.setBackground(Color.pink);
contentPane.add(labelInfo,null);
JLabelb1=newJLabel("
资源个数:
b1.setForeground(Color.blue);
JLabelb2=newJLabel("
进程个数:
b2.setForeground(Color.blue);
b1.setBounds(50,80,80,30);
contentPane.add(b1,null);
tf1.setBounds(180,80,170,30);
contentPane.add(tf1,null);
b2.setBounds(50,150,80,30);
contentPane.add(b2,null);
tf2.setBounds(180,150,170,30);
contentPane.add(tf2,null);
button1=newJButton("
确定"
button=newJButton("
重置"
button1.setBounds(80,200,80,30);
contentPane.add(button1,null);
button.setBounds(220,200,80,30);
contentPane.add(button,null);
this.setSize(400,300);
this.setResizable(false);
this.setTitle("
银行家算法(SXJ)"
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
f1=newJFrame();
labelInfo1=newJLabel("
请先输入最大需求和分配矩阵,然后单击初始化"
JPanelcontentPane1;
contentPane1=(JPanel)f1.getContentPane();
contentPane1.setLayout(null);
contentPane1.setBackground(Color.pink);
labelInfo1.setOpaque(true);
labelInfo1.setBounds(75,10,400,40);
labelInfo1.setBackground(Color.pink);
labelInfo1.setForeground(Color.blue);
contentPane1.add(labelInfo1,null);
JLabellabelAvailableLabel=newJLabel("
AllResource:
JLabellabelNeedLabel=newJLabel("
MaxNeed:
JLabellabelAllocationLabel=newJLabel("
allocated:
JLabellabelRequestLabel=newJLabel("
requestprocess:
labelNeedLabel.setBounds(75,90,100,20);
//x,y,width,heightcontentPane1.add(labelNeedLabel,null);
labelAllocationLabel.setBounds(75,240,100,20);
contentPane1.add(labelAllocationLabel,null);
labelAvailableLabel.setBounds(75,70,100,20);
contentPane1.add(labelAvailableLabel,null);
labelRequestLabel.setBounds(75,400,10
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 操作系统 实验 报告 利用 银行家 算法 避免 死锁
