THINK FIRST·CODE LATER

← All labs

Page-replacement simulator: FIFO, LRU, OPT and clock

Problem

Input: the algorithm (FIFO, LRU, OPT or CLOCK), the number of frames, then the reference string.

Rules:

  • Free frames are filled from left to right.
  • FIFO: evict the page loaded earliest. LRU: evict the page with the oldest last use. OPT: evict the page whose next use is farthest in the future (never used again = infinitely far; ties → lowest frame index).
  • CLOCK: each frame has a reference bit, set to 1 when the page is loaded or referenced. On replacement, starting at the hand: while the bit is 1, clear it and advance; evict the page at the hand, and advance the hand past the new page. While filling free frames, the hand moves to the frame after the one just filled.

For each reference print ref 2: [2 0 1] FAULT, evict 7 or ref 0: [2 0 1] hit (empty frames shown as -). Finally: LRU with 3 frames: 12 faults / 20 references (60.0% fault rate).

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.