0
0
FreeRTOSprogramming~20 mins

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

Choose your learning style9 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.