Week 7 (A, B, C)
Week 7 (A, B, C)
DESCRIPTION:
Page replacement algorithms are an important part of virtual memory management and it helps the OS to
decide which memory page can be moved out making space for the currently needed page. However, the
ultimate objective of all page replacement algorithms is to reduce the number of page faults.
FIFO-This is the simplest page replacement algorithm. In this algorithm, the operating system keeps track
of all pages in the memory in a queue, the oldest page is in the front of the queue. When a page needs to be
replaced page in the front of the queue selected for removal.
OPTIMAL- In this algorithm, pages are replaced which would not be used for the longest duration of time
in the future. This algorithm will give us less page fault when compared to other page replacement
algorithms.
ALGORITHM:
1. Start the process
2. Read number of pages n
3. Read number of pages no
4. Read page numbers into an array a[i]
5. Initialize avail[li]=0 .to check page hit
6. Replace the page with circular queue, while re-placing check page availability in the frame
Place avail[i]=1 if page is placed in the frame Count page faults
7. Print the results.
int fr[3];
void main()
void display():
flag1=0,flag2–0,pf-0,frsize=3,top=0;
for(i=0;i<3;i++)
fr[il=-1;
for(j-0;j<12j++)
if(fr[i]==page(jl)
if(flagl ==0)
for(i=0;i<frsize;i++)
if(fr[i]==-1)
frlil=page[il: flag2=1;break;
if(flag2=-0)
fr[topl=pageljl:
top++;
pf++;
if(top>=frsize)
top=0;
display):
}
printf("Number of page faults : %d ".pf+frsize);
void display()
int i: printf("\n"):
for(i=0;i<3;i++)
printf("%dt",fr[i);
OUTPUT:
2 -1 -1
23-1
2 3-1
23 1
531
52 1
5 24
5 24
3 24
32 4
3 54
3 52
ALGORITHM:
SOURCE CODE :
#include<stdio.h>
int fr[3]:
void main()
void display();
for(i=0;i<3;i++)
fr[i]=-1;
for(j-0:j<12;j++)
flagl=0,flag2-0;
for(i=0;i<3;i++)
if(fr[i]=p[jl)
flag1=1;
flag2=1;break;
if(flagl=-0)
for(i=0;i<3;i++)
if(fr[i]=-1)
fr[il=plil: flag2=1;
break;
if(flag2==0)
for(i=0;i<3;i++)
fs[i]=0;
for(k=j- 1,El:k=frsize-1;1++,k--)
for(i=0;i<3;i++)
if(fr[i]==p[k]) fs[i]=1;
for(i=0;i<3;i++)
if(fs[i]=-0)
index=i;
frlindex]=pljl;
pft+;
display):
void display()
int i; printf("\n");
for(i=0;i<3;i++)
printf("\t% d",fr[i):
OUTPUT:
2 -1 -1
2 3-1
2 3-1
23 1
251
25 1
25 4
25 4
35 4
3 5 2
3 5 2
3 5 2
No of page faults: 7
C) OPTIMAL
ALGORTHIM:
1. Start Program
2. Read Number Of Pages And Frames
3.Read Each Page Value
4. Search For Page In The Frames
5.If NotAvailable Allocate Free Frame
6. If No Frames Is Free Repalce The Page With The Page That Is Leastly Used
7.Print Page Number Of Page Faults
8.Stop process.
SOURCE CODE:
#include<stdio.h>
int fr[3], n, m;
void
display();
void main()
int ij,page[20],fs[10]:
int
float pr;
scanf("%d",&n);
printf("Enter the reference string: ");
for(i=0;i<n;i++)
scanf("%d",&page[i):
printf("Enter no of frames: "):
scanf("%d",&m);
for(i=0;i<m;it+)
fr[i]=-1; pf-m;
for(j-0j<nj++)
if(fr[il==page[jl)
if(flagl=-0)
for(i=0;i<m;it+)
if(fr[il=z-l)
fr[il=pageljl: flag2=l;
break;
if(flag2=-0)
for(i=0;i<m;i++)
Ig[i]=0;
for(i=0;i<m;i++)
for(k=j+1:k<=n:k++)
if(fr[i]==page[k)
lglil=k-j:
break;
found=0;
for(i=0;i<m;it+)
if(lg[il==0)
index=i;
found = 1;
break;
if(found==0)
max=lg[0);: index=0;
for(i-0;i<m;it+)
if(max<lglil)
{
max=lg[i];
index=i;
fr[index]=page[jl:
pft+:
display();:
pr-(float)pf/n* 100;
void display()
int i; for(i-0;i<m;i++)
printf("% d\t",fr[i]):
printf("n");
}
OUTPUT:
Enter length of the reference string: 12
Enter the reference string: 1 2 34 125 2345
1
Enter no of frames: 3
1-1 -1
12-1
123
124
124
124
125
125
125
325
425
425
Number of page faults: 7 Page fault rate =58.333332