linux-sysprog · beginner · ~15 min
Reason about pipe back-pressure.
A pipe buffer has a fixed capacity; when it fills up, write() blocks until a reader drains it (back-pressure). Decide whether the next write would block.
Implement int write_would_block(int inflight, int capacity, int n) that returns 1 if adding n bytes to the inflight already buffered would exceed capacity, else 0.
inflight: bytes currently buffered.capacity: total buffer size.n: bytes about to be written.1 if inflight + n > capacity, else 0.
write_would_block(10, 64, 20) -> 0
write_would_block(60, 64, 10) -> 1
write_would_block(60, 64, 4) -> 0 (exactly fills it)
inflight: buffered bytes; capacity: buffer size; n: bytes to write.
1 if inflight + n > capacity, else 0.
Filling exactly to capacity does not block.
int write_would_block(int inflight, int capacity, int n) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.