networking · beginner · ~15 min

Cap the maxevents argument

Bound the epoll_wait maxevents.

Challenge

Clamp a requested maxevents value into a safe range for epoll_wait.

Task

Implement int cap_maxevents(int requested, int cap).

Input

  • requested: the caller's desired maxevents.
  • cap: the maximum allowed value.

Output

Return requested clamped to the range [1, cap]: at least 1, never more than cap.

Example

cap_maxevents(16,  64)  ->  16
cap_maxevents(100, 64)  ->  64   (capped)
cap_maxevents(0,   64)  ->  1    (floored at 1)

Edge cases

  • A requested below 1 returns 1.

Input format

requested: the desired maxevents; cap: the maximum allowed.

Output format

requested clamped to [1, cap].

Constraints

Floor at 1, cap at cap.

Starter code

int cap_maxevents(int requested, int cap) {
    /* TODO */
    return 1;
}

Common mistakes

Assuming read/write transmits all requested bytes at once; ignoring socket file descriptor cleanup; unhandled endianness conversions.

Edge cases to handle

Short reads/writes; peer closing socket (recv returns 0); signal interruption (EINTR); non-blocking EAGAIN.

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