導航:首頁 > 源碼編譯 > c語言實現銀行家演算法

c語言實現銀行家演算法

發布時間:2022-06-17 04:38:42

❶ 拜託哪位大俠幫我用C/C++實現銀行家演算法模擬急急

銀行家演算法=-- -

1. 安全狀態: 在某時刻系統中所有進程可以排列一個安全序列:{P1,P2,`````Pn},剛稱此時,系統是安全的.
所謂安全序列{P1,P2,`````Pn}是指對於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);
}

❷ 銀行家演算法的演算法實現

在避免死鎖的方法中,所施加的限制條件較弱,有可能獲得令人滿意的系統性能。在該方法中把系統的狀態分為安全狀態和不安全狀態,只要能使系統始終都處於安全狀態,便可以避免發生死鎖。
銀行家演算法的基本思想是分配資源之前,判斷系統是否是安全的;若是,才分配。它是最具有代表性的避免死鎖的演算法。
設進程cusneed提出請求REQUEST [i],則銀行家演算法按如下規則進行判斷。
(1)如果REQUEST [cusneed] [i]<= NEED[cusneed][i],則轉(2);否則,出錯。
(2)如果REQUEST [cusneed] [i]<= AVAILABLE[i],則轉(3);否則,等待。
(3)系統試探分配資源,修改相關數據:
AVAILABLE[i]-=REQUEST[cusneed][i];
ALLOCATION[cusneed][i]+=REQUEST[cusneed][i];
NEED[cusneed][i]-=REQUEST[cusneed][i];
(4)系統執行安全性檢查,如安全,則分配成立;否則試探險性分配作廢,系統恢復原狀,進程等待。 (1)設置兩個工作向量Work=AVAILABLE;FINISH
(2)從進程集合中找到一個滿足下述條件的進程,
FINISH==false;
NEED<=Work;
如找到,執行(3);否則,執行(4)
(3)設進程獲得資源,可順利執行,直至完成,從而釋放資源。
Work=Work+ALLOCATION;
Finish=true;
GOTO 2
(4)如所有的進程Finish= true,則表示安全;否則系統不安全。
銀行家演算法流程圖
演算法(C語言實現) #include<STRING.H>#include<stdio.h>#include<stdlib.h>#include<CONIO.H>/*用到了getch()*/#defineM5/*進程數*/#defineN3/*資源數*/#defineFALSE0#defineTRUE1/*M個進程對N類資源最大資源需求量*/intMAX[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};/*系統可用資源數*/intAVAILABLE[N]={10,5,7};/*M個進程已分配到的N類數量*/intALLOCATION[M][N]={{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}};/*M個進程已經得到N類資源的資源量*/intNEED[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};/*M個進程還需要N類資源的資源量*/intRequest[N]={0,0,0};voidmain(){inti=0,j=0;charflag;voidshowdata();voidchangdata(int);voidrstordata(int);intchkerr();showdata();enter:{printf(請輸入需申請資源的進程號(從0到);printf(%d,M-1);printf():);scanf(%d,&i);}if(i<0||i>=M){printf(輸入的進程號不存在,重新輸入! );gotoenter;}err:{printf(請輸入進程);printf(%d,i);printf(申請的資源數 );printf(類別:ABC );printf();for(j=0;j<N;j++){scanf(%d,&Request[j]);if(Request[j]>NEED[i][j]){printf(%d,i);printf(號進程);printf(申請的資源數>進程);printf(%d,i);printf(還需要);printf(%d,j);printf(類資源的資源量!申請不合理,出錯!請重新選擇! );gotoerr;}else{if(Request[j]>AVAILABLE[j]){printf(進程);printf(%d,i);printf(申請的資源數大於系統可用);printf(%d,j);printf(類資源的資源量!申請不合理,出錯!請重新選擇! );gotoerr;}}}}changdata(i);if(chkerr()){rstordata(i);showdata();}elseshowdata();printf( );printf(按'y'或'Y'鍵繼續,否則退出 );flag=getch();if(flag=='y'||flag=='Y'){gotoenter;}else{exit(0);}}/*顯示數組*/voidshowdata(){inti,j;printf(系統可用資源向量: );printf(***Available*** );printf(資源類別:ABC );printf(資源數目:);for(j=0;j<N;j++){printf(%d,AVAILABLE[j]);}printf( );printf( );printf(各進程還需要的資源量: );printf(******Need****** );printf(資源類別:ABC );for(i=0;i<M;i++){printf();printf(%d,i);printf(號進程:);for(j=0;j<N;j++){printf(%d,NEED[i][j]);}printf( );}printf( );printf(各進程已經得到的資源量: );printf(***Allocation*** );printf(資源類別:ABC );for(i=0;i<M;i++){printf();printf(%d,i);printf(號進程:);/*printf(: );*/for(j=0;j<N;j++){printf(%d,ALLOCATION[i][j]);}printf( );}printf( );}/*系統對進程請求響應,資源向量改變*/voidchangdata(intk){intj;for(j=0;j<N;j++){AVAILABLE[j]=AVAILABLE[j]-Request[j];ALLOCATION[k][j]=ALLOCATION[k][j]+Request[j];NEED[k][j]=NEED[k][j]-Request[j];}}/*資源向量改變*/voidrstordata(intk){intj;for(j=0;j<N;j++){AVAILABLE[j]=AVAILABLE[j]+Request[j];ALLOCATION[k][j]=ALLOCATION[k][j]-Request[j];NEED[k][j]=NEED[k][j]+Request[j];}}/*安全性檢查函數*/intchkerr()//在假定分配資源的情況下檢查系統的安全性{intWORK[N],FINISH[M],temp[M];//temp[]用來記錄進程安全執行的順序inti,j,m,k=0,count;for(i=0;i<M;i++)FINISH[i]=FALSE;for(j=0;j<N;j++)WORK[j]=AVAILABLE[j];//把可利用資源數賦給WORK[]for(i=0;i<M;i++){count=0;for(j=0;j<N;j++)if(FINISH[i]==FALSE&&NEED[i][j]<=WORK[j])count++;if(count==N)//當進程各類資源都滿足NEED<=WORK時{for(m=0;m<N;m++)WORK[m]=WORK[m]+ALLOCATION[i][m];FINISH[i]=TRUE;temp[k]=i;//記錄下滿足條件的進程k++;i=-1;}}for(i=0;i<M;i++)if(FINISH[i]==FALSE){printf(系統不安全!!!本次資源申請不成功!!! );return1;}printf( );printf(經安全性檢查,系統安全,本次分配成功。 );printf( );printf(本次安全序列:);for(i=0;i<M;i++)//列印安全系統的進程調用順序{printf(進程);printf(%d,temp[i]);if(i<M-1)printf(->);}printf( );return0;}

