當前位置:首頁 » 操作系統 » c實現銀行家演算法

c實現銀行家演算法

發布時間: 2023-01-06 10:50:56

1. 銀行家演算法

銀行家演算法
#include "dos.h"
#include "conio.h"
#include "alloc.h"

int available[3]={3,3,2};
int max[6][3]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
int allocation[6][3]={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
int need[6][3]={{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}};
int live[5]={1,1,1,1,1};
int stay[3][3];
int request[3],process[5];
int i,j,p=5,pro,x,k1;
void biao()
{ x=1;
printf(" Max All Nee Ava\n");
printf("PRO A B C A B C A B C A B C\n");
for(i=0;i<p;i++)
{ if(live[i]==1)
{ printf("P%d ",i);
for(j=0;j<=2;j++)
{printf("%-3d",max[i][j]);}printf(" ");
for(j=0;j<=2;j++)
{printf("%-3d",allocation[i][j]);}printf(" ");
for(j=0;j<=2;j++)
{printf("%-3d",need[i][j]);}printf(" ");
}
if(x==1&&live[i]==1) {printf("%-3d%-3d%-3d",available[0],available[1],available[2]);x=0;}
if(live[i]==1) printf("\n");
}
}
int apply()
{ int v=1;
for(i=0;i<=2;i++)
{if(need[pro][i]<request[i]) {v=0;break;}
if(available[i]<request[i]) {v=0;break;}}
if(v==1){for(i=0;i<=2;i++)
{stay[0][i]=allocation[pro][i];allocation[pro][i]+=request[i];
stay[1][i]=need[pro][i];need[pro][i]-=request[i];
stay[2][i]=available[i];available[i]-=request[i];}
}
return(v);
}
int test()
{ int work[3],finish[5]={0,0,0,0,0},v,k=0,t=0;
for(i=0;i<p;i++)
if(live[i]==0) {finish[i]=1;k1--;}
for(i=0;i<=2;i++) work[i]=available[i];
while(1)
{ for(i=0;i<=4;i++)
{ if(finish[i]==0) { v=1;
for(j=0;j<=2;j++)
if(need[i][j]>work[j]) {v=0;break;}
if(v==1) { finish[i]=1;
for(j=0;j<=2;j++) work[j]+=allocation[i][j];
process[k]=i;k++;
}
}
}
if(t==k) break;
else t=k;
if(k==k1) break;
}
if(k==k1) return(1);
else return(0);

}
main()
{
while(1)
{biao();
k1=5;
printf("this system have five processes{0,1,2,3,4},please input one of the processes:P");
scanf("%d",&pro);
if(pro<0||pro>4) break;
printf("P%d three resource of request(,)thay are:",pro);
scanf("%d,%d,%d",&request[0],&request[1],&request[2]);
if(apply()==0) {printf("error or resource are not enough,P%d wait!\n",pro);
printf("please press any key to continue");}
else if(test()==0) {printf("system is not safe,P%d wait!\n",pro);
for(i=0;i<=2;i++)
{ allocation[pro][i]=stay[0][i];
need[pro][i]=stay[1][i];
available[i]=stay[2][i];}
printf("please press any key to continue");}
else {biao();printf("system is safe!\n");
printf("exist a safe order is:");
for(i=0;i<k1-1;i++)
printf("P%d>",process[i]);
printf("P%d",process[k1-1]);
printf("\n please press any key to continue");}
if(need[pro][0]==0&&need[pro][1]==0&&need[pro][2]==0)
for(i=0;i<=2;i++)
{available[i]+=max[pro][i];
live[pro]=0;}
getch();

delay(5000);
}
}

2. 銀行家演算法資源申請不成功

銀行家演算法是一種最有代表性的避免死鎖的演算法。把操作系統看作是銀行家,操作系統管理的資源相當於銀行家管理的資金,進程向操作系統請求分配資源相當於用戶向銀行家貸款。操作系統按照銀行家制定的規則為進程分配資源,當進程首次申請資源時,要測試該進程對資源的最大需求量,如果系統現存的資源可以滿足它的最大需求量則按當前的申請量分配資源,否則就推遲分配。當進程在執行中繼續申請資源時,先測試該進程已佔用的資源數與本次申請的資源數之和是否超過了該進程對資源的最大需求量。若超過則拒絕分配資源,若沒有超過則再測試系統現存的資源能否滿足該進程尚需的最大資源量,若能滿足則按當前的申請量分配資源,否則也要推遲分配。
本次課程設計通過用c語言編寫和調試實現銀行家演算法的程序,達到進一步掌握銀行家演算法,理解系統產生死鎖的原因以及系統避免死鎖的方法,增強理論聯系實際的能力的目的。

3. 在C++中,編寫的銀行家演算法中有以下的語句,麻煩幫忙解釋這3個語句,並用java實現,怎麼實現,謝謝先!!

在某時刻系統中所有進程可以排列一個安全序列:,剛稱此時,系統是安全的.
所謂安全序列是指對於P2,都有它所需要剩餘資源數量不大於系統掌握的剩餘的空間資源與所有Pi(j<i)所佔的資源之和.
2.不安全狀態可能產生死鎖.
目前狀態 最大需求 尚需
P1 3 9 6
P2 5 10 5
P3 2 4 2

在每一次進程中申請的資源,判定一下,若實際分配的話,之後系統是否安全.
3.銀行家演算法的思路:
1),進程一開始向系統提出最大需求量.
2),進程每次提出新的需求(分期貸款)都統計是否超出它事先提出的最大需求量.
3),若正常,則判斷該進程所需剩餘剩餘量(包括本次申請)是否超出系統所掌握的
剩餘資源量,若不超出,則分配,否則等待.
4.銀行家演算法的數據結構.
1),系統剩餘資源量A[n],其中A[n]表示第I類資源剩餘量.
2),各進程最大需求量,B[m][n],其中B[j][i]表示進程j對i
類資源最大需求.
3),已分配資源量C[m][n],其中C[j][i]表示系統j程已得到的第i資源的數量.
4),剩餘需求量.D[m][n],其中D[j][i]對第i資源尚需的數目.
5.銀行家演算法流程:當某時刻,某進程時,提出新的資源申請,系統作以下操作:
1),判定E[n]是否大於D[j][n],若大於,表示出錯.
2),判定E[n]是否大於系統剩餘量A[n],若大於,則該進程等待.
3),若以上兩步沒有問題,嘗試分配,即各變數作調整.
4),按照安全性推測演算法,判斷,分配過後,系統是否安全,若安全,則實際分配,否則,撤消分配,讓進程等待.
6."安全性檢測"演算法
1),先定義兩個變數,用來表示推算過程的數據.
F[n]=A[n],表示推算過程中,系統中剩餘資源量的變化.
J[n]=False表示推算過程中各進程是否假設"已完成"
2),流程:
在"剩餘"的進程中(在推算)過程中,一些進程假設已完成,查找D[j][n]<=F[n]的進程,找到後令J[j]=True
(假設該進程完成),F[n]+D[j][n](該進程所佔資源釋放),如此循環執行.
若最後,所有的F[n]=True(在推算過程中,所有進程均可以完成),則表示(分配過後)系統是安全的,否則系統是不安全的.

