0% found this document useful (0 votes)
12 views10 pages

Week 7 (A, B, C)

PROJECT_REPORT

Uploaded by

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

Week 7 (A, B, C)

PROJECT_REPORT

Uploaded by

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

PAGE REPLACEMENT ALGORITHMS

AIM:To implement FIFO page replacement technique.


a) FIFO b) LRU c) OPTIMAL

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.

LRU-In this algorithm page willbe replaced which is least recentlyused

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.

8. Stop the process.


A) FIRST IN FIRST OUT
SOURCE CODE :
#include<stdio.h>

int fr[3];

void main()

void display():

int i,j,page[12]={2,3,2, 1,5,2,4,5,3,2,5,2 );


int

flag1=0,flag2–0,pf-0,frsize=3,top=0;

for(i=0;i<3;i++)

fr[il=-1;

for(j-0;j<12j++)

flag1=0; flag2-0; for(i=0;i<12;i++)

if(fr[i]==page(jl)

flagl=1; flag2=1; break;


)

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

Number of page faults: 9


B) LEAST RECENTLY USED

AIM: To implement LRU page replacement technique.

ALGORITHM:

1. Start the process


2. Declare the size

3. Get the number of pages to be inserted

4. Get the value


5. Declare counter and stack

6. Select the least recently used page by counter value


7. Stack them according the selection.

& Display the values


9. Stop the process

SOURCE CODE :

#include<stdio.h>

int fr[3]:

void main()

void display();

int p[12]={2,3,2, 1,5,2,4,5,3,2,5,2) ,i.j, fs[3]:

int index.k.l.flag 1=0,flag2=0,pf-0,frsize=3;

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):

printf("|n no of page faults :%d",pf+frsize);

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

AIM: To implement optimalpage replacement technique.

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:

Program to simulate optimal page replacement */

#include<stdio.h>

int fr[3], n, m;
void

display();
void main()

int ij,page[20],fs[10]:

int

max,found-0,lg[3], index,k,1, flag1=0,flag2-0,pf-0;

float pr;

printf("Enter length of the reference string: ");

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++)

flag 1=0; flag2=0;


for(i=-0;i<m;i++)

if(fr[il==page[jl)

flag 1=1; flag2=1;


break;

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();:

printf("Number of page faults: % d\n", pf):

pr-(float)pf/n* 100;

printf("Page fault rate = %f\n", pr);

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

You might also like