《2022年操作系统实验报告-利用银行家算法避免死锁.docx》由会员分享,可在线阅读,更多相关《2022年操作系统实验报告-利用银行家算法避免死锁.docx(39页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选学习资料 - - - - - - - - - 电脑操作系统试验报告题目利用银行家算法防止死锁一、 试验目的: 1 、加深明白有关资源申请、防止死锁等概念,并体会和明白死锁和防止死锁的详细实施方法; 2 、要求编写和调试一个系统动态安排资源的简洁模拟程序,观看死锁产生的条件,并采纳银行家算法,有效的防止和防止死锁的发生;名师归纳总结 - - - - - - -第 1 页,共 20 页精选学习资料 - - - - - - - - - 二、试验内容:用银行家算法实现资源安排:设计五个进程 p0,p1,p2,p3,p4共享三类资源 A,B,C 的系统,例如, A,B,C 的资源数量分别为 10,5
2、,7;进程可动态地申请资源和释放资源, 系统按进程的申请动态地安排资源,要求程序具有显示和打印各进程的某一个时刻的资源安排表和安全序列;显示和打印各进程依次要求申请的资源号以及为某进程安排资源后的有关资源数据;三、问题分析与设计:1、算法思路:先对用户提出的恳求进行合法性检查,即检查恳求是否大于需要的,是否大于可利用的;假设恳求合法,就进行预安排,对安排后的 状态调用安全性算法进行检查;假设安全,就安排;假设担心全,就 拒绝申请,复原到原先的状态,拒绝申请;2、银行家算法步骤:1假如 Requesti or =Need, 就转向步骤 2 ;否就,认为出 错,由于它所需要的资源数已超过它所宣布的
3、最大值;2假如 Requestor=Available,就转向步骤 3;否就,表示系统中尚无足够的资源,进程必需等待;3系统摸索把要求的资源安排给进程 中的数值: Available=Available-Requesti; Pi, 并修改下面数据结构名师归纳总结 - - - - - - -第 2 页,共 20 页精选学习资料 - - - - - - - - - Allocation=Allocation+Request; Need=Need-Request; 4 系统执行安全性算法,检查此次资源安排后,系统是否处于安全状态;3、安全性算法步骤:1设置两个向量工作向量 Work;它表示系统可供应
4、进程连续运行所需要的各类资源数目,执行安全算法开头时,Work=Allocation; 布尔向量 Finish ;它表示系统是否有足够的资源安排给进程,使之运行完成,开头时先做Finishi=false,当有足够资源安排给进程时,令 Finishi=true;2从进程集合中找到一个能满意下述条件的进程:Finishi=false Needor=Work 如找到,执行步骤 3;否就,执行步骤 4;3当进程 P 获得资源后,可顺当执行,直至完成,并释放出安排给它的资源,故应执行: Work=Work+Allocation; Finishi=true; 转向步骤 2;4假如全部进程的Finishi=
5、true,就表示系统处于安全状态;否就,系统处于担心全状态;名师归纳总结 - - - - - - -第 3 页,共 20 页精选学习资料 - - - - - - - - - 4、流程图:系统主要过程流程图银行家算法流程图名师归纳总结 - - - - - - -第 4 页,共 20 页精选学习资料 - - - - - - - - - 安全性算法流程图名师归纳总结 - - - - - - -第 5 页,共 20 页精选学习资料 - - - - - - - - - 5、主要数据结构假设有 M个进程 N类资源,就有如下数据结构:int maxM*N M个进程对 N类资源的最大需求量int availa
6、bleN 系统可用资源数int allocatedM*N M个进程已经得到 N类资源的资源量int needM*N M 个进程仍需要 N类资源的资源量int worked 系统供应应进程连续运行所需的各类资源数目名师归纳总结 - - - - - - -第 6 页,共 20 页精选学习资料 - - - - - - - - - 四、 源代码import java.awt.*; import javax.swing.*; import java.util.*; import java.awt.event.*; import javax.swing.border.*; public class OsB
7、anker extends JFrame / 界面设计 JLabel labelInfo; JLabel labelInfo1; int resourceNum, processNum; int count = 0; JButton buttonRequest, buttonSetInit, button, button1, buttonsearch,button2; JTextField tf1, tf2; JTextField textAvailable; JTextField textAllocation; JTextField textNeed; JTextField textProc
8、essName; JTextField textRequest; int available; int max; int need; int allocated; int SafeSequence; int request; boolean Finish; int worked; boolean flag = false; JFrame f1; JFrame f2; JFrame f3; JTextArea jt; void display Border border = BorderFactory.createLoweredBevelBorder; Border borderTitled =
9、 BorderFactory.createTitledBorderborder, 按钮区 ; textAvailable = new JTextField5; textAllocation = new JTextField65; textNeed = new JTextField65; textProcessName = new JTextField; textProcessName.setEnabledfalse; textRequest = new JTextField5; tf1 = new JTextField20; 名师归纳总结 - - - - - - -第 7 页,共 20 页精选
10、学习资料 - - - - - - - - - tf2 = new JTextField20; labelInfo = new JLabel请先输入资源个数和进程个数16,后单击确定 ; JPanel contentPane; contentPane = JPanel this.getContentPane; contentPane.setLayoutnull; contentPane.setBackgroundColor.pink; labelInfo.setBounds50, 10, 300, 40; labelInfo.setOpaquetrue; labelInfo.setForegro
11、undColor.red; labelInfo.setBackgroundColor.pink; contentPane.addlabelInfo, null; JLabel b1 = new JLabel 资源个数 :; b1.setForegroundColor.blue; JLabel b2 = new JLabel 进程个数 :; b2.setForegroundColor.blue; b1.setBounds50, 80, 80, 30; contentPane.addb1, null; tf1.setBounds180, 80, 170, 30; contentPane.addtf
12、1, null; b2.setBounds50, 150, 80, 30; contentPane.addb2, null; tf2.setBounds180, 150, 170, 30; contentPane.addtf2, null; button1 = new JButton 确定 ; button = new JButton 重置 ; button1.setBounds80, 200, 80, 30; contentPane.addbutton1, null; button.setBounds220, 200, 80, 30; contentPane.addbutton, null;
13、 this.setSize400, 300; this.setResizablefalse; this.setTitle 银行家算法 SXJ; this.setLocationRelativeTonull; this.setDefaultCloseOperationEXIT_ON_CLOSE; this.setVisibletrue; f1 = new JFrame; labelInfo1 = new JLabel请先输入最大需求和安排矩阵,然后单击初始化; JPanel contentPane1; contentPane1 = JPanel f1.getContentPane; conten
14、tPane1.setLayoutnull; contentPane1.setBackgroundColor.pink; labelInfo1.setOpaquetrue; labelInfo1.setBounds75, 10, 400, 40; 名师归纳总结 - - - - - - -第 8 页,共 20 页精选学习资料 - - - - - - - - - labelInfo1.setBackgroundColor.pink; labelInfo1.setForegroundColor.blue; contentPane1.addlabelInfo1, null; JLabel labelAv
15、ailableLabel = new JLabelAllResource:; JLabel labelNeedLabel = new JLabelMaxNeed:; JLabel labelAllocationLabel = new JLabelallocated:; JLabel labelRequestLabel = new JLabelrequest process:; labelNeedLabel.setBounds75, 90, 100, 20; / x,y,width,height contentPane1.addlabelNeedLabel, null; labelAllocat
16、ionLabel.setBounds75, 240, 100, 20; contentPane1.addlabelAllocationLabel, null; labelAvailableLabel.setBounds75, 70, 100, 20; contentPane1.addlabelAvailableLabel, null; labelRequestLabel.setBounds75, 400, 100, 20; contentPane1.addlabelRequestLabel, null; JLabel labelProcessLabel1 = new JLabel 进程 1,
17、new JLabel 进程 2, new JLabel 进程 3, new JLabel 进程 4, new JLabel 进程 5, new JLabel 进程 6 ; JLabel labelProcessLabel2 = new JLabel 进程 1, new JLabel 进程 2, new JLabel 进程 3, new JLabel 进程 4, new JLabel 进程 5, new JLabel 进程 6 ; JPanel pPanel1 = new JPanel, pPanel2 = new JPanel, pPanel3 = new JPanel, pPanel4 =
18、new JPanel; pPanel1.setLayoutnull; pPanel2.setLayoutnull; /* * pPanel4.setLayoutnull; pPanel4.setBounds440,120,90,270; * pPanel4.setBorderborderTitled; */ buttonSetInit = new JButton 初始化 ; buttonsearch = new JButton 检测安全性 ; button2 = new JButton 重置 ; buttonRequest = new JButton 恳求资源 ; buttonSetInit.
19、setBounds420, 140, 100, 30; contentPane1.addbuttonSetInit, null; buttonsearch.setBounds420, 240, 100, 30; contentPane1.addbuttonsearch, null; button2.setBounds420, 340, 100, 30; contentPane1.addbutton2, null; buttonRequest.setBounds420, 425, 100, 30; contentPane1.addbuttonRequest, null; for int pi =
20、 0; pi 6; pi+ 名师归纳总结 - - - - - - -第 9 页,共 20 页精选学习资料 - - - - - - - - - labelProcessLabel1pi.setBounds0, 0 + pi * 20, 60, 20; labelProcessLabel2pi.setBounds0, 0 + pi * 20, 60, 20; pPanel1.setBounds75, 120, 60, 120; pPanel2.setBounds75, 270, 60, 120; for int pi = 0; pi 6; pi+ pPanel1.addlabelProcessLa
21、bel1pi, null; pPanel2.addlabelProcessLabel2pi, null; contentPane1.addpPanel1; contentPane1.addpPanel2; contentPane1.addpPanel4; for int si = 0; si 5; si+ for int pi = 0; pi 6; pi+ textNeedpisi = new JTextField; textNeedpisi .setBounds150 + si * 50, 120 + pi * 20, 50, 20; textNeedpisi.setEditablefals
22、e; textAllocationpisi = new JTextField; textAllocationpisi.setBounds150 + si * 50, 270 + pi * 20, 50, 20; textAllocationpisi.setEditablefalse; for int si = 0; si 5; si+ textAvailablesi = new JTextField; textAvailablesi.setEditablefalse; textAvailablesi.setBounds150 + si * 50, 70, 50, 20; textRequest
23、si = new JTextField; textRequestsi.setEditablefalse; textRequestsi.setBounds150 + si * 50, 430, 50, 20; contentPane1.addtextAvailablesi, null; contentPane1.addtextRequestsi, null; for int pi = 0; pi 6; pi+ for int si = 0; si 5; si+ contentPane1.addtextNeedpisi, null; contentPane1.addtextAllocationpi
24、si, null; textProcessName.setBounds80, 430, 50, 20; contentPane1.addtextProcessName, null; f1.setSize550, 500; 名师归纳总结 - - - - - - -第 10 页,共 20 页精选学习资料 - - - - - - - - - f1.setResizablefalse; f1.setTitle 银行家算法 SXJ; f1.setLocationRelativeTonull; f1.setDefaultCloseOperationEXIT_ON_CLOSE; / f1.setVisibl
25、etrue; f1.setVisiblefalse; f2 = new JFrame安全序列显示框; jt = new JTextArea75, 40; jt.setBackgroundColor.pink; jt.setForegroundColor.blue; JScrollPane scrollPane = new JScrollPanejt; / 加滚动条 scrollPane.setBorderBorderFactory.createLoweredBevelBorder;/ 边界 f2.getContentPane.addscrollPane; f2.setSize450, 400;
26、 f2.setResizablefalse; f2.setDefaultCloseOperationEXIT_ON_CLOSE; f2.setVisiblefalse; buttonSetInit.setEnabledfalse; buttonRequest.setEnabledfalse; buttonsearch.setEnabledfalse; button1.addActionListenernew ActionListener public void actionPerformedActionEvent e / labelInfo.setText请先初始化allocated和 Max
27、need,后单击初始化按钮 ; f1.setVisibletrue; buttonSetInit.setEnabledtrue; resourceNum = Integer.parseInttf1.getText; processNum = Integer.parseInttf2.getText; for int i = 0; i processNum; i+ for int j = 0; j resourceNum; j+ textNeedij.setEditabletrue; textAllocationij.setEditabletrue; textAvailablej.setEdita
28、bletrue; ; buttonSetInit.addActionListenernew ActionListener public void actionPerformedActionEvent e Init; buttonsearch.setEnabledtrue; ; buttonsearch.addActionListenernew ActionListener 名师归纳总结 - - - - - - -第 11 页,共 20 页精选学习资料 - - - - - - - - - public void actionPerformedActionEvent e count = 0; Sa
29、feSequence = new intprocessNum; worked = new intresourceNum; Finish = new booleanprocessNum; copyVectorworked, available; Safety0; jt.append安全序列数量: + count; if flag labelInfo1.setText当前系统状态:安全; f2.setVisibletrue; buttonRequest.setEnabledtrue; textProcessName.setEnabledtrue; for int i = 0; i resource
30、Num; i+ textRequesti.setEditabletrue; else labelInfo1.setText当前系统状态:担心全; buttonSetInit.setEnabledfalse; ; buttonRequest.addActionListenernew ActionListener public void actionPerformedActionEvent e count = 0; for int i = 0; i processNum; i+ Finishi = false; jt.setText; flag = false; RequestResource;
31、; button2.addActionListenernew ActionListener public void actionPerformedActionEvent e /* * tf1.setText; tf2.setText; */ f2.setVisiblefalse; jt.setText; for int i = 0; i processNum; i+ 名师归纳总结 - - - - - - -第 12 页,共 20 页精选学习资料 - - - - - - - - - for int j = 0; j resourceNum; j+ textNeedij.setText; text
32、Allocationij.setText; textAvailablej.setText; textRequestj.setText; / textNeedij.setEditablefalse; / textAllocationij.setEditablefalse; / textAvailablej.setEditablefalse; textRequestj.setEditablefalse; textProcessName.setText; Finishi = false; flag = false; buttonsearch.setEnabledfalse; / labelInfo.
33、setText请先输入资源个数和进程个数,后单击确定; ; button.addActionListenernew ActionListener public void actionPerformedActionEvent e tf1.setText; tf2.setText; f2.setVisiblefalse; jt.setText; flag = false; ; void copyVectorint v1, int v2 for int i = 0; i v1.length; i+ v1i = v2i; void Addint v1, int v2 for int i = 0; i
34、v1.length; i+ v1i += v2i; void Subint v1, int v2 for int i = 0; i v1.length; i+ 名师归纳总结 - - - - - - -第 13 页,共 20 页精选学习资料 - - - - - - - - - v1i -= v2i; boolean Smallerint v1, int v2 boolean value = true; for int i = 0; i v2i value = false; break; return value; public static void mainString args OsBank
35、er ob = new OsBanker; ob.display; / System.out.println +count; void Init / 初始化操作矩阵 available = new intresourceNum; for int i = 0; i resourceNum; i+ availablei = Integer.parseInttextAvailablei.getText; max = new intprocessNumresourceNum; allocated = new intprocessNumresourceNum; need = new intprocess
36、NumresourceNum; for int i = 0; i processNum; i+ for int j = 0; j resourceNum; j+ maxij = Integer.parseInttextNeedij.getText; allocatedij = Integer.parseInttextAllocationij .getText; for int i = 0; i resourceNum; i+ for int j = 0; j processNum; j+ needji = maxji - allocatedji; for int i = 0; i resour
37、ceNum; i+ for int j = 0; j processNum; j+ availablei -= allocatedji; if availablei 0 名师归纳总结 labelInfo.setText您输入的数据有误, 请重新输入 ; 第 14 页,共 20 页- - - - - - -精选学习资料 - - - - - - - - - void Safetyint n / 查找全部安全序列 if n = processNum count+; for int i = 0; i processNum; i+ jt.append 进程 + SafeSequencei + 1 + ;
38、 jt.appendn; flag = true; return; for int i = 0; i processNum; i+ if Finishi = false boolean OK = true; for int j = 0; j workedj OK = false; break; if OK for int j = 0; j resourceNum; j+ workedj += allocatedij; Finishi = true; SafeSequencen = i; Safetyn + 1; Finishi = false; SafeSequencen = -1; / nu
39、m+; for int j = 0; j resourceNum; j+ workedj -= allocatedij; 名师归纳总结 - - - - - - -第 15 页,共 20 页精选学习资料 - - - - - - - - - void RequestResource / 恳求资源jt.setText; int processname = Integer.parseInttextProcessName.getText - 1; request = new intresourceNum; for int i = 0; i resourceNum; i+ requesti = Integer.parseInttextRequesti.getText; if .Smallerrequest, needprocessname labelInfo.setText资源恳求不符该进程的需求量.; else if .Smallerrequest, available labelInfo1.setText可用资源不足以满意恳求, 进程