Resource manager task pattern in FreeRTOS - Time & Space Complexity
When using a resource manager task in FreeRTOS, it's important to know how the time to handle requests grows as more tasks ask for resources.
We want to see how the work inside the manager changes when the number of requests increases.
Analyze the time complexity of the following code snippet.
void ResourceManagerTask(void *pvParameters) {
ResourceRequest_t request;
for (;;) {
if (xQueueReceive(requestQueue, &request, portMAX_DELAY) == pdPASS) {
// Process request
AllocateResource(request.id);
xQueueSend(responseQueue, &request.id, 0);
}
}
}
This task waits for resource requests from other tasks, processes each request one by one, and sends back a response.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The infinite loop that waits and processes each request from the queue.
- How many times: Once per request received, repeating indefinitely as requests come in.
Each new request causes the task to do a fixed amount of work to allocate and respond.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 resource allocations and responses |
| 100 | 100 resource allocations and responses |
| 1000 | 1000 resource allocations and responses |
Pattern observation: The work grows directly with the number of requests, one by one.
Time Complexity: O(n)
This means the time to handle requests grows linearly as more requests come in.
[X] Wrong: "The resource manager handles all requests instantly, so time does not grow with more requests."
[OK] Correct: Each request must be processed one at a time, so more requests mean more total work and longer total processing time.
Understanding how tasks handle requests helps you design systems that stay responsive as load grows. This skill shows you can think about real-time system behavior clearly.
"What if the resource manager processed multiple requests in parallel? How would the time complexity change?"