Question

Simulator of Page Replace Management Visualizing page replacement algorithms includes (1) Least Recently Used (LRU) (2)...

Simulator of Page Replace Management

Visualizing page replacement algorithms includes (1) Least Recently Used (LRU) (2) Optimal (OPT) (3) First in and First out (FIFO) (4) Clocks (2) Second Chance. Development Language: c .

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.LRU

#include<stdio.h>
int RS[30],n,nf;
struct frame
{
   int value;
   int pos;
} Frame[10];
main()
{
   int i,j,k=0,check=0,pf=0,x,y,q,max=321;
   printf("Enter the frame size:");
   scanf("%d",&nf);
   printf("Enter the size of reference string:");
   scanf("%d",&n);
   printf("Enter the reference string:\n");
   for(i=0;i<n;i++)
       scanf("%d",&RS[i]);
   for(i=0;i<nf;i++)
       Frame[i].value=-1;
   for(i=0;i<n;i++)
   {
       for(j=0;j<nf;j++)
       {
           if(RS[i]==Frame[j].value)
               check=1;
       }
       if(check==0)
       {
           pf++;
           if(k<nf)
           {
               Frame[k].value=RS[i];
               k++;
           }
           else
           {
               for(x=0;x<nf;x++)
               {
                   for(y=i-1;y>=0;y--)
                   {
                       if(Frame[x].value==RS[y])
                       {
                           Frame[x].pos=y;
                           break;
                       }
                   }
               }
               for(q=0;q<nf;q++)
               {
                   if(Frame[q].pos<max)
                       max=Frame[q].pos;
               }
               for(q=0;q<nf;q++)
               {
                   if(Frame[q].value==RS[max])
                       Frame[q].value=RS[i];
               }
                               max=321;
           }
       }
       check=0;
   }
   printf("\nNumber of page faults:%d",pf);
}

2.Optimal:

#include<stdio.h>
//int page[30],n,nf;
struct frame
{
   int value;
   int pos;
} Frame[10];
main()
{
   int i,j,k=0,n,nf,check=0,pf=0,x,y,q,max=-9999,val;
   printf("Enter the frame size:");
   scanf("%d",&nf);
   printf("Enter the size of reference string:");
   scanf("%d",&n);
   int page[n];
   printf("Enter the reference string:\n");
   for(i=0;i<n;i++)
       scanf("%d",&page[i]);
   for(i=0;i<nf;i++)
       Frame[i].value=-1;
   for(i=0;i<n;i++)
   {
       check=0;
       for(j=0;j<nf;j++)
       {
           if(page[i]==Frame[j].value)
               check=1;
       }
       if(check==0)
       {
           pf++;
           if(k<nf)
           {
               Frame[k].value=page[i];
               k++;
           }
           else
           {
               for(x=0;x<nf;x++)
               {
                   for(y=i+1;y<n;y++)
                   {
                       if(Frame[x].value==page[y]) //It will checkes the last position of page while increment the j value
                       {
                           Frame[x].pos=y; // this is for position of the page
                           break;
                       }
                   }
                   if(Frame[x].pos<i) // if the page is not found in future then the page is replaced in that frame in below code
                       Frame[x].pos=9999;
               }
               for(x=0;x<nf;x++)
               {
                   if(Frame[x].pos>max) //this is for to check max position of each frame
                   {
                       max=Frame[x].pos;
                       val=Frame[x].value;
                       Frame[x].pos=i+1; // this is used to check the remaining frame position which is having less posi than max
                   }
               }
               for(x=0;x<nf;x++) // in this case the remaining pages are replaced which are not repeated
               {
                   if(Frame[x].value==val) //this is used to check for max posi and current frames are same are not
                       Frame[x].value=page[i];
               }
           }
       }
   }
   printf("\nNumber of page faults:%d",pf);
}

3.FIFO:

#include<stdio.h>
main()
{
   int m,n,i,j,k,f=0,old=0;
   printf("Enter no of frames:");
   scanf("%d",&m);
   printf("Enter no of pages:");
   scanf("%d",&n);
   int frames[m],pages[n];
   for(i=0;i<m;i++)
   {
       frames[i]=-1;
   }
   for(i=0;i<n;i++)
   {
       scanf("%d",&pages[i]);
   }
   for(i=0;i<n;i++)
   {
       k=0;
       for(j=0;j<m;j++)
       {
           if(pages[i]==frames[j])
           {
               k=1;
               break;
           }
       }
   if(k==0)
   {
       frames[old]   =pages[i];
       old++;
       f++;
       old=old%m;
   }
   printf("\n");
   for(j=0;j<m;j++)
   {
       printf("%d\t",frames[j]);
   }
   printf("\t\t\t\t%d",f);
   }

}

4.clock:

#include<stdio.h>
int main()
{
int n,p[100],f[10],ava,hit=0,usebit[10],i,j;
printf("enter the length of the Reference string: ");
scanf("%d",&n);
printf("enter the reference string: \n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
for(i=0;i<n;i++)
{
ava=0;
// found
for(j=0;j<3;j++)
{
if(p[i]==f[j])
{
ava=1;
hit++;
usebit[j]=1;
break;
}
}
//search for usebit 0
if(ava==0)
{
for(j=0;j<3;j++)
{
if(usebit[j]==0)
{
f[j]=p[i];
usebit[j]=1;
ava=1;
break;
}
}
}
// fifo
if(ava==0)
{
for(j=0;j<3;j++)
usebit[j]=0;
}
f[0]=p[i];
usebit[0]=1;
}
printf("The number of Hits: %d",hit);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Simulator of Page Replace Management Visualizing page replacement algorithms includes (1) Least Recently Used (LRU) (2)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT