Write the simulation program for demand paging and show the page scheduling and total number of page faults according the MRU page replacement algorithm. Assume the memory of n frames.
Reference String : 3, 4, 5, 6, 3, 4, 7, 3, 4, 5, 6, 7, 2, 4, 6
#include <stdio.h>
#include <stdlib.h>
#define MAX_FRAMES 100
#define REFERENCE_STRING_LENGTH 15
// Function prototypes
void print_memory(int memory[], int num_frames);
int find_most_recently_used(int memory[], int num_frames, int usage[], int usage_index);
int main() {
int memory[MAX_FRAMES];
int reference_string[REFERENCE_STRING_LENGTH] = {3, 4, 5, 6, 3, 4, 7, 3, 4, 5, 6, 7, 2, 4, 6};
int num_frames; // Number of frames in memory
int num_references = REFERENCE_STRING_LENGTH; // Length of the reference string
int page_faults = 0;
int usage[MAX_FRAMES]; // To track the most recently used pages
int usage_index = 0; // Index to keep track of MRU
// Initialize the memory with -1 (indicating empty frames)
printf("Enter the number of frames: ");
scanf("%d", &num_frames);
if (num_frames > MAX_FRAMES) {
printf("Number of frames exceeds maximum limit.\n");
return 1;
}
// Initialize memory and usage arrays
for (int i = 0; i < num_frames; i++) {
memory[i] = -1;
}
// Process each page in the reference string
for (int i = 0; i < num_references; i++) {
int current_page = reference_string[i];
int page_found = 0;
// Check if the page is already in memory
for (int j = 0; j < num_frames; j++) {
if (memory[j] == current_page) {
page_found = 1;
// Update usage order
usage[usage_index] = current_page;
usage_index = (usage_index + 1) % num_frames; // Circular increment
break;
}
}
if (!page_found) {
// Page fault
page_faults++;
printf("Page fault! Reference: %d\n", current_page);
if (usage_index < num_frames) {
// Add new page to memory
memory[usage_index] = current_page;
usage[usage_index] = current_page;
usage_index = (usage_index + 1) % num_frames; // Circular increment
} else {
// Replace the most recently used page
int mru_page = find_most_recently_used(memory, num_frames, usage, usage_index);
for (int j = 0; j < num_frames; j++) {
if (memory[j] == mru_page) {
memory[j] = current_page;
break;
}
}
// Update usage order
usage[usage_index] = current_page;
usage_index = (usage_index + 1) % num_frames; // Circular increment
}
} else {
printf("Page hit! Reference: %d\n", current_page);
}
// Print the state of memory
print_memory(memory, num_frames);
}
printf("Total number of page faults: %d\n", page_faults);
return 0;
}
// Function to print the state of the memory
void print_memory(int memory[], int num_frames) {
printf("Memory state: ");
for (int i = 0; i < num_frames; i++) {
if (memory[i] != -1) {
printf("%d ", memory[i]);
} else {
printf("_ ");
}
}
printf("\n");
}
// Function to find the most recently used page
int find_most_recently_used(int memory[], int num_frames, int usage[], int usage_index) {
int mru_page = usage[(usage_index - 1 + num_frames) % num_frames];
for (int i = 0; i < num_frames; i++) {
if (memory[i] == mru_page) {
return mru_page;
}
}
return -1; // Should never reach here if logic is correct
}
0 Comments