linux-sysprog · beginner · ~15 min
Identify the producer back-pressure condition.
A producer must wait on the 'not full' condition when the buffer has no free slot. Detect that back-pressure condition.
Implement int producer_blocks(int count, int cap) that returns 1 if the next produce would have to wait, else 0.
count: items currently in the buffer.cap: buffer capacity.1 if the buffer is full (count >= cap); else 0.
producer_blocks(3, 3) -> 1
producer_blocks(2, 3) -> 0
count >= cap: blocks (return 1).count: current item count; cap: buffer capacity.
1 if count >= cap (buffer full), else 0.
int producer_blocks(int count, int cap) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.