Page Replacement Algorithms in Operating Systems (OS)
1) Write the simulation program for demand paging and show the page scheduling and total number of page faults according the FIFO page replacement algorithm. Assume the memory of n frames.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int *refstring;
int *frame;
int no_of_ref;
int no_of_frame;
int pg_fault_cnt;
void Accept()
{
int i;
printf(“enter no of references:”);
scanf(“%d”,&no_of_ref);
refstring=(int*)malloc(sizeof(int)*no_of_ref);
for(i=0;i<no_of_ref;i++)
{
printf(“\nenter ref:%d:”,i+1);
scanf(“%d”,&refstring[i]);
}
printf(“\nenter no of frames:”);
scanf(“%d”,&no_of_frame);
frame=(int*)malloc(sizeof(int)*no_of_frame);
for(i=0;i<no_of_frame;i++)
{
frame[i]=-1;
}
}
int search(int val)
{
int i;
for(i=0;i<no_of_frame;i++)
{
if(val==frame[i])
return 0;
}
return -1;
}
int GetLRU(int posi)
{
int pos_k;
int pos=99;
int i,j;
for(i=0;i<no_of_frame;i++)
{
for(j=posi-1;j>=0;j–)
{
if(frame[i]==refstring[j])
{
if(j<pos)
{
pos=j;
pos_k=i;
}
break;
}
}
}
return pos_k;
}
void Lru()
{
int i;
int k=0;
int cnt;
for(i=0;k<no_of_frame&&i<no_of_ref;i++)
{
if(search(refstring[i])==-1)
{
pg_fault_cnt++;
frame[k]=refstring[i];
k++;
}
printf(“\n[%d]”,refstring[i]);
for(cnt=0;cnt<no_of_frame;cnt++)
{
if(frame[cnt]!=-1)
printf(“%5d”,frame[cnt]);
else
printf(“”);
}
}
while(i<no_of_ref)
{
if(search(refstring[i])==-1)
{
pg_fault_cnt++;
k=GetLRU(i);
frame[k]=refstring[i];
}
printf(“\n[%d]”,refstring[i]);
for(cnt=0;cnt<no_of_frame;cnt++)
{
if(frame[cnt]!=-1)
printf(“%5d”,frame[cnt]);
else
printf(“”);
}
i++;
}
}
void Display()
{
printf(“\npage fault count:%d\n”,pg_fault_cnt);
}
int main()
{
Accept();
Lru();
Display();
return 0;
}
2) Write the simulation program to implement demand paging and show the page scheduling and total number of page faults according to the LRU (using counter method) page replacement algorithm. Assume the memory of n frames.
#include<stdio.h>
#include<conio.h>
int p[20],nnn=0,no,d,dn,di,i,nv,j,n[4],nn,f=0,k,rn,cnt=0,c[4],q,q1,v=0;
int s,si,sri,sj,sk=0,st[4],s1,temp,serpos;
int val=1,ta[2],ti,tt,tj,tn[3],tf;
int s1i,s1j,cnt1=0;
int search(int);
int ser(int);
int sort(int);
void insert();
void display();
void dis();
void main()
{
clrscr();
insert();
clrscr();
for(j=0;j<nn;j++)
{
n[j]=p[j];
}
display();
k=0;
for(i=nn;i<no;i++)
{
nv=p[i];
rn=search(nv);
if(rn==1)
{
cnt1++;
printf(“%d :”,p[nnn]);
nnn++;
printf(“\n”);
}
else
{
k=sort(i);
n[k]=nv;val=0;
printf(“%d :”,p[nnn]);
nnn++;
dis();
printf(“\n”);
}
}
printf(“\nEnter number of page fault=%d”,(no-cnt1));
getch();
}
void insert()
{
printf(“\nEnter number of pages for Reference string=”);
scanf(“%d”,&no);
for(i=0;i<no;i++)
{
printf(“\nEnter number of frame for %d= “,i+1);
scanf(“%d”,&p[i]);
}
printf(“\nEnter the length of array=”);
scanf(“%d”,&nn);
}
int search(int x)
{
f=0;
for(j=0;j<nn;j++)
{
if(x==n[j])
f=1;
}
printf(“\n”);
return(f);
}
void display()
{
for(di=0;di<nn;di++)
{ printf(“\n”);
printf(“%d :”,p[nnn]);
nnn++;
for(dn=0;dn<=di;dn++)
{
printf(“%d “,n[dn]);
}
printf(“\n”);
}
}
void dis()
{
for(d=0;d<nn;d++)
{
printf(“%d “,n[d]);
}
}
int sort(int z)
{
if(val==1)
return 0;
else
{
ta[0]=p[z-1];
ta[1]=p[z-2];
for(ti=0;ti<2;ti++)
{
for(tj=0;tj<3;tj++)
{
if(ta[ti]==n[tj])
{
tn[ti]=tj;
}
}
}
for(ti=0;ti<3;ti++)
{
tt=ser(ti);
if(tt==0)
{
return ti;
}
}
}
}
int ser(int sn)
{ tf=0;
for(tj=0;tj<2;tj++)
{
if(sn==tn[tj])
{
tf=1;
}
}
return tf;
}
3 )Write the simulation program for demand paging and show the page scheduling and total number of page faults according the MFU page replacement algorithm. Assume the memory of n frames.
#include<stdio.h>
#include<conio.h>
int p[20],nnn=0,no,d,dn,di,i,nv,j,n[4],nn,f=0,k,cnt=0,c[4],q,q1,v;
int s,si,sri,sj,sk=0,st[4],s1,temp,serpos;
int s1i,s1j,rn,cnt1=0;
int search(int);
int sort(int);
void sort1();
void insert();
void display();
void dis();
void main()
{
clrscr();
insert();
clrscr();
for(j=0;j<nn;j++)
{
n[j]=p[j];
}
display();
k=0;
for(i=nn;i<no;i++)
{
nv=p[i];
rn=search(nv);
if(rn==1)
{
cnt1++;
printf(“%d :”,p[nnn]);
nnn++;
printf(“\n”);
}
else
{
k=sort(i);
printf(“k=%d “,k);
n[k]=nv;
printf(“%d :”,p[nnn]);
nnn++;
dis();
printf(“\n”);
}
}
printf(“\nNumber of pages fault=%d”,(no-cnt1));
getch();
}
void insert()
{
printf(“\nEnter number of pages for reference string=”);
scanf(“%d”,&no);
for(i=0;i<no;i++)
{
printf(“\nEnter the frame for %d= “,i+1);
scanf(“%d”,&p[i]);
}
printf(“\nEnter the length of array=”);
scanf(“%d”,&nn);
}
int search(int x)
{
f=0;
for(j=0;j<nn;j++)
{
if(x==n[j])
f=1;
}
printf(“\n”);
return(f);
}
void display()
{
for(di=0;di<nn;di++)
{ printf(“\n”);
printf(“%d :”,p[nnn]);
nnn++;
for(dn=0;dn<=di;dn++)
{
printf(“%d “,n[dn]);
}
printf(“\n”);
}
}
void dis()
{
for(d=0;d<nn;d++)
{
printf(“%d “,n[d]);
}
}
int sort(int z)
{
v=0;
for(q1=0;q1<3;q1++)
{ cnt=0;
for(q=0;q<z;q++)
{
if(n[q1]==p[q])
{
cnt++;
}
}
st[v]=cnt;
v++;
}
for(si=0;si<3;si++)
{ c[si]=n[si];
}
sort1();
if(st[0]==st[1])
{ rn=0;
if(sk==2)
{ rn=sk;sk=0; }
else{
rn=sk;
sk++; }
return(rn);
}
else
{
for(sri=0;sri<3;sri++)
{
if(n[sri]==c[0])
{
serpos=sri;
}
}
return(serpos);
}
}
void sort1()
{
for(s1i=0;s1i<3;s1i++)
{
for(s1j=s1i;s1j<3;s1j++)
{
if(st[s1i]>st[s1j])
{
temp=st[s1j];
st[s1j]=st[s1i];
st[s1i]=temp;
temp=c[s1j];
c[s1j]=c[s1i];
c[s1i]=temp;
}
}
}
}
-
Write the simulation program for demand paging and show the pagescheduling and total number of page faults according the optimal page replacement algorithm. Assume the memory of n frames.
-
#include<stdio.h>
void main()
{
int i,j,k,l,m,n,p,c=0,s;
int a[20],b[20],q,max;
printf(“enter no. of reference string: “);
scanf(“%d”,&n);
printf(“enter size of frame: “);
scanf(“%d”,&m);
printf(“enter the elements of ref. string: \n”);
for(i=0; i<n; i++)
scanf(“%d”,&a[i]);
for(j=0; j<m; j++)
b[j]=-1; //initialize all frame elements with -1
for(i=0; i<n; i++)
{
for(k=0; k<m; k++)
if(b[k]==a[i])
goto here;
for(j=0; j<m; j++)
{
if(b[j]==-1)//check if element already present in frame,if true then no page fault.
{
b[j]=a[i];
c++;
goto here;
}
}
if(j==m)
{
l=i+1,max=0;
for(j=0; j<m; j++)
{
for(s=l; s<n; s++)
{
if(a[s]==b[j])
{
if(s>max)
{
max=s;
p=j;
}
break;
}
}
if(s==n)
{
max=s;
p=j;
}
}
}
b[p]=a[i];
c++;
here:
printf(“\n\n”);
for(k=0; k<m; k++)
printf(” %d”,b[k]);
}
printf(“\n No of page fault is:%d”,c);}