❸ 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++;
}

❹ 求一個完美銀行家演算法實現的C++程序

C++代碼while( !Processes.empty() ) { bool found = false; list<Process>::iterator i = Processes.begin(); while ( i != Processes.end() ) { if(i->Maxmum - i->Current <= SystemAvaliable) { SystemAvaliable = SystemAvaliable + i->Current; Processes.erase(i); found = true; break; } ++i; } if( !found ){ return 1; }}return 0;( !Processes.empty() ) { bool found = false; list<Process>::iterator i = Processes.begin(); while ( i != Processes.end() ) { if(i->Maxmum - i->Current <= SystemAvaliable) { SystemAvaliable = SystemAvaliable + i->Current; Processes.erase(i); found = true; break; } ++i; } if( !found ){ return 1; }}return 0;while( !Processes.empty() ) { bool found = false; list<Process>::iterator i = Processes.begin(); while ( i != Processes.end() ) { if(i->Maxmum - i->Current <= SystemAvaliable) { SystemAvaliable = SystemAvaliable + i->Current; Processes.erase(i); found = true; break; } ++i; } if( !found ){ return 1; }}return 0;while( !Processes.empty() ) {
bool found = false;
list<Process>::iterator i = Processes.begin();
while ( i != Processes.end() ) {
if(i->Maxmum - i->Current <= SystemAvaliable) {
SystemAvaliable = SystemAvaliable + i->Current;
Processes.erase(i);
found = true;
break;
}
++i;
}
if( !found ){
return 1;
}
}
return 0;
參考: http://apt-blog.net/object_oriented_bankers_alogrithm

