Bird
Raised Fist0
FreeRTOSprogramming~10 mins

Task priority assignment in FreeRTOS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Task priority assignment
Create Task
↓
Assign Priority
↓
Add to Ready List
↓
Scheduler Chooses Highest Priority Task
↓
Task Runs
↓
Task Completes or Yields
↓
Scheduler Picks Next Highest Priority Task
When a task is created, it is given a priority. The scheduler always runs the highest priority task that is ready.
Execution Sample
FreeRTOS
xTaskCreate(task1, "Task1", 1000, NULL, 2, NULL);
xTaskCreate(task2, "Task2", 1000, NULL, 3, NULL);
// Scheduler runs task2 first because priority 3 > 2
Creates two tasks with different priorities; the scheduler runs the higher priority task first.
Execution Table
StepActionTask1 PriorityTask2 PriorityScheduler DecisionRunning Task
1Create Task12N/ANo tasks ready yetNone
2Create Task223Task2 has higher priorityTask2
3Task2 runs23Task2 runningTask2
4Task2 yields/completes23Task1 is next highestTask1
5Task1 runs23Task1 runningTask1
6Task1 completes23No tasks readyNone
💡 All tasks completed or no ready tasks left
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
Task1 PriorityN/A2222
Task2 PriorityN/AN/A333
Running TaskNoneNoneTask2Task1None
Key Moments - 2 Insights
Why does Task2 run before Task1 even though Task1 was created first?
Because Task2 has a higher priority (3) than Task1 (2), the scheduler always runs the highest priority ready task first as shown in step 2 of the execution_table.
What happens when the running task yields or completes?
The scheduler picks the next highest priority ready task to run, as seen in step 4 where Task2 yields and Task1 starts running.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which task is running at step 3?
ATask1
BTask2
CNone
DBoth
💡 Hint
Check the 'Running Task' column at step 3 in the execution_table.
At which step does the scheduler switch from Task2 to Task1?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Scheduler Decision' and 'Running Task' columns around steps 3 to 5.
If Task1 had priority 4 instead of 2, which task would run first after creation?
ATask1
BTask2
CNone
DBoth at the same time
💡 Hint
Refer to the 'Task1 Priority' and 'Task2 Priority' columns in variable_tracker and execution_table.
Concept Snapshot
Task priority assignment in FreeRTOS:
- Assign priority when creating a task.
- Scheduler runs highest priority ready task.
- Higher number means higher priority.
- Tasks with same priority share CPU time.
- Yielding or completing switches to next highest priority task.
Full Transcript
In FreeRTOS, when you create a task, you assign it a priority number. The scheduler always runs the task with the highest priority that is ready to run. For example, if Task1 has priority 2 and Task2 has priority 3, Task2 will run first because 3 is higher than 2. When Task2 finishes or yields, the scheduler switches to Task1. This process continues until all tasks complete or no tasks are ready. The priority number controls which task runs first, not the order tasks are created.

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