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

OS 12 c

The document provides a C program that implements the Optimal Page Replacement Algorithm for managing page faults in a system. It prompts the user to input the number of pages, the page reference string, and the number of frames, then calculates and displays the frames after each page reference along with the total number of page faults. The example output illustrates the program's functionality with a specific set of inputs.

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)
6 views

OS 12 c

The document provides a C program that implements the Optimal Page Replacement Algorithm for managing page faults in a system. It prompts the user to input the number of pages, the page reference string, and the number of frames, then calculates and displays the frames after each page reference along with the total number of page faults. The example output illustrates the program's functionality with a specific set of inputs.

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

12C TO IMPLEMENT THE VARIOUS PAGE REPLACEMENT ALGORITHM-


OPTIMAL PAGE REPLACEMENT ALGORITHM
PROGRAM:
#include <stdio.h>
int main() {
int pages[50], frames[10], n, f, i, j, k, pos, max, faults = 0, found;
printf("Enter number of pages: ");
scanf("%d", &n);
printf("Enter page reference string: ");
for (i = 0; i < n; i++)
scanf("%d", &pages[i]);
printf("Enter number of frames: ");
scanf("%d", &f);
for (i = 0; i < f; i++)
frames[i] = -1;
printf("\nPage\tFrames\n");
for (i = 0; i < n; i++) {
found = 0;
// Check if page already exists
for (j = 0; j < f; j++) {
if (frames[j] == pages[i]) {
found = 1;
break;
}
}
// If page is not in frame
if (!found) {
// Check for empty frame
for (j = 0; j < f; j++) {
if (frames[j] == -1) {
frames[j] = pages[i];
Sprno:9223

faults++;
found = 1;
break;
}
}
}
// If no empty frame and page not found — Replace Optimal
if (!found) {
int future[10];
for (j = 0; j < f; j++) {
future[j] = -1;
for (k = i + 1; k < n; k++) {
if (frames[j] == pages[k]) {
future[j] = k;
break;
}
}
}
// Find the page not used for longest time
max = -1;
pos = -1;
for (j = 0; j < f; j++) {
if (future[j] == -1) {
pos = j;
break;
} else if (future[j] > max) {
max = future[j];
pos = j;
}
}
Sprno:9223

frames[pos] = pages[i];
faults++;
}
// Display frames
printf("%d\t", pages[i]);
for (j = 0; j < f; j++) {
if (frames[j] == -1)
printf("- ");
else
printf("%d ", frames[j]);
}
printf("\n");
}
printf("\nTotal Page Faults = %d\n", faults);
return 0;
}

OUTPUT:
Enter number of pages: 5
Enter page reference string: 1 2 3 2 4
Enter number of frames: 3
Page Frames
1 1--
2 12-
3 123
2 123
4 423
Total Page Faults = 4

You might also like