#include "malloc.h"
#include "stdio.h"
#define alloclen sizeof(struct allocation)
#define maxlen sizeof(struct max)
#define avalen sizeof(struct available)
#define needlen sizeof(struct need)
#define finilen sizeof(struct finish)
#define pathlen sizeof(struct path)
struct allocation
{
int value;
struct allocation *next;
};
struct max
{
int value;
struct max *next;
};
struct available
{
int value;
struct available *next;
};
struct need
{
int value;
struct need *next;
};
struct path
{
int value;
struct path *next;
};
struct finish
{
int stat;
struct finish *next;
};
int main()
{
int row,colum,status=0,i,j,t,temp,processtest;
struct allocation *allochead,*alloc1,*alloc2,*alloctemp;
struct max *maxhead,*maxium1,*maxium2,*maxtemp;
struct available *avahead,*available1,*available2,*availabletemp,*workhead,*work1,*work2,*worktemp,*worktemp1;
struct need *needhead,*need1,*need2,*needtemp;
struct finish *finihead,*finish1,*finish2,*finishtemp;
struct path *pathhead,*path1,*path2,*pathtemp;
char c;
printf("\nPlease enter the type of sources the system has:\n");
scanf("%d",&colum);
printf("Please enter the number of processes now in the memory:\n");
scanf("%d",&row);
printf("Please enter the allocation array:\n");
for(i=0;i<row;i++)
{
printf("The allocation for process p%d:\n",i);
for (j=0;j<colum;j++)
{
printf("The type %c system resource allocated:\n",'A'+j);
if(status==0)
{
allochead=alloc1=alloc2=(struct allocation*)malloc(alloclen);
alloc1->next=alloc2->next=NULL;
scanf("%d",&allochead->value);
status++;
}
else
{
alloc2=(struct allocation *)malloc(alloclen);
scanf("%d,%d",&alloc2->value);
if(status==1)
{
allochead->next=alloc2;
status++;
}
alloc1->next=alloc2;
alloc1=alloc2;
}
}
}
alloc2->next=NULL;
status=0;
printf("Please enter the max array:\n");
for(i=0;i<row;i++)
{
printf("The max needed from process p%d:\n",i);
for (j=0;j<colum;j++)
{
printf("The type %c maxium system resource may needed:\n",'A'+j);
if(status==0)
{
maxhead=maxium1=maxium2=(struct max*)malloc(maxlen);
maxium1->next=maxium2->next=NULL;
scanf("%d",&maxium1->value);
status++;
}
else
{
maxium2=(struct max *)malloc(maxlen);
scanf("%d,%d",&maxium2->value);
if(status==1)
{
maxhead->next=maxium2;
status++;
}
maxium1->next=maxium2;
maxium1=maxium2;
}
}
}
maxium2->next=NULL;
status=0;
printf("Please enter the available array now exists in the system:\n");
for (j=0;j<colum;j++)
{
printf("The type %c available system resource number:\n",'A'+j);
if(status==0)
{
avahead=available1=available2=(struct available*)malloc(avalen);
workhead=work1=work2=(struct available*)malloc(avalen);
available1->next=available2->next=NULL;
work1->next=work2->next=NULL;
scanf("%d",&available1->value);
work1->value=available1->value;
status++;
}
else
{
available2=(struct available*)malloc(avalen);
work2=(struct available*)malloc(avalen);
scanf("%d,%d",&available2->value);
work2->value=available2->value;
if(status==1)
{
avahead->next=available2;
workhead->next=work2;
status++;
}
available1->next=available2;
available1=available2;
work1->next=work2;
work1=work2;
}
}
available2->next=NULL;
work2->next=NULL;
status=0;
alloctemp=allochead;
maxtemp=maxhead;
for(i=0;i<row;i++)
for (j=0;j<colum;j++)
{
if(status==0)
{
needhead=need1=need2=(struct need*)malloc(needlen);
need1->next=need2->next=NULL;
need1->value=maxtemp->value-alloctemp->value;
status++;
}
else
{
need2=(struct need *)malloc(needlen);
need2->value=(maxtemp->value)-(alloctemp->value);
if(status==1)
{
needhead->next=need2;
status++;
}
need1->next=need2;
need1=need2;
}
maxtemp=maxtemp->next;
alloctemp=alloctemp->next;
}
need2->next=NULL;
status=0;
for(i=0;i<row;i++)
{
if(status==0)
{
finihead=finish1=finish2=(struct finish*)malloc(finilen);
finish1->next=finish2->next=NULL;
finish1->stat=0;
status++;
}
else
{
finish2=(struct finish*)malloc(finilen);
finish2->stat=0;
if(status==1)
{
finihead->next=finish2;
status++;
}
finish1->next=finish2;
finish1=finish2;
}
}
finish2->next=NULL; /*Initialization compleated*/
status=0;
processtest=0;
for(temp=0;temp<row;temp++)
{
alloctemp=allochead;
needtemp=needhead;
finishtemp=finihead;
worktemp=workhead;
for(i=0;i<row;i++)
{
worktemp1=worktemp;
if(finishtemp->stat==0)
{
for(j=0;j<colum;j++,needtemp=needtemp->next,worktemp=worktemp->next)
if(needtemp->value<=worktemp->value)
processtest++;
if(processtest==colum)
{
for(j=0;j<colum;j++)
{
worktemp1->value+=alloctemp->value;
worktemp1=worktemp1->next;
alloctemp=alloctemp->next;
}
if(status==0)
{
pathhead=path1=path2=(struct path*)malloc(pathlen);
path1->next=path2->next=NULL;
path1->value=i;
status++;
}
else
{
path2=(struct path*)malloc(pathlen);
path2->value=i;
if(status==1)
{
pathhead->next=path2;
status++;
}
path1->next=path2;
path1=path2;
}
finishtemp->stat=1;
}
else
{
for(t=0;t<colum;t++)
alloctemp=alloctemp->next;
finishtemp->stat=0;
}
}
else
for(t=0;t<colum;t++)
{
needtemp=needtemp->next;
alloctemp=alloctemp->next;
}
processtest=0;
worktemp=workhead;
finishtemp=finishtemp->next;
}
}
path2->next=NULL;
finishtemp=finihead;
for(temp=0;temp<row;temp++)
{
if(finishtemp->value==0)
{
printf("\nWARNING,the system is in nonsafe status!\n");
exit(0);
}
finishtemp=finishtemp->next;
}
printf("\nThe system is in safe status!\n");
printf("\nThe safe sequence is: \n");
do
{
printf("p%d ",pathhead->value);
}
while(pathhead=pathhead->next);
}

