《银行家算法(操作系统实验报告)(共14页).doc》由会员分享,可在线阅读,更多相关《银行家算法(操作系统实验报告)(共14页).doc(14页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上实 验 报 告 课程名称: 计算机操作系统 实验名称: 银行家算法 _ 任课教师: 霍 林 专 业: 计算机科学与技术 班 级: 学 号: _姓 名:蓝冠恒_ _ 完成日期: 2009年10月15日 一、实验目的:加深对操作系统的进程管理与资源分配了解,深刻理解系统安全性检验算法。二、主要实验内容及要求: 实验内容 :利用银行家算法检验系统的安全性。实验要求 :1.输入系统进程数量n和资源类型数量m。 2.输入每类资源的数量。 3.输入每个进程每类资源的最大需求量和已获资源量。 4.检验系统的安全系。三、实验结果与结论:(经调试正确的源程序和程序的运行结果)编程员:蓝
2、冠恒程序源代码:package disFace;import org.eclipse.swt.SWT;import org.eclipse.swt.custom.StackLayout;import org.eclipse.swt.events.SelectionAdapter;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Displ
3、ay;import org.eclipse.swt.widgets.Label;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.Text;import org.eclipse.ui.forms.widgets.FormToolkit;import com.swtdesigner.SWTResourceManager;public class DisFace protected Shell shell;private final FormToolkit formToolkit = new FormToolki
4、t(Display.getDefault();private Composite composite;private Label label;private Label label_1;private Text processN;private Text kindM;private Button button1;private Text listText;/输出文本框private Composite composite4;private Label label_3;private Text kindNumbers; /资源数量输入框private Button button2;private
5、 Text theKind1;private Label label_5;private Label label_6;private Text MaxText; /最大资源需求输入框private Text AllocationText;/已获资源输入框private Button button3;private Label label_7;private Text theProcess;private Text theKind2;private Label label_11;private Button test;private int N;/进程数量private int M;/资源类型数
6、量int Max;/进程最大资源需求数组int Allocation;/进程已获资源数组int Need;/进程所需资源数组int Available;/起初可用资源数组int Work; /可提供资源数组boolean Finish;/进程完成标识int i=0;int j=0;int k=0;private Composite composite1;StackLayout stackLayout=new StackLayout();StackLayout stackLayout2=new StackLayout();private Composite composite3;private
7、Composite composite2;private Composite composite0;public static void main(String args) try DisFace window = new DisFace();window.open(); catch (Exception e) e.printStackTrace();public void open() Display display = Display.getDefault();createContents();shell.open();shell.layout();while (!shell.isDisp
8、osed() if (!display.readAndDispatch() display.sleep();protected void createContents() shell = new Shell();shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND);shell.setText(银行家算法实现);shell.setSize(733, 565);/*-面板-*/composite = new Composite(shell, SWT.BORDER);composite.setBack
9、ground(SWTResourceManager.getColor(SWT.COLOR_GREEN);composite.setBounds(10, 10, 697, 492);composite.setData(name, composite);formToolkit.adapt(composite);formToolkit.paintBordersFor(composite);composite0 = new Composite(composite, SWT.NONE);composite0.setBounds(0, 1, 210, 156);composite0.setData(nam
10、e, composite0);formToolkit.adapt(composite0);formToolkit.paintBordersFor(composite0);composite1 = new Composite(composite, SWT.NONE);composite1.setBackground(SWTResourceManager.getColor(SWT.COLOR_GREEN);composite1.setBounds(210, 1, 224, 156);composite1.setData(name, composite1);formToolkit.adapt(com
11、posite1);formToolkit.paintBordersFor(composite1);composite1.setLayout(stackLayout);composite2 = new Composite(composite1, SWT.BORDER);composite2.setBackgroundMode(SWT.INHERIT_FORCE);composite2.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE);composite2.setBackground(SWTResourceManager.getCo
12、lor(0, 255, 0);composite2.setData(name, composite2);formToolkit.adapt(composite2);formToolkit.paintBordersFor(composite2);composite3 = new Composite(composite, SWT.NONE);composite3.setBounds(434, 1, 263, 156);composite3.setData(name, composite3);formToolkit.adapt(composite3);formToolkit.paintBorders
13、For(composite3);composite3.setLayout(stackLayout2);composite4 = new Composite(composite3, SWT.BORDER);composite4.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN);composite4.setData(name, composite4);formToolkit.adapt(composite4);formToolkit.paintBordersFor(composite4);/*-标签-*/label_3
14、= new Label(composite2, SWT.HORIZONTAL);label_3.setBounds(59, 26, 78, 26);label_3.setFont(SWTResourceManager.getFont(微软雅黑, 12, SWT.NORMAL);label_3.setData(name, label_3);formToolkit.adapt(label_3, true, true);label_3.setText(资源数量为);label_5 = new Label(composite4, SWT.NONE);label_5.setFont(SWTResourc
15、eManager.getFont(微软雅黑, 12, SWT.NORMAL);label_5.setBounds(0, 55, 112, 26);label_5.setData(name, label_5);formToolkit.adapt(label_5, true, true);label_5.setText(最大资源需求量);label_6 = new Label(composite4, SWT.NONE);label_6.setFont(SWTResourceManager.getFont(微软雅黑, 12, SWT.NORMAL);label_6.setBounds(0, 87,
16、112, 26);label_6.setData(name, label_6);formToolkit.adapt(label_6, true, true);label_6.setText(已获资源数量);label_7 = new Label(composite4, SWT.NONE);label_7.setFont(SWTResourceManager.getFont(微软雅黑, 12, SWT.NORMAL);label_7.setBounds(0, 23, 46, 26);label_7.setData(name, label_7);formToolkit.adapt(label_7,
17、 true, true);label_7.setText(进 程);label_11 = new Label(composite4, SWT.NONE);label_11.setFont(SWTResourceManager.getFont(微软雅黑, 12, SWT.NORMAL);label_11.setBounds(80, 23, 50, 26);label_11.setData(name, label_11);formToolkit.adapt(label_11, true, true);label_11.setText(对资源);label_1 = new Label(composi
18、te0, SWT.NONE);label_1.setBounds(10, 65, 80, 23);label_1.setFont(SWTResourceManager.getFont(微软雅黑, 12, SWT.NORMAL);label_1.setData(name, label_1);formToolkit.adapt(label_1, true, true);label_1.setText(资源类量m);label = new Label(composite0, SWT.NONE);label.setBounds(10, 24, 80, 23);label.setFont(SWTReso
19、urceManager.getFont(微软雅黑, 12, SWT.NORMAL);label.setData(name, label);formToolkit.adapt(label, true, true);label.setText(进程数量n);/*-文本-*/theKind1 = new Text(composite2, SWT.READ_ONLY | SWT.CENTER | SWT.MULTI);theKind1.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE);theKind1.setBackground(SWT
20、ResourceManager.getColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW);theKind1.setBounds(22, 26, 31, 26);theKind1.setFont(SWTResourceManager.getFont(微软雅黑, 12, SWT.NORMAL);theKind1.setData(name, theKind1);formToolkit.adapt(theKind1, true, true);theKind1.setText(R+String.valueOf(0);listText = new Text(composite
21、, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL);listText.setCapture(true);listText.setForeground(SWTResourceManager.getColor(0, 0, 0);listText.setBackground(SWTResourceManager.getColor(51, 255, 255);listText.setBounds(0, 163, 696, 325);listText.setData(name, text_2);formToolkit.adapt(listTe
22、xt, true, true);kindNumbers = new Text(composite2, SWT.BORDER | SWT.CENTER);kindNumbers.setFont(SWTResourceManager.getFont(微软雅黑, 12, SWT.NORMAL);kindNumbers.setBackground(SWTResourceManager.getColor(SWT.COLOR_GREEN);kindNumbers.setBounds(143, 29, 38, 23);kindNumbers.setData(name, kindNumbers);formTo
23、olkit.adapt(kindNumbers, true, true);MaxText = new Text(composite4, SWT.BORDER | SWT.CENTER);MaxText.setFont(SWTResourceManager.getFont(微软雅黑, 12, SWT.NORMAL);MaxText.setBounds(118, 56, 61, 23);MaxText.setData(name, MaxText);formToolkit.adapt(MaxText, true, true);AllocationText = new Text(composite4,
24、 SWT.BORDER | SWT.CENTER);AllocationText.setFont(SWTResourceManager.getFont(微软雅黑, 12, SWT.NORMAL);AllocationText.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN);AllocationText.setBounds(118, 87, 61, 23);AllocationText.setData(name, AllocationText);formToolkit.adapt(AllocationText, true, t
25、rue);theProcess = new Text(composite4, SWT.READ_ONLY | SWT.CENTER);theProcess.setFont(SWTResourceManager.getFont(微软雅黑, 12, SWT.NORMAL);theProcess.setBounds(52, 23, 22, 26);theProcess.setData(name, theProcess);formToolkit.adapt(theProcess, true, true);theProcess.setText(P+String.valueOf(0);theKind2 =
26、 new Text(composite4, SWT.READ_ONLY | SWT.CENTER);theKind2.setFont(SWTResourceManager.getFont(微软雅黑, 12, SWT.NORMAL);theKind2.setBounds(136, 23, 43, 26);theKind2.setData(name, theKind2);formToolkit.adapt(theKind2, true, true);theKind2.setText(R+String.valueOf(0);processN = new Text(composite0, SWT.BO
27、RDER);processN.setBackground(SWTResourceManager.getColor(0, 255, 0);processN.setBounds(107, 21, 73, 23);processN.setFont(SWTResourceManager.getFont(微软雅黑, 12, SWT.NORMAL);processN.setData(name, text);formToolkit.adapt(processN, true, true);kindM = new Text(composite0, SWT.BORDER);kindM.setBounds(107,
28、 65, 73, 23);kindM.setFont(SWTResourceManager.getFont(微软雅黑, 12, SWT.NORMAL);kindM.setData(name, text_1);formToolkit.adapt(kindM, true, true);/*-按钮-*/button1 = new Button(composite0, SWT.NONE);button1.setForeground(SWTResourceManager.getColor(0, 255, 0);button1.setBackground(SWTResourceManager.getCol
29、or(SWT.COLOR_GREEN);button1.setBounds(107, 115, 72, 31);button1.addSelectionListener(new SelectionAdapter() public void widgetSelected(SelectionEvent e) button1Event(););button1.setFont(SWTResourceManager.getFont(微软雅黑, 12, SWT.NORMAL);button1.setData(name, button);formToolkit.adapt(button1, true, tr
30、ue);button1.setText(确定);button2 = new Button(composite2, SWT.NONE);button2.setBackground(SWTResourceManager.getColor(50, 205, 50);button2.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN);button2.setBounds(107, 114, 74, 32);button2.addSelectionListener(new SelectionAdapter() public void wid
31、getSelected(SelectionEvent e) button2Event(););button2.setFont(SWTResourceManager.getFont(微软雅黑, 12, SWT.NORMAL);button2.setData(name, button_1);formToolkit.adapt(button2, true, true);button2.setText(确定);button3 = new Button(composite4, SWT.NONE);button3.addSelectionListener(new SelectionAdapter() pu
32、blic void widgetSelected(SelectionEvent e) button3Event(););button3.setFont(SWTResourceManager.getFont(微软雅黑, 12, SWT.NORMAL);button3.setBounds(114, 116, 65, 32);button3.setData(name, button_2);formToolkit.adapt(button3, true, true);button3.setText(确定);test = new Button(shell, SWT.NONE);test.setBound
33、s(627, 502, 80, 27);test.setSelection(true);test.addSelectionListener(new SelectionAdapter() public void widgetSelected(SelectionEvent e) TestEvent(););test.setFont(SWTResourceManager.getFont(微软雅黑, 12, SWT.NORMAL);test.setData(name, button_3);formToolkit.adapt(test, true, true);test.setText(立刻检验);/定
34、义创建相关数组函数public void Create(int n,int m) Max=new intnm; /创建最大资源需求量数组 Allocation=new intnm;/创建已获资源量数组 Need=new intnm; /创建需要资源量数组 Available=new intm; /创建可用资源量数组 Work=new intm; /创建可供资源量数组 Finish=new booleann; /创建进程可否完成标识数组 listText.insert(进程); for(int h=0;h4;h+)listText.insert(资源类型);printReKind();listT
35、ext.insert(n);/定义打印资源类型的函数public void printReKind()for(int h=0;hM;h+)if(h10)listText.insert(String.valueOf(R+h+ );else listText.insert(String.valueOf(R+h+ );/定义button1的事件函数public void button1Event()N=Integer.parseInt(processN.getText();M=Integer.parseInt(kindM.getText();Create(N,M); /调用Create(n,m)方法
36、stackLayout.topControl=composite2;composite1.layout();composite0.setEnabled(false);/定义button2的事件函数,设定进程每类资源的数量public void button2Event()if(iM)Availablei=Integer.parseInt(kindNumbers.getText();kindNumbers.setText();i+;if(i!=M)theKind1.setText(R+String.valueOf(i);if(i=M)stackLayout2.topControl=composi
37、te4;composite3.layout();composite2.setEnabled(false);/定义button3的事件函数public void button3Event()if(jN)if(kM)Maxjk=Integer.parseInt(MaxText.getText();Allocationjk=Integer.parseInt(AllocationText.getText();MaxText.setText();AllocationText.setText();Needjk=Maxjk-Allocationjk;Availablek=Availablek-Allocat
38、ionjk;k+;if(k=M)k=0; j+;Work=Available; if(jN) theProcess.setText(P+String.valueOf(j); theKind2.setText(R+String.valueOf(k); /如果输入完成,则输出刚输入的信息if(j=N&k=0)composite4.setEnabled(false);for(j=0;jN;j+)listText.insert( P+String.valueOf(j)+:);listText.insert(最大需求);for(int h=0;hM;h+)if(Maxjh10)listText.inse
39、rt(String.valueOf(Maxjh)+ );else listText.insert(String.valueOf(Maxjh+ );listText.insert(已获资源);for(int h=0;hM;h+) if(Allocationjh10)listText.insert(String.valueOf(Allocationjh+ );else listText.insert(String.valueOf(Allocationjh+ ); listText.insert(需要资源);for(int h=0;hM;h+)if(Needjh10)listText.insert(
40、String.valueOf(Needjh+ );else listText.insert(String.valueOf(Needjh+ );listText.insert(可用资源);for(int h=0;hM;h+) if(Availableh10)listText.insert(String.valueOf(Availableh+ );else listText.insert(String.valueOf(Availableh+ );listText.insert(n);/检测按钮监听函数调用的方法public void TestEvent()int x,y,z,count=0,countTrue=0;listText.insert(n);listText.insert(资源类型);printReKind();listText.insert(n可供资源);for(int h=0;hM;h+)if(Workh10) listText.insert(String.valueOf(Workh)+ ); else listText.insert(String.valueOf(Workh+ );listText.insert(n);listText.insert(进程);for