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



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

ex:如果n的值是7,并且page in pop in 1 1 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 pagesn");
    scanf("%d",&n);
    printf("nEnter the number of framesn");
    scanf("%d",&k);
    printf("nEnter the page sequencen");
    //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 %dn", pageFault);
    printf("The hit ratio is %lfn",(float)pageFault/n); 
}

ex:如果n的值为7,并且PAGE中的页面为1 1 1 1 1 1 1代码正常工作,但是如果页面输入为1 2 3 4 3 2 1,则永远不会停止输入。/blockquote>

对我而言,它不会在没有结束的情况下获得输入,而是因为:

永远循环
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)变成false,而 永远不会结束。

的情况下
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

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

和其他如下通过用于循环而不是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;
    }

相关内容

最新更新