0

This code is for the page replacement 'FIFO' algorithm.

When I run the code it enters an infinite loop and doesn't end. I tried searching for it but I am unable to identify it.

Code:

#include <stdio.h>

int i,numOfPages,frameSize,frames[10],pages[30];
void fifo();
//void lru();
//void opt();

int main(){
int ch;

printf("\nEnter the total number of pages: ");
scanf("%d",&numOfPages);

printf("\nEnter the seq of pages: ");
for(i=0;i<numOfPages;i++)
    scanf("%d",&pages[i]);

printf("\nEnter the frame size: ");
scanf("%d",&frameSize);

printf("\n***MENU***");
printf("\n1.FIFO \t2.LRU \t3.OPT");
printf("\n\nEnter the choice: ");
scanf("%d",&ch);

do{
    switch(ch)
    {
        case 1: fifo();
            break;
        /*case 2: lru();
            break;
        case 3: opt();
            break;*/
        default: printf("Invalid choice!");

    }
}while(ch>0 && ch<4);

return 0;
}



void fifo(){

int currNum,pindex=0,findex=0,faults=0,flag;

for(i=0;i<frameSize;i++)
    frames[i] = -1;

while(pindex < numOfPages){

    flag=1;
    currNum = pages[pindex];

    for(i=0;i<frameSize;i++)
    {
        if(currNum==frames[i])
        {
            pindex++;
            flag=0;
            break;
        }
    }

    if(flag==1)
    {
    if(findex < frameSize)
    {
        frames[findex] = pages[pindex];
        pindex++;
        findex++;
        faults++;
    }else{
        findex = 0;
    }
    }

printf("\nCurrent Frames: ");
for(i=0;i<frameSize;i++)
    printf("%d \t",frames[i]);

}


printf("\n\nTotal number of page faults are: %d and Total number of page hits are: %d",faults,(numOfPages-faults));

}  
Lundin
  • 195,001
  • 40
  • 254
  • 396
Siddhartha
  • 11
  • 5

4 Answers4

0

Since you don't change the value of ch, it's quite possible that }while(ch>0 && ch<4); will loop indefinitely.

Remember that break exits the switch, not the loop.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

I can't test you program but i think the error is :

while(ch>0 && ch<4);

try to set a different value forch in fifo()

Engine
  • 5,360
  • 18
  • 84
  • 162
0

you're not getting another char here:

do{
switch(ch)
{
    case 1: fifo();
        break;
    /*case 2: lru();
        break;
    case 3: opt();
        break;*/
    default: printf("Invalid choice!");

}

}while(ch>0 && ch<4);

once the user enters a single char into 'ch', you keep looping with this one infinitley.

try adding

scanf("%d",&ch);

just before the 'while' statment

hcf
  • 148
  • 5
0

Simply put: You're getting some parameters from the user and use that in a conditional loop. However in the loop you never change or update the parameters values. So when the user chooses a value that will create a loop, it will also the next time, the next time.... it loops until infinity.

I understand that after each loop you want to ask the user again what it wants to do unless it inputs an invalid number. Then you print out "invalid choice" and stop. The simplest thing to do is move line 26 (do{) to line 10 (after line int ch;).

dj bazzie wazzie
  • 3,472
  • 18
  • 23