Labels

Saturday 17 November 2012

LRU Page Replacement Algo in C

/* program to implement page replacement using LRU algorithm*/
#include<stdio.h>
int i,j=1,k,l,re[30],p[10],ch,no,nr,c,al=0,a,line=6;
struct re
{
          int st,l,ps;
}opr;
main()
{
          clrscr();
          printf("enter the length of the reference string:");
          scanf("%d",&nr);
          printf("enter the reference string:");
          for(i=1;i<=nr;i++)
          scanf("%d",&re[i]);
          printf("\n enter the number of frames:");
          scanf("%d",&no);
          clrscr();
          for(i=1;i<=no;i++)
          p[i]=-1;
          opr.st=0;
          for(i=1;i<=nr;i++)
          {
                   al=0;
                   opr.st=100;
                   for(c=1;c<=no;c++)
                   if(re[i]==p[c])
                   al++;
                   if(al==0)
                   {
                             if(j<=no)
                             {
                                      p[j]=re[i];
                                      j++;
                             }
                             else
                             {
                                      for(k=1;k<=no;k++)
                                      {
                                                for(ch=i-1;ch>=1;ch--)
                                                {
                                                          a=0;
                                                          if(p[k]==re[ch])
                                                          {
                                                                   a++;
                                                                   break;
                                                          }
                                                }
                                                if(a!=0)
                                                {
                                                          if(opr.st>ch)
                                                          {
                                                                   opr.st=ch;
                                                                   opr.l=re[ch];
                                                                   opr.ps=k;
                                                          }
                                                }
                                                else if(a==0)
                                                {
                                                          opr.ps=k;
                                                          break;
                                                }
                                      }
                                      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