Aryan VERMA 22MIM10139 - Exp-6
Aryan VERMA 22MIM10139 - Exp-6
steps:
Program:
#include <stdio.h>
int main() {
int pages[] = {1, 3, 0, 3, 5, 6, 3};
int n = sizeof(pages) / sizeof(pages[0]);
int capacity = 3;
FIFO(pages, n, capacity);
return 0;
}
Result:
LRU (Least Recently Used) Page Replacement Algorithm
Steps:
● Initialize an empty frame array and a counter array to track the usage
of pages.
● Loop through the page requests.
● Check if the page is already in the frame and update its counter.
● If not, replace the least recently used page.
● Print the current state of the frame.
Program:
#include <stdio.h>
// If the page is not in the frame, replace the least recently used page
if (flag == 0) {
for (int j = 0; j < capacity; j++) {
if (frame[j] == -1) {
pos = j;
break;
} else if (counter[j] < least) {
least = counter[j];
pos = j;
}
}
frame[pos] = pages[i];
counter[pos] = ++time;
page_faults++;
}
int main() {
int pages[] = {1, 3, 0, 3, 5, 6, 3};
int n = sizeof(pages) / sizeof(pages[0]);
int capacity = 3;
LRU(pages, n, capacity);
return 0;
}
Result:
Optimal Page Replacement Algorithm
Steps:
Program:
#include <stdio.h>
int predict(int pages[], int frame[], int n, int index, int capacity) {
int res = -1, farthest = index;
for (int i = 0; i < capacity; i++) {
int j;
for (j = index; j < n; j++) {
if (frame[i] == pages[j]) {
if (j > farthest) {
farthest = j;
res = i;
}
break;
}
}
if (j == n) {
return i;
}
}
return (res == -1) ? 0 : res;
}
int page_faults = 0;
int main() {
int pages[] = {1, 3, 0, 3, 5, 6, 3};
int n = sizeof(pages) / sizeof(pages[0]);
int capacity = 3;
Optimal(pages, n, capacity);
return 0;
}
Result: