Feeding (kicking) the watchdog in Embedded C - Time & Space Complexity
We want to understand how the time to feed the watchdog changes as the program runs.
How often does the feeding action happen as the system runs?
Analyze the time complexity of the following code snippet.
void feed_watchdog(void) {
WDT->CLEAR = WDT_CLEAR_KEY;
}
int main(void) {
while(1) {
// ... other tasks ...
feed_watchdog();
}
return 0;
}
This code repeatedly feeds the watchdog inside an infinite loop to prevent system reset.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The infinite loop calling
feed_watchdog(). - How many times: The feeding happens continuously, as many times as the loop runs.
Since the loop runs forever, the feeding operation happens repeatedly without end.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 feedings |
| 100 | 100 feedings |
| 1000 | 1000 feedings |
Pattern observation: The number of feedings grows directly with the number of loop iterations.
Time Complexity: O(n)
This means the feeding operation happens once per loop iteration, growing linearly with the number of iterations.
[X] Wrong: "Feeding the watchdog is a one-time setup and does not repeat."
[OK] Correct: The watchdog must be fed regularly inside the loop to prevent reset, so it repeats continuously.
Understanding how often a watchdog is fed helps show you can reason about repeated operations in embedded systems, a useful skill for real-world programming.
"What if the feeding was done only once outside the loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand watchdog function
The watchdog timer resets the system if it is not fed in time, preventing hangs.Step 2: Purpose of feeding watchdog
Feeding the watchdog resets its timer, showing the system is running fine.Final Answer:
To reset the watchdog timer and prevent system reset -> Option DQuick Check:
Feeding watchdog = reset timer [OK]
- Thinking feeding increases clock speed
- Confusing feeding with power saving
- Assuming feeding starts program execution
FeedWatchdog()?Solution
Step 1: Identify correct function call syntax
In C, calling a function requires parentheses and a semicolon.Step 2: Check each option
FeedWatchdog(); uses correct capitalization and parentheses; others are incorrect syntax.Final Answer:
FeedWatchdog(); -> Option AQuick Check:
Function call syntax = functionName(); [OK]
- Omitting parentheses when calling function
- Using wrong capitalization
- Using brackets instead of parentheses
void main() {
init_watchdog(5); // 5 seconds timeout
while(1) {
// no FeedWatchdog() call here
}
}Solution
Step 1: Understand watchdog timeout behavior
If the watchdog is not fed within the timeout, it triggers a system reset.Step 2: Analyze code behavior
No FeedWatchdog() call means timer is not reset, so system resets after 5 seconds.Final Answer:
System resets after 5 seconds -> Option BQuick Check:
Not feeding watchdog = system reset [OK]
- Assuming system runs normally without feeding
- Expecting compilation error for missing feed
- Thinking watchdog disables automatically
void main() {
init_watchdog(10);
while(1) {
FeedWatchdog;
delay(1000);
}
}Solution
Step 1: Check FeedWatchdog usage
FeedWatchdog is a function and must be called with parentheses.Step 2: Analyze code snippet
FeedWatchdog; is missing parentheses, so it does not call the function.Final Answer:
Missing parentheses in FeedWatchdog call -> Option AQuick Check:
Function call needs () [OK]
- Forgetting parentheses on function calls
- Assuming delay is incorrect without context
- Thinking timeout value causes error
Solution
Step 1: Understand watchdog timeout vs loop time
The loop takes longer than the watchdog timeout, so feeding once per loop is too late.Step 2: Feeding watchdog multiple times inside loop
Calling FeedWatchdog() multiple times before 3 seconds resets timer and prevents reset.Final Answer:
Call FeedWatchdog() multiple times inside the loop before 3 seconds pass -> Option CQuick Check:
Feed watchdog before timeout expires [OK]
- Assuming increasing timeout is always possible
- Feeding watchdog only once after long loop
- Removing feeding calls thinking it helps
Ready to prove it?
Pass the test and this pattern moves unknown
