1、它表示系统是否有足够的资源分配给进程,使之运行完成,开始Finish(I)=false;当有足够资源分配给进程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银行家
2、算法类package BankerAlgorithm;public class Banker 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;/判断进程是否可以得到资源 pri
3、vate int processCount;/进程数 private int sourceCount;/资源数 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 i
4、ntprocessCountsourceCount; this.Allocation = 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 intprocessCo
5、unt; for (int i = 0; i processCount; i+) for (int j = 0; j AllSourcei) return false; return true; public boolean isMaxAllocation(int max,int allocation) /检查已分配资源数是否小于最大资源数 if ( allocationi maxi) public boolean isEnoughNeed(int row) /工作向量是否满足需求矩阵 if (Needrowi Worki) this.flag = 0; this.flag = 1; publ
6、ic boolean isEnoughReq(int row) /请求矩阵是否满足需求矩阵 if (Needrowi Availablei) public boolean isAvailable(int available) if (availablei 0) public boolean checkNeed(int need) /检查需求矩阵中资源是否都为0 if (needi!=0) public boolean isSafe() /检查是否安全 int i; for( i=0; iprocessCount ;i+) if(isEnoughNeed(Tempi-1) CountWork(T
7、empi-1); continue; else break; if(i=processCount) return true; else return false; public void CountWork(int row) /计算工作向量 Worki = Worki + Allocationrowi; public void getSafeList(int k, int n) /递归求安全序列 int m,a; if( k = n ) for(int j=0;jsourceCount;j+) Workj = Availablej; if(isSafe() for(a=0 ; a; safeL
8、ist += Tempa+n count+; for(int j=0; j j+) Workj = Availablej; else for(int i=k;i=n;i+) /排列 m=Tempk; Tempk=Tempi; Tempi=m; getSafeList(k+1, n); public void changeDataString() /设置数据 String s_available=new String(); String s_allocation = new String(); String s_need = new String(); String s_max = new St
9、ring(); s_available += this.Availablej + s_allocation += this.Allocationij + s_need += this.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 allocateSourc
10、e(int row) if (checkNeed(Needi) for (int j = 0; this.Availablej += this.Allocationij; this.Allocationij = 0; this.Maxij = 0; for(int i = 0; this.Availablei = this.Availablei-this.Requesti; this.Allocationrow-1i = this.Allocationrow-1i+this.Requesti; this.Needrow-1i = this.Needrow-1i-this.Requesti; c
11、hangeDataString(); public boolean requestSource(int row) count = 0; if (isEnoughReq(row-1) & isEnoughAva(row-1) allocateSource(row); return true; return false; public int getAvailable() return Available; public void setAvailable(int available) Available = available; public String getSafeList() retur
12、n safeList; 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; pub
13、lic int getFlag() return flag; public void setFlag(int flag) this.flag = flag;2界面类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.swt.widgets.Table
14、Item;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.ScrolledComposite;i
15、mport 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 Text10; private Tex
16、t 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 Text securityText; private int row; private Ban
17、ker 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(); * Open the window. public void open() Display display = Display.getDefault(); createContents();
18、 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