linux-sysprog · beginner · ~15 min
Detect a timeout from timestamps.
Comparing elapsed time against a budget is the manual version of an alarm timeout. Decide whether a deadline has passed.
Implement int timed_out(long start, long now, long timeout) that returns 1 if now - start >= timeout, else 0.
start: start timestamp.now: current timestamp.timeout: allowed duration.1 if the elapsed time now - start has reached timeout, else 0.
timed_out(100, 160, 50) -> 1
timed_out(100, 120, 50) -> 0
timed_out(100, 150, 50) -> 1 (exactly at the deadline)
timeout: counts as timed out (return 1).start, now: timestamps; timeout: allowed duration.
1 if now - start >= timeout, else 0.
Reaching the deadline exactly counts as timed out.
int timed_out(long start, long now, long timeout) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.