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

PROGRAM Lru

The document outlines a program for implementing the Least Recently Used (LRU) page replacement algorithm. It describes the input requirements, initialization process, page replacement loop, and how to handle page faults, along with providing sample outputs for different page reference sequences. The program is written in C and demonstrates how to track page usage and replace pages in a frame based on their usage count.

Uploaded by

divvigadu
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)
3 views6 pages

PROGRAM Lru

The document outlines a program for implementing the Least Recently Used (LRU) page replacement algorithm. It describes the input requirements, initialization process, page replacement loop, and how to handle page faults, along with providing sample outputs for different page reference sequences. The program is written in C and demonstrates how to track page usage and replace pages in a frame based on their usage count.

Uploaded by

divvigadu
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/ 6

PROGRAM: 6

PAGE REPLACEMENT ALGORITHM

Design, develop and implement page replacement using LRU algorithms. Assume
suitable input required to demonstrate the results.

Algorithm :

Input:

Read the total number of pages (n).

Read the page reference sequence (pg array) of length n.

Read the frame size (f).

Initialization:

Initialize arrays count and fr to keep track of page usage count and frames, respectively.

Initialize variables i, j, fault, flag, temp, current, dist, max, m, cnt, p, x.

Set all elements of count and fr to -1.

Page Replacement Loop:

Iterate over the page reference sequence (pg array) from index i = 0 to n-1.

Set flag to 0.

Page Presence Check:

For each page reference (temp), check if it is already present in the frames (fr).

If found, set flag to 1 and update the usage count (count) for that frame to the current position
(i).

Handling Page Faults:

If the page is not found in the frames (flag is 0), check if there is space in the frames.

If space is available (fault < f), add the page to an empty frame and set the usage count to the
current position (i).
If frames are full, determine the least recently used page to replace.

Find the page with the smallest usage count (count) representing the least recently used page.

Replace the least recently used page with the current page and update the usage count.

Printing Frames:

After each page reference, print the contents of the frames (fr array).

Output:

After processing all page references, print the total number of page faults (fault).

Program :

#include <stdio.h>

int n, pg[30], fr[10];

void lru();

int main()

int i;

printf("Enter total no of pages: \n");

scanf("%d", &n);

printf("Enter sequence:\n");

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

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

lru(); // Call the lru function

return 0; // Return 0 to indicate successful completion


}

void lru()

int count[10], i, j, fault = 0, flag, f, temp, max, m, x;

printf("Enter the frame size: \n");

scanf("%d", &f);

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

count[i] = 0;

fr[i] = -1;

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

flag = 0;

temp = pg[i];

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

if (temp == fr[j])

flag = 1;

count[j] = i; // Update the count to the current position

break;

if (flag == 0)
{

if (fault < f)

fr[fault] = temp;

count[fault] = i; // Set the count to the current position

fault++;

else

max = 0;

for (m = 1; m < f; m++)

if (count[m] < count[max])

max = m;

fr[max] = temp;

count[max] = i; // Update the count to the current position

fault++;

printf("\n");

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

printf("%d\t", fr[x]);
}

printf("\nTotal no of fault size=%d\n", fault);

Output 1:

Enter total no of pages:

Enter sequence:

1234567

Enter the frame size:

1 -1 -1

1 2 -1

1 2 3

4 2 3

4 5 3

4 5 6

7 5 6

Total no of fault size=7

Output 2:

Enter total no of pages:

Enter sequence:

12123
Enter the frame size:

1 -1 -1

1 2 -1

1 2 -1

1 2 -1

1 2 3

Total no of fault size=3

You might also like