Google Search

Thursday, October 6, 2011

write a C Program to implement page replacement using LFU algorithm


/*Program to implement page replacement using LFU algorithm*/
#include<stdio.h>
int i,j=1,s,k,l,re[30],p[10],ch,no,nr,c,a1=0,a,line=6,nk;
struct
{
          int st,l,ps,pos;
}opr;
main()
{
          clrscr();
          printf("Enter length of reference string:");
          scanf("%d",&nr);
          printf(" Enter reference string:");
          for(i=1;i<=nr;i++)
          scanf("%d",&re[i]);
          printf("\n Enter number of frames:");
          scanf("%d",&no);
          clrscr();
          for(i=1;i<=no;i++)
          p[i]=-1;
          opr.st=100;
          for(i=1;i<=nr;i++)
          {
                   a1=0;
                   opr.st=100;
                   opr.pos=100;
                   for(c=1;c<=no;c++)
                   if(re[i]==p[c])
                   a1++;
                   if(a1==0)
                   {
                             if(j<=no)
                             {
                                      p[j]=re[i];
                                      j++;
                             }
                             else
                             {
                                      for(k=1;k<=no;k++)
                                      {
                                                a=0;
                                                for(ch=i-1;ch>0;ch--)
                                                {
                                                          if(p[k]==re[ch])
                                                          {
                                                                   a++;
                                                                   nk=ch;
                                                          }
                                                }
                                                if(a>1)
                                                {
                                                          if(opr.st>a)
                                                          {
                                                                   opr.st=a;
                                                                   opr.ps=k;
                                                          }
                                                          else
                                                          if(opr.st==a)
                                                          {
                                                                   if(opr.pos>ch)
                                                                   opr.ps=k;
                                                          }
                                                }
                                                else
                                                if(a==1)
                                                {
                                                          if(opr.pos>nk)
                                                          {
                                                                   opr.pos=nk;
                                                                   opr.ps=k;
                                                                   opr.st=a;
                                                          }
                                                }
                                      }
                                      p[(opr.ps)]=re[i];
                             }
                   }
                   display(no,p,i);
          }
          printf("\n");
          getch();
}

display(int no,int p[],int i)
{
          int k;
          if(i==1)
          {
                   printf("\t\t\t");
                   for(k=1;k<=no;k++)
                   printf("_ _");
          }
          printf("\n%d",re[i]);
          gotoxy(25,line++);
          for(k=1;k<=no;k++)
          {
                   printf("|");
                   printf("_");
                   if(p[k]!=-1)
                   printf("%d",p[k]);
                   else
                   printf(" ");
                   printf("_");
          }
          printf("|");
}

0 comments:

Post a Comment