Bird
Raised Fist0
FreeRTOSprogramming~20 mins

Priority-based scheduling in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Priority Scheduling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of task execution order with different priorities

Consider three FreeRTOS tasks with priorities 2, 3, and 1 respectively. Each task prints its name once when it runs. What is the order of printed task names?

FreeRTOS
void Task1(void *pvParameters) {
    printf("Task1\n");
    vTaskDelete(NULL);
}
void Task2(void *pvParameters) {
    printf("Task2\n");
    vTaskDelete(NULL);
}
void Task3(void *pvParameters) {
    printf("Task3\n");
    vTaskDelete(NULL);
}

int main() {
    xTaskCreate(Task1, "Task1", 1000, NULL, 2, NULL);
    xTaskCreate(Task2, "Task2", 1000, NULL, 3, NULL);
    xTaskCreate(Task3, "Task3", 1000, NULL, 1, NULL);
    vTaskStartScheduler();
    return 0;
}
ATask1\nTask3\nTask2\n
BTask2\nTask1\nTask3\n
CTask3\nTask1\nTask2\n
DTask1\nTask2\nTask3\n
Attempts:
2 left
💡 Hint

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

❓ Predict Output
intermediate
2:00remaining
Output when two tasks have the same priority

Two FreeRTOS tasks have the same priority and both are ready to run. Each prints its name once. What is the possible output order?

FreeRTOS
void TaskA(void *pvParameters) {
    printf("TaskA\n");
    vTaskDelete(NULL);
}
void TaskB(void *pvParameters) {
    printf("TaskB\n");
    vTaskDelete(NULL);
}

int main() {
    xTaskCreate(TaskA, "TaskA", 1000, NULL, 2, NULL);
    xTaskCreate(TaskB, "TaskB", 1000, NULL, 2, NULL);
    vTaskStartScheduler();
    return 0;
}
ANo output, deadlock
BTaskA\nTaskB\n
CTaskB\nTaskA\n
DEither TaskA\nTaskB\n or TaskB\nTaskA\n
Attempts:
2 left
💡 Hint

Tasks with the same priority are scheduled round-robin.

🔧 Debug
advanced
2:00remaining
Identify the cause of priority inversion

Given three tasks with priorities 3 (high), 2 (medium), and 1 (low). The low priority task holds a mutex needed by the high priority task. The medium priority task runs continuously. What problem occurs and why?

FreeRTOS
/*
TaskLow (priority 1) locks mutex and starts long processing
TaskHigh (priority 3) tries to lock mutex and blocks
TaskMedium (priority 2) runs continuously
*/
ANo problem, TaskHigh preempts others immediately
BDeadlock because TaskHigh and TaskLow wait on each other
CPriority inversion because TaskMedium prevents TaskLow from running and releasing mutex
DStarvation of TaskMedium because TaskHigh blocks it
Attempts:
2 left
💡 Hint

Think about how medium priority tasks affect low priority tasks holding resources needed by high priority tasks.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in task creation

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

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

Check the parameter types and count for xTaskCreate.

🚀 Application
expert
2:00remaining
Determine the number of context switches in priority scheduling

In a FreeRTOS system, three tasks with priorities 3, 2, and 1 are ready. The highest priority task runs for 5 ticks, then blocks. The next highest runs for 3 ticks, then blocks. The lowest priority task runs for 4 ticks. How many context switches occur in total?

A4
B3
C5
D6
Attempts:
2 left
💡 Hint

Count switches when tasks block and scheduler switches to next ready task.

Practice

(1/5)
1.

What does priority-based scheduling in FreeRTOS ensure?

easy
A. Tasks run randomly without any priority
B. Tasks run in the order they were created
C. The highest priority task runs first
D. All tasks run equally regardless of priority

Solution

  1. Step 1: Understand priority-based scheduling

    Priority-based scheduling means tasks with higher priority get CPU time before lower priority tasks.
  2. Step 2: Apply this to FreeRTOS task execution

    FreeRTOS runs the highest priority task that is ready to run first, preempting lower priority tasks.
  3. Final Answer:

    The highest priority task runs first -> Option C
  4. Quick Check:

    Priority-based scheduling = highest priority runs first [OK]
