PROGRAM Lru
PROGRAM Lru
Design, develop and implement page replacement using LRU algorithms. Assume
suitable input required to demonstrate the results.
Algorithm :
Input:
Initialization:
Initialize arrays count and fr to keep track of page usage count and frames, respectively.
Iterate over the page reference sequence (pg array) from index i = 0 to n-1.
Set flag to 0.
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).
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>
void lru();
int main()
int i;
scanf("%d", &n);
printf("Enter sequence:\n");
scanf("%d", &pg[i]);
void lru()
scanf("%d", &f);
count[i] = 0;
fr[i] = -1;
flag = 0;
temp = pg[i];
if (temp == fr[j])
flag = 1;
break;
if (flag == 0)
{
if (fault < f)
fr[fault] = temp;
fault++;
else
max = 0;
max = m;
fr[max] = temp;
fault++;
printf("\n");
printf("%d\t", fr[x]);
}
Output 1:
Enter sequence:
1234567
1 -1 -1
1 2 -1
1 2 3
4 2 3
4 5 3
4 5 6
7 5 6
Output 2:
Enter sequence:
12123
Enter the frame size:
1 -1 -1
1 2 -1
1 2 -1
1 2 -1
1 2 3