Master the fundamental concepts of memory management through this focused micro-challenge.
Swapping moves cold pages to a swap file or partition, freeing physical frames for hotter data. On access, a major page fault reads the page back. Thrashing happens when reclaim cost exceeds useful work.
Kernel swap path:
For example, a workstation with 8 GB RAM running 12 GB of idle browser tabs may keep working sets in RAM while cold tabs live in swap slot 4421.
Linux's kswapd daemon and its two-list active/inactive LRU approximation , a more scalable cousin of the victim selection you implement here , decide which pages get written to swap the moment memory pressure hits. Get victim selection wrong and you reproduce thrashing, the exact failure mode where a server burns more cycles swapping pages than serving requests.
Before you call the implementation done, walk failure modes on purpose. Test empty structures, single-element edge cases, maximum concurrency, and errno paths that must not crash the program. OS code usually fails in production when happy-path tests pass but invariants break under contention or memory pressure.
Keep structures small and name fields after kernel counterparts when possible. That lets you read man pages and kernel source side by side while you work. Print observable events during development; remove noisy logs once tests pass reliably.
You will implement a swap file backend and fault-in path for evicted pages. This exercise requires tracking page fault types (minor vs major) before and after swap pressure.
Implement swapping simulation.
Requirements:
Test:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github