The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Wednesday, August 17, 2011

Optimal Page Replacement Algorithm



#include<stdio.h>
#include<conio.h>
#define max 100
#define min 10

int ref[max],count,frame[min],n;

void input()
{
 int temp;
 do
 {
  clrscr();
  printf("\n\n\tEnter the number of page frames : ");
  scanf("%d",&n);
 }while(n>min || n <= 0);
 count = 0;
 printf("\n\n\tEnter the reference string (-1 for end) : ");
 scanf("%d",&temp);
 while(temp != -1 && count <= max)
 {
  ref[count++]=temp;
  scanf("%d",&temp);
 }
}

int find_pos(int nm)
{
 int ps,l,big=nm;
 for(int k=0;k<n;k++)
 {
  l = nm+1;
  while(ref[l] != frame[k] && l < count)
    l++;
  if(l>big)
  {
   big = l;
   ps = k;
  }
 }
return ps;
}

void OPT()
{
 int i,j,fault=0;
 for(i=0;i<n;i++)
  frame[i] = -1;
 int ind,pos=0;
 printf("\n\nPage       Page Status          Page Fault      Frame Status\n");
 printf("-------------------------------------------------------------------------------");
 for(i=0;i<count;i++)
 {
  ind = 0;
  printf("\n %d\t",ref[i]);
  for(j=0;j<n;j++)
  {
   if(frame[j] == -1)
   {
    ind = 1;
    pos = j;
    break;
   }
   if(frame[j] == ref[i])
   {
   printf("Already Inserted\t   No\t\t");
   ind = 2;
   break;
   }
  }
  if(ind != 2)
  {
   printf("Inserting in Frame\t   Yes\t\t");
   if(ind == 0)
    pos = find_pos(i);
   frame[pos] = ref[i];
   fault ++;
  }
  for(j=0;j<n;j++)
  {
   if(frame[j] == -1)
    printf(" -");
   else
    printf(" %d",frame[j]);
  }
 }
 printf("\n\n\n\n\tEnd to inserting the reference string.");
 printf("\n\n\tTotal page fault is %d.",fault);
}


void main()
{
input();
OPT();
getch();
}

No comments:

Post a Comment