4. 求大神!怎樣用C/C++實現銀行家演算法,並實現可視化的運行界面,界面清楚地反映出系統的運行結果

用MFC(C++)可以實現

5. 高分求銀行家演算法c語言版

#include "malloc.h"
#include "stdio.h"
#include "stdlib.h"
#define alloclen sizeof(struct allocation)
#define maxlen sizeof(struct max)
#define avalen sizeof(struct available)
#define needlen sizeof(struct need)
#define finilen sizeof(struct finish)
#define pathlen sizeof(struct path)
struct allocation
{
int value;
struct allocation *next;
};
struct max
{
int value;
struct max *next;
};
struct available /*可用資源數*/
{
int value;
struct available *next;
};
struct need /*需求資源數*/
{
int value;
struct need *next;
};
struct path
{
int value;
struct path *next;
};
struct finish
{
int stat;
struct finish *next;
};
int main()
{
int row,colum,status=0,i,j,t,temp,processtest;
struct allocation *allochead,*alloc1,*alloc2,*alloctemp;
struct max *maxhead,*maxium1,*maxium2,*maxtemp;
struct available *avahead,*available1,*available2,*workhead,*work1,*work2,*worktemp,*worktemp1;
struct need *needhead,*need1,*need2,*needtemp;
struct finish *finihead,*finish1,*finish2,*finishtemp;
struct path *pathhead,*path1,*path2;
printf("\n請輸入系統資源的種類數:");
scanf("%d",&colum);
printf("請輸入現時內存中的進程數:");
scanf("%d",&row);
printf("請輸入已分配資源矩陣:\n");
for(i=0;i<row;i++)
{
for (j=0;j<colum;j++)
{
printf("請輸入已分配給進程 p%d 的 %c 種系統資源:",i,'A'+j);
if(status==0)
{
allochead=alloc1=alloc2=(struct allocation*)malloc(alloclen);
alloc1->next=alloc2->next=NULL;
scanf("%d",&allochead->value);
status++;
}
else
{
alloc2=(struct allocation *)malloc(alloclen);
scanf("%d,%d",&alloc2->value);
if(status==1)
{
allochead->next=alloc2;
status++;
}
alloc1->next=alloc2;
alloc1=alloc2;
}
}
}
alloc2->next=NULL;
status=0;
printf("請輸入最大需求矩陣:\n");
for(i=0;i<row;i++)
{
for (j=0;j<colum;j++)
{
printf("請輸入進程 p%d 種類 %c 系統資源最大需求:",i,'A'+j);
if(status==0)
{
maxhead=maxium1=maxium2=(struct max*)malloc(maxlen);
maxium1->next=maxium2->next=NULL;
scanf("%d",&maxium1->value);
status++;
}
else
{
maxium2=(struct max *)malloc(maxlen);
scanf("%d,%d",&maxium2->value);
if(status==1)
{
maxhead->next=maxium2;
status++;
}
maxium1->next=maxium2;
maxium1=maxium2;
}
}
}
maxium2->next=NULL;
status=0;
printf("請輸入現時系統剩餘的資源矩陣:\n");
for (j=0;j<colum;j++)
{
printf("種類 %c 的系統資源剩餘:",'A'+j);
if(status==0)
{
avahead=available1=available2=(struct available*)malloc(avalen);
workhead=work1=work2=(struct available*)malloc(avalen);
available1->next=available2->next=NULL;
work1->next=work2->next=NULL;
scanf("%d",&available1->value);
work1->value=available1->value;
status++;
}
else
{
available2=(struct available*)malloc(avalen);
work2=(struct available*)malloc(avalen);
scanf("%d,%d",&available2->value);
work2->value=available2->value;
if(status==1)
{
avahead->next=available2;
workhead->next=work2;
status++;
}
available1->next=available2;
available1=available2;
work1->next=work2;
work1=work2;
}
}
available2->next=NULL;
work2->next=NULL;
status=0;
alloctemp=allochead;
maxtemp=maxhead;
for(i=0;i<row;i++)
for (j=0;j<colum;j++)
{
if(status==0)
{
needhead=need1=need2=(struct need*)malloc(needlen);
need1->next=need2->next=NULL;
need1->value=maxtemp->value-alloctemp->value;
status++;
}
else
{
need2=(struct need *)malloc(needlen);
need2->value=(maxtemp->value)-(alloctemp->value);
if(status==1)
{
needhead->next=need2;
status++;
}
need1->next=need2;
need1=need2;
}
maxtemp=maxtemp->next;
alloctemp=alloctemp->next;
}
need2->next=NULL;
status=0;
for(i=0;i<row;i++)
{
if(status==0)
{
finihead=finish1=finish2=(struct finish*)malloc(finilen);
finish1->next=finish2->next=NULL;
finish1->stat=0;
status++;
}
else
{
finish2=(struct finish*)malloc(finilen);
finish2->stat=0;
if(status==1)
{
finihead->next=finish2;
status++;
}
finish1->next=finish2;
finish1=finish2;
}
}
finish2->next=NULL; /*Initialization compleated*/
status=0;
processtest=0;
for(temp=0;temp<row;temp++)
{
alloctemp=allochead;
needtemp=needhead;
finishtemp=finihead;
worktemp=workhead;
for(i=0;i<row;i++)
{
worktemp1=worktemp;
if(finishtemp->stat==0)
{
for(j=0;j<colum;j++,needtemp=needtemp->next,worktemp=worktemp->next)
if(needtemp->value<=worktemp->value)
processtest++;
if(processtest==colum)
{
for(j=0;j<colum;j++)
{
worktemp1->value+=alloctemp->value;
worktemp1=worktemp1->next;
alloctemp=alloctemp->next;
}
if(status==0)
{
pathhead=path1=path2=(struct path*)malloc(pathlen);
path1->next=path2->next=NULL;
path1->value=i;
status++;
}
else
{
path2=(struct path*)malloc(pathlen);
path2->value=i;
if(status==1)
{
pathhead->next=path2;
status++;
}
path1->next=path2;
path1=path2;
}
finishtemp->stat=1;
}
else
{
for(t=0;t<colum;t++)
alloctemp=alloctemp->next;
finishtemp->stat=0;
}
}
else
for(t=0;t<colum;t++)
{
needtemp=needtemp->next;
alloctemp=alloctemp->next;
}
processtest=0;
worktemp=workhead;
finishtemp=finishtemp->next;
}
}
path2->next=NULL;
finishtemp=finihead;
for(temp=0;temp<row;temp++)
{
if(finishtemp->stat==0)
{
printf("\n系統處於非安全狀態!\n");
exit(0);
}
finishtemp=finishtemp->next;
}
printf("\n系統處於安全狀態.\n");
printf("\n安全序列為: \n");
do
{
printf("p%d ",pathhead->value);
}
while(pathhead=pathhead->next);
printf("\n");
return 0;
}

6. 銀行家演算法是如何實現的

銀行家演算法是從當前狀態出發,逐個按安全序列檢查各客戶中誰能完成其工作,然後假定其完成工作且歸還全部貸款,再進而檢查下一個能完成工作的客戶。如果所有客戶都能完成工作,則找到一個安全序列,銀行家才是安全的。

�7�4 與預防死鎖的幾種方法相比較,限制條件少,資源利用程度提高了。

�7�4 缺點:該演算法要求客戶數保持固定不變,這在多道程序系統中是難以做到的;該演算法保證所有客戶在有限的時間內得到滿足,但實時客戶要求快速響應,所以要考慮這個因素;由於要尋找一個安全序列,實際上增加了系統的開銷.

Banker algorithm 最重要的一點是:保證操作系統的安全狀態!這也是操作系統判斷是否分配給一個進程資源的標准!那什麼是安全狀態?舉個小例子,進程 P 需要申請 8 個資源(假設都是一樣的),已經申請了 5 個資源,還差 3 個資源。若這個時候操作系統還剩下 2 個資源。很顯然,這個時候操作系統無論如何都不能再分配資源給進程 P 了,因為即使全部給了他也不夠,還很可能會造成死鎖。若這個時候操作系統還有 3 個資源,無論 P 這一次申請幾個資源,操作系統都可以滿足他,因為操作系統可以保證 P 不死鎖,只要他不把剩餘的資源分配給別人,進程 P 就一定能順利完成任務。 為什麼銀行家演算法是可行的呢?這里需要嚴格的證明一下。不管任何時候,操作系統分配資源的時候都可以保證當前接受資源的進程不會陷入死鎖,因為操作系統總是可以滿足該進程需要的資源的。假設有 n 個進程 {p1, p2, p3, … pn} ,最後一個分配到資源的是 pi , pi 還需要 mi 個資源,假設此時操作系統還有 m 個資源剩餘。那麼很顯然 m>=mi !而且如果之後操作系統又把資源分配給其他進程了,假設是 pj , pj 還需要 mj 個資源,同理可知 m>=mj !也就是說在所有的進程中,還需要的資源數總是有小於 m 的!這樣就可以保證資源數永遠不會為 0 ,即使可能暫時性為 0 。另外,還需要保證資源數不會減少!而且,所有已經分配到資源的進程總有一天會歸還它所擁有的資源!根據操作系統再分配的時候的狀態即可判定。

