0% found this document useful (0 votes)
11 views6 pages

PageReplacementAlgorithms EXP

The document contains two algorithms for page replacement in operating systems: FIFO (First-In-First-Out) and LRU (Least Recently Used). Each algorithm is implemented in C, where FIFO replaces the oldest page in memory when a page fault occurs, while LRU replaces the least recently used page. The output demonstrates the number of page faults for a given reference string and number of frames.
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)
11 views6 pages

PageReplacementAlgorithms EXP

The document contains two algorithms for page replacement in operating systems: FIFO (First-In-First-Out) and LRU (Least Recently Used). Each algorithm is implemented in C, where FIFO replaces the oldest page in memory when a page fault occurs, while LRU replaces the least recently used page. The output demonstrates the number of page faults for a given reference string and number of frames.
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/ 6

FIFO ALGORITHM:

#include <stdio.h>

int main()

int referenceString[10], pageFaults = 0, m, n, s, pages, frames;

printf("\nEnter the number of Pages:\t");

scanf("%d", &pages);

printf("\nEnter reference string values:\n");

for( m = 0; m < pages; m++)

printf("Value No. [%d]:\t", m + 1);

scanf("%d", &referenceString[m]);

printf("\n What are the total number of frames:\t");

scanf("%d", &frames);

int temp[frames];

for(m = 0; m < frames; m++)

temp[m] = -1;

for(m = 0; m < pages; m++)

s = 0;

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

if(referenceString[m] == temp[n])

s++;

pageFaults--;
}

pageFaults++;

if((pageFaults <= frames) && (s == 0))

temp[m] = referenceString[m];

else if(s == 0)

temp[(pageFaults - 1) % frames] = referenceString[m];

printf("\n");

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

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

printf("\nTotal Page Faults:\t%d\n", pageFaults);

return 0;

OUTPUT:
LRU PROGRAM:

#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 reference string:");

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

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

printf("Enter no of frames:");

scanf("%d",&f);

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

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

OUTPUT:

Enter no of pages:10

Enter the reference string:7 5 9 4 3 7 9 6 2 1

Enter no of frames:3

7 5

7 5 9

4 5 9

4 3 9

4 3 7

9 3 7

9 6 7

9 6 2

1 6 2

The no of page faults 10

You might also like