0
0
FreeRTOSprogramming~5 mins

Multiple tasks running concurrently in FreeRTOS - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Multiple tasks running concurrently
O(n)
Understanding Time Complexity

When multiple tasks run at the same time in FreeRTOS, it's important to understand how the total work grows as we add more tasks.

We want to know how the time to manage all tasks changes when the number of tasks increases.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


void vTask1(void *pvParameters) {
    while(1) {
        // Task 1 work
        vTaskDelay(100 / portTICK_PERIOD_MS);
    }
}

void vTask2(void *pvParameters) {
    while(1) {
        // Task 2 work
        vTaskDelay(100 / portTICK_PERIOD_MS);
    }
}

int main() {
    xTaskCreate(vTask1, "Task1", 1000, NULL, 1, NULL);
    xTaskCreate(vTask2, "Task2", 1000, NULL, 1, NULL);
    vTaskStartScheduler();
    return 0;
}
    

This code creates two tasks that run repeatedly and share CPU time managed by FreeRTOS.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Each task runs an infinite loop doing work and then delaying.
  • How many times: Each task repeats its loop forever, switching happens many times.
How Execution Grows With Input

As we add more tasks, the scheduler must switch between them more often, increasing the total management work.

Number of Tasks (n)Approx. Scheduler Operations
2Low switching overhead
10More frequent context switches
100Much more switching and management

Pattern observation: The scheduler's work grows roughly in proportion to the number of tasks.

Final Time Complexity

Time Complexity: O(n)

This means the time to manage all tasks grows linearly as we add more tasks.

Common Mistake

[X] Wrong: "Adding more tasks won't affect performance because tasks run independently."

[OK] Correct: The scheduler must switch between all tasks, so more tasks mean more switching and overhead.

Interview Connect

Understanding how task management scales helps you design efficient multitasking systems and shows you grasp real-time system behavior.

Self-Check

"What if tasks had different priorities? How would that affect the time complexity of scheduling?"