操作系统课程设计任务书:银行家算法设计 银行家算法 操作系统课程设计

作者&投稿:佼杜 (若有异议请与网页底部的电邮联系)
#include<stdio.h>
#include<stdlib.h>
struct type
{
int a;
int b;
int c;
};
struct allocation
{
struct type value;
struct allocation *next;
};
struct max
{
struct type value;
struct max *next;
};
struct available
{
struct type value;
struct available *next;
};
struct need
{
struct type value;
struct need *next;
};
struct path
{
int value;
struct path *next;
};
struct finish
{
int value;
struct finish *next;
};

void main()
{
int p,status=0,i,j,temp,flag=0;
struct allocation *allochead,*alloc1,*alloc2,*alloctemp;
struct max *maxhead,*max1,*max2,*maxtemp;
struct available *availablehead,*workhead,*worktemp;
struct need *needhead,*need1,*need2,*needtemp;
struct path *pathhead,*path1,*path2,*pathtemp;
struct finish *finishhead,*finish1,*finish2,*finishtemp;

printf("请输入进程的数目\n");
scanf("%d",&p);
for(i=0;i<p;i++)
{
printf("\n输入进程p%d已经分配的资源\n",i+1);
if(flag==0)
{
allochead=alloc1=alloc2=(struct allocation*)malloc(sizeof(struct allocation));
printf("\t当前资源类型是 %c\t",'a');
scanf("%d",&alloc1->value.a);
printf("\t当前资源类型是 %c\t",'b');
scanf("%d",&alloc1->value.b);
printf("\t当前资源类型是 %c\t",'c');
scanf("%d",&alloc1->value.c);
flag++;
allochead=alloc1;
}
else
{
alloc2=(struct allocation*)malloc(sizeof(struct allocation));
printf("\t当前资源类型是 %c\t",'a');
scanf("%d",&alloc2->value.a);
printf("\t当前资源类型是 %c\t",'b');
scanf("%d",&alloc2->value.b);
printf("\t当前资源类型是 %c\t",'c');
scanf("%d",&alloc2->value.c);

alloc1->next=alloc2;
alloc1=alloc2;
flag++;
}
}
alloc2->next=NULL;
flag=0;

for(i=0;i<p;i++)
{
printf("\n输入进程p%d要求的最大数目\n",i+1);
if(flag==0)
{
maxhead=max1=max2=(struct max*)malloc(sizeof(struct max));
printf("\t当前资源类型是 %c\t",'a');
scanf("%d",&max1->value.a);
printf("\t当前资源类型是 %c\t",'b');
scanf("%d",&max1->value.b);
printf("\t当前资源类型是 %c\t",'c');
scanf("%d",&max1->value.c);
maxhead=max1;
flag++;
}
else
{
max2=(struct max*)malloc(sizeof(struct max));
printf("\t当前资源类型是 %c\t",'a');
scanf("%d",&max2->value.a);
printf("\t当前资源类型是 %c\t",'b');
scanf("%d",&max2->value.b);
printf("\t当前资源类型是 s %c\t",'c');
scanf("%d",&max2->value.c);
max1->next=max2;
max1=max2;
flag++;
}
}
max2->next=NULL;
flag=0;

printf("\n请输入可以利用是资源数目\n");
availablehead=workhead=(struct available*)malloc(sizeof(struct available));
printf("\n");
printf("\t当前资源类型是 %c\t",'a');
scanf("%d",&availablehead->value.a);
printf("\t当前资源类型是 %c\t",'b');
scanf("%d",&availablehead->value.b);
printf("\t当前资源类型是 %c\t",'c');
scanf("%d",&availablehead->value.c);
workhead=availablehead;
workhead->value=availablehead->value;
flag=0;

alloctemp=allochead;
maxtemp=maxhead;
for(i=0;i<p;i++)
{
if(flag==0)
{
needhead=need1=need2=(struct need*)malloc(sizeof(struct need));
need1->next=need2->next=NULL;
need1->value.a=(maxtemp->value.a)-(alloctemp->value.a);
need1->value.b=(maxtemp->value.b)-(alloctemp->value.b);
need1->value.c=(maxtemp->value.c)-(alloctemp->value.c);
needhead=need1;
flag++;
}
else
{
need2=(struct need*)malloc(sizeof(struct need));
need2->value.a=(maxtemp->value.a)-(alloctemp->value.a);
need2->value.b=(maxtemp->value.b)-(alloctemp->value.b);
need2->value.c=(maxtemp->value.c)-(alloctemp->value.c);

need1->next=need2;
need1=need2;
flag++;
}
maxtemp=maxtemp->next;
alloctemp=alloctemp->next;
}
need2->next=NULL;
flag=0;

for(i=0;i<p;i++)
{
if(flag==0)
{
finishhead=finish1=finish2=(struct finish*)malloc(sizeof(struct finish));
finish1->next=finish2->next=NULL;
finish1->value=0;
finishhead=finish1;
flag++;
}
else
{
finish2=(struct finish*)malloc(sizeof(struct finish));
finish2->value=0;

finish1->next=finish2;
finish1=finish2;
flag++;
}
}
finish2->next=NULL;
flag=0;

for(temp=0;temp<p;temp++)
{
alloctemp=allochead;
needtemp=needhead;
finishtemp=finishhead;
worktemp=workhead;

for(j=0;j<p;j++)
{
if(finishtemp->value==0)
{
if((needtemp->value.a<=worktemp->value.a)&&(needtemp->value.b<=worktemp->value.b)&&(needtemp->value.c<=worktemp->value.c))
{
worktemp->value.a+=alloctemp->value.a;
worktemp->value.b+=alloctemp->value.b;
worktemp->value.c+=alloctemp->value.c;
finishtemp->value=1;
if(flag==0)
{
pathhead=path1=path2=(struct path*)malloc(sizeof(struct path));
path1->next=path2->next=NULL;
path1->value=j+1;
pathhead=path1;
flag++;
}
else
{
path2=(struct path*)malloc(sizeof(struct path));
path2->value=j+1;
path1->next=path2;
path1=path2;
flag++;
}
finishtemp=finishtemp->next;
alloctemp=alloctemp->next;
needtemp=needtemp->next;
}
else
{
finishtemp=finishtemp->next;
alloctemp=alloctemp->next;
needtemp=needtemp->next;
}
}
else
{
finishtemp=finishtemp->next;
alloctemp=alloctemp->next;
needtemp=needtemp->next;
}

}
}
path2->next=NULL;
finishtemp=finishhead;
pathtemp=pathhead;
for(temp=0;temp<p;temp++)
{
if(finishtemp->value==0)
{
printf("\n警告!当前系统是不安全的\n");
exit(0);
}
finishtemp=finishtemp->next;
}
printf("\n当前系统是安全的!\n");
printf("\n安全序列为: \n");
for(i=0;i<p;i++)
{
printf("p%d\t",pathhead->value);
pathhead=pathhead->next;
}
}

