LFU Program
#include<stdio.h>
struct page
{
int pno;
int counter;
}pages[10];
void print(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf(" %d ",a[i]);
}
printf("\n");
}
int search(int e,int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
if(e==a[i])
{
return 1;
}
}
return 0;
}
void initialize()
{
int i=0;
for(i=0;i<10;i++)
{
pages[i].counter=0;
}
}
void main()
{
int prs[30],prss,nf,i,j,pno,mm[10]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
int min_t=999,min_p,pf=0;
clrscr();
printf("Enter MM size(no. of frames):");
scanf("%d",&nf);
printf("enter page ref. string size:");
scanf("%d",&prss);
printf("enter page ref string:");
for(i=0;i<prss;i++)
{
scanf("%d",&prs[i]);
}
initialize();//making all page counter=0
//LFU algo.
for(i=0;i<prss;i++)
{
pno=prs[i];
pages[pno].counter++;//increment counter for referenced page
min_t=999;
if(search(prs[i],mm,nf)==0)//when referred page is not in mm
{
for(j=0;j<nf;j++)//to get least counter page in MM
{
pno=mm[j];
if(pages[pno].counter<min_t)
{
min_t=pages[pno].counter;
min_p=j;
}
}
mm[min_p]=prs[i];
printf("--Page Fault--");
pf++;
}
print(mm,nf);
}
printf("\nNo. of Page Faults:%d",pf);
getch();
}
Output: