代码
#include<stdio.h>
#include<string.h>
void worst_fit(int space[],int requests[],int m,int n)
{
int cp[m];
int i,j;
for(i=0;i<m;i++)
cp[i]=space[i];
int allocation[n];
memset(allocation,-1,sizeof(allocation));
int check[m];
for(i=0;i<n;i++)
{
memset(check,0,sizeof(check));
int max=-1;
for(j=0;j<m;j++)
{
if(requests[i]<=space[j])
{
check[j]=1;
if(max==-1)
max=j;
}
}
//printf("%d \n",requests[i]);
for(j=0;j<m;j++)
{
if(check[j]!=0)
{
if(space[j]>space[max])
max=j;
}
}
//printf("%d \n",requests[i]);
allocation[i]=cp[max];//copy of space should done: pending
printf("%d %d -\n",requests[i],space[max]);
space[max]=space[max]-requests[i];
printf("%d %d\n",requests[i],space[max]);
}
printf("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < n; i++)
{
//printf("%d\n",requests[i]);
printf("%d \t\t%d \t\t",i+1,requests[i]);
if (allocation[i] != -1)
printf("%d \n",allocation[i]);
else
printf("Not Allocated \n");
}
}
int main()
{
int i;
int s[]={100, 500, 200, 300, 600};
int r[]={212, 417, 112, 426};
int m = sizeof(s) / sizeof(s[0]);
int n = sizeof(r) / sizeof(r[0]);
worst_fit(s,r,m,n);
}
由于以下代码段
,输出显示意外错误printf("%d %d -\n",requests[i],space[max]);
space[max]=space[max]-requests[i];
printf("%d %d\n",requests[i],space[max]);
上面的输出将是
212 600 -
212 388
417 500 -
417 83
112 388 -
112 276
426 426 -
0 0
我不明白如何将存储在请求[i]中的426减少为0。
完整的输出是
212 600 -
212 388
417 500 -
417 83
112 388 -
112 276
426 426 -
0 0
Process No. Process Size Block no.
1 212 600
2 417 500
3 112 600
4 0 Not Allocated