Kick only when everyone is alive
Feed the watchdog only if every required task has checked in since the last kick. A medium embedded C problem (Watchdogs), run and graded in the browser.
medium · Watchdogs · watchdog · supervisor · rtos
Each task sets its own bit in alive when it completes a cycle. A supervisor runs periodically and decides whether to kick the watchdog.
Write supervisor_tick(alive, required, kick_fn):
- if every bit set in
requiredis also set in*alive, callkick_fn()once, - then, kick or not, clear
*aliveso tasks must check in again next period, - return true if it kicked.
A hung task never sets its bit, so the watchdog is allowed to expire and reset the system.
Starter code
bool supervisor_tick(uint8_t *alive, uint8_t required, void (*kick_fn)(void)) {
kick_fn();
return true;
}Example tests
Everyone checked in
uint8_t alive = 0x07;
bool k = supervisor_tick(&alive, 0x07, kick);
show(k, alive);Expected output:
kicked=1 kicks=1 alive=0x00One task is missing
uint8_t alive = 0x05;
bool k = supervisor_tick(&alive, 0x07, kick);
show(k, alive);Expected output:
kicked=0 kicks=0 alive=0x00Learn it first
- Watchdog timer · Independent and windowed watchdogs, and feeding them correctly.
More Watchdogs problems
- Watchdog reload value · Turn a timeout in milliseconds into the reload count of a prescaled watchdog clock.
- Greenhouse: report why it restarted · Read the reset-cause flags and report the one that matters.