176 lines
		
	
	
	
		
			4.9 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			176 lines
		
	
	
	
		
			4.9 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
#include <stdio.h>
 | 
						|
#include <stdlib.h>
 | 
						|
#include <stdbool.h>
 | 
						|
 | 
						|
// 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;
 | 
						|
}
 |