0% found this document useful (0 votes)
8 views2 pages

6lru Ubuntu

The document describes a C program that implements the Least Recently Used (LRU) page replacement algorithm. The program takes in a page reference string, number of frames, and outputs the frame contents at each step and the number of page faults. It uses an array to track the frames and checks if the requested page is already in the frames before determining if a replacement is needed.

Uploaded by

sagar patole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

6lru Ubuntu

The document describes a C program that implements the Least Recently Used (LRU) page replacement algorithm. The program takes in a page reference string, number of frames, and outputs the frame contents at each step and the number of page faults. It uses an array to track the frames and checks if the requested page is already in the frames before determining if a replacement is needed.

Uploaded by

sagar patole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Expt no. 6.

3 C program for LRU page replacement :

#include<stdio.h>
void print(int frameno,int frame[])
{
int j;
for(j=0;j<frameno;j++)
printf("%d\t",frame[j]);
printf("\n");
}
int main()
{
int i,j,k,n,page[50],frameno,frame[10],move=0,flag,count=0,count1,repindex,
check[50]={0};
float rate;
printf("Enter the number of pages\n");
scanf("%d",&n);
printf("Enter the page reference numbers\n");
for(i=0;i<n;i++)
scanf("%d",&page[i]);
printf("Enter the number of frames\n");
scanf("%d",&frameno);
for(i=0;i<frameno;i++)
frame[i]=-1;
printf("Page reference string\tFrames\n");
for(i=0;i<n;i++)
{
printf("%d\t\t\t",page[i]);
flag=0;
for(j=0;j<frameno;j++)
{
if(page[i]==frame[j])
{
flag=1;
printf("No replacement\n");
break;
}
}
if(flag==0&&count<frameno)
{
frame[move]=page[i];
move=(move+1)%frameno;
count++;
print(frameno,frame);
}
else if(flag==0)
{
count1=0;
for(j=i-1;j>=0;j--)
{
for(k=0;k<frameno;k++)
{
if(page[j]==frame[k]&&check[page[j]]==0)
{
check[page[j]]=1;
count1++;
repindex=k;
k=frameno;
}
}
if(count1==frameno)
break;
}
frame[repindex]=page[i];
count++;
print(frameno,frame);
}
for(j=0;j<50;j++)
check[j]=0;

}
rate=(float)count/(float)n;
printf("Number of page faults is %d\n",count);
printf("Fault rate is %f\n",rate);
return 0;
}
OUTPUT :

You might also like