Bird
Raised Fist0
FreeRTOSprogramming~10 mins

Choosing priorities for real applications in FreeRTOS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Choosing priorities for real applications
Identify tasks
↓
Assign priorities based on importance
↓
Check for resource conflicts
↓
Adjust priorities to avoid starvation
↓
Implement and test
↓
Monitor and fine-tune
This flow shows how to pick task priorities step-by-step in a real FreeRTOS application.
Execution Sample
FreeRTOS
void TaskA(void *pvParameters) {
  for(;;) {
    // High priority task work
    vTaskDelay(100 / portTICK_PERIOD_MS);
  }
}

void TaskB(void *pvParameters) {
  for(;;) {
    // Low priority task work
    vTaskDelay(200 / portTICK_PERIOD_MS);
  }
}
Two tasks run with different priorities and delays to show priority effects.
Execution Table
StepRunning TaskPriorityActionResult
1TaskAHighStarts runningTaskA runs first
2TaskAHighCalls vTaskDelay(100 / portTICK_PERIOD_MS)TaskA blocks, scheduler runs TaskB
3TaskBLowStarts runningTaskB runs while TaskA is blocked
4TaskBLowCalls vTaskDelay(200 / portTICK_PERIOD_MS)TaskB blocks, no ready tasks
5Idle TaskLowestRunsCPU idle until TaskA delay ends
6TaskAHighDelay ends, becomes readyTaskA runs again
7TaskAHighRepeats cycleTaskA keeps priority advantage
💡 Simulation ends after one full cycle of TaskA and TaskB delays.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 6Final
TaskA StateReadyBlocked (delay)BlockedBlockedRunningRunning
TaskB StateReadyRunningRunningBlocked (delay)BlockedBlocked
CPU Running TaskNoneTaskBTaskBIdleTaskATaskA
Key Moments - 3 Insights
Why does TaskA run before TaskB even though both are ready at the start?
Because TaskA has a higher priority (see execution_table step 1), FreeRTOS always runs the highest priority ready task.
What happens when TaskA calls vTaskDelay?
TaskA blocks and stops running (step 2), so the scheduler runs the next highest priority ready task, TaskB (step 3).
Why does the Idle Task run at step 5?
Both TaskA and TaskB are blocked on delay, so no user tasks are ready; the Idle Task runs to keep CPU busy.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which task is running at step 3?
ATaskB
BIdle Task
CTaskA
DNo task running
💡 Hint
Check the 'Running Task' column at step 3 in the execution_table.
At which step does TaskA become ready again after delay?
AStep 4
BStep 6
CStep 2
DStep 7
💡 Hint
Look for when TaskA state changes from blocked to ready in variable_tracker and execution_table.
If TaskB had higher priority than TaskA, what would happen at step 1?
ATaskA runs first
BIdle Task runs first
CTaskB runs first
DBoth run simultaneously
💡 Hint
Recall FreeRTOS runs the highest priority ready task (see concept_flow and execution_table step 1).
Concept Snapshot
In FreeRTOS, assign task priorities based on importance.
Higher priority tasks preempt lower ones.
Use delays or blocking to allow lower priority tasks to run.
Avoid priority inversion and starvation by adjusting priorities.
Test and monitor to fine-tune priorities for smooth operation.
Full Transcript
Choosing priorities in FreeRTOS means deciding which tasks are more important so they run first. We start by listing tasks, then assign priorities based on how critical they are. The scheduler always runs the highest priority ready task. If a high priority task blocks (like calling vTaskDelay), lower priority tasks get CPU time. The Idle Task runs only when no other tasks are ready. This example shows TaskA with high priority running first, then blocking, letting TaskB run. When both block, the Idle Task runs. Understanding this helps avoid problems like starvation and ensures smooth multitasking.

Practice

(1/5)
1. In FreeRTOS, what does a higher task priority number mean?
easy
A. The task runs before lower priority tasks
B. The task uses less memory
C. The task runs slower
D. The task is paused

Solution

  1. Step 1: Understand task priority meaning

    In FreeRTOS, task priority determines which task the scheduler runs first.
  2. Step 2: Higher number means higher priority

    A higher priority number means the task is more important and runs before lower priority tasks.
  3. Final Answer:

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

    Higher priority = runs first [OK]
