📄 exam5te.c
字号:
#include "dos.h"
#include "conio.h"
#include "alloc.h"
int available[10]={3,3,2,3,3,2,3,3,2,5};
int max[6][10]={{7,5,3,3,2,2,3,2,2,1},{3,2,2,3,2,2,3,2,2,1},{9,2,0,2,3,2,2,3,2,1},{2,2,2,3,2,2,3,2,2,1},{4,3,3,3,2,2,3,2,2,1}};
int allocation[6][10]={{0,1,0,0,1,0,0,1,0,1},{2,0,0,0,1,0,0,1,0,1},{3,0,2,2,0,0,0,1,0,0},{2,1,1,3,0,2,2,0,0,0},{0,0,2,2,1,1,3,0,2,2}};
int need[6][10]={{7,4,3,3,2,2,3,2,2,1},{1,2,2,3,2,2,3,2,2,1},{6,0,0,3,2,2,3,2,2,1},{0,1,1,2,0,2,3,2,2,3},{4,3,1,2,0,2,3,2,2,3}};
int alive[5]={1,1,1,1,1};
int stay[3][3];
int request[10],process[5];
int i,j,p=5,pro,x,k1;
void label()
{ x=1;
printf("\n Max Allocation Need Available\n");
printf("PRO A B C D E F G H I J A B C D E F G H I J A B C D E F G H I J A B C D E F G H I J\n");
for(i=0;i<p;i++)
{ if(alive[i]==1)
{ printf("P%d ",i);
for(j=0;j<=9;j++)
{printf("%-3d",max[i][j]);}printf(" ");
for(j=0;j<=9;j++)
{printf("%-3d",allocation[i][j]);}printf(" ");
for(j=0;j<=9;j++)
{printf("%-3d",need[i][j]);}printf(" ");
}
if(x==1&&alive[i]==1) {printf("%-3d%-3d%-3d%-3d%-3d%-3d%-3d%-3d%-3d%-3d",available[0],available[1],available[2],available[3],available[4],available[5],available[6],available[7],available[8],available[9]);x=0;}
if(alive[i]==1) printf("\n");
}
}
int go()
{ int v=1;
for(i=0;i<=9;i++)
{if(need[pro][i]<request[i]) {v=0;break;}
if(available[i]<request[i]) {v=0;break;}}
if(v==1){for(i=0;i<=9;i++)
{stay[0][i]=allocation[pro][i];allocation[pro][i]+=request[i];
stay[1][i]=need[pro][i];need[pro][i]-=request[i];
stay[2][i]=available[i];available[i]-=request[i];}
}
return(v);
}
int test()
{ int work[10],finish[5]={0,0,0,0,0},v,k=0,t=0;
for(i=0;i<p;i++)
if(alive[i]==0) {finish[i]=1;k1--;}
for(i=0;i<=9;i++) work[i]=available[i];
while(1)
{ for(i=0;i<=4;i++)
{ if(finish[i]==0) { v=1;
for(j=0;j<=9;j++)
if(need[i][j]>work[j]) {v=0;break;}
if(v==1) { finish[i]=1;
for(j=0;j<=9;j++) work[j]+=allocation[i][j];
process[k]=i;k++;
}
}
}
if(t==k) break;
else t=k;
if(k==k1) break;
}
if(k==k1) return(1);
else return(0);
}
main()
{
while(1)
{label();
k1=5;
printf("Please choose one process(0,1,2,3,4):P");
scanf("%d",&pro);
if(pro<0||pro>4) break;
printf("The ten numbers of P%d's requests(,):",pro);
scanf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",&request[0],&request[1],&request[2],&request[3],&request[4],&request[5],&request[6],&request[7],&request[8],&request[9]);
if(go()==0) {printf("Error\n",pro);
printf("Press any key to continue...");}
else if(test()==0) {printf("The system is not safe!\n",pro);
for(i=0;i<=9;i++)
{ allocation[pro][i]=stay[0][i];
need[pro][i]=stay[1][i];
available[i]=stay[2][i];}
printf("Press any key to continue...");}
else {label();printf("The system is now safe!\n");
printf("The sequenceof safety:");
for(i=0;i<k1-1;i++)
printf("P%d>",process[i]);
printf("P%d",process[k1-1]);
printf("\n Press any key to continue...");}
if(need[pro][0]==0&&need[pro][1]==0&&need[pro][2]==0&&need[pro][3]==0&&need[pro][4]==0&&need[pro][5]==0&&need[pro][6]==0&&need[pro][7]==0&&need[pro][8]==0&&need[pro][9]==0)
for(i=0;i<=9;i++)
{available[i]+=max[pro][i];
alive[pro]=0;}
getch();
delay(5000);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -