Bird
Raised Fist0
FreeRTOSprogramming~20 mins

Task priority assignment in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Task 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 code?

Consider the following FreeRTOS code snippet that creates two tasks with different priorities. What will be the output printed by the tasks?

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

void Task1(void *pvParameters) {
    printf("Task1 running with priority %lu\n", (unsigned long)uxTaskPriorityGet(NULL));
    vTaskDelete(NULL);
}

void Task2(void *pvParameters) {
    printf("Task2 running with priority %lu\n", (unsigned long)uxTaskPriorityGet(NULL));
    vTaskDelete(NULL);
}

int main(void) {
    xTaskCreate(Task1, "Task1", 1000, NULL, 2, NULL);
    xTaskCreate(Task2, "Task2", 1000, NULL, 3, NULL);
    vTaskStartScheduler();
    return 0;
}
ATask2 running with priority 2\nTask1 running with priority 3
BTask1 running with priority 2\nTask2 running with priority 3
CTask2 running with priority 3\nTask1 running with priority 2
DTask1 running with priority 3\nTask2 running with priority 2
Attempts:
2 left
💡 Hint

Remember that FreeRTOS runs the highest priority task that is ready first.

🧠 Conceptual
intermediate
1:30remaining
Which FreeRTOS API sets the priority of a running task?

In FreeRTOS, which function is used to change the priority of a task that is already running?

AvTaskPrioritySet(TaskHandle_t xTask, UBaseType_t uxNewPriority)
BvTaskDelete(TaskHandle_t xTask)
CxTaskCreate(TaskFunction_t pxTaskCode, const char * const pcName, uint16_t usStackDepth, void *pvParameters, UBaseType_t uxPriority, TaskHandle_t *pxCreatedTask)
DuxTaskPriorityGet(TaskHandle_t xTask)
Attempts:
2 left
💡 Hint

Look for the function that explicitly mentions setting priority.

🔧 Debug
advanced
2:30remaining
Why does this FreeRTOS task priority code cause starvation?

Examine the code below. Why does Task1 never get to run?

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

void Task1(void *pvParameters) {
    while(1) {
        printf("Task1 running\n");
        vTaskDelay(pdMS_TO_TICKS(100));
    }
}

void Task2(void *pvParameters) {
    while(1) {
        printf("Task2 running\n");
    }
}

int main(void) {
    xTaskCreate(Task1, "Task1", 1000, NULL, 1, NULL);
    xTaskCreate(Task2, "Task2", 1000, NULL, 2, NULL);
    vTaskStartScheduler();
    return 0;
}
ATask1 deletes itself immediately, so it never runs.
BTask1 has higher priority but calls vTaskDelay incorrectly.
CBoth tasks have the same priority causing a deadlock.
DTask2 has higher priority and never blocks, so Task1 starves.
Attempts:
2 left
💡 Hint

Think about task priorities and blocking behavior.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this FreeRTOS task creation code

Which option contains the correct syntax to create a task with priority 5?

AxTaskCreate(TaskFunction, Task, 1000, NULL, 5, NULL);
BxTaskCreate(TaskFunction, "Task", 1000, NULL, 5, NULL);
CxTaskCreate(TaskFunction, "Task", 1000, NULL, "5", NULL);
DxTaskCreate(TaskFunction, "Task", 1000, NULL, 5);
Attempts:
2 left
💡 Hint

Check the parameter types and count.

🚀 Application
expert
3:00remaining
How to dynamically change task priority to avoid starvation?

You have two tasks: TaskA with priority 3 and TaskB with priority 5. TaskB runs continuously and causes TaskA starvation. Which approach correctly changes TaskB's priority dynamically to allow TaskA to run?

AUse vTaskPrioritySet(NULL, 2) inside TaskB to lower its priority temporarily.
BUse vTaskPrioritySet(TaskAHandle, 6) to increase TaskA priority above TaskB.
CDelete TaskB and recreate it with lower priority.
DUse vTaskSuspend(TaskBHandle) to pause TaskB permanently.
Attempts:
2 left
💡 Hint

Think about temporarily lowering the running task's priority.

Practice

(1/5)
1.

In FreeRTOS, what does a higher task priority number mean?

easy
A. The task runs only when idle
B. The task runs after lower priority tasks
C. The task runs before lower priority tasks
D. The task is deleted automatically

