linux-sysprog · beginner · ~15 min

cgroup memory remaining

Compute remaining quota without underflow.

Challenge

A cgroup caps memory at limit; exceeding it triggers the OOM killer. Report how much headroom is left, clamped so it never reads as negative.

Task

Implement long cgroup_remaining(long limit, long used) that returns limit - used, but never below 0.

Input

  • limit: the cgroup memory limit.
  • used: memory currently used.

Output

limit - used if positive, otherwise 0.

Example

cgroup_remaining(100, 30)    ->   70
cgroup_remaining(100, 100)   ->   0
cgroup_remaining(100, 140)   ->   0   (over limit, clamped)

Edge cases

  • used >= limit: return 0 (no negative remaining).

Input format

limit: cgroup memory limit; used: memory in use.

Output format

limit - used, floored at 0.

Constraints

Never return a negative remaining value.

Starter code

long cgroup_remaining(long limit, long used) {
    /* TODO */
    return 0;
}

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