7. 銀行家演算法的C語言程序

1.根據下面給出的系統中資源分配情況,以及各個進程的資源申請情況,通過銀行家演算法來判斷各進程的資源請求能否滿足(要求記錄程序的運行過程)。 已分配的

8. C編程完成銀行家演算法的進程調度及同步模擬的實現

#include<stdio.h>
voidrukou(intjinchenggeshu,intziyuangeshu)
{
inti,j,k=0;
intzanyong[100][100];
intxuqiuzuida[100][100];
intxuyaoziyuangeshu[100][100];
intmeizhongziyuangeshu[100];
intmeizhongzuyuanshengyugeshu[100];
intjieguo[100];
intshifouanquan=0;
intkongshu;
intgaibianshuju;
intyongyouziyuanjishu;
for(i=0;i<jinchenggeshu;i++)
for(j=0;j<ziyuangeshu;j++)
{
printf(" 請輸入第%d進程現在佔用第%d種資源的個數:",i+1,j+1);
scanf("%d",&zanyong[i][j]);
}
for(i=0;i<jinchenggeshu;i++)
for(j=0;j<ziyuangeshu;j++)
{
printf(" 請輸入第%d進程現在所需最大第%d種資源的個數:",i+1,j+1);
scanf("%d",&xuqiuzuida[i][j]);
}
for(i=0;i<ziyuangeshu;i++)
{
printf(" 請輸入第%d種資源的個數:",i+1);
scanf("%d",&meizhongziyuangeshu[i]);
}
printf("如果有輸入錯誤想改變 ");
gaibian:printf("想改變佔用的數據請輸入1 ");
printf("想改變最大資源需求量的數據請輸入2 ");
printf("想改變資源中的數據請輸入3 ");
printf("退出改變數據請輸入4 ");
scanf("%d",&gaibianshuju);
if(gaibianshuju==1)
{
printf("請輸入改變哪個進程中的數據:");
scanf("%d",&i);
printf("請輸入改變哪種資源中的數據:");
scanf("%d",&j);
printf("請輸入新數據:");
scanf("%d",&zanyong[i][j]);
gotogaibian;
}
elseif(gaibianshuju==2)
{
printf("請輸入改變哪個進程中的數據:");
scanf("%d",&i);
printf("請輸入改變哪種資源中的數據:");
scanf("%d",&j);
printf("請輸入新數據:");
scanf("%d",&xuyaoziyuangeshu[i][j]);
gotogaibian;
}
elseif(gaibianshuju==3)
{
printf("請輸入改變哪種資源中的數據:");
scanf("%d",&j);
printf("請輸入新數據:");
scanf("%d",&meizhongziyuangeshu[j]);
gotogaibian;
}
elseif(gaibianshuju==4);
else{
printf("您選擇的無效,請從新選擇 :");
gotogaibian;
}
for(i=0;i<jinchenggeshu;i++)
for(j=0;j<ziyuangeshu;j++)
xuyaoziyuangeshu[i][j]=xuqiuzuida[i][j]-zanyong[i][j];
for(j=0;j<ziyuangeshu;j++)
{
yongyouziyuanjishu=0;
for(i=0;i<jinchenggeshu;i++)
yongyouziyuanjishu+=zanyong[i][j];
meizhongzuyuanshengyugeshu[j]=meizhongziyuangeshu[j]-yongyouziyuanjishu;
}
while(1)
{
shifouanquan++;
for(i=0;i<jinchenggeshu;i++)
{
if(zanyong[i][0]!=-1)
{
kongshu=0;
for(j=0;j<ziyuangeshu;j++)
if(meizhongzuyuanshengyugeshu[j]>=xuyaoziyuangeshu[i][j])
kongshu+=1;
if(kongshu==ziyuangeshu)
{
for(j=0;j<ziyuangeshu;j++)
meizhongzuyuanshengyugeshu[j]+=zanyong[i][j];
zanyong[i][0]=-1;
jieguo[k]=i+1;
k++;
}
}
}
if(k==jinchenggeshu)
{
printf("T0時刻是安全狀態,安全序列為:");
for(i=0;i<jinchenggeshu;i++)
printf("%d",jieguo[i]);
break;
}
if(shifouanquan>k)
{
printf("T0時刻是不安全狀態!");
break;
}
}
}
intmain()
{
intjinchenggeshu,ziyuangeshu;
printf("請輸入進程個數:");
scanf("%d",&jinchenggeshu);
printf("請輸入資源種類數:");
scanf("%d",&ziyuangeshu);
rukou(jinchenggeshu,ziyuangeshu);
return0;
}

