Bird
Raised Fist0
FreeRTOSprogramming~20 mins

vTaskPrioritySet() dynamic priority in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Priority Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this FreeRTOS task priority change?

Consider the following FreeRTOS code snippet where a task's priority is changed dynamically. What will be the printed output?

FreeRTOS
#include "FreeRTOS.h"
#include "task.h"
#include <stdio.h>

void vTaskFunction(void *pvParameters) {
    UBaseType_t initialPriority = uxTaskPriorityGet(NULL);
    printf("Initial Priority: %u\n", (unsigned int)initialPriority);
    vTaskPrioritySet(NULL, initialPriority + 2);
    UBaseType_t newPriority = uxTaskPriorityGet(NULL);
    printf("New Priority: %u\n", (unsigned int)newPriority);
    vTaskDelete(NULL);
}

int main(void) {
    TaskHandle_t xHandle = NULL;
    xTaskCreate(vTaskFunction, "Task", configMINIMAL_STACK_SIZE, NULL, 3, &xHandle);
    vTaskStartScheduler();
    return 0;
}
A
Initial Priority: 3
New Priority: 5
B
Initial Priority: 3
New Priority: 3
C
Initial Priority: 0
New Priority: 2
D
Initial Priority: 5
New Priority: 7
Attempts:
2 left
💡 Hint

Remember that vTaskPrioritySet() changes the priority of the task immediately.

🧠 Conceptual
intermediate
1:30remaining
What happens if you set a task priority higher than configMAX_PRIORITIES?

In FreeRTOS, what is the behavior when vTaskPrioritySet() is called with a priority value greater than configMAX_PRIORITIES - 1?

AThe system crashes with a runtime error.
BThe priority wraps around to zero.
CThe priority is set to the maximum allowed priority, <code>configMAX_PRIORITIES - 1</code>.
DThe priority is set to the requested value without any limit.
Attempts:
2 left
💡 Hint

FreeRTOS limits task priorities to a maximum defined by configMAX_PRIORITIES.

🔧 Debug
advanced
2:30remaining
Why does this code cause a runtime error when changing task priority?

Examine the code below. It attempts to change the priority of a task but causes a runtime error. What is the cause?

FreeRTOS
void vTaskFunction(void *pvParameters) {
    vTaskPrioritySet(NULL, 10);
    for(;;) {}
}

int main(void) {
    xTaskCreate(vTaskFunction, "Task", configMINIMAL_STACK_SIZE, NULL, 5, NULL);
    vTaskStartScheduler();
    return 0;
}
AvTaskStartScheduler() must be called before creating tasks.
BvTaskPrioritySet() cannot be called from within a task function.
CThe task stack size is too small causing stack overflow.
DPriority 10 exceeds configMAX_PRIORITIES, causing invalid priority assignment.
Attempts:
2 left
💡 Hint

Check the maximum allowed priority in FreeRTOS configuration.

📝 Syntax
advanced
1:30remaining
Which option correctly changes a task's priority dynamically?

Given a task handle xTaskHandle, which code snippet correctly changes its priority to 4?

AvTaskPrioritySet(xTaskHandle, 4);
BvTaskPrioritySet(&xTaskHandle, 4);
CvTaskPrioritySet(4, xTaskHandle);
DvTaskPrioritySet(xTaskHandle, &4);
Attempts:
2 left
💡 Hint

Check the function signature: void vTaskPrioritySet(TaskHandle_t xTask, UBaseType_t uxNewPriority);

🚀 Application
expert
3:00remaining
How many tasks have priority 3 after this code runs?

Given the following code, how many tasks will have priority 3 after execution?

FreeRTOS
TaskHandle_t xTask1, xTask2, xTask3;

void vTask1(void *pvParameters) { for(;;) {} }
void vTask2(void *pvParameters) { for(;;) {} }
void vTask3(void *pvParameters) { for(;;) {} }

int main(void) {
    xTaskCreate(vTask1, "Task1", configMINIMAL_STACK_SIZE, NULL, 2, &xTask1);
    xTaskCreate(vTask2, "Task2", configMINIMAL_STACK_SIZE, NULL, 3, &xTask2);
    xTaskCreate(vTask3, "Task3", configMINIMAL_STACK_SIZE, NULL, 1, &xTask3);

    vTaskPrioritySet(xTask1, 3);
    vTaskPrioritySet(xTask3, 3);

    vTaskStartScheduler();
    return 0;
}
A2
B3
C1
D0
Attempts:
2 left
💡 Hint

Check which tasks had their priority changed to 3.

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