linux-sysprog · beginner · ~15 min

timerfd expirations

Compute periodic timer ticks.

Challenge

A periodic timerfd fires once per full interval; reading it returns how many times it has expired. Compute that count from elapsed time.

Task

Implement long timer_expirations(long elapsed_ms, long interval_ms) that returns how many full interval_ms periods fit in elapsed_ms. If interval_ms <= 0, return 0.

Input

  • elapsed_ms: milliseconds elapsed since the timer started.
  • interval_ms: timer period in milliseconds.

Output

The number of completed periods (elapsed_ms / interval_ms, integer division), or 0 when interval_ms <= 0.

Example

timer_expirations(1000, 200)   ->   5
timer_expirations(950, 200)    ->   4   (partial period doesn't count)
timer_expirations(100, 0)      ->   0

Edge cases

  • interval_ms <= 0: return 0 (avoid divide-by-zero).
  • A partial final period does not count.

Input format

elapsed_ms: time elapsed; interval_ms: timer period.

Output format

Completed periods (integer division), or 0 if interval <= 0.

Constraints

Guard interval_ms <= 0; partial periods don't count.

Starter code

long timer_expirations(long elapsed_ms, long interval_ms) {
    /* TODO */
    return 0;
}

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