9. 銀行家演算法C++描述

#include <iostream>
#include <string>
#define M 3 //資源的種類數
#define N 5 //進程的個數

void output(int iMax[N][M],int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N]); //統一的輸出格式
bool safety(int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N]);
bool banker(int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N]);

int main()
{
int i,j;
//當前可用每類資源的資源數
int iAvailable[M]={3,3,2};
//系統中N個進程中的每一個進程對M類資源的最大需求
int iMax[N][M]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
//iNeed[N][M]每一個進程尚需的各類資源數
//iAllocation[N][M]為系統中每一類資源當前已分配給每一進程的資源數
int iNeed[N][M],iAllocation[N][M]={{0,1,1},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
//進程名
char cName[N]={'a','b','c','d','e'};
bool bExitFlag=true; //退出標記
char ch; //接收選擇是否繼續提出申請時傳進來的值

bool bSafe; //存放安全與否的標志
//計算iNeed[N][M]的值
for(i=0;i<N;i++)
for(j=0;j<M;j++)
iNeed[i][j]=iMax[i][j]-iAllocation[i][j];
//輸出初始值
output(iMax,iAllocation,iNeed,iAvailable,cName);
//判斷當前狀態是否安全
bSafe=safety(iAllocation,iNeed,iAvailable,cName);

//是否繼續提出申請
while(bExitFlag)
{
cout<<"\n"<<"繼續提出申請?\ny為是;n為否。\n";
cin>>ch;
switch(ch)
{
case 'y':
//cout<<"調用銀行家演算法";
bSafe=banker(iAllocation,iNeed,iAvailable,cName);
if (bSafe) //安全,則輸出變化後的數據
output(iMax,iAllocation,iNeed,iAvailable,cName);
break;
case 'n':
cout<<"退出。\n";
bExitFlag=false;
break;
default:
cout<<"輸入有誤,請重新輸入:\n";
}
}
}

//輸出
void output(int iMax[N][M],int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N])
{
int i,j;

cout<<"\n\t Max \tAllocation\t Need \t Available"<<endl;
cout<<"\tA B C\tA B C\tA B C\t A B C"<<endl;

for(i=0;i<N;i++)
{
cout<<cName[i]<<"\t";

for(j=0;j<M;j++)
cout<<iMax[i][j]<<" ";
cout<<"\t";

for(j=0;j<M;j++)
cout<<iAllocation[i][j]<<" ";
cout<<"\t";

for(j=0;j<M;j++)
cout<<iNeed[i][j]<<" ";
cout<<"\t";
cout<<" ";

//Available只需要輸出一次
if (i==0)
for(j=0;j<M;j++)
cout<<iAvailable[j]<<" ";

cout<<endl;
}
}