Solution

  1. Step 1: Understand task priority meaning

    In FreeRTOS, tasks with higher priority numbers get CPU time before lower priority tasks.
  2. Step 2: Relate priority number to execution order

    A higher number means the task is more important and runs first.
  3. Final Answer:

    The task runs before lower priority tasks -> Option C
  4. Quick Check:

    Higher priority = runs first [OK]
Hint: Higher number means higher priority, runs first [OK]
Common Mistakes:
  • Thinking lower number means higher priority
  • Confusing priority with task state
  • Assuming priority controls memory usage
2.

Which of the following is the correct way to assign a priority of 3 to a task in FreeRTOS using xTaskCreate?

void vTaskFunction(void *pvParameters) { /* Task code */ }

xTaskCreate(vTaskFunction, "Task1", 1000, NULL, ?, NULL);
easy
A. taskPriority = 3
B. "3"
C. priority = 3
D. 3

Solution

  1. Step 1: Check xTaskCreate parameters

    The fifth parameter is the task priority and expects an integer value.
  2. Step 2: Identify correct priority assignment

    Passing 3 directly as an integer is correct; strings or assignments are invalid here.
  3. Final Answer:

    3 -> Option D
  4. Quick Check:

    Priority parameter = integer value [OK]
Hint: Priority is an integer, no quotes or assignments [OK]
Common Mistakes:
  • Using quotes around priority number
  • Writing assignment expressions inside parameters
  • Passing variable names without declaration
3.

Given the following FreeRTOS code snippet, which task will run first?

void TaskA(void *pvParameters) { while(1) {} }
void TaskB(void *pvParameters) { while(1) {} }

xTaskCreate(TaskA, "A", 1000, NULL, 2, NULL);
xTaskCreate(TaskB, "B", 1000, NULL, 4, NULL);
medium
A. TaskB
B. TaskA
C. Both run simultaneously
D. Neither runs

Solution

  1. Step 1: Identify task priorities

    TaskA has priority 2, TaskB has priority 4.
  2. Step 2: Determine which runs first

    TaskB has higher priority, so it runs before TaskA.
  3. Final Answer:

    TaskB -> Option A
  4. Quick Check:

    Higher priority task runs first [OK]
Hint: Higher priority number runs first [OK]
Common Mistakes:
  • Assuming tasks run in creation order
  • Thinking equal priority means simultaneous run
  • Ignoring priority values
4.

What is wrong with this FreeRTOS task creation code snippet?

void vTask(void *pvParameters) { /* code */ }

xTaskCreate(vTask, "Task", 1000, NULL, 5, NULL);
xTaskCreate(vTask, "Task", 1000, NULL, -1, NULL);
medium
A. Priority cannot be negative
B. Task name must be unique
C. Stack size must be zero
D. Task function must return int

Solution

  1. Step 1: Check priority values

    FreeRTOS priorities must be zero or positive integers; negative values are invalid.
  2. Step 2: Validate other parameters

    Task name can be reused, stack size 1000 is valid, and task function returns void as required.
  3. Final Answer:

    Priority cannot be negative -> Option A
  4. Quick Check:

    Priority >= 0 required [OK]
Hint: Priority must be zero or positive [OK]
Common Mistakes:
  • Using negative priority values
  • Thinking task names must be unique
  • Expecting task function to return int
5.

You have three tasks with priorities 1, 3, and 3. How should you assign priorities to ensure the most important task runs first and the least important runs last?

Choose the best priority assignment:

  • A: Task1=1, Task2=3, Task3=3
  • B: Task1=3, Task2=3, Task3=1
  • C: Task1=3, Task2=1, Task3=3
  • D: Task1=1, Task2=1, Task3=3
hard
A. Task1=1 (lowest), Task2=3, Task3=3 (highest)
B. Task1=3 (highest), Task2=3, Task3=1 (lowest)
C. Task1=3 (highest), Task2=1, Task3=3 (highest)
D. Task1=1, Task2=1, Task3=3 (highest)

Solution

  1. Step 1: Identify importance order

    The most important task should have highest priority (3), least important lowest (1).
  2. Step 2: Match priorities to tasks

    Task1=3 (highest), Task2=3, Task3=1 (lowest) assigns highest priority (3) to Task1 and Task2, and lowest (1) to Task3, ensuring important tasks run first.
  3. Final Answer:

    Task1=3 (highest), Task2=3, Task3=1 (lowest) -> Option B
  4. Quick Check:

    Highest priority = most important task [OK]
Hint: Assign highest number to most important task [OK]
Common Mistakes:
  • Assigning same priority to all tasks
  • Giving lowest priority to important tasks
  • Ignoring task importance order
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