A virtual memory management simulation implemented in C. This project demonstrates paging, page faults, and page replacement algorithms.
This project requires a Linux environment to compile and run.
It relies on POSIX-specific headers and features (sys/mman.h, ucontext.h, signals) to simulate hardware page faults.
- Linux (Ubuntu, Fedora, etc.)
- WSL (Windows Subsystem for Linux) on Windows
- macOS (may require slight adjustments for
ucontextdefinitions)
Note: This project will not compile with MinGW or Visual Studio on Windows directly.
This program simulates a virtual memory system mapping virtual pages to physical frames using a page table. It handles page faults by swapping pages in from an emulated disk.
It supports multiple page replacement policies:
- FIFO: First-In, First-Out.
- RAND: Random page replacement.
- CUSTOM: A custom prefetching algorithm that attempts to load the next sequential page.
To compile the project, run:
makeThis generates the virtmem executable.
Usage:
./virtmem <npages> <nframes> <policy> <program><npages>: Number of pages in the virtual address space.<nframes>: Number of physical frames available.<policy>: Replacement policy (fifo,rand, orcustom).<program>: The test program to run (alpha,beta,gamma,delta).
- npages: Must be >= 1.
- nframes: Must be >= 1.
- policy:
fifo: Evicts the oldest page.rand: Evicts a random page.custom: Uses a predictive prefetching strategy.
- program:
alpha: Linear scan access pattern.beta: Random access pattern.gamma/delta: Other specific access patterns to test algorithm performance.
Run with 100 pages, 10 frames, FIFO policy, executing the 'scan' program (alpha):
./virtmem 100 10 fifo alphaRun with 50 pages, 5 frames, Random policy, executing the 'sort' program (beta):
./virtmem 50 5 rand betaThe program will simulate memory accesses and output the final statistics:
- Page Faults: Number of times a page had to be fetched from disk.
- Disk Reads: Number of blocks read from the swap disk.
- Disk Writes: Number of blocks written to the swap disk (evicted dirty pages).
Summary: Page Faults - 120 | Disk Reads - 120 | Disk Writes - 45