Event-driven architecture in FreeRTOS - Time & Space Complexity
When using event-driven architecture in FreeRTOS, tasks wait for events to happen before running. We want to understand how the time to handle events changes as more events occur.
How does the system's work grow when more events are sent and processed?
Analyze the time complexity of the following code snippet.
// Task waits for events and processes them
void vEventTask(void *pvParameters) {
EventBits_t uxBits;
for (;;) {
uxBits = xEventGroupWaitBits(xEventGroup, EVENT_1 | EVENT_2, pdTRUE, pdFALSE, portMAX_DELAY);
if (uxBits & EVENT_1) {
// Handle EVENT_1
}
if (uxBits & EVENT_2) {
// Handle EVENT_2
}
}
}
This task waits for one or more events and then processes each event that occurred.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The infinite loop that waits for events and processes them.
- How many times: The loop runs forever, processing events as they come.
Each time an event occurs, the task wakes and processes it. If more events happen at once, the task processes each one in turn.
| Input Size (number of events) | Approx. Operations |
|---|---|
| 1 | 1 event processed |
| 5 | 5 events processed |
| 10 | 10 events processed |
Pattern observation: The work grows directly with the number of events to handle.
Time Complexity: O(n)
This means the time to process events grows linearly with the number of events received.
[X] Wrong: "The task processes all events instantly, so time does not grow with more events."
[OK] Correct: Each event needs some time to be handled, so more events mean more total processing time.
Understanding how event-driven tasks scale helps you design responsive and efficient embedded systems. This skill shows you can think about system behavior as workload changes.
"What if the task handled events using multiple smaller tasks instead of one? How would the time complexity change?"