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).

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

os10-1-data-1.txt

FIFO 3
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
What your program must print for os10-1-data-1.txt
ref 2: [2 0 1] FAULT, evict 7
ref 0: [2 0 1] hit
ref 3: [2 3 1] FAULT, evict 0
ref 0: [2 3 0] FAULT, evict 1

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < os10-1-data-1.txt

Windows PowerShell does not understand <:

Get-Content os10-1-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

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.