//安全性演算法,進行安全性檢查;安全返回true,並且輸出安全序列,不安全返回false,並輸出不安全的提示;
bool safety(int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N])
{
}

//定位ch對應的進程名在數組中的位置
//沒找見返回-1,否則返回數組下標
int locate(char cName[N],char ch)
{
int i;
for(i=0;i<N;i++)
if (cName[i]==ch) //找到
return i;
//未找到
return -1;
}

//提出申請,返回提出申請的進程名對應的下標
int request(char cName[N],int iRequest[M])
{
int i,loc;
char ch;
bool bFlag=true;

//判斷輸入的進程名是否有誤
while(bFlag)
{
//輸出進程名
for(i=0;i<N;i++)
cout<<cName[i]<<"\t";

//輸入提出申請的進程名
cout<<"\n輸入提出資源申請的進程名:\n";
cin>>ch;

//定位ch對應的進程名在進程名數組中的位置
loc=locate(cName,ch);
//沒找到,重新輸入
if (loc==-1)
cout<<"\n您輸入的進程名有誤!請重新輸入";
//找到,退出循環
else
bFlag=false;
}

//輸入提出申請的資源數
cout<<"輸入申請各類資源的數量:\n";
for(i=0;i<M;i++)
cin>>iRequest[i];

//返回提出申請的進程名對應的下標
return loc;
}

