Consider the following FreeRTOS code snippet where a task's priority is changed dynamically. What will be the printed output?
#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; }
Remember that vTaskPrioritySet() changes the priority of the task immediately.
The task is created with priority 3. The call to vTaskPrioritySet() increases it by 2, so the new priority is 5.
In FreeRTOS, what is the behavior when vTaskPrioritySet() is called with a priority value greater than configMAX_PRIORITIES - 1?
FreeRTOS limits task priorities to a maximum defined by configMAX_PRIORITIES.
FreeRTOS clamps the priority to the maximum allowed value if a higher value is requested.
Examine the code below. It attempts to change the priority of a task but causes a runtime error. What is the cause?
void vTaskFunction(void *pvParameters) {
vTaskPrioritySet(NULL, 10);
for(;;) {}
}
int main(void) {
xTaskCreate(vTaskFunction, "Task", configMINIMAL_STACK_SIZE, NULL, 5, NULL);
vTaskStartScheduler();
return 0;
}Check the maximum allowed priority in FreeRTOS configuration.
Setting a priority higher than configMAX_PRIORITIES - 1 is invalid and can cause runtime errors.
Given a task handle xTaskHandle, which code snippet correctly changes its priority to 4?
Check the function signature: void vTaskPrioritySet(TaskHandle_t xTask, UBaseType_t uxNewPriority);
The first argument is the task handle, the second is the new priority value.
Given the following code, how many tasks will have priority 3 after execution?
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;
}Check which tasks had their priority changed to 3.
Initially, Task2 has priority 3. Then Task1 and Task3 priorities are set to 3. So total tasks with priority 3 are 3.
But Task2 was already priority 3, so after changes, Task1 and Task3 also have priority 3, making 3 tasks total.