MIT-Curricular/OS/C/Week10/q2.c
2025-04-11 08:30:01 +05:30

169 lines
5.3 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <limits.h> // For INT_MAX
// Structure to represent a frame and its last used time
typedef struct {
int page_number;
int last_used_time;
} Frame;
// Function to find if a page exists in frames and return its index
// Also updates the last_used_time if found
int find_page(Frame frames[], int n_frames, int page, int current_time) {
for (int i = 0; i < n_frames; i++) {
if (frames[i].page_number == page) {
frames[i].last_used_time = current_time; // Update time on hit
return i; // Page found (Hit)
}
}
return -1; // Page not found (Fault)
}
// Function to find the index of the Least Recently Used (LRU) page
int find_lru_index(Frame frames[], int n_frames) {
int min_time = INT_MAX;
int lru_index = 0;
for (int i = 0; i < n_frames; i++) {
// Frame must contain a valid page (not -1)
if (frames[i].page_number != -1 && frames[i].last_used_time < min_time) {
min_time = frames[i].last_used_time;
lru_index = i;
}
}
return lru_index;
}
// Function to print the current state of frames
void print_frames(Frame frames[], int n_frames) {
printf("[");
for (int i = 0; i < n_frames; i++) {
if (frames[i].page_number == -1) {
printf(" - ");
} else {
printf(" %d ", frames[i].page_number);
}
}
printf("]\n");
}
int main() {
int n_frames, n_pages;
int *pages = NULL; // Reference string
Frame *frames = NULL; // Frames in memory
int page_faults = 0;
int page_hits = 0;
int current_time = 0; // Counter to track usage time
int frame_idx = 0; // Index for filling empty frames initially
// 1. Get Inputs
printf("Enter the number of frames: ");
if (scanf("%d", &n_frames) != 1 || n_frames <= 0) {
fprintf(stderr, "Error: Invalid number of frames.\n");
return 1;
}
printf("Enter the number of pages in the reference string: ");
if (scanf("%d", &n_pages) != 1 || n_pages <= 0) {
fprintf(stderr, "Error: Invalid number of pages.\n");
return 1;
}
// 2. Allocate Memory
pages = (int *)malloc(n_pages * sizeof(int));
if (pages == NULL) {
perror("Failed to allocate memory for pages");
return 1;
}
frames = (Frame *)malloc(n_frames * sizeof(Frame));
if (frames == NULL) {
perror("Failed to allocate memory for frames");
free(pages); // Clean up already allocated memory
return 1;
}
printf("Enter the page reference string (space-separated %d integers):\n", n_pages);
for (int i = 0; i < n_pages; i++) {
if (scanf("%d", &pages[i]) != 1) {
fprintf(stderr, "Error reading page reference string.\n");
free(pages);
free(frames);
return 1;
}
}
// 3. Initialize Frames
for (int i = 0; i < n_frames; i++) {
frames[i].page_number = -1; // -1 indicates empty frame
frames[i].last_used_time = -1; // Initialize time
}
printf("\n--- LRU Simulation Start ---\n");
printf("Frames: %d | Reference String Length: %d\n\n", n_frames, n_pages);
// 4. Process Page References
for (int i = 0; i < n_pages; i++) {
current_time++; // Increment time step for each reference
int current_page = pages[i];
printf("Ref: %d -> ", current_page);
int found_index = find_page(frames, n_frames, current_page, current_time);
if (found_index != -1) {
// Page Hit
page_hits++;
printf("Hit ");
} else {
// Page Fault
page_faults++;
printf("Fault ");
// Find a place for the new page
int replace_index = -1;
// Check for an empty frame first
for(int k=0; k < n_frames; k++){
if(frames[k].page_number == -1){
replace_index = k;
break;
}
}
if (replace_index != -1) {
// Use the empty frame
frames[replace_index].page_number = current_page;
frames[replace_index].last_used_time = current_time;
printf("(loaded into empty frame %d) ", replace_index);
} else {
// No empty frames, find LRU page to replace
replace_index = find_lru_index(frames, n_frames);
printf("(replaced P%d in frame %d) ", frames[replace_index].page_number, replace_index);
frames[replace_index].page_number = current_page;
frames[replace_index].last_used_time = current_time;
}
}
print_frames(frames, n_frames); // Show frame status after each step
}
// 5. Output Results
printf("\n--- LRU Simulation End ---\n");
printf("Total Page References: %d\n", n_pages);
printf("Total Page Hits: %d\n", page_hits);
printf("Total Page Faults: %d\n", page_faults);
if (n_pages > 0) {
printf("Hit Rate: %.2f%%\n", (double)page_hits / n_pages * 100.0);
printf("Fault Rate: %.2f%%\n", (double)page_faults / n_pages * 100.0);
} else {
printf("Hit Rate: N/A\n");
printf("Fault Rate: N/A\n");
}
// 6. Free Memory
free(pages);
free(frames);
return 0;
}