帮忙解决下我粗糙版的银行家算法错误
#include<stdio.h>
int available[3]={10,5,7};
struct pcb /*进程动态需求表*/
{
int max[6][3],allocation[6][3],need[6][3];
}
int Init(struct pcb *p)/*T0时刻需求表*/
{
int i,j;
for(i=0;i<5;i++)
{
printf("请输入进程p[%d]需要的最大资源量:",i);
for(j=0;j<3;j++)
{
scanf("%d",&(p->max[i][j]));
if(p->max[i][j]>available[j]) return 0;
printf(" ");
}
printf("\n请输入进程p[%d]分配的资源量:",i);
for(j=0;j<3;j++)/*可以改成初始分配函数*/
{
scanf("%d",&(p->allocation[i][j]));
printf(" ");
}
for(j=0;j<3;j++)
{
p->need[i][j]=p->max[i][j]-p->allocation[i][j];
printf(" ");
}
}
return 1;
}
int request(struct pcb *p,int i)/*可以改成按照某种算法自动请求向量*/
{
int k,j,m,n;
printf("请输入%d号进程请求资源量:");
for(k=0;k<3;k++)
{
scanf("%d",&j);
if(j>p->need[i][k]||j>available[k])/*这里应该进行压栈,执行下个请求信号量*/
{
printf("错误");
return 0;
}
else
{
available[k]=available[k]-j;
p->allocation[i][k]+=j;
p->need[i][k]=p->need[i][k]-j;
}
printf(" ");
}
return 1;
}
int safe(struct pcb *p)
{
int flag[2]={1,0},work[3],finish[5]={0},i,j=0;
for(i=0;i<3;i++)
work[i]=available[i];
while(1)
{
if(finish[j]==1) j++;
for(i=0;i<3;i++)
if(p->max[j][i]<=work[i])
continue;
else
{flag[0]=0;break;}
if(flag[0]==1)
{
for(i=0;i<3;i++)
work[i]+=p->allocation[j][i];
finish[j]=1;
flag[1]=j;
}
j++;
if(j==5) j=0;
if(flag[0]==0&&flag[1]==j) return 0;
}
return 1;
}
int main()
{
int i,j,s[3];
struct pcb p;
for(i=0;i<3;i++)
s[i]=available[i];
Init(&p);
if(safe(&p)==0)
{
printf("错误");
return 0;
}
printf("请输入请求向量的进程:");
scanf("%d",&j);
i=request(&p,j);
if(safe(&p)==0)
{
for(i=0;i<3;i++)
available[i]=s[i];
return 0;
}
}
出现的错误是:
E:\学习\代码\银行家安全算法.cpp(7) : error C2628: 'pcb' followed by 'int' is illegal (did you forget a ';'?)
E:\学习\代码\银行家安全算法.cpp(16) : error C2440: 'return' : cannot convert from 'const int' to 'struct pcb'
No constructor could take the source type, or constructor overload resolution was ambiguous
E:\学习\代码\银行家安全算法.cpp(31) : error C2440: 'return' : cannot convert from 'const int' to 'struct pcb'
No constructor could take the source type, or constructor overload resolution was ambiguous
执行 cl.exe 时出错.
银行家安全算法.obj - 1 error(s), 0 warning(s)