diff --git a/OS/C/Week10/q2.c b/OS/C/Week10/q2.c index e69de29..45d1079 100644 --- a/OS/C/Week10/q2.c +++ b/OS/C/Week10/q2.c @@ -0,0 +1,169 @@ +#include +#include +#include // 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; +} diff --git a/OS/C/Week10/qq1.c b/OS/C/Week10/qq1.c new file mode 100644 index 0000000..72d05c3 --- /dev/null +++ b/OS/C/Week10/qq1.c @@ -0,0 +1,176 @@ +#include +#include +#include + +// Function to check if a page exists in frames +bool isPagePresent(int* frames, int num_frames, int page) { + for (int i = 0; i < num_frames; i++) { + if (frames[i] == page) { + return true; + } + } + return false; +} + +// FIFO Page Replacement Algorithm +void fifo(int* reference_string, int num_pages, int num_frames) { + int* frames = (int*)malloc(num_frames * sizeof(int)); + int page_faults = 0; + int frame_index = 0; + + // Initialize frames with -1 (indicating empty) + for (int i = 0; i < num_frames; i++) { + frames[i] = -1; + } + + printf("\nFIFO Page Replacement Algorithm:\n"); + printf("Reference String: "); + for (int i = 0; i < num_pages; i++) { + printf("%d ", reference_string[i]); + } + printf("\n\n"); + + for (int i = 0; i < num_pages; i++) { + printf("Page %d: ", reference_string[i]); + + // Check if page already exists in frames + if (!isPagePresent(frames, num_frames, reference_string[i])) { + // Replace page at current frame_index + frames[frame_index] = reference_string[i]; + frame_index = (frame_index + 1) % num_frames; + page_faults++; + + printf("Page Fault! Frames: "); + } else { + printf("No Page Fault. Frames: "); + } + + // Print current state of frames + for (int j = 0; j < num_frames; j++) { + if (frames[j] == -1) { + printf("[ ] "); + } else { + printf("[%d] ", frames[j]); + } + } + printf("\n"); + } + + printf("\nTotal Page Faults (FIFO): %d\n", page_faults); + free(frames); +} + +// Function to find index of page that will be used farthest in future +int findOptimalPage(int* reference_string, int* frames, int num_frames, int num_pages, int current_position) { + int farthest = -1; + int index = -1; + + for (int i = 0; i < num_frames; i++) { + int j; + for (j = current_position + 1; j < num_pages; j++) { + if (frames[i] == reference_string[j]) { + if (j > farthest) { + farthest = j; + index = i; + } + break; + } + } + + // If page is never used in future + if (j == num_pages) { + return i; + } + } + + // If all pages will be used in future, return the one used farthest + return (index == -1) ? 0 : index; +} + +// Optimal Page Replacement Algorithm +void optimal(int* reference_string, int num_pages, int num_frames) { + int* frames = (int*)malloc(num_frames * sizeof(int)); + int page_faults = 0; + + // Initialize frames with -1 (indicating empty) + for (int i = 0; i < num_frames; i++) { + frames[i] = -1; + } + + printf("\nOptimal Page Replacement Algorithm:\n"); + printf("Reference String: "); + for (int i = 0; i < num_pages; i++) { + printf("%d ", reference_string[i]); + } + printf("\n\n"); + + for (int i = 0; i < num_pages; i++) { + printf("Page %d: ", reference_string[i]); + + // Check if page already exists in frames + if (!isPagePresent(frames, num_frames, reference_string[i])) { + int free_frame = -1; + + // Check if there's an empty frame + for (int j = 0; j < num_frames; j++) { + if (frames[j] == -1) { + free_frame = j; + break; + } + } + + if (free_frame != -1) { + // If empty frame exists, use it + frames[free_frame] = reference_string[i]; + } else { + // Find optimal page to replace + int replace_index = findOptimalPage(reference_string, frames, num_frames, num_pages, i); + frames[replace_index] = reference_string[i]; + } + + page_faults++; + printf("Page Fault! Frames: "); + } else { + printf("No Page Fault. Frames: "); + } + + // Print current state of frames + for (int j = 0; j < num_frames; j++) { + if (frames[j] == -1) { + printf("[ ] "); + } else { + printf("[%d] ", frames[j]); + } + } + printf("\n"); + } + + printf("\nTotal Page Faults (Optimal): %d\n", page_faults); + free(frames); +} + +int main() { + int num_pages, num_frames; + + printf("Enter number of frames: "); + scanf("%d", &num_frames); + + printf("Enter number of pages in reference string: "); + scanf("%d", &num_pages); + + int* reference_string = (int*)malloc(num_pages * sizeof(int)); + + printf("Enter the reference string (page numbers):\n"); + for (int i = 0; i < num_pages; i++) { + scanf("%d", &reference_string[i]); + } + + // Run FIFO algorithm + fifo(reference_string, num_pages, num_frames); + + // Run Optimal algorithm + optimal(reference_string, num_pages, num_frames); + + free(reference_string); + return 0; +}