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

銀行家演算法work

發布時間: 2022-05-03 17:45:06

① 高分求銀行家演算法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;
}

② (2)在銀行家演算法的安全性演算法中,為什麼不用變數Available,而又定義一個臨時變數work

在安全性演算法中,工作向量work的初值為Available的值,在執行安全性演算法的過程中work的值是變化的,若資源分配後系統處於安全狀態,使用Available是沒關系,但是如果系統處於不安全狀態,必須恢復為原來的資源分配狀態,即Available。

③ 操作系統銀行家演算法

不會分配,看一下銀行家演算法的流程。
可以看到 在step(1)若Request<=Need, goto step(2);否則錯誤返回.
原因如下,每個進程開始之前,都必須聲明自己需要的各類資源的最大值Max。
Need 需求資源 = Max 最大需求 - Allocation 已分配資源
進程運行過程中,不能再要比Need還多的資源。

參考書 操作系統概念(OS concepts Six Edition)

演算法:
n:系統中進程的總數
m:資源類總數

符號說明:
Available 可用剩餘資源
Max 最大需求
Allocation 已分配資源
Need 需求資源
Request 請求資源

當進程pi提出資源申請時, 系統執行下列
步驟:("="為賦值符號, "=="為等號)
step(1)若Request<=Need, goto step(2);否則錯誤返回
step(2)若Request<=Available, goto step(3);否則進程等待
step(3)假設系統分配了資源, 則有:
Available=Available-Request;
Allocation=Allocation+Request;
Need=Need-Request
若系統新狀態是安全的, 則分配完成
若系統新狀態是不安全的, 則恢復原狀態, 進程等待
為進行安全性檢查, 定義數據結構:
Work:ARRAY[1...m] of integer;
Finish:ARRAY[1...n] of Boolean;
安全性檢查的步驟:
step (1):
Work=Available;
Finish=false;
step (2) 尋找滿足條件的i:
a. Finish==false;
b. Need<=Work;
如果不存在, goto step(4)
step(3)
Work=Work+Allocation;
Finish=true;
goto step(2)
step (4) 若對所有i, Finish=true, 則系統處於安全狀態, 否則處於不安全狀態

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

⑤ 銀行家演算法當中為什麼不用變數Available,而又定義一個臨時變數work

這是因為:安全性演算法中判斷是否安全。不能改變Available數組的值。做檢驗時,要用到Available數組的值。

⑥ 銀行家演算法

什麼是銀行家演算法:
銀行家演算法是一種最有代表性的避免死鎖的演算法。在避免死鎖方法中允許進程動態地申請資源,但系統在進行資源分配之前,應先計算此次分配資源的安全性,若分配不會導致系統進入不安全狀態,則分配,否則等待。為實現銀行家演算法,系統必須設置若干數據結構。

