Master the fundamental concepts of memory management through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksSwapping 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.
Simulate swapping. There are only N physical frames. When they are all in use and a new page is needed, the kernel evicts the least recently used page. A page whose only up-to-date copy is in RAM must first be written to a swap slot (a swap-out). A page that was swapped in and hasn't been modified since still has a valid copy in its slot, so it can just be dropped, which is the cheap case. Touching a swapped-out page later causes a major fault that reads it back.
cLoading…
out of swap space (nothing changes).cLoading…
The eviction note is appended to the fault line that caused it.
Input:
cLoading…
Output:
cLoading…
Hidden tests cover a page rewritten after being swapped in (swapped out again to its same slot), running out of swap space, free releasing both a frame and a slot, and a scan larger than RAM repeated twice (LRU's worst case).