Bird
Raised Fist0
FreeRTOSprogramming~10 mins

vTaskPrioritySet() dynamic priority in FreeRTOS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - vTaskPrioritySet() dynamic priority
Start with Task at Priority P
↓
Call vTaskPrioritySet(Task, NewPriority)
↓
Check if NewPriority is valid
↓
Update Task Priority
↓
Scheduler adjusts task order
↓
Task runs with new priority
This flow shows how calling vTaskPrioritySet changes a task's priority dynamically, then the scheduler updates task order accordingly.
Execution Sample
FreeRTOS
vTaskPrioritySet(xTaskHandle, 3);
// Task priority changes from 1 to 3
// Scheduler reorders tasks based on new priority
This code changes a task's priority from 1 to 3 dynamically at runtime.
Execution Table
StepActionCurrent PriorityNew PriorityScheduler ReactionResult
1Task created1-Task added to ready list at priority 1Task priority = 1
2Call vTaskPrioritySet(xTaskHandle, 3)13Scheduler reorders tasksTask priority updated to 3
3Scheduler runs3-Task moved to higher priority queueTask runs sooner
4Call vTaskPrioritySet(xTaskHandle, 0)30Scheduler reorders tasksTask priority updated to 0 (lowest)
5Scheduler runs0-Task moved to lowest priority queueTask runs later
6Call vTaskPrioritySet(xTaskHandle, 6)06Scheduler reorders tasksTask priority updated to 6 (highest)
7Scheduler runs6-Task moved to highest priority queueTask runs immediately
8Call vTaskPrioritySet(xTaskHandle, 10)610Invalid priority (above max)Priority unchanged, error handled
9End---Execution stops
💡 Execution stops after invalid priority attempt or normal completion
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 8Final
Task Priority130666
Key Moments - 3 Insights
Why doesn't the task priority change when an invalid priority is set?
Because vTaskPrioritySet checks if the new priority is within valid limits before updating. See execution_table row 8 where priority 10 is invalid, so priority stays at 6.
How does changing priority affect when the task runs?
Scheduler moves the task to the queue matching its new priority. Higher priority tasks run sooner. See rows 3, 5, and 7 for scheduler reactions.
Can a task priority be set lower than zero?
No, priorities below zero are invalid and ignored, similar to above max priority. This prevents errors and keeps scheduler stable.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the task priority after vTaskPrioritySet is called?
A3
B6
C0
D1
💡 Hint
Check the 'New Priority' and 'Result' columns at step 4 in execution_table.
At which step does the scheduler move the task to the highest priority queue?
AStep 7
BStep 5
CStep 3
DStep 2
💡 Hint
Look for 'Task moved to highest priority queue' in the Scheduler Reaction column.
If you try to set a task priority above the max allowed, what happens according to the execution_table?
APriority is set to max allowed
BPriority remains unchanged and error is handled
CPriority is set to zero
DTask is deleted
💡 Hint
See step 8 in execution_table for handling invalid priority.
Concept Snapshot
vTaskPrioritySet(TaskHandle, NewPriority)
- Changes task priority dynamically
- Scheduler reorders tasks immediately
- NewPriority must be valid (0 to configMAX_PRIORITIES-1)
- Higher priority tasks run before lower ones
- Invalid priorities are ignored
Full Transcript
This visual trace shows how vTaskPrioritySet changes a FreeRTOS task's priority at runtime. Starting with a task at priority 1, calling vTaskPrioritySet with a new priority updates the task's priority if valid. The scheduler then reorders tasks so higher priority tasks run sooner. If an invalid priority is given, the change is ignored to keep system stability. The variable tracker shows the task priority changing step-by-step. Key moments clarify why invalid priorities don't change the task and how priority affects scheduling. The quiz tests understanding of priority changes and scheduler reactions.

Practice

(1/5)
1.

What does the FreeRTOS function vTaskPrioritySet() do?

easy
A. It changes the priority of a task while the program is running.
B. It creates a new task with a fixed priority.
C. It deletes a task permanently.
D. It suspends a task temporarily.

Solution

  1. Step 1: Understand the function purpose

    vTaskPrioritySet() is designed to change the priority of an existing task dynamically during runtime.
  2. Step 2: Compare with other task functions

    Creating, deleting, or suspending tasks are done by different functions, not vTaskPrioritySet().
  3. Final Answer:

    It changes the priority of a task while the program is running. -> Option A
  4. Quick Check:

    vTaskPrioritySet() changes priority [OK]
Hint: Remember: vTaskPrioritySet changes priority dynamically [OK]
Common Mistakes:
  • Confusing vTaskPrioritySet with task creation functions
  • Thinking it deletes or suspends tasks
  • Assuming priority is fixed at task creation
2.

