Byte-based I/O over Block API (Multithreading)
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-10-20
Problem Statement
Design and implement a thread-safe byte-oriented I/O interface that internally uses a lower-level block-based storage API. Multiple threads should be able to read and write bytes concurrently while the system ensures correctness, ordering, and efficient mapping of byte requests to block operations.
Background
1.<span></span>Many storage devices expose block-level APIs (e.g., read/write fixed-size 4KB blocks).
2.<span></span>Applications often need byte-level access (read/write arbitrary lengths at arbitrary offsets).
3.<span></span>Bridging these requires:
4.<span></span>Mapping byte ranges → underlying block ranges
5.<span></span>Handling unaligned reads/writes (read-modify-write cycles)
6.<span></span>Synchronizing concurrent access to avoid corruption
Functional Requirements
1.<span></span>Underlying Block API (given)
2.<span></span>read_block(block_id, buffer) → Reads one fixed-size block into buffer.
3.<span></span>write_block(block_id, buffer) → Writes one fixed-size block from buffer.
4.<span></span>BLOCK_SIZE is fixed (e.g., 4KB).
5.<span></span>Byte I/O API (to implement)
6.<span></span>read(offset, length, buffer) → Read length bytes starting at offset.
7.<span></span>write(offset, length, buffer) → Write length bytes starting at offset.
8.<span></span>Both may span multiple blocks and may be unaligned.
9.<span></span>Concurrency Requirements
10.<span></span>Multiple threads may call read and write simultaneously.
11.<span></span>Writes must be atomic at the byte-range level: readers should never see torn writes.
12.<span></span>Concurrent writes to disjoint regions must not block each other.
13.<span></span>Synchronization must avoid bottlenecks (no global lock on the whole device).
14.<span></span>Correctness
15.<span></span>Reads overlapping with writes should see either old or new data, never corruption.
16.<span></span>For unaligned writes, ensure proper read-modify-write of affected blocks.
17.<span></span>Handle large I/O efficiently (avoid excessive copying).
Constraints
1.<span></span>Block size = 4KB (fixed).
2.<span></span>File/device size up to 10 GB.
3.<span></span>Up to 100+ concurrent threads.
4.<span></span>Performance target: throughput close to raw block API when requests are aligned and large.
Example Scenario
1.<span></span>Thread-1 calls write(1000, 50, bufA) → modifies bytes 1000–1049 (spanning part of one block).
2.<span></span>Thread-2 calls read(1020, 40, bufB) → must see either the old or new version of those bytes, not a mix.
3.<span></span>Thread-3 calls write(8192, 4096, bufC) → aligned full-block write, should not block Thread-1 or Thread-2 since it touches different blocks.
Deliverables
1.<span></span>A class design (e.g., BlockDevice, ByteIOWrapper).
2.<span></span>Thread-safe implementation of read and write.
3.<span></span>Synchronization strategy (per-block locks, lock striping, or lock-free if possible).
4.<span></span>Tests for:
5.<span></span>Single-thread correctness
6.<span></span>Concurrent overlapping/non-overlapping reads/writes
7.<span></span>Performance under 100+ threads