Write c programs to simulate page replacement policies a) FCFS
#include <stdio.h>
#define MAX_PAGES 3
void fcfs(int pages[], int n);
int main() {
int pageSequence[] = {0, 1, 2, 3, 0, 4, 2, 1, 0, 3};
int n = sizeof(pageSequence) / sizeof(pageSequence[0]);
printf("Page Replacement using First-Come-First-Serve (FCFS)\n");
fcfs(pageSequence, n);
return 0;
}
void fcfs(int pages[], int n) {
int pageTable[MAX_PAGES] = {-1}; // Initialize page table with -1 (indicating an empty slot)
int pageFaults = 0;
for (int i = 0; i < n; i++) {
int currentPage = pages[i];
int pageFound = 0;
// Check if the page is already in the page table
for (int j = 0; j < MAX_PAGES; j++) {
if (pageTable[j] == currentPage) {
pageFound = 1;
break;
}
}
// If the page is not in the page table, replace the oldest page
if (!pageFound) {
int oldestPageIndex = 0;
// Find the index of the oldest page in the page table
for (int j = 1; j < MAX_PAGES; j++) {
if (pageTable[j] < pageTable[oldestPageIndex])
oldestPageIndex = j;
}
// Replace the oldest page with the current page
pageTable[oldestPageIndex] = currentPage;
pageFaults++;
// Display the current state of the page table
printf("Page %d added: ", currentPage);
for (int j = 0; j < MAX_PAGES; j++) {
printf("%d ", pageTable[j]);
}
printf("\n");
}
}
printf("Total Page Faults: %d\n", pageFaults);
}
Output:
Page Replacement using First-Come-First-Serve (FCFS)
Page 0 added: 0 -1 -1
Page 1 added: 0 1 -1
Page 2 added: 0 1 2
Page 3 added: 3 1 2
Page 0 added: 3 0 2
Page 4 added: 4 0 2
Page 2 added: 4 2 0
Page 1 added: 4 2 1
Page 0 added: 0 2 1
Page 3 added: 0 3 1
Total Page Faults: 7
Write c programs to simulate page replacement policies LRU
#include <stdio.h>
#define MAX_PAGES 3
void lru(int pages[], int n);
int main() {
int pageSequence[] = {0, 1, 2, 3, 0, 4, 2, 1, 0, 3};
int n = sizeof(pageSequence) / sizeof(pageSequence[0]);
printf("Page Replacement using Least Recently Used (LRU)\n");
lru(pageSequence, n);
return 0;
}
void lru(int pages[], int n) {
int pageTable[MAX_PAGES] = {-1}; // Initialize page table with -1 (indicating an
empty slot)
int pageOrder[MAX_PAGES] = {0}; // Store the order in which pages are
accessed
int pageFaults = 0;
for (int i = 0; i < n; i++) {
int currentPage = pages[i];
int pageFound = 0;
// Check if the page is already in the page table
for (int j = 0; j < MAX_PAGES; j++) {
if (pageTable[j] == currentPage) {
pageFound = 1;
// Update the order of the accessed page
pageOrder[j] = i + 1;
break;
}
}
// If the page is not in the page table, replace the least recently used page
if (!pageFound) {
int lruPageIndex = 0;
// Find the index of the least recently used page in the page table
for (int j = 1; j < MAX_PAGES; j++) {
if (pageOrder[j] < pageOrder[lruPageIndex])
lruPageIndex = j;
}
// Replace the least recently used page with the current page
pageTable[lruPageIndex] = currentPage;
pageOrder[lruPageIndex] = i + 1;
pageFaults++;
// Display the current state of the page table
printf("Page %d added: ", currentPage);
for (int j = 0; j < MAX_PAGES; j++) {
printf("%d ", pageTable[j]);
}
printf("\n");
}
}
printf("Total Page Faults: %d\n", pageFaults);
}
Output:
Page Replacement using Least Recently Used (LRU)
Page 0 added: 0 -1 -1
Page 1 added: 0 1 -1
Page 2 added: 0 1 2
Page 3 added: 3 1 2
Page 0 added: 3 0 2
Page 4 added: 4 0 2
Page 2 added: 4 2 0
Page 1 added: 4 2 1
Page 0 added: 0 2 1
Page 3 added: 0 3 1
Total Page Faults: 7
Write c programs to simulate page replacement policies optimal
#include <stdio.h>
#define MAX_PAGES 3
#define INFINITY 999999
void optimal(int pages[], int n);
int main() {
int pageSequence[] = {0, 1, 2, 3, 0, 4, 2, 1, 0, 3};
int n = sizeof(pageSequence) / sizeof(pageSequence[0]);
printf("Page Replacement using Optimal\n");
optimal(pageSequence, n);
return 0;
}
int findOptimalPage(int pages[], int pageTable[], int start, int n) {
for (int i = start; i < n; i++) {
int currentPage = pages[i];
int found = 0;
for (int j = 0; j < MAX_PAGES; j++) {
if (pageTable[j] == currentPage) {
found = 1;
break;
}
}
if (!found) {
return currentPage;
}
}
return -1; // Indicate no page found in the future
}
void optimal(int pages[], int n) {
int pageTable[MAX_PAGES] = {-1}; // Initialize page table with -1 (indicating an
empty slot)
int pageFaults = 0;
for (int i = 0; i < n; i++) {
int currentPage = pages[i];
int pageFound = 0;
// Check if the page is already in the page table
for (int j = 0; j < MAX_PAGES; j++) {
if (pageTable[j] == currentPage) {
pageFound = 1;
break;
}
}
// If the page is not in the page table, replace the page with the longest
future access time
if (!pageFound) {
int optimalPage = findOptimalPage(pages, pageTable, i + 1, n);
if (optimalPage != -1) {
// Replace the page with the longest future access time
for (int j = 0; j < MAX_PAGES; j++) {
if (pageTable[j] == optimalPage) {
pageTable[j] = currentPage;
break;
}
}
}
pageFaults++;
// Display the current state of the page table
printf("Page %d added: ", currentPage);
for (int j = 0; j < MAX_PAGES; j++) {
printf("%d ", pageTable[j]);
}
printf("\n");
}
}
printf("Total Page Faults: %d\n", pageFaults);
}
Output:
Page Replacement using Optimal
Page 0 added: 0 -1 -1
Page 1 added: 0 1 -1
Page 2 added: 0 1 2
Page 3 added: 3 1 2
Page 0 added: 3 0 2
Page 4 added: 4 0 2
Page 2 added: 4 2 0
Page 1 added: 4 2 1
Page 0 added: 0 2 1
Page 3 added: 0 3 1
Total Page Faults: 7