C语言 Ubuntu银行家算法
求助虽然这是个作业,但是真心想学习。现在在做操作系统的银行家算法,我学的是C语言,所以,用C实现银行家算法。
下面是程序代码。
#include<stdio.h>
#include<conio.h>
struct pcb
{
int rdy[10];
int nd[10];
}jc[10],tjc[10];
int sys[10],tsys[10];
int nn;
int n;
int g;
int qq;
int ri=0;
int a=0;
int ww[10];
int wait[10][10];
int req[10];
int as[10];
int zhuang[10];
char tcs;
void inputpcb()
{
int i,j;
printf("please input the num of source: ");
scanf("%d",&nn);
l:if(nn>1000 || nn <0) {printf("\nplease input the num of source: ");scanf("%d",&nn);goto l;}
printf("\nplease input the num of available: ");
for(i=1;i<=nn;i++)
{scanf("%d",&tsys[i]);
o:if(tsys[i]>1000 || tsys[i] <0) {printf("\nplease input the num of available: ");scanf("%d",&tsys[i]);goto o;}
}
printf("\nplease input the num of PCB: ");
scanf("%d",&n);
t:if(n>1000 || n <0) {printf("\nplease input the num of PCB: ");scanf("%d",&n);goto t;}
printf("\nplease input the PCB:\n");
for(i=1;i<=n;i++)
{printf("\nthe %dth PCB,",i);
printf("the already and the need\n");
for(j=1;j<=nn;j++)
{scanf("%d",&(tjc[i].rdy[j]));
tt:if(tjc[i].rdy[j]>100 || tjc[i].rdy[j]<0) {printf("\nplease input the already: ");scanf("%d",&(tjc[i].rdy[j]));goto tt;}
}
for(j=1;j<=nn;j++)
{scanf("%d",&(tjc[i].nd[j]));
ttt:if(tjc[i].nd[j]>100 || tjc[i].nd[j]<0) {printf("\nplease input the need: ");scanf("%d",&(tjc[i].nd[j]));goto ttt;}
}
}
}
void csh()
{
int i,j;
for(i=1;i<=nn;i++) sys[i]=tsys[i];
for(i=1;i<=n;i++)
for(j=1;j<=nn;j++) {jc[i].rdy[j]=tjc[i].rdy[j];jc[i].nd[j]=tjc[i].nd[j];}
for(i=1;i<=n;i++) zhuang[i]=0;
}
void outputpcb()
{
int i,j;
printf("allocation need available\n\n");
for(i=1;i<=n;i++)
{
printf(" ");
for(j=1;j<=nn;j++) printf("%d ",tjc[i].rdy[j]);
printf(" ");
for(j=1;j<=nn;j++) printf("%d ",tjc[i].nd[j]);
printf(" ");
if(i==1) for(j=1;j<=nn;j++) printf("%d ",tsys[j]);
printf("\n");
}
if(n==0)
{printf(" ");
for(j=1;j<=nn;j++) printf("%d ",tsys[j]);
printf("\n\n");
}
printf(" the wait query:\n");
for(i=1;i<=a;i++)
{printf("\n%d ",ww[i]);
for(j=1;j<=nn;j++)
printf("%d ",wait[i][j]);
}
printf("\n\n");
}
void inputreq()
{
int i,j=0;
printf("please input the PCB requested\n\n If you have no request,you can check the wait!\n\n");
scanf("%d",&qq);
oo:if(qq>1000 || qq<0) {printf("please input the PCB requested\n\n");scanf("%d",&qq);}
for(i=1;i<=n;i++)
if(qq==i) j++;
if(j<1) {printf("meiyou xiangtong de jincheng,please input the PCB requested\n\n");scanf("%d",&qq);goto oo;}
printf("please input the source requested\n\n");
for(i=1;i<=nn;i++)
{scanf("%d",&req[i]);
ooo:if(req[i]>1000 ||req[i]<0) {printf("please input the source requested\n\n");scanf("%d",&req[i]);goto ooo;}
}
}
int locate()
{
int i,j,k;
for(g=1;g<=n;g++)
{j=0;
if(zhuang[g]==0)
{for(i=1;i<=nn;i++)
if(jc[g].nd[i]>sys[i] )
{j=1;i=nn;}
if(j==0)
{
for(k=1;k<=nn;k++)
{ sys[k]+=jc[g].rdy[k];
zhuang[g]=1;
}
return(g);
}
}
}
return 0;
}
int ss(int ri)
{
int i;
for(i=1;i<=n;i++)
if(locate()!=0) as[++ri]=g;
if(i==n+1 && ri==n)
return 1;
else
return 0;
}
void output()
{
int i;
printf("\nthe sort is ");
for(i=1;i<=n;i++)
printf("%d ",as[i]);
}
int canloc()
{
int i,j;
j=0;
for(i=1;i<=nn;i++)
if(req[i]>(jc[qq].nd[i])) j=1;
if(j==1) return 2;
for(i=1;i<=nn;i++)
if(req[i]>sys[i]) j=1;
if(j==0) return 1;
else return 0;
}
int change(int a[10],int b)
{
int i;
for(i=1;i<=nn;i++)
{
jc[b].nd[i]-=a[i];
jc[b].rdy[i]+=a[i];
sys[i]-=a[i];
}
}
int exchange(int a[10],int b)
{
int i;
for(i=1;i<=n;i++)
{
jc[b].nd[i]+=a[i];
jc[b].rdy[i]-=a[i];
sys[i]+=a[i];
}
}
void agree()
{
int i;
for(i=1;i<=nn;i++) tsys[i]=tsys[i]-req[i];
for(i=1;i<=nn;i++) tjc[qq].nd[i]=tjc[qq].nd[i]-req[i];
for(i=1;i<=nn;i++) tjc[qq].rdy[i]=tjc[qq].rdy[i]+req[i];
}
void back()
{
int i,j,k,d;
printf("shuru xiangyao chexiao de PCB hao or 0: ");
scanf("%d",&k);
while(k!=0)
{
for(i=1;i<=nn;i++)
{
sys[i]+= jc[k].rdy[i];
tsys[i]+=tjc[k].rdy[i];
}
for(i=k;i<=n;i++)
for(j=1;j<=nn;j++)
{
jc[i].rdy[j]=jc[i+1].rdy[j];
jc[i].nd[j]=jc[i+1].nd[j];
tjc[i].rdy[j]=tjc[i+1].rdy[j];
tjc[i].nd[j]=tjc[i+1].nd[j];
}
for(i=1;i<=a;i++)
{
while (ww[i]==k)
{
for(j=i;j<=a;j++)
{
ww[j]=ww[j+1];
for(d=1;d<=nn;d++)
wait[j][d]=wait[j+1][d];
}
a-=1;
}
}
n-=1;
printf("shuru xiangyao chexiao de PCB hao or 0: ");
scanf("%d",&k);
}
pstart();
}
int pstart()
{
printf("\n1.If you want to tuichu you can input a;\n");
printf("2.If you want to chexiao zuoye you can input b;\n");
printf("3.If you want to shengqing ziyuan you can input c;\n");
scanf("%c",&tcs);
if(tcs=='a')
exit(0);
else
if(tcs=='b') back();
}
main()
{
int i,j;
system("cls");
printf("Welcome to the yinhangjiasuanfa system!\n\n");
pstart();
inputpcb();
while(1)
{
csh();
system("cls");
outputpcb();
inputreq();
if(canloc()==1)
{
change(req,qq);
if(ss(ri)==1)
{output();agree();}
else
{exchange(req,qq);
printf("the sys is not safe!!!");
initscr();
getch();
endwin();
}
}
else
if (canloc()==2)
{ printf("the request is bigger than need,stop the process and the next\n\n");
}
else
{ww[++a]=qq;
for(i=1;i<=nn;i++)
wait[a][i]=req[i];
for(i=1;i<=a;i++)
{printf("\n%d ",ww[i]);
for(j=1;j<=nn;j++)
printf("%d ",wait[i][j]);
}
printf("\n\nthe availble is not enough for the request,save it and wait!\nThe next\n\n");
}
pstart();
initscr();
getch();
endwin();
}
}
我学的仅仅是皮毛,这段代码是参考的百度文库里的。
我自己修改了,但是还是运行不了。
出现 conio.h: No such file or directory
求助
如果各位实在觉得我的代码很垃圾,求各位帮忙修改代码。
谢谢!