0
0
FreeRTOSprogramming~5 mins

vTaskPrioritySet() dynamic priority in FreeRTOS - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: vTaskPrioritySet() dynamic priority
O(P)
Understanding Time Complexity

When changing a task's priority dynamically in FreeRTOS, it's important to know how the time taken grows as the system has more tasks.

We want to understand how the cost of setting a new priority changes with the number of tasks.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


// Change the priority of a task dynamically
void changeTaskPriority(TaskHandle_t task, UBaseType_t newPriority) {
    vTaskPrioritySet(task, newPriority);
}
    

This code changes the priority of a given task to a new level during runtime.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: The function internally may check the ready lists for tasks at different priorities.
  • How many times: It scans through priority levels, which depends on the total number of priority levels configured.
How Execution Grows With Input

Changing a task's priority involves checking priority lists. The time grows with the number of priority levels, not the number of tasks.

Input Size (priority levels)Approx. Operations
55 checks
1010 checks
2020 checks

Pattern observation: The cost grows linearly with the number of priority levels, not tasks.

Final Time Complexity

Time Complexity: O(P)

This means the time to set a task's priority grows linearly with the number of priority levels configured in the system.

Common Mistake

[X] Wrong: "Changing a task's priority takes time proportional to the total number of tasks in the system."

[OK] Correct: The operation depends on the number of priority levels, which is usually fixed and small, not on how many tasks exist.

Interview Connect

Understanding how task priority changes scale helps you reason about real-time system responsiveness and scheduling efficiency.

Self-Check

"What if the system had a very large number of priority levels? How would that affect the time complexity of vTaskPrioritySet()?"