最近最近在C中用于操作系统的算法

时间:2014-04-25 20:46:45

标签: operating-system lru

我制作了这段代码,但这显示了页面错误的不同值。请帮我。 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;
}

0 个答案:

没有答案