-2

In the following code for optimal page replacement algorithm, the scanf under first for loop is not stopping at the value of 'n' if i enter integer inputs. It works perfectly if the input is repetitive.

EX: If the value of n is 7, and page in put is 1 1 1 1 1 1 1 The code works fine But if the page input is 1 2 3 4 3 2 1 it never stops taking input.

I have even tried explicit declaration in the for loop like i<7 but it still doesn't work Here is my code:

#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); 
}
  • Try put bellow your scanf this command: `fflush(stdin);`. Should look like: `scanf("%d",&page[i]); fflush(stdin);` – Esdras Xavier Feb 25 '19 at 17:43
  • 1
    Can you create a [mcve]? Are you sure the error is not elsewhere? – Johnny Mopp Feb 25 '19 at 17:46
  • Did you mean to reset `temp_j=0;` just before the `while(temp_j – Weather Vane Feb 25 '19 at 17:53
  • Add `printf("I/O done\n"); fflush(stdout);` after the input loop. Does that appear on the screen in both the OK and failure cases? My suspicion is that you have a problem elsewhere. If the problem is in the input, you don't need an awful lot of the code you show to demonstrate the problem. (Incidentally, repeating `int` is not harmful — don't spread three lines of variable definitions with a single `int`.) – Jonathan Leffler Feb 25 '19 at 17:58
  • _[…time passes…]_ I added the `printf()` I suggested and it worked for me with `n` at 7, `k` at 4, and input `1 2 3 4 3 2 1` on a single line. It said: `The final frames status is:` —  `12343The number of page fault is 5` — `The hit ratio is 0.714286`. I worry about the fact that 5 frames are printed where I only allowed it to use 4. With luck, the repeated 3 is a consequence of that — otherwise, it is a bad problem to have. (I think there should be just 4 page faults for the data given — there are other problems for you to solve as yet.) – Jonathan Leffler Feb 25 '19 at 18:04
  • 1
    @EsdrasXavier: Be cautious about suggesting `fflush(stdin)`. See [Using `fflush(stdin)`](https://stackoverflow.com/questions/2979209/using-fflushstdin) for the details, but it only works reliably on Windows systems and is not a general purpose solution. It isn't even particularly desirable; it would have stopped me entering `1 2 3 4 3 2 1` on a single line — which would be annoying as it would have thrown away the data I carefully typed. – Jonathan Leffler Feb 25 '19 at 18:06
  • My bad about that. – Esdras Xavier Feb 25 '19 at 18:09
  • @EsdrasXavier Nope, its not working either. – Satyam Srivastava Feb 25 '19 at 18:20
  • @JonathanLeffler Please try it with n=12,k=3 or 4, page sequence: 1 2 3 4 1 2 5 1 2 3 4 5 – Satyam Srivastava Feb 25 '19 at 18:26
  • Can you guys please check that i f I am failing at the logical level. I want to implement optimal page replacement algorithm. Thank you – Satyam Srivastava Feb 25 '19 at 18:28

2 Answers2

0

EX: If the value of n is 7, and page in put is 1 1 1 1 1 1 1 The code works fine But if the page input is 1 2 3 4 3 2 1 it never stops taking input.

For me it is not getting input without ending, but loops forever because of :

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

if (page[temp_i]==frame[temp_j]) is false there is no change allowing (temp_j<k) to become false and the while never ends.

In case of

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

you do not enter in the while forever case, but

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

you enter in it


It seems more logic to always do temp_j++; like that :

        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++;
        }

also reseting temp_j of course

now the execution is :

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
  • 31,755
  • 7
  • 21
  • 36
  • If I uncomment the temp_i++ accompanied with an else and remove the temp_i++ below it. The input problem is so;ved but it is going me segementation fault(core dumped) – Satyam Srivastava Feb 25 '19 at 18:18
  • @SatyamSrivastava probably because `temp_i++` become too large and you go out of _page_, you also need to check `temp_i` value in the while – bruno Feb 25 '19 at 18:19
  • Thats what I did in my original code, by then thats giving me a problem in input as you pointed out. Any suggestions? – Satyam Srivastava Feb 25 '19 at 18:31
-1

I found answer in two ways. One as suggested by @bruno in the original answer

and the other as following by using for loops instead of 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;
    }