1

我制作了这段代码,但是这显示了页面错误的不同值。请帮我。time 是一个数组,用于存储页面被引用的次数。而“foo”是一个函数,旨在返回帧列表中一页的最短时间。

    #include <stdio.h>

    int p,i,b,a[50],f[30],counter,fault;
    int time[10] = {0};
    int foo (void);

    int main (void)
    {

    int min,flag,j;
     printf("Enter the number of frames\n");
     scanf("%d",&b);
     for ( i = 0; i < b; i++ )
     f[i] = -1;

     printf("Enter the number of pages\n");
     scanf("%d",&p);

     printf("Enter the pages\n");
     for ( i = 0; i < p; i++ )
     scanf("%d",&a[i]);

     fault = 0, counter = 0;

     for (i = 0; i < p; i++)
     {
       flag = 0;
       if ( i < b )
       {
         f[i] = a[i];
         fault++;
         time[i]++;
        }
       else
      {
       for ( j = 0; j < b; j++ )
       {
        if ( a[i] == f[j] )
        {
         flag = 1;
         time[j]++;

       }
      }

    if ( flag == 0 )
    {
     min = foo();
     printf("The page replaced for %d page is at pos %d\n",i,min);
     f[min] = a[i];
     fault++;
     time[min]++;
    }
   }
  }

  printf("The number of page faults are %d\n",fault);
  return 0;
}

    int foo (void)      

 {
  int z,bar1, bar2;

bar1 = time[0];
bar2 = 0;
for ( z = 1; z < b; z++ )
{
if ( time[z] < bar1 )
{
bar1 = time[z];
bar2 = z;
}
}
return bar2;
}
4

0 回答 0