❺ 銀行家演算法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++的 我的報告

❻ 演算法上機實驗如圖所示,用c語言實現

#include<stdio.h>

#include<stdlib.h>

struct node {

int data;//數據域

struct node *R;//左孩子

struct node *L;//右孩子

};

int a[] = {1, 2, 3, -1, -1, -1, 4, 5,7,-1,-1,-1,6,-1,-1};//二叉樹序列

int k = 0;

node* buildTree(node *tree) {//創建二叉樹

int data=a[k++];//

if (data== - 1) {//-1代表空結點

tree = NULL;

}

else {//非空結點

tree = (node*)malloc(sizeof(node));//分配內存

tree->data = data;//數據域賦值

tree->L = tree->R = NULL;//左右孩子賦空

tree->L=buildTree(tree->L);//前往左孩子

tree->R=buildTree(tree->R);//前往右孩子

}

return tree;//返回根結點地址

}

void dfs1(node *tree) {//前序遍歷

if (tree) {

printf("%d ", tree->data);

dfs1(tree->L);

dfs1(tree->R);

}

}

void dfs2(node *tree) {//中序

if (tree) {

dfs2(tree->L);

printf("%d ", tree->data);

dfs2(tree->R);

}

}

void dfs3(node *tree) {//後序

if (tree) {

dfs3(tree->L);

dfs3(tree->R);

printf("%d ", tree->data);

}

}

void level(node *tree){

//層次遍歷,類似與bfs(廣度優先搜索)

//需要一個隊列作為輔助數據結構

node* q[100];//隊列

int f=0,r=0;//頭,尾指針

q[r++]=tree;//根結點入隊

while(f!=r){

node *t=q[f++];//出隊

printf("%d ",t->data);//輸出

if(t->L!=NULL){//非空左孩子入隊

q[r++]=t->L;

}

if(t->R!=NULL){//非空右孩子入隊

q[r++]=t->R;

}

}

}

int count(node *tree){

if(tree==NULL){

return 0;

}

else{

int n,m;

n=count(tree->L);//左子樹結點個數

m=count(tree->R);//右子樹結點個數

return n+m+1;//返回左右子樹結點個數之和

}

}

int main() {

node *tree = NULL;

tree=buildTree(tree);

printf(" 前序遍歷: ");

dfs1(tree);

printf(" 中序遍歷: ");

dfs2(tree);

printf(" 後序遍歷: ");

dfs3(tree);

printf(" 層次遍歷: ");

level(tree);

printf(" 二叉樹結點個數:%d",count(tree));

return 0;

}

❼ 求n個數的全排列,n不定。用c語言。用於銀行家演算法中求安全序列

好久沒用c了,所以代碼可能要用到偽代碼
先定義a[maxn]
用子函數遞歸
void p(int x)
{
if (n == x+1)
{
//foreach a print
//輸出數組a
}
for (int i=1 to n)
{
a[x] = i;
p(x+1);
a[x] = 0;
}
}
主函數main調用p(n)

編程實現銀行家演算法

