linux-sysprog · beginner · ~15 min

Would the producer block?

Identify the producer back-pressure condition.

Challenge

A producer must wait on the 'not full' condition when the buffer has no free slot. Detect that back-pressure condition.

Task

Implement int producer_blocks(int count, int cap) that returns 1 if the next produce would have to wait, else 0.

Input

  • count: items currently in the buffer.
  • cap: buffer capacity.

Output

1 if the buffer is full (count >= cap); else 0.

Example

producer_blocks(3, 3)   ->   1
producer_blocks(2, 3)   ->   0

Edge cases

  • count >= cap: blocks (return 1).

Input format

count: current item count; cap: buffer capacity.

Output format

1 if count >= cap (buffer full), else 0.

Starter code

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.