Write the simulation program to implement demand paging and show the page scheduling and total number of page faults according to the Optimal page replacement algorithm. Assume the memory of n frames.
Reference String : 3, 4, 5, 4, 3, 4, 7, 2, 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_page_to_replace(int memory[], int num_frames, int reference_string[], int current_index, int num_references);

int main() {
    int memory[MAX_FRAMES];
    int reference_string[REFERENCE_STRING_LENGTH] = {3, 4, 5, 4, 3, 4, 7, 2, 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;

    // 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
    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;
                break;
            }
        }

        if (!page_found) {
            // Page fault
            page_faults++;
            printf("Page fault! Reference: %d\n", current_page);

            if (i < num_frames) {
                // Add new page to memory
                memory[i] = current_page;
            } else {
                // Replace the page using Optimal page replacement
                int page_to_replace = find_page_to_replace(memory, num_frames, reference_string, i, num_references);
                for (int j = 0; j < num_frames; j++) {
                    if (memory[j] == page_to_replace) {
                        memory[j] = current_page;
                        break;
                    }
                }
            }
        } 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 page to replace using the Optimal page replacement algorithm
int find_page_to_replace(int memory[], int num_frames, int reference_string[], int current_index, int num_references) {
    int farthest_use = -1;
    int page_to_replace = -1;

    // Check each page in memory
    for (int i = 0; i < num_frames; i++) {
        int j;
        for (j = current_index + 1; j < num_references; j++) {
            if (memory[i] == reference_string[j]) {
                break;
            }
        }

        // If the page is not found in future references
        if (j == num_references) {
            return memory[i];
        }

        // If this page is used farther in the future
        if (j > farthest_use) {
            farthest_use = j;
            page_to_replace = memory[i];
        }
    }

    return page_to_replace;
}