实验二 银行家算法Word文档下载推荐.docx
- 文档编号:19906865
- 上传时间:2023-01-12
- 格式:DOCX
- 页数:34
- 大小:211.61KB
实验二 银行家算法Word文档下载推荐.docx
《实验二 银行家算法Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《实验二 银行家算法Word文档下载推荐.docx(34页珍藏版)》请在冰豆网上搜索。
它表示系统是否有足够的资源分配给进程,使之运行完成,开始Finish(I)=false;
当有足够资源分配给进程Pi时,令Finish(i)=true;
2.从进程集合中找到一个能满足下述条件的进程。
Finish(i)==false;
Needi≤work;
如找到则执行步骤3;
否则,执行步骤4;
3.当进程Pi获得资源后,可顺利执行直到完成,并释放出分配给它的资源,故应执行
Work=work+Allocationi
Finish(i)=true;
转向步骤2;
4.若所有进程的Finish(i)都为true,则表示系统处于安全状态;
否则,系统处于不安全状态。
六、流程图
七、源程序代码
1.银行家算法类
packageBankerAlgorithm;
publicclassBanker{
privateint[]Available;
//可利用资源向量
privateint[]AllSource;
//总资源数
privateint[][]Max;
//最大需求矩阵
privateint[][]Allocation;
//已分配资源矩阵
privateint[][]Need;
//需求矩阵
privateint[]Work;
//工作向量
privateint[]Request;
//资源请求向量
privateint[]Temp;
//交换向量
//privateboolean[]Finish;
//判断进程是否可以得到资源
privateintprocessCount;
//进程数
privateintsourceCount;
//资源数
privateStringsafeList=newString();
privateintcount;
privateString[][]dataStrings;
privateintflag;
publicvoidinitBanker(intprocessCount,intsourceCount){
this.processCount=processCount;
this.sourceCount=sourceCount;
this.Max=newint[processCount][sourceCount];
this.Allocation=newint[processCount][sourceCount];
this.Need=newint[processCount][sourceCount];
this.Work=newint[sourceCount];
this.Request=newint[sourceCount];
//this.Finish=newboolean[processCount];
this.dataStrings=newString[processCount][6];
this.Temp=newint[processCount];
for(inti=0;
i<
processCount;
i++){
for(intj=0;
j<
6;
j++){
dataStrings[i][j]=newString();
}
}
}
publicvoidinitMaxAllocationNeed(int[][]max,int[][]allocation,int[][]need,introw){//初始化其余矩阵
sourceCount;
this.Max[row-1][i]=max[row-1][i];
this.Allocation[row-1][i]=allocation[row-1][i];
this.Need[row-1][i]=need[row-1][i];
publicvoidinitAvailable(intsourceCount,int[]available){//初始化可利用资源矩阵
this.Available=newint[sourceCount];
this.Available[i]=available[i];
publicvoidinitAllSource(intsourceCount,int[]available){//初始化资源总数矩阵
this.AllSource=newint[sourceCount];
this.AllSource[i]=available[i];
publicvoidinitTemp(int[]temp,introw){//初始化临时矩阵
this.Temp[row-1]=temp[row-1];
publicvoidinitRequest(int[]request){//初始化请求矩阵
this.Request[i]=request[i];
publicbooleanisEnoughAll(int[]max){//检查最大资源矩阵是否小于资源总数
if(max[i]>
AllSource[i]){
returnfalse;
returntrue;
publicbooleanisMaxAllocation(int[]max,int[]allocation){//检查已分配资源数是否小于最大资源数
if(allocation[i]>
max[i]){
publicbooleanisEnoughNeed(introw){//工作向量是否满足需求矩阵
if(Need[row][i]>
Work[i]){
this.flag=0;
this.flag=1;
publicbooleanisEnoughReq(introw){//请求矩阵是否满足需求矩阵
if(Need[row][i]<
Request[i]){
publicbooleanisEnoughAva(introw){//请求矩阵是否满足需求矩阵
if(Request[i]>
Available[i]){
publicbooleanisAvailable(int[]available){
if(available[i]<
0){
publicbooleancheckNeed(int[]need){//检查需求矩阵中资源是否都为0
if(need[i]!
=0){
}
publicbooleanisSafe(){//检查是否安全
inti;
for(i=0;
i<
processCount;
i++){
if(isEnoughNeed(Temp[i]-1)){
CountWork(Temp[i]-1);
continue;
elsebreak;
if(i==processCount)returntrue;
elsereturnfalse;
publicvoidCountWork(introw){//计算工作向量
Work[i]=Work[i]+Allocation[row][i];
publicvoidgetSafeList(intk,intn){//递归求安全序列
intm,a;
if(k==n){
for(intj=0;
j<
sourceCount;
j++)
Work[j]=Available[j];
if(isSafe()){
for(a=0;
a<
processCount-1;
a++)
safeList+=Temp[a]+"
->
"
;
safeList+=Temp[a]+"
+"
\n"
count++;
for(intj=0;
j<
j++)
Work[j]=Available[j];
}else{
for(inti=k;
i<
=n;
i++){//排列
m=Temp[k];
Temp[k]=Temp[i];
Temp[i]=m;
getSafeList(k+1,n);
publicvoidchangeDataString(){//设置数据
Strings_available=newString();
Strings_allocation=newString();
Strings_need=newString();
Strings_max=newString();
s_available+=this.Available[j]+"
"
s_allocation+=this.Allocation[i][j]+"
s_need+=this.Need[i][j]+"
s_max+=this.Max[i][j]+"
this.dataStrings[i][1]=i+1+"
this.dataStrings[i][2]=s_max;
this.dataStrings[i][3]=s_allocation;
this.dataStrings[i][4]=s_need;
this.dataStrings[0][5]=s_available;
publicvoidallocateSource(introw){
if(checkNeed(Need[i])){
for(intj=0;
this.Available[j]+=this.Allocation[i][j];
this.Allocation[i][j]=0;
this.Max[i][j]=0;
}
for(inti=0;
this.Available[i]=this.Available[i]-this.Request[i];
this.Allocation[row-1][i]=this.Allocation[row-1][i]+this.Request[i];
this.Need[row-1][i]=this.Need[row-1][i]-this.Request[i];
changeDataString();
publicbooleanrequestSource(introw){
count=0;
if(isEnoughReq(row-1)&
&
isEnoughAva(row-1)){
allocateSource(row);
returntrue;
returnfalse;
publicint[]getAvailable(){
returnAvailable;
publicvoidsetAvailable(int[]available){
Available=available;
publicStringgetSafeList(){
returnsafeList;
publicvoidsetSafeList(StringsafeList){
this.safeList=safeList;
publicintgetCount(){
returncount;
publicvoidsetCount(intcount){
this.count=count;
publicString[][]getDataStrings(){
returndataStrings;
publicvoidsetDataStrings(String[][]dataStrings){
this.dataStrings=dataStrings;
publicintgetFlag(){
returnflag;
publicvoidsetFlag(intflag){
this.flag=flag;
}
2.界面类
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Shell;
importorg.eclipse.swt.SWT;
importorg.eclipse.swt.widgets.Label;
importorg.eclipse.swt.widgets.MessageBox;
importorg.eclipse.swt.widgets.TableItem;
importorg.eclipse.swt.widgets.Text;
importorg.eclipse.swt.widgets.Button;
importorg.eclipse.swt.widgets.Table;
importorg.eclipse.swt.widgets.TableColumn;
importorg.eclipse.swt.events.SelectionAdapter;
importorg.eclipse.swt.events.SelectionEvent;
importorg.eclipse.swt.custom.ScrolledComposite;
importorg.eclipse.wb.swt.SWTResourceManager;
publicclassBankerSWT{
protectedShellshell;
privateTextsourceCountText;
privateTextproCountText;
privateTextcountText;
privateText[]sourceTexts=newText[10];
privateText[]maxTexts=newText[10];
privateText[]allocationTexts=newText[10];
privateText[]requestTexts=newText[10];
privateButtonbutton;
privateLabellabel_2;
privateButtonbutton_1;
privateLabellabel_3;
privateLabellabel_4;
privateButtonbutton_2;
privateLabellblNewLabel;
privateTabletable;
privateTextrequsetText;
privateTextsecurityText;
privateintrow;
privateBankerbanker=newBanker();
/**
*Launchtheapplication.
*@paramargs
*/
publicstaticvoidmain(String[]args){
try{
BankerSWTwindow=newBankerSWT();
window.open();
}catch(Exceptione){
e.printStackTrace();
*Openthewindow.
publicvoidopen(){
Displaydisplay=Display.getDefault();
createContents();
shell.open();
shell.layout();
while(!
shell.isDisposed()){
if(!
display.readAndDispatch()){
display.sleep();
*Createcontentsofthewindow.
protectedvoidcreateContents(){
shell=newShell();
shell.setSize(550,475);
shell.setText("
银行家算法"
);
Labellabel=newLabel(shell,SWT.NONE);
label.setBounds(10,10,84,17);
label.setText("
\u8BF7\u8F93\u5165\u8D44\u6E90\u6570\uFF1A"
sourceCountText=newText(shell,SWT.BORDER);
sourceCountText.setBounds(100,10,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验二 银行家算法 实验 银行家 算法
![提示](https://static.bdocx.com/images/bang_tan.gif)