Consider two tasks created with different priorities. What will be the output order of the print statements?
void Task1(void *pvParameters) {
printf("Task 1 running\n");
printf("Task 1 finished\n");
vTaskDelete(NULL);
}
void Task2(void *pvParameters) {
printf("Task 2 running\n");
printf("Task 2 finished\n");
vTaskDelete(NULL);
}
int main() {
xTaskCreate(Task1, "Task1", 1000, NULL, 2, NULL);
xTaskCreate(Task2, "Task2", 1000, NULL, 3, NULL);
vTaskStartScheduler();
return 0;
}Remember, higher priority tasks preempt lower priority ones immediately.
Task2 has priority 3, higher than Task1's priority 2. So Task2 runs first, prints its messages, then Task1 runs.
You have three tasks: A (high priority), B (medium priority), and C (low priority). Task C holds a resource needed by Task A. Which priority assignment helps avoid priority inversion?
Think about priority inheritance to prevent blocking.
Temporarily boosting Task C's priority prevents it from being preempted by Task B, avoiding priority inversion.
Given three tasks with priorities 1, 2, and 3, the system freezes after startup. What is the likely cause?
void TaskLow(void *pvParameters) {
while(1) {
// Does some work
}
}
void TaskMedium(void *pvParameters) {
while(1) {
// Does some work
}
}
void TaskHigh(void *pvParameters) {
while(1) {
// Does some work
}
}
int main() {
xTaskCreate(TaskLow, "Low", 1000, NULL, 1, NULL);
xTaskCreate(TaskMedium, "Medium", 1000, NULL, 2, NULL);
xTaskCreate(TaskHigh, "High", 1000, NULL, 3, NULL);
vTaskStartScheduler();
return 0;
}Consider how higher priority tasks affect lower priority ones.
TaskHigh runs continuously without delay, starving lower priority tasks and freezing system progress.
Choose the correct way to set a task's priority to 5 after creation.
TaskHandle_t xHandle = NULL; xTaskCreate(TaskFunction, "Task", 1000, NULL, 3, &xHandle); // Set priority here
Check the FreeRTOS API naming conventions.
The correct API to set a task priority is vTaskPrioritySet().
Three tasks are created with priorities 4, 3, and 2. Task with priority 4 calls vTaskDelay(100) in its loop. How many tasks run concurrently during the delay?
void TaskHigh(void *pvParameters) {
while(1) {
// Do work
vTaskDelay(pdMS_TO_TICKS(100));
}
}
void TaskMid(void *pvParameters) {
while(1) {
// Do work
}
}
void TaskLow(void *pvParameters) {
while(1) {
// Do work
}
}
int main() {
xTaskCreate(TaskHigh, "High", 1000, NULL, 4, NULL);
xTaskCreate(TaskMid, "Mid", 1000, NULL, 3, NULL);
xTaskCreate(TaskLow, "Low", 1000, NULL, 2, NULL);
vTaskStartScheduler();
return 0;
}Remember that vTaskDelay makes the task blocked, allowing lower priority tasks to run.
When TaskHigh delays, it is blocked. TaskMid (priority 3) runs next and never blocks or yields, starving TaskLow (priority 2).