要解釋銀行家演算法,必須先解釋操作系統安全狀態和不安全狀態。
安全序列是指一個進程序列{P1,…,Pn}是安全的,如果對於每一個進程Pi(1≤i≤n),它以後尚需要的資源量不超過系統當前剩餘資源量與所有進程Pj (j < i )當前佔有資源量之和。
安全狀態
如果存在一個由系統中所有進程構成的安全序列P1,…,Pn,則系統處於安全狀態。安全狀態一定是沒有死鎖發生。
不安全狀態
不存在一個安全序列。不安全狀態不一定導致死鎖。
原理:
我們可以把操作系統看作是銀行家,操作系統管理的資源相當於銀行家管理的資金,進程向操作系統請求分配資源相當於用戶向銀行家貸款。
為保證資金的安全,銀行家規定:
(1) 當一個顧客對資金的最大需求量不超過銀行家現有的資金時就可接納該顧客;
(2) 顧客可以分歧貸款,但貸款的總數不能超過最大需求量;
(3) 當銀行家現有的資金不能滿足顧客尚需的貸款數額時,對顧客的貸款可推遲支付,但總能使顧客在有限的時間里得到貸款;
(4) 當顧客得到所需的全部資金後,一定能在有限的時間里歸還所有的資金.
操作系統按照銀行家制定的規則為進程分配資源,當進程首次申請資源時,要測試該進程對資源的最大需求量,如果系統現存的資源可以滿足它的最大需求量則按當前的申請量分配資源,否則就推遲分配。當進程在執行中繼續申請資源時,先測試該進程已佔用的資源數與本次申請的資源數之和是否超過了該進程對資源的最大需求量。若超過則拒絕分配資源,若沒有超過則再測試系統現存的資源能否滿足該進程尚需的最大資源量,若能滿足則按當前的申請量分配資源,否則也要推遲分配。
程序舉例:
已知進程{P0,P1,P2,P3,P4},有三類系統資源A、B、C的數量分別為10、5、7,在T0時刻的資源
(1)若進程P1請求資源,發出請求向量Request1(1,0,2),編寫程序用銀行家演算法判斷系統能否將資源分配給它;
(2)若進程P2提出請求Request(0,1,0),用銀行家演算法程序驗證系統能否將資源分配給它。
程序代碼:
P1進程提出的請求,可以分配。
P2進程不能分配,因為請求的B類資源超過了它的最大值。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXSIZE 50
void main()
{
unsigned int Available[MAXSIZE]; //可利用資源向量
unsigned int Max[MAXSIZE][MAXSIZE]; //最大需求矩陣
unsigned int Allocation[MAXSIZE][MAXSIZE]; //已分配矩陣
unsigned int Need[MAXSIZE][MAXSIZE]; //需求矩陣
unsigned int Request[MAXSIZE]; //請求向量
unsigned int Work[MAXSIZE]; //工作向量
bool Finish[MAXSIZE]; //是否有足夠資源分配給進程,使之運行完成
unsigned int SafeSequence[MAXSIZE]; //安全序列

int i,j;
int p; //請求資源的進程的下標
int temp = 0; //安全序列下標
int total = 0;
int N;
int M;

printf("請輸入進程數N=");
scanf("%d",&N);
printf("請輸入資源種類數M=");
scanf("%d",&M);

//用戶輸入數據,初始化Available數組
printf("初始化可用資源數組:\n");
for(i=0; i<M; i++)
{
printf("\t%c類資源:",65+i);
scanf("%d",&Available[i]);
}

//用戶輸入數據,初始化Max數組
printf("初始化最大需求數組:\n");
for(i=0; i<N; i++)
{
printf("\tP%d進程最大需要\n",i);
for(j=0; j<M; j++)
{
printf("\t\t%c類資源:",65+j);
scanf("%d",&Max[i][j]);
}
}

//用戶輸入數據,初始化Allocation數組
printf("初始化已分配資源數組:\n");
for(i=0; i<N; i++)
{
printf("\tP%d進程已分配\n",i);
for(j=0; j<M; j++)
{
printf("\t\t%c類資源:",65+j);
scanf("%d",&Allocation[i][j]);
}
}

//初始化Need數組
for(i=0; i<N; i++)
for(j=0; j<M; j++)
{
Need[i][j] = Max[i][j] - Allocation[i][j];
}

//進程發出資源請求後檢查
do
{
printf("資源請求:\n");
printf("\t輸入請求資源的進程下標:");
scanf("%d",&p);
printf("\t進程P%d請求\n",p);
//初始化請求向量
for(i=0; i<M; i++)
{
printf("\t\t%c類資源:",65+i);
scanf("%d",&Request[i]);
}
for(i=0; i<M; i++) //檢查Request <= Need ?
if(Request[i] > Need[p][i])
{
printf("\t請求的%c類資源數超過它所宣布的最大值!\n",65+i);
break;
}
if(i == M) //通過上層檢查,繼續檢查Request <= Available ?
{
for(i=0; i<M; i++)
if(Request[i] > Available[i])
{
printf("\t尚無足夠%c類資源,P%d須等待!\n",65+i,p);
break;
}
}
if(i == M) //嘗試分配
{
for(i=0; i<M; i++)
{
Available[i] -= Request[i];
Allocation[p][i] += Request[i];
Need[p][i] -= Request[i];
}

}
}while(i<M);

//初始化Work,Finish向量
for(i=0; i<M; i++)
{
Work[i] = Available[i];
}
for(i=0; i<N; i++)
{
Finish[i] = false;
}

//安全性演算法
do
{
total = temp;
for(i=0; i<N; i++)
{
if(Finish[i] == false)
{
for(j=0; j<M; j++)
if(Need[i][j] > Work[j])
{
break;
}
if(j == M) //各類資源都滿足Need <= Work
{
for(j=0; j<M; j++)
{
Work[j] += Allocation[i][j]; //釋放資源
}
Finish[i] = true;
SafeSequence[temp++] = i; //加入安全序列
}
}
}
}while(total != temp); //所有進程檢查一遍之後,如果安全序列有變化,則進行下一輪
//否則說明所有的Finish都為true,或者因沒有安全序列退出循環

if(temp == N)
{
printf("安全序列:");
for(temp=0; temp<N; temp++)
{
printf("P%d ",SafeSequence[temp]);
}
}
else
{
printf("系統處於不安全狀態!不能分配!\n");
}
getchar();
getchar();
}
這個程序還行,輸入有點麻煩,我自己編寫的是用文件輸入系統描述信息的,但是缺少說明,怕你搞不明白。希望對你有所幫助!

