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). Withthrough, the block is written to disk immediately (disk write) and stays clean; withback, 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: hitfor each read/write. sync: write all dirty blocks, mark them clean:sync: 2 dirty blocks written(singularblockfor 1).crash: printCRASH: lost updates to blocks [4, 5](sorted dirty blocks, ornone) and empty the cache.
At the end: Operations: n, hits: h, disk reads: r, disk writes: w (reads and writes only count as operations).