0% found this document useful (0 votes)
3 views

OS 12 b

The document presents a C program that implements the Least Recently Used (LRU) page replacement algorithm. It prompts the user for the number of frames and pages, and then processes a page reference string to track page faults and display the current frame state. The output demonstrates the number of page faults encountered during execution.

Uploaded by

jayaprakash9223
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)
3 views

OS 12 b

The document presents a C program that implements the Least Recently Used (LRU) page replacement algorithm. It prompts the user for the number of frames and pages, and then processes a page reference string to track page faults and display the current frame state. The output demonstrates the number of page faults encountered during execution.

Uploaded by

jayaprakash9223
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/ 3

Sprno:9223

12B TO IMPLEMENT THE VARIOUS PAGE REPLACEMENT ALGORITHM-


LEAST RECENTLY USED ALGORITHM
PROGRAM:
#include <stdio.h>
int main() {
int frames, pages, i, j, k, time = 0, faults = 0;
int found, least, pos;
printf("Enter number of frames: ");
scanf("%d", &frames);
printf("Enter number of pages: ");
scanf("%d", &pages);
int pageSeq[pages], frame[frames], counter[frames];
printf("Enter page reference string: ");
for (i = 0; i < pages; i++)
scanf("%d", &pageSeq[i]);
// Initialize frames and counters
for (i = 0; i < frames; i++) {
frame[i] = -1;
counter[i] = 0;
}
printf("\nPage\tFrames\n");
for (i = 0; i < pages; i++) {
found = 0;
// Check if page is already in frame
for (j = 0; j < frames; j++) {
if (frame[j] == pageSeq[i]) {
time++;
counter[j] = time;
found = 1;
break;
}
Sprno:9223

}
// Page not found - page fault occurs
if (!found) {
// Find least recently used (smallest counter value)
least = counter[0];
pos = 0;
for (j = 1; j < frames; j++) {
if (counter[j] < least) {
least = counter[j];
pos = j;
}
}
frame[pos] = pageSeq[i];
time++;
counter[pos] = time;
faults++;
// Print frame content
printf("%d\t", pageSeq[i]);
for (k = 0; k < frames; k++) {
if (frame[k] == -1)
printf("- ");
else
printf("%d ", frame[k]);
}
printf("\n");
} else {
printf("%d\tNo page fault\n", pageSeq[i]);
}
}
printf("\nTotal Page Faults = %d\n", faults);
Sprno:9223

return 0;
}
OUTPUT:
Enter number of frames: 3

Enter number of pages: 7

Enter page reference string: 1 3 0 3 5 6 3

Page Frames

1 1--

3 13-

0 130

3 No page fault

5 530

6 536

3 No page fault

Total Page Faults = 5

You might also like