此代码用于页面替换“FIFO”算法。
当我运行代码时,它会进入无限循环并且不会结束。我尝试搜索它,但我无法识别它。
代码:
#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));
}