页面替换算法的代码中的逻辑错误(错误)

时间:2019-02-25 17:31:27

标签: c debugging operating-system scanf

在以下用于最佳页面替换算法的代码中,如果我输入整数输入,则第一个for循环下的scanf不会在'n'值处停止。如果输入是重复的,则效果很好。

EX:如果n的值为7,并且放入的页面为1 1 1 1 1 1 1 该代码工作正常 但是如果页面输入是1 2 3 4 3 2 1 它永远不会停止接受输入。

我什至尝试过像i <7这样的for循环中的显式声明,但仍然无法正常工作 这是我的代码:

#include<stdio.h>
#include<stdlib.h>
static int MAX =999;
//to find out maximum
int maxim(int a[], int size)
    {
        if(size <= 0) return -1;
        int i, max_i = 0;
        int max = a[0];
        for(i = 1; i < size; ++i)
        {
            if(a[i] > max)
            {
                max = a[i];
                max_i = i;
            }
        }
        return max_i;
    }
int main()
{
    int i,j,k,n,temp_i=0,maximum,temp_j,
    count=0,l,pageFault,
    page[100],frame[50],position[50];
    printf("Enter the number of pages\n");
    scanf("%d",&n);
    printf("\nEnter the number of frames\n");
    scanf("%d",&k);
    printf("\nEnter the page sequence\n");
    //The problem is in the following two line
    for(i=0;i<n;i++)
        scanf("%d",&page[i]);
    for(i=0;i<k;i++)
        frame[i]=-1;
    i=0;
    while(i<k)
    {
        frame[i]=page[i];
        i++;
        count+=1;
    }
    for(i=k;i<n;i++)
    {
        for(j=0;j<k;j++)
        {
            if(frame[j]==page[i])
            break;
        }
        if(j==k)
        {
            temp_i=i;temp_j=0;
            for(l=0;l<k;l++)
                position[l]=MAX;
            while(temp_i<n)
            {   
                while(temp_j<k)
                {
                    if(page[temp_i]==frame[temp_j])
                    {
                        position[temp_j]=temp_i;
                        temp_j++;
                    }
                    //temp_i++;
                }
                temp_i++;
            }
            maximum=maxim(position,k);
            frame[maximum]=page[i];
            count+=1;
        }
    }
    printf("\nThe final frames status is:\n");
    for(i=0;i<k;i++)
    printf("%d",frame[i]); 
    pageFault=count;
    printf("The number of page fault is %d\n", pageFault);
    printf("The hit ratio is %lf\n",(float)pageFault/n); 
}

2 个答案:

答案 0 :(得分:0)

  

EX:如果n的值是7,并且放入的页面是1 1 1 1 1 1 1 1代码可以正常工作,但是如果页面输入是1 2 3 4 3 2 1,则它永远不会停止输入。

对我来说,它不会无休止地获取输入,而是因为:

而永远循环
while(temp_j<k)
{
  if(page[temp_i]==frame[temp_j])
  {
     position[temp_j]=temp_i;
     temp_j++;
  }
  //temp_i++;
}

如果(page[temp_i]==frame[temp_j])为假,则没有更改允许(temp_j<k)变为假,并且 while 永不结束。

如果是

pi@raspberrypi:/tmp $ ./a.out
Enter the number of pages
7

Enter the number of frames
1

Enter the page sequence
1 1 1 1 1 1 1

The final frames status is:
1The number of page fault is 1
The hit ratio is 0.142857

您不会永远进入,但

pi@raspberrypi:/tmp $ ./a.out
Enter the number of pages
7

Enter the number of frames
1

Enter the page sequence
1 2 3 4 5 6 7

您输入


总是这样做temp_j++;似乎更具逻辑性:

        while(temp_i<n)
        {   
            temp_j=0;
            while(temp_j<k)
            {
                if(page[temp_i]==frame[temp_j])
                {
                    position[temp_j]=temp_i;
                }
                temp_j++;
            }
            temp_i++;
        }

当然也重置了temp_j

现在执行的是:

Enter the number of pages
12

Enter the number of frames
3

Enter the page sequence
1 2 3 4 1 2 5 1 2 3 4 5

The final frames status is:
523The number of page fault is 9
The hit ratio is 0.750000

答案 1 :(得分:0)

我以两种方式找到答案。 @bruno在原始答案中建议的一个

,另一个如下 通过使用for循环而不是while

if(j==k)
    {
        temp_i=i;temp_j=0;
        for(l=0;l<k;l++)
            position[l]=MAX;
        for(temp_i=i;temp_i<n;temp_i++)
        {   
            for(temp_j=0;temp_j<k;temp_j++)
            {
             if(page[temp_i]==frame[temp_j])
                {
                    position[temp_j]=temp_i;
                }
             //temp_i++;
            }
        }
        maximum=maxim(position,k);
        frame[maximum]=page[i];
        count+=1;
    }