| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 909 人关注过本帖
标题:C语言 Ubuntu银行家算法
只看楼主 加入收藏
蕙质兰心宿
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-5-6
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
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
求助
如果各位实在觉得我的代码很垃圾,求各位帮忙修改代码。
谢谢!
搜索更多相关主题的帖子: include 银行家 please 
2013-05-06 15:33
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:10 
conio.h  这个是16位dos下的东东吧  现在的编译器 一般都不带这类文件了吧

DO IT YOURSELF !
2013-05-06 16:53
czz5242199
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:660
专家分:2400
注 册:2011-10-26
收藏
得分:10 
避免出现死锁的那个?

有这么长吗?
2013-05-06 17:10
蕙质兰心宿
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-5-6
收藏
得分:0 
回复 3楼 czz5242199
老师说要写200~300行。
2013-05-13 18:11
蕙质兰心宿
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-5-6
收藏
得分:0 
回复 2楼 wp231957
所以?去掉?
2013-05-13 18:12
快速回复:C语言 Ubuntu银行家算法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018901 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved