THINK FIRST·CODE LATER

← All labs

Write-back vs. write-through cache

Problem

Simulate a block cache with LRU replacement.

Input: policy (through or back), capacity (blocks), then operations: read b, write b, sync, crash.

  • read b: hit if cached; on a miss, read the block from disk (disk read) and insert it.
  • write b: a miss inserts the block without reading it (full-block write). With through, the block is written to disk immediately (disk write) and stays clean; with back, it becomes dirty.
  • Eviction (when inserting into a full cache): remove the least recently used block; if it is dirty, write it to disk (disk write).
  • Print read 3: miss / write 1: hit for each read/write.
  • sync: write all dirty blocks, mark them clean: sync: 2 dirty blocks written (singular block for 1).
  • crash: print CRASH: lost updates to blocks [4, 5] (sorted dirty blocks, or none) and empty the cache.

At the end: Operations: n, hits: h, disk reads: r, disk writes: w (reads and writes only count as operations).

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.

os11-4-data-1.txt

through 3
read 1
write 1
write 1
write 2
read 3
write 4
read 1
write 5
crash
What your program must print for os11-4-data-1.txt
read 1: miss
write 1: hit
write 1: hit
write 2: miss
read 3: miss
write 4: miss
read 1: miss
write 5: miss
CRASH: lost updates to blocks none
Operations: 8, hits: 2, disk reads: 3, disk writes: 5

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 < os11-4-data-1.txt

Windows PowerShell does not understand <:

Get-Content os11-4-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.