#include <string.h>
#include <stdio.h>
#include <iostream.h>
#define FALSE 0
#define TRUE 1
#define W 10
#define R 10
int M ; // 總進程數
int N ; // 資源種類
int ALL_RESOURCE[W];// 各種資源的數目總和
int MAX[W][R]; // M個進程對N類資源最大資源需求量
int AVAILABLE[R]; // 系統可用資源數
int ALLOCATION[W][R]; // M個進程已經得到N類資源的資源量
int NEED[W][R]; // M個進程還需要N類資源的資源量
int Request[R]; // 請求資源個數
void output()
{
int i,j;
cout<<endl<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"各種資源的總數量:"<<endl;
for (j=0;j<N;j++)
cout<<" 資源"<<j<<": "<<ALL_RESOURCE[j];
cout<<endl;
cout<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"目前各種資源可利用的數量為:"<<endl;
for (j=0;j<N;j++)
cout<<" 資源"<<j<<": "<<AVAILABLE[j];
cout<<endl;
cout<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"各進程還需要的資源數量:"<<endl<<endl;
for(i=0;i<N;i++)
cout<<" 資源"<<i;
cout<<endl;
for (i=0;i<M;i++)
{
cout<<"進程"<<i<<": ";
for (j=0;j<N;j++)
cout<<NEED[i][j]<<" ";
cout<<endl;
}
cout<<endl;
cout<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"各進程已經得到的資源量: "<<endl<<endl;
for(i=0;i<N;i++)
cout<<" 資源"<<i;
cout<<endl;
for (i=0;i<M;i++)
{
cout<<"進程"<<i<<": ";
for (j=0;j<N;j++)
cout<<ALLOCATION[i][j]<<" ";
cout<<endl;
}
cout<<endl;
}

void distribute(int k)
{
int j;
for (j=0;j<N;j++)
{
AVAILABLE[j]=AVAILABLE[j]-Request[j];
ALLOCATION[k][j]=ALLOCATION[k][j]+Request[j];
NEED[k][j]=NEED[k][j]-Request[j];
}
}

void restore(int k)
{
int j;
for (j=0;j<N;j++)
{
AVAILABLE[j]=AVAILABLE[j]+Request[j];
ALLOCATION[k][j]=ALLOCATION[k][j]-Request[j];
NEED[k][j]=NEED[k][j]+Request[j];
}
}

int check()
{
int WORK[R],FINISH[W];
int i,j;
for(j=0;j<N;j++) WORK[j]=AVAILABLE[j];
for(i=0;i<M;i++) FINISH[i]=FALSE;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
if(FINISH[i]==FALSE&&NEED[i][j]<=WORK[j])
{
WORK[j]=WORK[i]+ALLOCATION[i][j];
}
}

FINISH[i]=TRUE;
}
for(i=0;i<M;i++)
{
if(FINISH[i]==FALSE)
{
cout<<endl;
cout<<" 系統不安全!!! 本次資源申請不成功!!!"<<endl;
cout<<endl;
return 1;
}
else
{
cout<<endl;
cout<<" 經安全性檢查,系統安全,本次分配成功。"<<endl;
cout<<endl;
return 0;
}

}
}

void bank() // 銀行家演算法
{
int i=0,j=0;
char flag='Y';
while(flag=='Y'||flag=='y')
{
i=-1;
while(i<0||i>=M)
{
cout<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<endl<<" 請輸入需申請資源的進程號:";
cin>>i;
if(i<0||i>=M) cout<<" 輸入的進程號不存在,重新輸入!"<<endl;
}
cout<<" 請輸入進程"<<i<<"申請各類資源的數量:"<<endl;
for (j=0;j<N;j++)
{
cout<<" 資源"<<j<<": ";
cin>>Request[j];
if(Request[j]>NEED[i][j]) // 若請求的資源數大於進程還需要i類資源的資源量j
{
cout<<endl<<" 進程"<<i<<"申請的資源數大於進程"<<i<<"還需要"<<j<<"類資源的數量!";
cout<<" 若繼續執行系統將處於不安全狀態!"<<endl;
flag='N';
break;
}
else
{
if(Request[j]>AVAILABLE[j]) // 若請求的資源數大於可用資源數
{
cout<<endl<<" 進程"<<i<<"申請的資源數大於系統可用"<<j<<"類資源的數量!";
cout<<" 若繼續執行系統將處於不安全狀態!"<<endl;
flag='N';
break;
}

}

}
if(flag=='Y'||flag=='y')
{
distribute(i); // 調用change(i)函數,改變資源數
if(check()) // 若系統安全
{
restore(i); // 調用restore(i)函數,恢復資源數
output(); // 輸出資源分配情況
}
else // 若系統不安全
output(); // 輸出資源分配情況
}
else // 若flag=N||flag=n
cout<<endl;
cout<<" 是否繼續銀行家演算法演示,按'Y'或'y'鍵繼續,按'N'或'n'鍵退出演示: ";
cin>>flag;
}
}

