linux-sysprog · beginner · ~15 min

Clamp to a soft limit

Apply a resource ceiling.

Challenge

A process may use a resource up to its soft rlimit; requests above it are capped. Apply that ceiling.

Task

Implement long clamp_to_limit(long want, long soft_limit) that returns want capped at soft_limit — that is, the smaller of the two.

Input

  • want: the requested amount.
  • soft_limit: the resource ceiling.

Output

min(want, soft_limit).

Example

clamp_to_limit(50, 100)    ->   50
clamp_to_limit(150, 100)   ->   100
clamp_to_limit(100, 100)   ->   100

Edge cases

  • Equal values: return that value.

Input format

want: requested amount; soft_limit: ceiling.

Output format

The smaller of want and soft_limit.

Starter code

long clamp_to_limit(long want, long soft_limit) {
    /* TODO */
    return want;
}

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