#include “stdio.h”
int Finish [10] = { 0 }; /* 进程最多10个 */
int Max [10][10] = { 0 }; /* 资源数最多10个 */
int Alloc [10][10] = { 0 };
int Need [10][10] = { 0 };
int Available [10] = { 10 };
int Request [10][10] = { 0 }; purse hanger
int main ()
{
int n,m;
int i,j;
int c=1;
printf(“\n输入进程个数和资源种类数 [ 都不超过10 ]\n “);
scanf(“%d%d”, &n, &m );
printf(“\n输入每类资源的数量\n”);
for( i=0; i<= m-1; i++ )
{
scanf(“%d”, & Available [i]);
}
printf(“\n输入每个进程对每类资源的最大需求量\n”);
for( i=0; i<= n-1; i++ )
{
for( j=0; j<= m-1; )
{
scanf(“%d”, & Max [i][j]);
if( Max [i][j] <= Available [j] ) { Need [i][j] = Max [i][j]; j++; }
else { printf(“错误!重输\n”); }
}
}
Need[10][10]=Max[10][10];
Print( n, m );
while( c )
{
Allocation ( n, m );
Print( n, m );
printf(“\n如果打印区空白务必按0退出 否则按1继续分配资源\n”); /* 打印区空白按1会导致死机 */
scanf(“%d”,&c);
}
return 0;
}
cheap louis vuitton handbags
/* 资源预分配模块 */
int Allocation ( int n, int m )
{
int i,j;
printf(“\n输入每个进程被分配的每类资源量 \n “);
for( i=0; i<= n-1; i++ )
{
for( j=0; !Finish [i] && j<= m-1; )
{
scanf(“%d”, & Request [i][j]);
if( Request [i][j] <= Need [i][j] ) j++;
else { printf(“错误!重输\n”); }
}
}
for( i=0; i<= n-1; i++ )
{
for( j=0; !Finish [i] && j<= m-1; j++ )
{
Available [j] -= Request [i][j];
Need [i][j] -= Request [i][j];
}
}
Deal ( n, m );
return 0;
}
/* 校验预分配模块 */
int Deal( int n, int m )
{
int Tag = 0;
int i,j,k;
for( i=0; i<= n-1; i++ )
{
for( j=0; !Finish [i] && j<= m-1; j++ )
{
if( Need [i][j]> Available [j] ) break;
}
if( j == m ) /* 有进程可以撤退 */
{
for( k=0; k<= n-1; k++ )
{
for( j=0; !Finish [k] && j<= m-1; j++ )
{
Alloc [k][j] += Request [k][j];
}
}
Finish [i] = 1; /* i记录撤退进程 */
Tag = 1;
for(j=0; j<= m-1; j++) Available [j] += Alloc [i][j];
break;
}
}
if( !Tag ) /* 无进程可以撤退 还原相应矩阵 */
{
for( i=0; i<= n-1; i++ )
{
for( j=0; !Finish [i] && j<= m-1; j++ )
{
Available [j] += Request [i][j];
Need [i][j] += Request [i][j];
}
}
Allocation ( n, m );
}
return 0;
}
/* 打印进程对资源利用情况 */
int Print( int n, int m )
{
int i,j;
printf(“\n进程对资源利用情况如下:\n”);
for( i=0; i<= n-1; i++ )
{
for( j=0; !Finish [i] && j<= m-1; j++ )
{
printf(“\n%d <- %d[ %d ] %d -> %d[ %d ]\t”,i, j, Alloc [i][j], i, j, Need [i][j]);
}
}
return 0;
}