Assignment 07: Non-Contiguous Memory Management: Paging and Replacement

I. Objective & Theoretical Framework

In modern computer operating systems, paging is a memory management scheme that permits the physical address space of a process to be non-contiguous, eliminating external fragmentation. The operating system retrieves data from secondary storage in same-size blocks called pages, which are loaded into available physical memory blocks called frames.

When a process references a page not currently in physical memory, a page fault occurs. Page replacement algorithms are fundamental to demand paging, completing the separation between logical and physical memory and providing programmers with an enormous virtual memory space.

You will implement the address translation mechanism and simulate the following page replacement algorithms:

II. Prerequisite Knowledge & Resources

III. Starter Code & Partial Implementations

The following skeleton demonstrates the basic setup for a FIFO page replacement simulation. It highlights the importance of initializing your frame array to -1 so that a logical page 0 is not mistakenly assumed to already be in memory.

#include <stdio.h>

int main() {
    int i, j, k, frames, pages, faults = 0, count = 0;
    int ref_string[25], m[10];

    printf("Enter the length of reference string: ");
    scanf("%d", &pages);
    printf("Enter the reference string: ");
    for(i = 0; i < pages; i++) {
        scanf("%d", &ref_string[i]);
    }
    printf("Enter no. of frames: ");
    scanf("%d", &frames);

    // Initialize frames to -1 to indicate they are empty
    for(i = 0; i < frames; i++) {
        m[i] = -1;
    }

    printf("\n The Page Replacement Process is -- \n");

    for(i = 0; i < pages; i++) {
        // [Insert logic to check if ref_string[i] is already in m[]]

        // [Insert logic to replace page at m[count] if a fault occurs]

        // [Insert logic to increment faults and manage the FIFO circular counter]
    }

    // [Insert Final Output Statements]

    return 0;
}

IV. Step-by-Step Task List

  1. Paging Address Translation: Write a C program to simulate the paging technique. Prompt the user for total memory size, page size, and the logical page tables for various processes. Ask the user for a logical address (process number, page number, and offset) and calculate the corresponding physical address.

  2. FIFO Replacement: Complete the starter code above. Simulate the FIFO replacement algorithm and print the state of the frames after every page reference. Calculate and output the total number of page faults.

  3. LRU Replacement: Create a new C program. LRU associates each page with the time of its last use. You will need a count or timestamp array parallel to your frames array. Every time a page is referenced (whether it causes a fault or not), update its timestamp. When a fault occurs, scan the timestamps to find the minimum value and replace that frame.

  4. LFU Replacement: Create a new C program. Instead of time, track the frequency of accesses. Create a cntr array. Increment the counter when a page is accessed. On a page fault, find the frame with the minimum counter value and replace it.

V. Common Pitfalls & Debugging Strategies

VI. Real-World Case Study

Page replacement is the bedrock of modern virtual memory. When you open too many browser tabs and your computer slows to a crawl, you are experiencing “thrashing”—a state where the OS is spending more CPU cycles swapping pages in and out of the physical RAM to the secondary storage (the pagefile or swap partition) than it is executing actual application code. While modern Solid State Drives (SSDs) make page faults drastically faster to resolve than older mechanical Hard Disk Drives (HDDs), efficient algorithms like LRU remain critical to maintaining system responsiveness.

VII. Advanced Variant Tasks


VIII. Resources & Further Reading

SDB Watermark