操作系统实验报告-利用银行家算法避免死锁(2)

2019-04-14 16:42

5、主要数据结构

假设有M个进程N类资源,则有如下数据结构:

int max[M*N] M个进程对N类资源的最大需求量 int available[N] 系统可用资源数

int allocated[M*N] M个进程已经得到N类资源的资源量 int need[M*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; 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 textProcessName; 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 = BorderFactory.createTitledBorder(border, \按钮区\ textAvailable = new JTextField[5]; textAllocation = new JTextField[6][5]; textNeed = new JTextField[6][5];

textProcessName = new JTextField(\ textProcessName.setEnabled(false); textRequest = new JTextField[5]; tf1 = new JTextField(20);

tf2 = new JTextField(20);

labelInfo = new JLabel(\请先输入资源个数和进程个数(1~6),后单击确定\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(labelInfo, 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, 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.setResizable(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(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);

JLabel labelAvailableLabel = new JLabel(\ JLabel labelNeedLabel = new JLabel(\

JLabel labelAllocationLabel = new JLabel(\ JLabel labelRequestLabel = new JLabel(\ labelNeedLabel.setBounds(75, 90, 100, 20);

// x,y,width,height

contentPane1.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, 100, 20); contentPane1.add(labelRequestLabel, null); JLabel[] labelProcessLabel1 = { new JLabel(\进程1\new JLabel(\进程2\ new JLabel(\进程3\进程4\进程5\ new JLabel(\进程6\ JLabel[] labelProcessLabel2 = { new JLabel(\进程1\new JLabel(\进程2\ new JLabel(\进程3\进程4\进程5\ new JLabel(\进程6\ JPanel pPanel1 = new JPanel(), pPanel2 = new JPanel(), pPanel3 = new JPanel(), pPanel4 = new JPanel();

pPanel1.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(\请求资源\ buttonSetInit.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, null); for (int pi = 0; pi < 6; pi++) {

labelProcessLabel1[pi].setBounds(0, 0 + pi * 20, 60, 20); labelProcessLabel2[pi].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(labelProcessLabel1[pi], null); pPanel2.add(labelProcessLabel2[pi], null);

}

contentPane1.add(pPanel1); contentPane1.add(pPanel2); contentPane1.add(pPanel4); for (int si = 0; si < 5; si++)

for (int pi = 0; pi < 6; pi++) {

textNeed[pi][si] = new JTextField(); textNeed[pi][si]

.setBounds(150 + si * 50, 120 + pi * 20, 50, 20); textNeed[pi][si].setEditable(false);

textAllocation[pi][si] = new JTextField();

textAllocation[pi][si].setBounds(150 + si * 50, 270 + pi * 20, 50, 20);

textAllocation[pi][si].setEditable(false); }

for (int si = 0; si < 5; si++) {

textAvailable[si] = new JTextField(); textAvailable[si].setEditable(false);

textAvailable[si].setBounds(150 + si * 50, 70, 50, 20); textRequest[si] = new JTextField(); textRequest[si].setEditable(false);

textRequest[si].setBounds(150 + si * 50, 430, 50, 20); contentPane1.add(textAvailable[si], null); contentPane1.add(textRequest[si], null); }

for (int pi = 0; pi < 6; pi++)

for (int si = 0; si < 5; si++) {

contentPane1.add(textNeed[pi][si], null);

contentPane1.add(textAllocation[pi][si], null); }

textProcessName.setBounds(80, 430, 50, 20); contentPane1.add(textProcessName, null); f1.setSize(550, 500);


操作系统实验报告-利用银行家算法避免死锁(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:翻译与本地化管理

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: