0% found this document useful (0 votes)
72 views8 pages

Page Replacement

The document discusses three page replacement algorithms: FIFO, LRU, and Optimal. FIFO replaces the oldest page, LRU replaces the least recently used page, and Optimal replaces the page that will not be used for the longest time in the future. Code examples are provided for each algorithm to track the page frames and calculate the number of page faults.

Uploaded by

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

Page Replacement

The document discusses three page replacement algorithms: FIFO, LRU, and Optimal. FIFO replaces the oldest page, LRU replaces the least recently used page, and Optimal replaces the page that will not be used for the longest time in the future. Code examples are provided for each algorithm to track the page frames and calculate the number of page faults.

Uploaded by

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

EX:2 PAGE REPLACEMENT ALGORITHM

20.03.21
1.FIFO:

#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)

T.PRABAVATHI(20MA32)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}

2. LRU:

#include<stdio.h>

main()

int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];

printf("Enter no of pages:");

scanf("%d",&n);

printf("Enter the page number:");

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

scanf("%d",&p[i]);

printf("Enter no of frames:");

scanf("%d",&f);

T.PRABAVATHI(20MA32)
q[k]=p[k];

printf("\n\t%d\n",q[k]);

c++;

k++;

for(i=1;i<n;i++)

c1=0;

for(j=0;j<f;j++)

if(p[i]!=q[j])

c1++;

if(c1==f)

c++;

if(k<f)

q[k]=p[i];

k++;

for(j=0;j<k;j++)

printf("\t%d",q[j]);

printf("\n");

else

for(r=0;r<f;r++)

c2[r]=0;

for(j=i-1;j<n;j--)

if(q[r]!=p[j])

T.PRABAVATHI(20MA32)
c2[r]++;

else

break;

for(r=0;r<f;r++)

b[r]=c2[r];

for(r=0;r<f;r++)

for(j=r;j<f;j++)

if(b[r]<b[j])

t=b[r];

b[r]=b[j];

b[j]=t;

for(r=0;r<f;r++)

if(c2[r]==b[0])

q[r]=p[i];

printf("\t%d",q[r]);

printf("\n");

printf("\nThe no of page faults is %d",c);

T.PRABAVATHI(20MA32)
3. OPTIMAL:

#include<stdio.h>

int main()

int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2, flag3, i, j, k, pos,
max, faults = 0;

printf("Enter number of pages: ");

scanf("%d", &no_of_pages);

printf("Enter the page number : ");

for(i = 0; i < no_of_pages; ++i){

scanf("%d", &pages[i]);

printf("Enter number of frames: ");

scanf("%d", &no_of_frames);

T.PRABAVATHI(20MA32)
for(i = 0; i < no_of_frames; ++i){

frames[i] = -1;

for(i = 0; i < no_of_pages; ++i){

flag1 = flag2 = 0;

for(j = 0; j < no_of_frames; ++j){

if(frames[j] == pages[i]){

flag1 = flag2 = 1;

break;

if(flag1 == 0){

for(j = 0; j < no_of_frames; ++j){

if(frames[j] == -1){

faults++;

frames[j] = pages[i];

flag2 = 1;

break;

if(flag2 == 0){

flag3 =0;

for(j = 0; j < no_of_frames; ++j){

temp[j] = -1;

T.PRABAVATHI(20MA32)
for(k = i + 1; k < no_of_pages; ++k){

if(frames[j] == pages[k]){

temp[j] = k;

break;

for(j = 0; j < no_of_frames; ++j){

if(temp[j] == -1){

pos = j;

flag3 = 1;

break;

if(flag3 ==0){

max = temp[0];

pos = 0;

for(j = 1; j < no_of_frames; ++j){

if(temp[j] > max){

max = temp[j];

pos = j;

frames[pos] = pages[i];

faults++;

T.PRABAVATHI(20MA32)
}

printf("\n");

for(j = 0; j < no_of_frames; ++j){

printf("%d\t", frames[j]);

printf("\n\nTotal Page Faults = %d", faults);

return 0;

T.PRABAVATHI(20MA32)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy