networking · beginner · ~15 min

Clamp the listen backlog

Bound a listen() backlog argument.

Challenge

The listen() backlog argument should be kept within a sensible range. Clamp a requested backlog so it never goes below 0 or above a maximum.

Task

Implement int clamp_backlog(int requested, int max) that returns requested clamped to the range [0, max].

Input

  • requested: the requested backlog (may be negative).
  • max: the upper cap.

Output

Returns requested if it is within [0, max], otherwise the nearest bound.

Example

clamp_backlog(50, 128)    ->   50
clamp_backlog(500, 128)   ->   128
clamp_backlog(-1, 128)    ->   0

Edge cases

  • Negative requested clamps up to 0.
  • requested above max clamps down to max.

Input format

Two integers: the requested backlog and the maximum.

Output format

requested clamped into the range [0, max].

Constraints

Floor the result at 0 and cap it at max.

Starter code

int clamp_backlog(int requested, int max) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.