银行家算法防止死锁-银行家算法 第3页

银行家算法防止死锁-银行家算法 第3页
 {
             LeftRes[i]=10;//初始化总资源数为 10
             for (j=1;j<=3;j++)
             {
                   LeftRes[i]-=pro[j].p[i];//减去已经被占据的资源
             }
       }

       // 以下输出资源状况表
       printf("\n===========================================================
====\n");
       printf("%-20s","当前剩余资源");
       for (i=1;i<=3;i++)
       {
             printf("%8ld",LeftRes[i]);
       }
       for (i=1;i<=3;i++)
       {
             printf("\n%s%ld%-15s","进程",i,"(占用/最大)");
             for (j=1;j<=3;j++)
             {
                   printf("%4ld/%3ld",pro[i].p[j],pro[i].tot[j]);
             }
       }
       printf("\n===========================================================
====\n\n");

}

bool safe(long x)//函数功能:检查分配是否安全
{
       long i,j,y[4];
       bool change=1;
       bool T[4];
       long stack[4],top=0;

       memset(T,0,sizeof(T));
       for (i=1;i<=3;i++)
       {
             y[i]=LeftRes[i]-qu[i];
             pro[x].p[i]+=qu[i];
       }

       while (change)
  {
             change=0;
             for (i=1;i<=3;i++)
             {
                   if (T[i])
                          continue;
                   if      (y[1]>=pro[i].tot[1]-pro[i].p[1]            &&       y[2]>=pro[i].tot[2]-pro[i].p[2]            &&
y[3]>=pro[i].tot[3]-pro[i].p[3])
                   {
                          for (j=1;j<=3;j++)
                          {
                                y[j]+=pro[i].p[j];
                          }
                          T[i]=1;
                          change=1;
                          stack[++top]=i;
                   }
             }
       }
       for (i=1;i<=3;i++)
       {
             if (!T[i])
             {
                   return 0;
             }
       }
       printf(" 申请成功!  安全序列为: ");
       for (i=1;i<=top;i++)
             printf("%2ld",stack[i]);
       return 1;
}

void require(long x)//函数功能:尝试分配资源给进程x
{
       long i,j;

       if (LeftRes[1]<qu[1] || LeftRes[2]<qu[2] || LeftRes[3]<qu[3])
       {
             printf(" 申请被拒绝!  没有足够的剩余资源。\n");
             return ;
       }
       if (avoid)
       {
             if (safe(x))   {
                   ;
             }
             else
             {
                   printf(" 申请被拒绝!  不符合安全规则。\n");
                   return ;
             }
       }
       else
       {
             printf(" 申请成功!\n");
       }

       printf("\n===========================================================
====\n");
       printf("%-20s","当前剩余资源");
       for (i=1;i<=3;i++)
       {
             printf("%8ld",LeftRes[i]-qu[i]);
       }
       for (i=1;i<=3;i++)
       {
             printf("\n%s%ld%-15s","进程",i,"(占用/最大)");
             for (j=1;j<=3;j++)
             {
                   if (i==x && !avoid)
                         pro[i].p[j]+=qu[i];
                   printf("%4ld/%3ld",pro[i].p[j],pro[i].tot[j]);
             }
       }
       printf("\n===========================================================
====\n\n");

}

void allot()//函数功能:模拟资源分配过程
{
       long i,j;

       printf("请输入申请资源的进程ID,以及申请的各种资源数量:\n");
       scanf("%ld",&i);
       for (j=1;j<=3;j++)
       {
                   scanf("%ld",&qu[j]);
             }
             require(i);
}

int main()
 {
             init();
             allot();

             return 0;
}

上一页  [1] [2] [3] 

Copyright © 2007-2012 www.chuibin.com 六维论文网 版权所有