C中的FIFO页面替换,输出错误

时间:2016-04-27 00:47:57

标签: c

我试图从具有页面框架和参考字符串的文本文件输入中读取,例如:在我的输入文件中我有3,7,0,1,2,0,3,0,4,2,3,0, 3,2,1,2,其中起始3是页面帧的数量,从7号起,它是参考字符串。 expected output

#include <stdio.h>
#include <string.h>
void FIFO(char [],char [],int,int);

//FIFO
void FIFO(char s[],char Fi[],int n,int f)
{
int i,j=0,k,flag=0,cnt=0;
printf("\n\tPAGE\t    FRAMES\t  FAULTS");
for(i=0;i<n;i++)
{
    for(k=0;k<f;k++)
    {
        if(Fi[k]==s[i])
            flag=1;
    }

    if(flag==0)
    {
        printf("\n\t%c\t",s[i]);
        Fi[j]=s[i];
        j++;

        for(k=0;k<f;k++)
        {
            printf("   %c",Fi[k]);
        }
        printf("\tPage-fault%d =",cnt);
        cnt++;
        // sum = cnt++;
        printf(" Total Faults",cnt);

    }

    else
    {
        flag=0;
        printf("\n\t%c\t",s[i]);
        for(k=0;k<f;k++)
        {
            printf("   %c",Fi[k]);
        }

        printf("\tNo page-fault");
    }
    if(j==f)
        j=0;
}

}

int main()
{

int n1, n2,n3,n4;
int ch,YN=1,i,l,f,k,j;
char Fi[30],s[25];
char g[0];

/*printf("\nPlease Enter Number of Frames:" );
scanf("%d",&f);
printf("\nPlease Enter the length of the string: ");
scanf("%d",&l);
printf("\nPlease Enter the string: ");
scanf("%s",s);*/

FILE *fp1;                                      // File Pointer Declaration

fp1 = fopen("Inr.txt","r");                     // File Opening by File Name and Read Mode r
printf("The number of Frames");
fscanf(fp1,"%d",&f);



for(i=0;i<f;i++)
    //fscanf(fp1,"%d,",&f);
    Fi[i]=-1;
fclose(fp1);
do
{

    printf("\n_________ Please Select One Option __________\n");
    printf("\n\t1:FIFO\t2:EXIT");
    printf("\n\n\tEnter your choice: ");
    scanf("%d",&ch);

    switch(ch)
    {
        case 1:
            for(i=0;i<f;i++)
            {
                Fi[i]=-1;
            }

            FIFO(s,Fi,l,f);
            break;
        case 2:
            exit(0);
    }
    printf("\n\n\tDo u want to continue IF YES PRESS 1\n\n\tIF NO PRESS 0 : ");
    scanf("%d",&YN);
}while(YN==1);return(0);

}

1 个答案:

答案 0 :(得分:0)

也许这段代码会让你入门......

#include <stdio.h>
#include <string.h>

//FIFO
void FIFO(
      int  *I__pages,
      char *I__frames,
      int   I__maxPages,
      int   I__maxFrames
      )
   {
   int pageIndex;
   int j=0;
   int pageFaults=0;
   int frameIndex;
   int noFault=0;

   for(frameIndex=0; frameIndex<I__maxFrames; frameIndex++)
      I__frames[frameIndex]=-1;

   printf("\tPAGE\t  FRAMES\tFAULTS\n");
   for(pageIndex=0; pageIndex < I__maxPages; ++pageIndex)
      {
      printf("\t%d\t", I__pages[pageIndex]);

      for(frameIndex=0; frameIndex<I__maxFrames; frameIndex++)
         {
         if(I__frames[frameIndex]==I__pages[pageIndex])
            noFault=1;
         }

      if(noFault)
         {
         noFault=0;
         for(frameIndex=0; frameIndex<I__maxFrames; frameIndex++)
            printf(" %2d",I__frames[frameIndex]);

         printf("\tNo page-fault\n");
         continue;
         }

      I__frames[j++] = I__pages[pageIndex];
      if(j==I__maxFrames)
         j=0;

      for(frameIndex=0; frameIndex<I__maxFrames; frameIndex++)
         printf(" %2d",I__frames[frameIndex]);

      printf("\tPage-fault%d =", pageFaults++);
      printf(" Total Faults\n");
      }

   return;   
   }

int main()
   {
   int ch,YN=1;
   int maxPages = 0;
   int maxFrames;
   char frames[30];
   int  pages[25];
   FILE *fp1 = NULL;                               // File Pointer Declaration

   fp1 = fopen("Inr.txt","r");                     // File Opening by File Name and Read Mode r
   fscanf(fp1,"%d,", &maxFrames);                   // Read the maxFrames value first.

   while(EOF != fscanf(fp1,"%d,",&pages[maxPages]))
      ++maxPages;

   fclose(fp1);

   do {
      printf("\n_________ Please Select One Option __________\n");
      printf("\n\t1:FIFO\t2:EXIT");
      printf("\n\n\tEnter your choice: ");
      scanf("%d",&ch);
      switch(ch)
         {
         case 1:
            FIFO(pages, frames, maxPages, maxFrames);
            break;

        case 2:
            return(0);
         }

      printf("\n\n\tDo u want to continue IF YES PRESS 1\n\n\tIF NO PRESS 0 : ");
      scanf("%d",&YN);
      } while(YN==1);

   return(0);
   }