这个的确搞不懂!

用VC++6.0编写 操作系统课程设计:银行家算法~

本学期刚上操作系统的课,可惜没学好,惭愧

这个刚做过,直接贴给你好了,给分吧
#include
#include
#include
#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";
}
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<<"申请的资源大于最大需求量,请重新输入!
";
}
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;
}

操作系统(死锁避免)---银行家算法解题
答:银行家算法是死锁避免的重要算法。银行家算法:资源==钱;收回资源==收回贷款;收不回资源==不会放贷;例题:假设系统中有三类互斥资源R1,R2,R3。可用资源分别是9,8,5.在T0时刻系统有P1,P2,P3,P4,P5五个进程,...

银行家算法步骤
答:在分配资源时,需要遵循以下步骤:计算每个进程所需资源量与系统实际资源量之间的差值。根据差值判断该进程是否会发生死锁。如果一个进程所需资源量大于系统实际资源量,则该进程无法被分配资源。如果一个进程所需资源量小于系统...

银行家算法
答:银行家算法是一种最有代表性的避免死锁的算法。在避免死锁方法中允许进程动态地申请资源,但系银行家算法统在进行资源分配之前,应先计算此次分配资源的安全性,若分配不会导致系统进入不安全状态,则分配,否则等待。为实现银...

用C语言或C++编写操作系统作业:银行家算法
答:step (4) 若对所有i,Finish=true,则系统处于安全状态,否则处于不安全状态 /* 银行家算法,操作系统概念(OS concepts Six Edition) reedit by Johnny hagen,SCAU,run at vc6.0 */ #include "malloc.h" #include "stdio.h" #incl...

网络操作系统中的银行家算法是什么?
答:. 银行家算法 设Requesti是进程Pi的请求向量,如果Requesti[j]=K,表示进程Pi需要K个Rj类型的资源。当Pi发出资源请求后,系统按下述步骤进行检查:�(1) 如果Requesti[j]≤Need[i,j],便转向步骤2;否则...

银行家算法(操作系统)
答:P1的需求小于可用资源数,先满足P1的请求,然后回收P1资源:可用资源变为 (3,3,2)+(2,0,0)=(5,3,2);这时P3可分配,P3结束后回收资源,可用资源为(5,3,2)+(2,1,1)=(7,4,3)这时P0可分配,...

简述银行家算法的主要思想并说明该
答:简述银行家算法的主要思想,并说明该算法是否可以用于解决现实中的死锁问题,如下:银行家算法是死锁避免的经典算法,其核心思想是:进程动态地申请资源,每次申请资源时系统都执行安全状态检查算法判断本次申请是否会造成系统处于...

操作系统-银行家算法问题
答:3)系统会去满足;若此时去满足,则剩余资源为:A:1 B:1 C1 D:2 此时,各进程的状态:已占有资源 最大需求数 A B C D A B C D P1 0 0 0 0 0 0 1 2 ...

浅析银行家算法
答:银行家算法中的数据结构。为了实现银行家算法,在系统中必须设置这样四个数据结构,分别用来描述系统中可利用的资源,所有进程对资源的最大需求,系统中的资源分配以及所有进程还需要多少资源的情况。(1)可利用资源向量Available...

操作系统银行家算法
答:不会分配,看一下银行家算法的流程。可以看到 在step(1)若Request<=Need, goto step(2);否则错误返回.原因如下,每个进程开始之前,都必须声明自己需要的各类资源的最大值Max。Need 需求资源 = Max 最大需求 - ...