1、.计算机操作系统实验报告题目利用银行家算法避免死锁一、 实验目的:1 、加深了解有关资源申请、避免死锁等概念,并体会和了解死锁和避免死锁的具体实施方法。2 、要求编写和调试一个系统动态分配资源的简单模拟程序,观察死锁产生的条件,并采用银行家算法,有效的防止和避免死锁的发生。;.二、实验内容:用银行家算法实现资源分配: 设计五个进程p0,p1,p2,p3,p4共享三类资源A,B,C的系统, 例如,A,B,C的资源数量分别为 10,5,7。进程可动态地申请资源和释放资源,系统按进程的申请动态地分配资源,要求程序具有显示和打印各进程的某一个时刻的资源分配表和安全序列;显示和打印各进程依次要求申请的资
2、源号以及为某进程分配资源后的有关资源数据。三、问题分析与设计:1、算法思路:先对用户提出的请求进行合法性检查,即检查请求是否大于需要的,是否大于可利用的。若请求合法,则进行预分配,对分配后的状态调用安全性算法进行检查。若安全,则分配;若不安全,则拒绝申请,恢复到原来的状态,拒绝申请。2、银行家算法步骤:(1)如果 Requestior =Need,则转向步骤(2);否则,认为出错,因为它所需要的资源数已超过它所宣布的最大值。 (2)如果 Requestor=Available,则转向步骤(3);否则,表示系统中尚无足够的资源,进程必须等待。 (3)系统试探把要求的资源分配给进程Pi,并修改下面
3、数据结构中的数值: Available=Available-Requesti; Allocation=Allocation+Request; Need=Need-Request; (4)系统执行安全性算法,检查此次资源分配后,系统是否处于安全状态。3、安全性算法步骤:(1) 设置两个向量 工作向量 Work。它表示系统可提供进程继续运行所需要的各类资源数目,执行安全算法开始时,Work=Allocation; 布尔向量 Finish。它表示系统是否有足够的资源分配给进程, 使之运行完成,开始时先做 Finishi=false,当有足够资源分配给进程时,令 Finishi=true。(2) 从进
4、程集合中找到一个能满足下述条件的进程: Finishi=false Needor=Work 如找到,执行步骤(3);否则,执行步骤(4)。(3) 当进程 P 获得资源后,可顺利执行,直至完成,并释放出分配给它的资源,故应执行: Work=Work+Allocation; Finishi=true; 转向步骤(2)。(4) 如果所有进程的 Finishi=true,则表示系统处于安全状态; 否则,系统处于不安全状态。4、流程图:系统主要过程流程图银行家算法流程图安全性算法流程图5、主要数据结构假设有M 个进程N 类资源,则有如下数据结构:int maxM*N M 个进程对N 类资源的最大需求量i
5、nt availableN 系统可用资源数int allocatedM*N M 个进程已经得到N 类资源的资源量int needM*N M 个进程还需要N 类资源的资源量int worked 系统提供给进程继续运行所需的各类资源数目 四、源代码import java.awt.*; import javax.swing.*; import java.util.*; import java.awt.event.*; import javax.swing.border.*; public class OsBanker extends JFrame / 界面设计JLabel labelInfo; JL
6、abel labelInfo1; int resourceNum, processNum; int count = 0; JButton buttonRequest, buttonSetInit, button, button1, buttonsearch,button2; JTextField tf1, tf2; JTextField textAvailable; JTextField textAllocation; JTextField textNeed; JTextField textProcessName; JTextField textRequest; int available;
7、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 = BorderFactory.createTitledBorder(border, 按钮
8、区); textAvailable = new JTextField5; textAllocation = new JTextField65; textNeed = new JTextField65; textProcessName = new JTextField(); textProcessName.setEnabled(false); textRequest = new JTextField5; tf1 = new JTextField(20); tf2 = new JTextField(20); labelInfo = new JLabel(请先输入资源个数和进程个数(16),后单击确
9、定); JPanel contentPane; 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(labelInf
10、o, null); JLabel b1 = new JLabel(资源个数:); b1.setForeground(Color.blue); JLabel b2 = new JLabel(进程个数:); 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,
11、 null); tf2.setBounds(180, 150, 170, 30); contentPane.add(tf2, null); button1 = new JButton( 确 定 ); button = new JButton( 重 置 ); 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.setResi
12、zable(false); this.setTitle(银行家算法(SXJ); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); f1 = new JFrame(); labelInfo1 = new JLabel(请先输入最大需求和分配矩阵,然后单击初始化); JPanel contentPane1; contentPane1 = (JPanel) f1.getContentPane(); contentPane1.setLayout(n
13、ull); 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); JLabel labelAvailableLabel = new JLabel(AllResource:); JLabel labelNeedLabe
14、l = new JLabel(MaxNeed:); JLabel labelAllocationLabel = new JLabel(allocated:); JLabel labelRequestLabel = new JLabel(request process:); labelNeedLabel.setBounds(75, 90, 100, 20); / x,y,width,height contentPane1.add(labelNeedLabel, null); labelAllocationLabel.setBounds(75, 240, 100, 20); contentPane
15、1.add(labelAllocationLabel, null); labelAvailableLabel.setBounds(75, 70, 100, 20); contentPane1.add(labelAvailableLabel, null); labelRequestLabel.setBounds(75, 400, 100, 20); contentPane1.add(labelRequestLabel, null); JLabel labelProcessLabel1 = new JLabel(进程 1), new JLabel(进程 2), new JLabel(进程 3),
16、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 = new JPanel(); pPane
17、l1.setLayout(null); pPanel2.setLayout(null); /* * pPanel4.setLayout(null); pPanel4.setBounds(440,120,90,270); * pPanel4.setBorder(borderTitled); */ buttonSetInit = new JButton( 初 始 化 ); buttonsearch = new JButton(检测安全性); button2 = new JButton( 重 置 ); buttonRequest = new JButton( 请 求 资 源 ); buttonSet
18、Init.setBounds(420, 140, 100, 30); contentPane1.add(buttonSetInit, null); buttonsearch.setBounds(420, 240, 100, 30); contentPane1.add(buttonsearch, null); button2.setBounds(420, 340, 100, 30); contentPane1.add(button2, null); buttonRequest.setBounds(420, 425, 100, 30); contentPane1.add(buttonRequest
19、, null); for (int pi = 0; pi 6; pi+) labelProcessLabel1pi.setBounds(0, 0 + pi * 20, 60, 20); labelProcessLabel2pi.setBounds(0, 0 + pi * 20, 60, 20); pPanel1.setBounds(75, 120, 60, 120); pPanel2.setBounds(75, 270, 60, 120); for (int pi = 0; pi 6; pi+) pPanel1.add(labelProcessLabel1pi, null); pPanel2.
20、add(labelProcessLabel2pi, null); contentPane1.add(pPanel1); contentPane1.add(pPanel2); contentPane1.add(pPanel4); for (int si = 0; si 5; si+) for (int pi = 0; pi 6; pi+) textNeedpisi = new JTextField(); textNeedpisi .setBounds(150 + si * 50, 120 + pi * 20, 50, 20); textNeedpisi.setEditable(false); t
21、extAllocationpisi = new JTextField(); textAllocationpisi.setBounds(150 + si * 50, 270 + pi * 20, 50, 20); textAllocationpisi.setEditable(false); for (int si = 0; si 5; si+) textAvailablesi = new JTextField(); textAvailablesi.setEditable(false); textAvailablesi.setBounds(150 + si * 50, 70, 50, 20); t
22、extRequestsi = new JTextField(); textRequestsi.setEditable(false); textRequestsi.setBounds(150 + si * 50, 430, 50, 20); contentPane1.add(textAvailablesi, null); contentPane1.add(textRequestsi, null); for (int pi = 0; pi 6; pi+) for (int si = 0; si 5; si+) contentPane1.add(textNeedpisi, null); conten
23、tPane1.add(textAllocationpisi, null); textProcessName.setBounds(80, 430, 50, 20); contentPane1.add(textProcessName, null); f1.setSize(550, 500); f1.setResizable(false); f1.setTitle(银行家算法(SXJ); f1.setLocationRelativeTo(null); f1.setDefaultCloseOperation(EXIT_ON_CLOSE); / f1.setVisible(true); f1.setVi
24、sible(false); f2 = new JFrame(安全序列显示框); jt = new JTextArea(75, 40); jt.setBackground(Color.pink); jt.setForeground(Color.blue); JScrollPane scrollPane = new JScrollPane(jt); / 加滚动条scrollPane.setBorder(BorderFactory.createLoweredBevelBorder();/边 界(f2.getContentPane().add(scrollPane); f2.setSize(450,
25、400); f2.setResizable(false); f2.setDefaultCloseOperation(EXIT_ON_CLOSE); f2.setVisible(false); buttonSetInit.setEnabled(false); buttonRequest.setEnabled(false); buttonsearch.setEnabled(false); button1.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) 化按钮); / labelInf
26、o.setText(请先初始化 allocated 和 Maxneed,后单击初始f1.setVisible(true); buttonSetInit.setEnabled(true); resourceNum = Integer.parseInt(tf1.getText(); processNum = Integer.parseInt(tf2.getText(); for (int i = 0; i processNum; i+) for (int j = 0; j resourceNum; j+) textNeedij.setEditable(true); textAllocationij
27、.setEditable(true); textAvailablej.setEditable(true); ); buttonSetInit.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) Init(); buttonsearch.setEnabled(true); ); buttonsearch.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) count = 0;
28、 SafeSequence = new intprocessNum; worked = new intresourceNum; Finish = new booleanprocessNum; copyVector(worked, available); Safety(0); jt.append(安全序列数量: + count); if (flag) labelInfo1.setText(当前系统状态:安全); f2.setVisible(true); buttonRequest.setEnabled(true); textProcessName.setEnabled(true); for (i
29、nt i = 0; i resourceNum; i+) textRequesti.setEditable(true); else labelInfo1.setText(当前系统状态:不安全); buttonSetInit.setEnabled(false); ); buttonRequest.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) count = 0; for (int i = 0; i processNum; i+) Finishi = false; jt.setText(); flag = false; RequestResource(); ); button2.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) /* * tf1.setText(); tf2.setText(); */ f2.setVisible(false); jt.setText(); for (int i = 0; i processN
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2