Which of the following is the correct syntax to change a task's priority using vTaskPrioritySet()?

TaskHandle_t xTaskHandle;
// Change priority to 3
_____;
easy
A. vTaskPrioritySetPriority(xTaskHandle, 3);
B. vTaskPrioritySet(xTaskHandle, 3);
C. vTaskPrioritySet(3, xTaskHandle);
D. vTaskSetPriority(xTaskHandle, 3);

Solution

  1. Step 1: Recall the function signature

    The correct function is vTaskPrioritySet(TaskHandle_t xTask, UBaseType_t uxNewPriority), so the task handle comes first, then the new priority.
  2. Step 2: Check the options

    vTaskPrioritySet(xTaskHandle, 3); matches the correct syntax exactly. Options A, B, and D either swap parameters or use incorrect function names.
  3. Final Answer:

    vTaskPrioritySet(xTaskHandle, 3); -> Option B
  4. Quick Check:

    Correct syntax = vTaskPrioritySet(task, priority) [OK]
Hint: Function name and parameter order matter: task handle first [OK]
Common Mistakes:
  • Swapping the order of parameters
  • Using incorrect function names
  • Passing priority before task handle
3.

Consider the following code snippet:

TaskHandle_t xTask1;
// Assume xTask1 is created with priority 2
vTaskPrioritySet(xTask1, 5);
UBaseType_t newPriority = uxTaskPriorityGet(xTask1);
printf("%u", newPriority);

What will be printed?

medium
A. 0
B. 2
C. 5
D. Compilation error

Solution

  1. Step 1: Understand priority change

    The call vTaskPrioritySet(xTask1, 5) changes the task's priority from 2 to 5.
  2. Step 2: Check the priority retrieval

    uxTaskPriorityGet(xTask1) returns the current priority, which is now 5.
  3. Final Answer:

    5 -> Option C
  4. Quick Check:

    Priority updated to 5, so output is 5 [OK]
Hint: Priority changes immediately, so get returns new value [OK]
Common Mistakes:
  • Assuming priority stays the same after set
  • Confusing priority with task handle
  • Thinking printf prints task handle
4.

What is wrong with this code snippet?

TaskHandle_t xTask2 = NULL;
vTaskPrioritySet(xTask2, 4);
medium
A. The function vTaskPrioritySet returns a value that is ignored.
B. Priority 4 is invalid; priorities must be 0 or 1 only.
C. vTaskPrioritySet requires a pointer to the task, not the handle.
D. xTask2 is NULL, so setting priority causes undefined behavior.

Solution

  1. Step 1: Check the task handle value

    The variable xTask2 is set to NULL, meaning it does not point to a valid task.
  2. Step 2: Understand effect of NULL handle

    Calling vTaskPrioritySet() with a NULL handle leads to undefined behavior or runtime error because no valid task exists to change priority.
  3. Final Answer:

    xTask2 is NULL, so setting priority causes undefined behavior. -> Option D
  4. Quick Check:

    NULL task handle causes error [OK]
Hint: Never pass NULL as task handle to vTaskPrioritySet [OK]
Common Mistakes:
  • Ignoring NULL task handle
  • Assuming priority range is only 0 or 1
  • Confusing handle with pointer to handle
5.

You have two tasks: TaskA with priority 3 and TaskB with priority 5. You want TaskA to temporarily run with higher priority than TaskB during a critical section. Which code snippet correctly achieves this?

TaskHandle_t xTaskA, xTaskB;
// Initially priorities: TaskA=3, TaskB=5
// Critical section starts
_____;
// Critical section ends
vTaskPrioritySet(xTaskA, 3);
hard
A. vTaskPrioritySet(xTaskA, 6);
B. vTaskPrioritySet(xTaskB, 2);
C. vTaskPrioritySet(xTaskA, 5);
D. vTaskPrioritySet(xTaskB, 3);

Solution

  1. Step 1: Identify priority requirements

    TaskA must have higher priority than TaskB (which is 5) during the critical section.
  2. Step 2: Choose correct priority change

    Setting TaskA to 6 makes it higher than TaskB's 5. Options A and D lower TaskB's priority instead. vTaskPrioritySet(xTaskA, 5); sets TaskA to 5, equal to TaskB, not higher.
  3. Step 3: Restore original priority after critical section

    After the critical section, TaskA's priority is reset to 3 as shown.
  4. Final Answer:

    vTaskPrioritySet(xTaskA, 6); -> Option A
  5. Quick Check:

    TaskA priority > TaskB priority during critical section [OK]
Hint: Raise TaskA priority above TaskB, then restore after [OK]
Common Mistakes:
  • Setting TaskA priority equal to TaskB
  • Lowering TaskB priority instead of raising TaskA
  • Not restoring original priority after critical section
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard