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

问题描述 投票:-1回答:2

在下面的最佳页面替换算法代码中,如果i输入整数输入,则首先for循环的scanf不会停止在'n'的值。如果输入是重复的,它可以完美地工作。

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

我甚至在for循环中尝试过显式声明,如i <7,但它仍然不起作用这是我的代码:

#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); 
}
c debugging operating-system scanf
2个回答
0
投票

EX:如果n的值是7,并且页面输入是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)变得虚假而且永远不会结束。

的情况下

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

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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.