Hint: Highest priority task always runs first in FreeRTOS [OK]
Common Mistakes:
  • Thinking tasks run in creation order
  • Assuming equal CPU time for all tasks
  • Believing tasks run randomly
2.

Which of the following is the correct way to set a task priority when creating a task in FreeRTOS?

xTaskCreate(TaskFunction, "TaskName", 1000, NULL, ___, &TaskHandle);
easy
A. task name
B. stack size
C. task handle
D. priority

Solution

  1. Step 1: Identify parameters of xTaskCreate

    The fifth parameter in xTaskCreate is the task priority.
  2. Step 2: Match parameter to correct argument

    Passing 'priority' sets the task's priority level correctly.
  3. Final Answer:

    priority -> Option D
  4. Quick Check:

    Fifth parameter in xTaskCreate = priority [OK]
Hint: Priority is the fifth argument in xTaskCreate [OK]
Common Mistakes:
  • Confusing stack size with priority
  • Using task handle as priority
  • Passing task name instead of priority
3.

Given the following FreeRTOS tasks with priorities, which task will run first?

Task A priority = 2
Task B priority = 3
Task C priority = 1
medium
A. Task A
B. Task B
C. Task C
D. All run simultaneously

Solution

  1. Step 1: Compare task priorities

    Task B has priority 3, which is higher than Task A (2) and Task C (1).
  2. Step 2: Determine which task runs first

    FreeRTOS runs the highest priority task first, so Task B runs before others.
  3. Final Answer:

    Task B -> Option B
  4. Quick Check:

    Highest priority (3) = Task B [OK]
Hint: Highest number priority runs first in FreeRTOS [OK]
Common Mistakes:
  • Choosing task with lowest priority
  • Assuming tasks run simultaneously
  • Ignoring priority values
4.

Identify the error in this FreeRTOS task creation code snippet:

BaseType_t result = xTaskCreate(TaskFunc, "Task1", 500, NULL, 0, NULL);
if(result != pdPASS) {
    // Handle error
}

What is wrong with the priority value?

medium
A. Priority 0 means lowest priority but is valid
B. Priority 0 is invalid and causes error
C. Priority must be greater than 5
D. Priority should be a negative number

Solution

  1. Step 1: Understand priority 0 in FreeRTOS

    Priority 0 is the lowest priority and is valid in FreeRTOS.
  2. Step 2: Check if priority 0 causes error

    Priority 0 does not cause an error; task creation can succeed with it.
  3. Final Answer:

    Priority 0 means lowest priority but is valid -> Option A
  4. Quick Check:

    Priority 0 is valid lowest priority [OK]
Hint: Priority 0 is valid and lowest priority in FreeRTOS [OK]
Common Mistakes:
  • Thinking priority 0 is invalid
  • Assuming priority must be > 5
  • Using negative priority values
5.

You have three tasks with priorities 2, 4, and 3. Task with priority 4 is blocked waiting for an event. Which task will FreeRTOS schedule next?

hard
A. Task with priority 3
B. Task with priority 2
C. Task with priority 4
D. No task runs until priority 4 task is ready

Solution

  1. Step 1: Understand task states and scheduling

    FreeRTOS schedules the highest priority task that is ready (not blocked).
  2. Step 2: Identify ready tasks

    Task with priority 4 is blocked, so it cannot run now. Tasks with priority 3 and 2 are ready.
  3. Step 3: Choose highest priority ready task

    Between priority 3 and 2, priority 3 is higher, so it runs next.
  4. Final Answer:

    Task with priority 3 -> Option A
  5. Quick Check:

    Highest ready priority = 3 [OK]
Hint: Blocked tasks don't run; next highest ready priority runs [OK]
Common Mistakes:
  • Trying to run blocked task
  • Ignoring task states
  • Assuming no task runs if highest priority blocked
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