Bird
Raised Fist0
FreeRTOSprogramming~10 mins

Priority-based scheduling in FreeRTOS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Priority-based scheduling
Ready Tasks Queue
↓
Check Highest Priority Task
↓
Run Highest Priority Task
↓
Task Completes or Blocks
↓
Update Ready Queue
↩Back to Check Highest Priority Task
The scheduler always picks the ready task with the highest priority to run next. When a task finishes or blocks, the scheduler updates the queue and picks the next highest priority task.
Execution Sample
FreeRTOS
void vTask1(void *pvParameters) {
  for(;;) {
    // Task 1 code
    vTaskDelay(100 / portTICK_PERIOD_MS);
  }
}

void vTask2(void *pvParameters) {
  for(;;) {
    // Task 2 code
    vTaskDelay(50 / portTICK_PERIOD_MS);
  }
}
Two tasks run with different priorities; the scheduler runs the higher priority task first whenever it is ready.
Execution Table
StepReady TasksHighest Priority TaskActionOutput/State
1Task1(Priority 2), Task2(Priority 1)Task1Run Task1Task1 runs
2Task1 (delayed), Task2 (ready)Task2Run Task2Task2 runs
3Task1 (ready), Task2 (delayed)Task1Run Task1Task1 runs
4Task1 (delayed), Task2 (ready)Task2Run Task2Task2 runs
5Task1 (ready), Task2 (delayed)Task1Run Task1Task1 runs
6Task1 (delayed), Task2 (ready)Task2Run Task2Task2 runs
7Task1 (ready), Task2 (delayed)Task1Run Task1Task1 runs
8Task1 (delayed), Task2 (ready)Task2Run Task2Task2 runs
9Task1 (ready), Task2 (delayed)Task1Run Task1Task1 runs
10Task1 (delayed), Task2 (ready)Task2Run Task2Task2 runs
11No ready tasksNoneIdleCPU idle, waiting for tasks
ExitNo ready tasksNoneStopNo tasks ready to run
💡 No tasks are ready; scheduler idles or waits for interrupts.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8After 9After 10Final
Task1 StateReadyRunningDelayedReadyDelayedReadyDelayedReadyDelayedReadyDelayedIdle
Task2 StateReadyReadyRunningDelayedReadyDelayedReadyDelayedReadyDelayedReadyIdle
Key Moments - 3 Insights
Why does Task2 run immediately after Task1 delays, even though Task1 has higher priority?
Because Task1 is delayed (blocked), it is not ready to run. The scheduler picks the highest priority task that is ready, which is Task2 at that moment (see execution_table rows 2 and 4).
What happens if both tasks are ready at the same time?
The scheduler always runs the task with the higher priority first. Here, Task1 has priority 2 and Task2 has priority 1, so Task1 runs first whenever both are ready (see execution_table rows 1, 3, 5).
Why does the CPU become idle at the end?
When no tasks are ready to run (all delayed or blocked), the scheduler idles the CPU waiting for an event or interrupt (see execution_table row 11).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which task runs at Step 3?
ATask2
BTask1
CNo task runs
DBoth tasks run simultaneously
💡 Hint
Check the 'Highest Priority Task' column at Step 3 in the execution_table.
At which step does the CPU become idle according to the execution table?
AStep 11
BStep 9
CStep 10
DStep 1
💡 Hint
Look for the row where 'Highest Priority Task' is 'None' and action is 'Idle'.
If Task1 priority was lower than Task2, which task would run first when both are ready?
AThey run alternately
BTask1
CTask2
DCPU idles
💡 Hint
Priority-based scheduling always runs the highest priority ready task first (see key_moments about priority).
Concept Snapshot
Priority-based scheduling in FreeRTOS:
- Scheduler picks the ready task with highest priority.
- If higher priority task is blocked, lower priority runs.
- Tasks run until they block, delay, or yield.
- CPU idles if no tasks are ready.
- Priorities are numeric; higher number means higher priority.
Full Transcript
Priority-based scheduling in FreeRTOS means the scheduler always runs the ready task with the highest priority number. If that task blocks or delays, the scheduler picks the next highest priority ready task. Tasks run in a loop, and when no tasks are ready, the CPU idles. This example shows two tasks with different priorities running alternately as they delay. The execution table traces which task runs at each step, and the variable tracker shows task states changing from ready to running to delayed. Key moments clarify why lower priority tasks run only when higher priority ones are blocked. The visual quiz tests understanding of task selection and CPU idling based on priority and readiness.

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