void version()
{
cout<<endl;
cout<<"\t 銀 行 家 算 法 "<<endl;
}

void main() // 主函數
{
int i=0,j=0,p;
version();
getchar();
cout<<endl<<"請輸入總進程數:";
cin>>M;
cout<<endl<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"請輸入總資源種類:";
cin>>N;
cout<<endl<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"請輸入各類資源總數:(需要輸入數為"<<N<<"個)";
for(i=0;i<N;i++)
cin>>ALL_RESOURCE[i];
cout<<endl<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"輸入各進程所需要的各類資源的最大數量:(需要輸入數為"<<M*N<<"個)";
for (i=0;i<M;i++)
{
for (j=0;j<N;j++)
{
do
{
cin>>MAX[i][j];
if (MAX[i][j]>ALL_RESOURCE[j])
cout<<endl<<"佔有資源超過了聲明的該資源總數,請重新輸入"<<endl;
}
while (MAX[i][j]>ALL_RESOURCE[j]);
}
}
cout<<endl<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"輸入各進程已經占據的各類資源的數量:(需要輸入數為"<<M
*N<<"個)";
for (i=0;i<M;i++)
{
for (j=0;j<N;j++)
{
do
{
cin>>ALLOCATION[i][j];
if (ALLOCATION[i][j]>MAX[i][j])
cout<<endl<<"佔有資源超過了聲明的最大資源,請重新輸入"<<endl;
}
while (ALLOCATION[i][j]>MAX[i][j]);
}
}
for (j=0;j<N;j++) // 初始化資源數量
{
p=ALL_RESOURCE[j];
for (i=0;i<M;i++)
{
p=p-ALLOCATION[i][j];// 減去已經被占據的資源
AVAILABLE[j]=p;
if(AVAILABLE[j]<0)
AVAILABLE[j]=0;
}
}
for (i=0;i<M;i++)
for(j=0;j<N;j++)
NEED[i][j]=MAX[i][j]-ALLOCATION[i][j];
output();
bank();
}

閱讀全文

與c語言實現銀行家演算法相關的資料

熱點內容
androidapi版本號 瀏覽:784
小豬佩奇配音解壓吃東西 瀏覽:283
程序員怎麼申請公司年會 瀏覽:870
圈店app的預約頭條功能怎麼樣 瀏覽:34
雲伺服器設置ad域 瀏覽:316
我的世界無盡貪婪奇點壓縮器 瀏覽:394
源碼資源免費分享網 瀏覽:686
批量qq號有效驗證源碼 瀏覽:511
本科程序員五年工資 瀏覽:902
創維電視櫃怎麼安裝app 瀏覽:852
可愛的程序員陸漓劇照 瀏覽:851
怎樣把截屏壓縮成300kb 瀏覽:225
dart文件加密 瀏覽:40
java對接攝像頭源碼 瀏覽:887
安卓項目開發實例附源碼 瀏覽:728
程序員蘋果全家桶 瀏覽:197
遠程命令阻塞 瀏覽:731
有網頁源碼怎麼查數據 瀏覽:100
win10下make編譯速度過慢 瀏覽:866
微機原理編譯環境 瀏覽:19