bool banker(int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N])
{
}

這個是c++的 我的報告

10. c語言銀行家演算法安全性判別

把1作為參數傳給yanzheng() yanzheng(int m)

然後驗證函數里修改:

work=Avaliable;
i=m;
while(i<m)
{
if(Finish[i]==false&&Need[i]<=work)
{
work=work+Allocation[i];
Finish[i]=true;
anquan[k]=i;
k++;
i=0;
}
else
i++;
}
熱點內容
安卓系統時間在哪裡設置 發布:2025-05-11 10:10:37 瀏覽:986
我的世界租賃伺服器怎麼換皮膚 發布:2025-05-11 10:09:59 瀏覽:949
sql插入兩張表 發布:2025-05-11 09:55:33 瀏覽:758
日本編程語言 發布:2025-05-11 09:53:52 瀏覽:844
手機店設置的初始密碼一般是多少 發布:2025-05-11 09:33:15 瀏覽:402
昂科威選擇哪個配置 發布:2025-05-11 09:25:50 瀏覽:36
怎麼解決安卓視頻全屏卡頓 發布:2025-05-11 09:14:55 瀏覽:726
匯編從編譯到執行 發布:2025-05-11 09:09:04 瀏覽:258
安卓系統低版本如何升級 發布:2025-05-11 09:04:44 瀏覽:252
認證類型加密演算法 發布:2025-05-11 08:58:35 瀏覽:562