linux-sysprog · beginner · ~10 min
Absolute-time scheduling — the core of timerfd, setitimer, every cron alternative.
Implement int timer_due(long now_ms, long fire_at_ms) returning 1
if fire_at_ms <= now_ms (i.e. the timer has fired), 0 otherwise.
Then implement long timer_remaining_ms(long now_ms, long fire_at_ms) returning
how many ms remain (0 if the timer is due).
timerfd lets you turn a timer into a file descriptor. The core idea: 'absolute moment X in the future'. Once you internalise that, the whole API makes sense.
Current ms + fire-at ms.
0/1 (due?) and ms remaining.
No real syscalls.
int timer_due(long now_ms, long fire_at_ms) { /* TODO */ return 0; }
long timer_remaining_ms(long now_ms, long fire_at_ms) { /* TODO */ return 0; }
Using subtraction without checking for negative — fire - now may go negative once due.
fire_at_ms == now_ms (just due). Far-future timer. Already-past timer.
O(1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.