Interrupt priority levels in Embedded C - Time & Space Complexity
When working with interrupt priority levels, it's important to understand how the system handles multiple interrupts and how this affects the time spent processing them.
We want to know how the time to handle interrupts grows as more interrupts with different priorities occur.
Analyze the time complexity of the following code snippet.
void ISR_Handler(void) {
if (interrupt_priority == HIGH) {
// Handle high priority interrupt quickly
process_high_priority();
} else {
// Handle low priority interrupt
process_low_priority();
}
}
This code handles interrupts by checking their priority and processing them accordingly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Handling each interrupt as it occurs.
- How many times: Once per interrupt event, but interrupts can happen many times.
Each interrupt is handled individually. If more interrupts happen, the total time spent handling them grows proportionally.
| Number of Interrupts (n) | Approx. Handling Time |
|---|---|
| 10 | Time to handle 10 interrupts |
| 100 | Time to handle 100 interrupts |
| 1000 | Time to handle 1000 interrupts |
Pattern observation: The total handling time grows directly with the number of interrupts.
Time Complexity: O(n)
This means the time to handle interrupts grows linearly with the number of interrupts received.
[X] Wrong: "Interrupts with higher priority take more time and slow down the system exponentially."
[OK] Correct: Each interrupt is handled individually and quickly; priority only decides order, not exponential time increase.
Understanding how interrupt priority affects processing time helps you design responsive embedded systems and shows you can reason about real-time behavior.
"What if interrupts could preempt each other multiple times? How would that affect the time complexity?"