0
0
Embedded Cprogramming~5 mins

Window watchdog concept in Embedded C - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Window watchdog concept
O(n)
Understanding Time Complexity

We want to understand how the time cost grows when using a window watchdog in embedded C code.

Specifically, how often the watchdog check runs affects the program's timing.

Scenario Under Consideration

Analyze the time complexity of the following embedded C code snippet using a window watchdog.


void WindowWatchdog_Refresh(void) {
    if (IsWithinWindow()) {
        RefreshWatchdog();
    }
}

void MainLoop(void) {
    while(1) {
        WindowWatchdog_Refresh();
        PerformTasks();
    }
}
    

This code refreshes the watchdog only if the current time is within a safe window, inside an infinite main loop.

Identify Repeating Operations

Look for repeated actions that affect timing.

  • Primary operation: The call to WindowWatchdog_Refresh() inside the infinite loop.
  • How many times: It runs continuously, repeating as long as the device is on.
How Execution Grows With Input

The loop runs forever, so the number of operations grows linearly with time, not input size.

Input Size (n)Approx. Operations
10 (time units)10 calls to WindowWatchdog_Refresh
100 (time units)100 calls
1000 (time units)1000 calls

Pattern observation: The number of calls grows directly with time passed, like counting seconds.

Final Time Complexity

Time Complexity: O(n)

This means the number of operations grows directly with the running time or loop iterations.

Common Mistake

[X] Wrong: "The watchdog refresh runs only once, so it has constant time."

[OK] Correct: The refresh happens repeatedly inside the infinite loop, so it grows with time, not just once.

Interview Connect

Understanding how often watchdog refreshes run helps you reason about timing and reliability in embedded systems.

Self-Check

"What if the watchdog refresh was called only after a fixed delay instead of every loop? How would the time complexity change?"