當前位置:首頁 » 編程語言 » 用c語言實現銀行家演算法
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

用c語言實現銀行家演算法

發布時間: 2022-09-27 07:23:50

1. 求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)

2. 求一個銀行家演算法c語言模擬,能運行的,謝謝。

在什麼地方運行(即運行環境是什麼),用什麼語言寫,要達到什麼功能,你要說清楚

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

4. 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++;
}

5. 怎樣用C語言實現銀行家演算法

#include<stdio.h>
struct claim
{
int user;
int num[3];
}claims;
int input()
{
printf("please input your request:user(0~4):\n");
scanf("%d",&claims.user);
printf("input the number of resource a:\n");
scanf("%d",&claims.num[0]);
printf("input the number of resource b:\n");
scanf("%d",&claims.num[1]);
printf("input the number of resource c:\n");
scanf("%d",&claims.num[2]);
return 1;
}
int safety_chk(int alloc[][3],int need[][3],int avail[3])
{
int work[3],finish[5];
for(int p=0;p<5;p++)//i大於2後對WORK是無意義的
{
work[p]=avail[p];
finish[p]=0;
}
for(int i=0;i<5;i++)
{
if(finish[i]==0&&
need[i][0]<=work[0]&&
need[i][1]<=work[1]&&
need[i][2]<=work[2] )
{
for(int j=0;j<3;j++)
work[j]=alloc[i][j]+work[j];
finish[i]=1;
i=-1;//重頭再來
}
}
for(i=0;i<5;i++)
{
if(finish[i]==0)
return 0;
}
return 1;
}

int process(int alloc[][3],int need[][3],int avail[3])
{
int ret;
input();
for(int i=0;i<3;i++) //out of resource number
{
if(claims.num[i]>need[claims.user][i]||claims.num[i]>avail[i])
return 0;
}
for(i=0;i<3;i++)//trying
{
avail[i]=avail[i]-claims.num[i];
alloc[claims.user][i]=alloc[claims.user][i]+claims.num[i];
need[claims.user][i]=need[claims.user][i]-claims.num[i];
}
if((ret=safety_chk(alloc,need,avail)==0))
{

printf("safety_chk's result %d \n",0);
for(i=0;i<3;i++)
{
avail[i]=avail[i]+claims.num[i];
alloc[claims.user][i]=alloc[claims.user][i]-claims.num[i];
need[claims.user][i]=need[claims.user][i]+claims.num[i];
}
return 0;
}
else
{

printf("safety_chk's result %d \n",1);
}
return 1;
}
void main()
{
int alloc[5][3]={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
int need[5][3]={{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}};
int avail[3]={3,3,2};
if(process(alloc,need,avail)==0)
printf("sorry,we cannot help you!\n");
else printf("operation complete!\n");
return;
}

6. 操作系統中怎樣編程實現銀行家演算法

#include<iostream.h>
#include<string.h>
#include<stdio.h>
#define False 0
#define True 1
int Max[100][100]={0};//各進程所需各類資源的最大需求
int Avaliable[100]={0};//系統可用資源
char name[100]={0};//資源的名稱
int Allocation[100][100]={0};//系統已分配資源
int Need[100][100]={0};//還需要資源
int Request[100]={0};//請求資源向量
int temp[100]={0};//存放安全序列
int Work[100]={0};//存放系統可提供資源
int M=100;//作業的最大數為100
int N=100;//資源的最大數為100
void showdata()//顯示資源矩陣
{
int i,j;
cout<<"系統目前可用的資源[Avaliable]:"<<endl;
for(i=0;i<N;i++)
cout<<name[i]<<" ";
cout<<endl;
for (j=0;j<N;j++)
cout<<Avaliable[j]<<" ";//輸出分配資源
cout<<endl;
cout<<" Max Allocation Need"<<endl;
cout<<"進程名 ";
for(j=0;j<3;j++){
for(i=0;i<N;i++)
cout<<name[i]<<" ";
cout<<" ";
}
cout<<endl;
for(i=0;i<M;i++){
cout<<" "<<i<<" ";
for(j=0;j<N;j++)
cout<<Max[i][j]<<" ";
cout<<" ";
for(j=0;j<N;j++)
cout<<Allocation[i][j]<<" ";
cout<<" ";
for(j=0;j<N;j++)
cout<<Need[i][j]<<" ";
cout<<endl;
}
}

int changdata(int i)//進行資源分配
{
int j;
for (j=0;j<M;j++) {
Avaliable[j]=Avaliable[j]-Request[j];
Allocation[i][j]=Allocation[i][j]+Request[j];
Need[i][j]=Need[i][j]-Request[j];
}
return 1;
}
int safe()//安全性演算法
{
int i,k=0,m,apply,Finish[100]={0};
int j;
int flag=0;
Work[0]=Avaliable[0];
Work[1]=Avaliable[1];
Work[2]=Avaliable[2];
for(i=0;i<M;i++){
apply=0;
for(j=0;j<N;j++){
if (Finish[i]==False&&Need[i][j]<=Work[j]){
apply++;
if(apply==N){
for(m=0;m<N;m++)
Work[m]=Work[m]+Allocation[i][m];//變分配數
Finish[i]=True;
temp[k]=i;
i=-1;
k++;
flag++;
}
}
}
}
for(i=0;i<M;i++){
if(Finish[i]==False){
cout<<"系統不安全"<<endl;//不成功系統不安全
return -1;
}
}
cout<<"系統是安全的!"<<endl;//如果安全,輸出成功
cout<<"分配的序列:";
for(i=0;i<M;i++){//輸出運行進程數組
cout<<temp[i];
if(i<M-1) cout<<"->";
}
cout<<endl;
return 0;
}
void share()//利用銀行家演算法對申請資源對進行判定
{
char ch;
int i=0,j=0;
ch='y';
cout<<"請輸入要求分配的資源進程號(0-"<<M-1<<"):";
cin>>i;//輸入須申請的資源號
cout<<"請輸入進程 "<<i<<" 申請的資源:"<<endl;
for(j=0;j<N;j++)
{
cout<<name[j]<<":";
cin>>Request[j];//輸入需要申請的資源
}
for (j=0;j<N;j++){
if(Request[j]>Need[i][j])//判斷申請是否大於需求,若大於則出錯
{
cout<<"進程 "<<i<<"申請的資源大於它需要的資源";
cout<<" 分配不合理,不予分配!"<<endl;
ch='n';
break;
}
else {
if(Request[j]>Avaliable[j])//判斷申請是否大於當前資源,若大於則
{ //出錯
cout<<"進程"<<i<<"申請的資源大於系統現在可利用的資源";
cout<<" 分配出錯,不予分配!"<<endl;
ch='n';
break;
}
}
}
if(ch=='y') {
changdata(i);//根據進程需求量變換資源
showdata();//根據進程需求量顯示變換後的資源
safe();//根據進程需求量進行銀行家演算法判斷
}
}
void addresources(){//添加資源
int n,flag;
cout<<"請輸入需要添加資源種類的數量:";
cin>>n;
flag=N;
N=N+n;
for(int i=0;i<n;i++){
cout<<"名稱:";
cin>>name[flag];
cout<<"數量:";
cin>>Avaliable[flag++];
}
showdata();
safe();
}
void delresources(){//刪除資源
char ming;
int i,flag=1;
cout<<"請輸入需要刪除的資源名稱:";
do{
cin>>ming;
for(i=0;i<N;i++)
if(ming==name[i]){
flag=0;
break;
}
if(i==N)
cout<<"該資源名稱不存在,請重新輸入:";
}
while(flag);
for(int j=i;j<N-1;j++)
{
name[j]=name[j+1];
Avaliable[j]=Avaliable[j+1];

}
N=N-1;
showdata();
safe();
}
void changeresources(){//修改資源函數
cout<<"系統目前可用的資源[Avaliable]:"<<endl;
for(int i=0;i<N;i++)
cout<<name[i]<<":"<<Avaliable[i]<<endl;
cout<<"輸入系統可用資源[Avaliable]:"<<endl;
cin>>Avaliable[0]>>Avaliable[1]>>Avaliable[2];
cout<<"經修改後的系統可用資源為"<<endl;
for (int k=0;k<N;k++)
cout<<name[k]<<":"<<Avaliable[k]<<endl;
showdata();
safe();
}
void addprocess(){//添加作業
int flag=M;
M=M+1;
cout<<"請輸入該作業的最打需求量[Max]"<<endl;
for(int i=0;i<N;i++){
cout<<name[i]<<":";
cin>>Max[flag][i];
Need[flag][i]=Max[flag][i]-Allocation[flag][i];
}
showdata();
safe();
}
int main()//主函數
{

int i,j,number,choice,m,n,flag;
char ming;
cout<<"*****************資源管理系統的設計與實現*****************"<<endl;
cout<<"請首先輸入系統可供資源種類的數量:";
cin>>n;
N=n;
for(i=0;i<n;i++)
{
cout<<"資源"<<i+1<<"的名稱:";
cin>>ming;
name[i]=ming;
cout<<"資源的數量:";
cin>>number;
Avaliable[i]=number;
}
cout<<endl;
cout<<"請輸入作業的數量:";
cin>>m;
M=m;
cout<<"請輸入各進程的最大需求量("<<m<<"*"<<n<<"矩陣)[Max]:"<<endl;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>Max[i][j];
do{
flag=0;
cout<<"請輸入各進程已經申請的資源量("<<m<<"*"<<n<<"矩陣)[Allocation]:"<<endl;
for(i=0;i<m;i++)
for(j=0;j<n;j++){
cin>>Allocation[i][j];
if(Allocation[i][j]>Max[i][j])
flag=1;
Need[i][j]=Max[i][j]-Allocation[i][j];
}
if(flag)
cout<<"申請的資源大於最大需求量,請重新輸入!\n";
}
while(flag);

showdata();//顯示各種資源
safe();//用銀行家演算法判定系統是否安全
while(choice)
{
cout<<"**************銀行家演算法演示***************"<<endl;
cout<<" 1:增加資源 "<<endl;
cout<<" 2:刪除資源 "<<endl;
cout<<" 3:修改資源 "<<endl;
cout<<" 4:分配資源 "<<endl;
cout<<" 5:增加作業 "<<endl;
cout<<" 0:離開 "<<endl;
cout<<"*******************************************"<<endl;
cout<<"請選擇功能號:";
cin>>choice;
switch(choice)
{
case 1: addresources();break;
case 2: delresources();break;
case 3: changeresources();break;
case 4: share();break;
case 5: addprocess();break;
case 0: choice=0;break;
default: cout<<"請正確選擇功能號(0-5)!"<<endl;break;
}
}
return 1;
}

7. 急!銀行家演算法用C語言編寫.全部程序.

銀行家演算法
銀行家演算法是一種最有代表性的避免死鎖的演算法。
要解釋銀行家演算法,必須先解釋操作系統安全狀態和不安全狀態。
安全狀態:如果存在一個由系統中所有進程構成的安全序列P1,…,Pn,則系統處於安全狀態。安全狀態一定是沒有死鎖發生。
不安全狀態:不存在一個安全序列。不安全狀態不一定導致死鎖。
那麼什麼是安全序列呢?
安全序列:一個進程序列{P1,…,Pn}是安全的,如果對於每一個進程Pi(1≤i≤n),它以後尚需要的資源量不超過系統當前剩餘資源量與所有進程Pj (j < i )當前佔有資源量之和。
銀行家演算法:
我們可以把操作系統看作是銀行家,操作系統管理的資源相當於銀行家管理的資金,進程向操作系統請求分配資源相當於用戶向銀行家貸款。操作系統按照銀行家制定的規則為進程分配資源,當進程首次申請資源時,要測試該進程對資源的最大需求量,如果系統現存的資源可以滿足它的最大需求量則按當前的申請量分配資源,否則就推遲分配。當進程在執行中繼續申請資源時,先測試該進程已佔用的資源數與本次申請的資源數之和是否超過了該進程對資源的最大需求量。若超過則拒絕分配資源,若沒有超過則再測試系統現存的資源能否滿足該進程尚需的最大資源量,若能滿足則按當前的申請量分配資源,否則也要推遲分配。
演算法:
n:系統中進程的總數
m:資源類總數
Available: ARRAY[1..m] of integer;
Max: ARRAY[1..n,1..m] of integer;
Allocation: ARRAY[1..n,1..m] of integer;
Need: ARRAY[1..n,1..m] of integer;
Request: ARRAY[1..n,1..m] of integer;
符號說明:
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,則系統處於安全狀態,否則處於不安全狀態
/* 銀行家演算法,操作系統概念(OS concepts Six Edition)
reedit by Johnny hagen,SCAU,run at vc6.0
*/
#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;
}