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