Hint: Higher number means task runs first [OK]
Common Mistakes:
  • Thinking higher priority means slower task
  • Confusing priority with memory usage
  • Assuming priority pauses the task
2. Which of the following is the correct way to set a task priority in FreeRTOS?
easy
A. xTaskCreate(taskFunction, "Task", stackSize, NULL, -1, NULL);
B. xTaskCreate(taskFunction, "Task", stackSize, NULL, "high", NULL);
C. xTaskCreate(taskFunction, "Task", stackSize, NULL, 5, NULL);
D. xTaskCreate(taskFunction, "Task", stackSize, NULL, 0.5, NULL);

Solution

  1. Step 1: Check parameter for priority in xTaskCreate

    The fifth parameter in xTaskCreate is the task priority and must be an integer.
  2. Step 2: Validate options

    xTaskCreate(taskFunction, "Task", stackSize, NULL, 5, NULL); uses integer 5, which is valid. xTaskCreate(taskFunction, "Task", stackSize, NULL, "high", NULL); uses a string, invalid. xTaskCreate(taskFunction, "Task", stackSize, NULL, -1, NULL); uses negative priority, invalid. xTaskCreate(taskFunction, "Task", stackSize, NULL, 0.5, NULL); uses float, invalid.
  3. Final Answer:

    xTaskCreate(taskFunction, "Task", stackSize, NULL, 5, NULL); -> Option C
  4. Quick Check:

    Priority must be integer >= 0 [OK]
Hint: Priority is an integer in xTaskCreate [OK]
Common Mistakes:
  • Using string or float for priority
  • Using negative numbers for priority
  • Confusing parameter order
3. Given three tasks with priorities: TaskA=3, TaskB=5, TaskC=2. Which task will run first when all are ready?
medium
A. TaskA
B. They run simultaneously
C. TaskC
D. TaskB

Solution

  1. Step 1: Compare task priorities

    TaskB has priority 5, TaskA has 3, TaskC has 2.
  2. Step 2: Highest priority runs first

    Since 5 is highest, TaskB runs first when all are ready.
  3. Final Answer:

    TaskB -> Option D
  4. Quick Check:

    Highest priority = 5 = TaskB [OK]
Hint: Highest priority number runs first [OK]
Common Mistakes:
  • Choosing lowest priority task
  • Thinking tasks run simultaneously
  • Ignoring priority values
4. You set two tasks with the same priority in FreeRTOS. What is a likely issue?
medium
A. Tasks will share CPU time but may cause unpredictable timing
B. Tasks may starve lower priority tasks
C. One task will never run
D. System will crash immediately

Solution

  1. Step 1: Understand equal priority behavior

    Tasks with same priority share CPU time via time slicing.
  2. Step 2: Effects of equal priority

    This can cause unpredictable timing because tasks switch frequently, but no starvation occurs.
  3. Final Answer:

    Tasks will share CPU time but may cause unpredictable timing -> Option A
  4. Quick Check:

    Same priority = time slicing, possible timing issues [OK]
Hint: Same priority tasks share CPU, timing may vary [OK]
Common Mistakes:
  • Assuming one task never runs
  • Expecting system crash
  • Ignoring time slicing behavior
5. You have three tasks: SensorRead (must respond fast), DataLogging (less urgent), and UIUpdate (medium urgency). How should you assign priorities?
hard
A. DataLogging=3, UIUpdate=2, SensorRead=1
B. SensorRead=3, UIUpdate=2, DataLogging=1
C. UIUpdate=3, SensorRead=2, DataLogging=1
D. All tasks priority 1

Solution

  1. Step 1: Identify task urgency

    SensorRead must respond fastest, so highest priority. UIUpdate is medium, DataLogging less urgent.
  2. Step 2: Assign priorities accordingly

    Assign SensorRead=3 (highest), UIUpdate=2, DataLogging=1 (lowest).
  3. Final Answer:

    SensorRead=3, UIUpdate=2, DataLogging=1 -> Option B
  4. Quick Check:

    Fastest task gets highest priority [OK]
Hint: Fastest task gets highest priority [OK]
Common Mistakes:
  • Giving low priority to fastest task
  • Assigning same priority to all
  • Reversing urgency 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