LRU Program
#include<stdio.h>
struct page
{
int pno;
int at;
}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 main()
{
int prs[30],prss,nf,i,j,pno,mm[10]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
int counter=0,min_t=999,min_p,pf=0;
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]);
}
//LRU algo.
for(i=0;i<prss;i++)
{
counter++;//logical clock ticks for every page ref.
pno=prs[i];
pages[pno].at=counter;//storing time of page ref.
min_t=999;
if(search(prs[i],mm,nf)==0)
{
for(j=0;j<nf;j++)
{
pno=mm[j];
if(pages[pno].at<min_t)
{
min_t=pages[pno].at;
min_p=j;
}
}
mm[min_p]=prs[i];
printf("--Page Fault--");
pf++;
}
print(mm,nf);
}
printf("\nNo. of Page Faults:%d",pf);
}
struct page
{
int pno;
int at;
}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 main()
{
int prs[30],prss,nf,i,j,pno,mm[10]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
int counter=0,min_t=999,min_p,pf=0;
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]);
}
//LRU algo.
for(i=0;i<prss;i++)
{
counter++;//logical clock ticks for every page ref.
pno=prs[i];
pages[pno].at=counter;//storing time of page ref.
min_t=999;
if(search(prs[i],mm,nf)==0)
{
for(j=0;j<nf;j++)
{
pno=mm[j];
if(pages[pno].at<min_t)
{
min_t=pages[pno].at;
min_p=j;
}
}
mm[min_p]=prs[i];
printf("--Page Fault--");
pf++;
}
print(mm,nf);
}
printf("\nNo. of Page Faults:%d",pf);
}
OutPut:-
Enter MM size(no. of frames):3
enter page ref. string size:20
enter page ref string:7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
--Page Fault-- 7 -1 -1
--Page Fault-- 7 0 -1
--Page Fault-- 7 0 1
--Page Fault-- 2 0 1
2 0 1
--Page Fault-- 2 0 3
2 0 3
--Page Fault-- 4 0 3
--Page Fault-- 4 0 2
--Page Fault-- 4 3 2
--Page Fault-- 0 3 2
0 3 2
0 3 2
--Page Fault-- 1 3 2
1 3 2
--Page Fault-- 1 0 2
1 0 2
--Page Fault-- 1 0 7
1 0 7
1 0 7
No. of Page Faults:12
enter page ref. string size:20
enter page ref string:7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
--Page Fault-- 7 -1 -1
--Page Fault-- 7 0 -1
--Page Fault-- 7 0 1
--Page Fault-- 2 0 1
2 0 1
--Page Fault-- 2 0 3
2 0 3
--Page Fault-- 4 0 3
--Page Fault-- 4 0 2
--Page Fault-- 4 3 2
--Page Fault-- 0 3 2
0 3 2
0 3 2
--Page Fault-- 1 3 2
1 3 2
--Page Fault-- 1 0 2
1 0 2
--Page Fault-- 1 0 7
1 0 7
1 0 7
No. of Page Faults:12
No comments:
Post a Comment