⑦ 銀行家演算法具有以下多種功能:

銀行家演算法程序代碼
#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();
}

實驗結果分析
1.根據下面給出的系統中資源分配情況,以及各個進程的資源申請情況,通過銀行家演算法來判斷各進程的資源請求能否滿足(要求記錄程序的運行過程)。
已分配的資源 最大需求量
A B C A B C
P1 0 1 0 7 5 3
P2 2 0 0 3 2 2
P3 3 0 2 9 0 2
P4 2 1 1 2 2 2
P5 0 0 2 4 3 3
剩餘資源 A B C
3 3 2

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

在避免死鎖的方法中,所施加的限制條件較弱,有可能獲得令人滿意的系統性能。在該方法中把系統的狀態分為安全狀態和不安全狀態,只要能使系統始終都處於安全狀態,便可以避免發生死鎖。
銀行家演算法的基本思想是分配資源之前,判斷系統是否是安全的;若是,才分配。它是最具有代表性的避免死鎖的演算法。
設進程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;}

⑨ 銀行家演算法的實現,安全性演算法中 這條語句是什麼意思Work[j]∶=Work[i]+Allocation[i,j];

work[j]表示當前系統可用的第j類資源,Allocation[i][j]表示當前已經分配給進程i使用的第j類資源數量。

Work[j]= Work[j]+ Allocation[i][j]

這句的意思是目前進程已經利用手上資源完成相關工作了,這些已分配的資源可以重新歸還系統了,所以系統可用的第j類資源work[j]就增加了,增加量就是當前進程想要歸還的資源量Allocation[i][j]

如有疑惑歡迎追問!

⑩ 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++;
}
熱點內容
編程兒歌 發布:2024-10-06 14:03:32 瀏覽:635
所有程序都能反編譯嗎 發布:2024-10-06 13:48:39 瀏覽:605
城際快車源碼 發布:2024-10-06 13:28:10 瀏覽:132
什麼配置主機直播好 發布:2024-10-06 13:22:11 瀏覽:906
office緩存 發布:2024-10-06 13:20:03 瀏覽:540
我的世界國際版比較熱門的伺服器 發布:2024-10-06 13:04:10 瀏覽:534
8g緩存還剩多少怎麼看 發布:2024-10-06 12:59:52 瀏覽:232
物流公司怎麼配置電子面單 發布:2024-10-06 12:54:12 瀏覽:804
魔域歸來手游掛機腳本 發布:2024-10-06 12:42:54 瀏覽:352
福建伺服器雲空間 發布:2024-10-06 12:42:42 瀏覽:102