linux-sysprog · beginner · ~15 min
Compute remaining quota without underflow.
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.
Implement long cgroup_remaining(long limit, long used) that returns limit - used, but never below 0.
limit: the cgroup memory limit.used: memory currently used.limit - used if positive, otherwise 0.
cgroup_remaining(100, 30) -> 70
cgroup_remaining(100, 100) -> 0
cgroup_remaining(100, 140) -> 0 (over limit, clamped)
used >= limit: return 0 (no negative remaining).limit: cgroup memory limit; used: memory in use.
limit - used, floored at 0.
Never return a negative remaining value.
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.