1、实验二 银行家算法实验二 银行家算法一、实验目的死锁会引起计算机工作僵死,因此操作系统中必须防止。本实验的目的在于让学生独立的使用高级语言编写和调试一个系统动态分配资源的简单模拟程序,了解死锁产生的条件和原因,并采用银行家算法有效地防止死锁的发生,以加深对课堂上所讲授的知识的理解。二、实验要求设计有n个进程共享m个系统资源的系统,进程可动态的申请和释放资源,系统按各进程的申请动态的分配资源。系统能显示各个进程申请和释放资源,以及系统动态分配资源的过程,便于用户观察和分析;三、数据结构1 可利用资源向量Available ,它是一个含有m个元素的数组,其中的每一个元素代表一类可利用的资源的数目,
2、其初始值是系统中所配置的该类全部可用资源数目。其数值随该类资源的分配和回收而动态地改变。如果Available(j)=k,标是系统中现有Rj类资源k个。2 最大需求矩阵Max,这是一个nm的矩阵,它定义了系统中n个进程中的每一个进程对m类资源的最大需求。如果Max(i,j)=k,表示进程i需要Rj类资源的最大数目为k。3 分配矩阵Allocation,这是一个nm的矩阵,它定义了系统中的每类资源当前一分配到每一个进程的资源数。如果Allocation(i,j)=k,表示进程i当前已经分到Rj类资源的数目为k。Allocation i表示进程i的分配向量,有矩阵Allocation的第i行构成。
3、4 需求矩阵Need,这是一个nm的矩阵,用以表示每个进程还需要的各类资源的数目。如果Need(i,j)=k,表示进程i还需要Rj类资源k个,才能完成其任务。Need i表示进程i的需求向量,由矩阵Need的第i行构成。上述三个矩阵间存在关系:Need(i,j)=Max(i,j)-Allocation(i,j);四、银行家算法参考教材P96五、安全性算法1 设置两个向量。Work:它表示系统可提供给进程继续运行的各类资源数目,它包含m个元素,开始执行安全性算法时,Work = Available。Finish:它表示系统是否有足够的资源分配给进程,使之运行完成,开始Finish(I)=fals
4、e;当有足够资源分配给进程Pi时,令Finish(i)=true;2 从进程集合中找到一个能满足下述条件的进程。Finish(i)= = false;Need i work;如找到则执行步骤3;否则,执行步骤4;3 当进程Pi获得资源后,可顺利执行直到完成,并释放出分配给它的资源,故应执行Work = work + Allocation i Finish(i)=true;转向步骤2;4 若所有进程的Finish(i)都为true,则表示系统处于安全状态;否则,系统处于不安全状态。六、流程图七、源程序代码1银行家算法类package BankerAlgorithm;public class Ba
5、nker private int Available;/可利用资源向量 private int AllSource;/总资源数 private int Max;/最大需求矩阵 private int Allocation;/已分配资源矩阵 private int Need;/需求矩阵 private int Work;/工作向量 private int Request;/资源请求向量 private int Temp;/交换向量/ private boolean Finish;/判断进程是否可以得到资源 private int processCount;/进程数 private int sou
6、rceCount;/资源数 private String safeList = new String(); private int count; private String dataStrings; private int flag; public void initBanker(int processCount,int sourceCount) this.processCount = processCount; this.sourceCount = sourceCount; this.Max = new intprocessCountsourceCount; this.Allocation
7、 = new intprocessCountsourceCount; this.Need = new intprocessCountsourceCount; this.Work = new intsourceCount; this.Request = new intsourceCount;/ this.Finish = new booleanprocessCount; this.dataStrings = new StringprocessCount6; this.Temp = new intprocessCount; for (int i = 0; i processCount; i+) f
8、or (int j = 0; j 6; j+) dataStringsij = new String(); public void initMaxAllocationNeed(int max,int allocation,int need,int row) /初始化其余矩阵 for (int i = 0; i sourceCount; i+) this.Maxrow-1i = maxrow-1i; this.Allocationrow-1i = allocationrow-1i; this.Needrow-1i = needrow-1i; public void initAvailable(i
9、nt sourceCount,int available)/初始化可利用资源矩阵 this.Available = new intsourceCount; for (int i = 0; i sourceCount; i+) this.Availablei = availablei; public void initAllSource(int sourceCount,int available) /初始化资源总数矩阵 this.AllSource = new intsourceCount; for (int i = 0; i sourceCount; i+) this.AllSourcei =
10、 availablei; public void initTemp(int temp,int row) /初始化临时矩阵 this.Temprow-1 = temprow-1; public void initRequest(int request) /初始化请求矩阵 this.Request = new intsourceCount; for (int i = 0; i sourceCount; i+) this.Requesti = requesti; public boolean isEnoughAll(int max) /检查最大资源矩阵是否小于资源总数 for (int i = 0;
11、 i AllSourcei) return false; return true; public boolean isMaxAllocation(int max,int allocation) /检查已分配资源数是否小于最大资源数 for (int i = 0; i maxi) return false; return true; public boolean isEnoughNeed(int row) /工作向量是否满足需求矩阵 for (int i = 0; i Worki) this.flag = 0; return false; this.flag = 1; return true;
12、public boolean isEnoughReq(int row) /请求矩阵是否满足需求矩阵 for (int i = 0; i sourceCount; i+) if (Needrowi Requesti) this.flag = 0; return false; this.flag = 1; return true; public boolean isEnoughAva(int row) /请求矩阵是否满足需求矩阵 for (int i = 0; i Availablei) this.flag = 0; return false; this.flag = 1; return true
13、; public boolean isAvailable(int available) for (int i = 0; i sourceCount; i+) if (availablei 0) return false; return true; public boolean checkNeed(int need) /检查需求矩阵中资源是否都为0 for (int i = 0; i sourceCount; i+) if (needi!=0) return false; return true; public boolean isSafe() /检查是否安全 int i; for( i=0;
14、iprocessCount ;i+) if(isEnoughNeed(Tempi-1) CountWork(Tempi-1); continue; else break; if(i=processCount) return true; else return false; public void CountWork(int row) /计算工作向量 for (int i = 0; i sourceCount; i+) Worki = Worki + Allocationrowi; public void getSafeList(int k, int n) /递归求安全序列 int m,a; i
15、f( k = n ) for(int j=0;jsourceCount;j+) Workj = Availablej; if(isSafe() for(a=0 ; a; safeList += Tempa+n; count+; for(int j=0; jsourceCount; j+) Workj = Availablej; else for(int i=k;i=n;i+) /排列 m=Tempk; Tempk=Tempi; Tempi=m; getSafeList(k+1, n); m=Tempk; Tempk=Tempi; Tempi=m; public void changeDataS
16、tring() /设置数据 for (int i = 0; i processCount; i+) String s_available=new String(); String s_allocation = new String(); String s_need = new String(); String s_max = new String(); for (int j = 0; j sourceCount; j+) s_available += this.Availablej + ; s_allocation += this.Allocationij + ; s_need += this
17、.Needij + ; s_max += this.Maxij + ; this.dataStringsi1 = i+1+; this.dataStringsi2 = s_max; this.dataStringsi3 = s_allocation; this.dataStringsi4 = s_need; this.dataStrings05 = s_available; public void allocateSource(int row) for (int i = 0; i processCount; i+) if (checkNeed(Needi) for (int j = 0; j
18、sourceCount; j+) this.Availablej += this.Allocationij; this.Allocationij = 0; this.Maxij = 0; for(int i = 0; i sourceCount; i+) this.Availablei = this.Availablei-this.Requesti; this.Allocationrow-1i = this.Allocationrow-1i+this.Requesti; this.Needrow-1i = this.Needrow-1i-this.Requesti; changeDataStr
19、ing(); public boolean requestSource(int row) count = 0; if (isEnoughReq(row-1) & isEnoughAva(row-1) allocateSource(row); return true; else return false; public int getAvailable() return Available; public void setAvailable(int available) Available = available; public String getSafeList() return safeL
20、ist; public void setSafeList(String safeList) this.safeList = safeList; public int getCount() return count; public void setCount(int count) this.count = count; public String getDataStrings() return dataStrings; public void setDataStrings(String dataStrings) this.dataStrings = dataStrings; public int
21、 getFlag() return flag; public void setFlag(int flag) this.flag = flag; 2界面类package BankerAlgorithm;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.SWT;import org.eclipse.swt.widgets.Label;import org.eclipse.swt.widgets.MessageBox;import org.eclipse
22、.swt.widgets.TableItem;import org.eclipse.swt.widgets.Text;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Table;import org.eclipse.swt.widgets.TableColumn;import org.eclipse.swt.events.SelectionAdapter;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.custom.S
23、crolledComposite;import org.eclipse.wb.swt.SWTResourceManager;public class BankerSWT protected Shell shell; private Text sourceCountText; private Text proCountText; private Text countText; private Text sourceTexts = new Text10; private Text maxTexts = new Text10; private Text allocationTexts = new T
24、ext10; private Text requestTexts = new Text10; private Button button; private Label label_2; private Button button_1; private Label label_3; private Label label_4; private Button button_2; private Label lblNewLabel; private Table table; private Text requsetText; private String dataStrings; private T
25、ext securityText; private int processCount; private int sourceCount; private int row; private Banker banker = new Banker(); /* * Launch the application. * param args */ public static void main(String args) try BankerSWT window = new BankerSWT(); window.open(); catch (Exception e) e.printStackTrace()
26、; /* * Open the window. */ public void open() Display display = Display.getDefault(); createContents(); shell.open(); shell.layout(); while (!shell.isDisposed() if (!display.readAndDispatch() display.sleep(); /* * Create contents of the window. */ protected void createContents() shell = new Shell(); shell.setSize(550, 475); shell.setText(银行家算法); Label label = new Label(shell, SWT.NONE); label.setBounds(10, 10, 84, 17); label.setText(u8BF7u8F93u5165u8D44u6E90u6570uFF1A); sourceCountText = new Text(shell, SWT.BORDER); sourceCountText.setBounds(100, 10,
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1