Write the simulation program to implement demand paging and show the page scheduling and total number of page faults according to the FIFO 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 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;

    int front = 0; // Index of the oldest page

    int rear = -1; // Index of the newest page

    int frame_count = 0; // Current number of pages in memory


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

    }


    printf("Reference String: ");

    for (int i = 0; i < num_references; i++) {

        printf("%d ", reference_string[i]);

    }

    printf("\n");


    // 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);


            // Check if memory is full

            if (frame_count < num_frames) {

                // Add new page to memory

                rear = (rear + 1) % num_frames;

                memory[rear] = current_page;

                frame_count++;

            } else {

                // Replace the oldest page

                memory[front] = current_page;

                front = (front + 1) % num_frames;

                rear = (rear + 1) % num_frames;

            }

        } 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");

}