Virtual Memory Simulator (C)

A C simulator for address translation, page faults, and FIFO/LRU replacement.

Concept illustration of sixteen virtual pages mapped to four physical memory frames
Concept illustration of virtual pages, resident frames, and a backing store.

Trace an address through virtual memory

This group assignment implements a virtual memory simulator in C. It models 16 virtual pages, four physical frames, and eight words per page, backed by a 128-word simulated disk.

I focused on the read/write paths, page-fault handling, FIFO and LRU replacement logic, and the interactive inspection commands. The goal was to make the behavior observable as well as functional.


A read can change the memory layout

The page table maps a virtual page to a physical frame and records valid and dirty state. A read or write first translates the address. If the page is not resident, the simulator reports a page fault and loads it from the backing store.

When no frame is free, a replacement policy selects a resident page to evict. FIFO uses the order in which pages were loaded; LRU uses tracked access recency. These policies let the same command sequence illustrate different replacement decisions.


Preserve modified pages

A write updates physical memory and marks the page dirty. Before evicting a dirty page, the simulator copies its words back to the backing store. A clean page can be replaced without that write-back.

The order of these operations matters. The page table, resident frame, and backing store must remain consistent after both the old page’s eviction and the new page’s arrival.


Inspect the state

Commands include read, write, showmain, and showptable. The latter two expose physical-frame contents and page-table entries, making it possible to follow a fault or replacement step by step.

gcc hw.c -o vmsim
./vmsim       # FIFO
./vmsim LRU   # LRU
> write 19 7
A Page Fault Has Occurred
> read 19
7
> showptable
> showmain 0
> quit

What I learned

The project made virtual memory feel less like a diagram and more like a state machine. One access can trigger translation, selection, write-back, invalidation, loading, and another translation. Implementing those steps reinforced the value of separating a policy decision from the mechanics that preserve the data.