Bird
Raised Fist0
FreeRTOSprogramming~5 mins

Preemptive scheduling behavior in FreeRTOS

Choose your learning style10 modes available
Introduction

Preemptive scheduling lets the system switch tasks automatically to keep things running smoothly. It helps important tasks get CPU time right away.

When you want your program to respond quickly to important events.
When multiple tasks need to run and some are more urgent than others.
When you want to make sure no single task blocks others for too long.
When you need real-time behavior in embedded systems like robots or sensors.
Syntax
FreeRTOS
void vTaskStartScheduler( void );

// Tasks are created with xTaskCreate(), specifying priority.
// Higher priority tasks preempt lower priority ones automatically.

Preemptive scheduling is enabled by default in FreeRTOS.

Task priorities determine which task runs first.

Examples
Task1 has higher priority (2) than Task2 (1), so Task1 runs first and can preempt Task2.
FreeRTOS
xTaskCreate(Task1, "Task 1", 1000, NULL, 2, NULL);
xTaskCreate(Task2, "Task 2", 1000, NULL, 1, NULL);
vTaskStartScheduler();
Tasks with the same priority share CPU time in a round-robin way.
FreeRTOS
xTaskCreate(TaskA, "Task A", 1000, NULL, 3, NULL);
xTaskCreate(TaskB, "Task B", 1000, NULL, 3, NULL);
vTaskStartScheduler();
Sample Program

This program creates two tasks. The high priority task will run first and preempt the low priority task when it wakes up, resulting in alternating output messages.

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

void TaskHighPriority(void *pvParameters) {
    for (;;) {
        printf("High priority task running\n");
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

void TaskLowPriority(void *pvParameters) {
    for (;;) {
        printf("Low priority task running\n");
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

int main(void) {
    xTaskCreate(TaskLowPriority, "LowPriority", 1000, NULL, 1, NULL);
    xTaskCreate(TaskHighPriority, "HighPriority", 1000, NULL, 2, NULL);
    vTaskStartScheduler();
    for(;;); // Should never reach here
    return 0;
}
OutputSuccess
Important Notes

Preemptive scheduling helps keep your system responsive.

Make sure high priority tasks do not run forever, or lower priority tasks may starve.

Use vTaskDelay or similar to let other tasks run.

Summary

Preemptive scheduling lets higher priority tasks interrupt lower ones automatically.

It is useful for real-time and responsive systems.

Task priorities control which task runs first.

Practice

(1/5)
1. What happens in FreeRTOS when a higher priority task becomes ready while a lower priority task is running under preemptive scheduling?
easy
A. The scheduler ignores the higher priority task until the lower priority task yields.
B. The lower priority task continues running until it finishes.
C. Both tasks run at the same time.
D. The higher priority task immediately preempts the lower priority task and starts running.

Solution

  1. Step 1: Understand preemptive scheduling behavior

    In preemptive scheduling, the system always runs the highest priority task that is ready.
  2. Step 2: Apply to the scenario

    When a higher priority task becomes ready, it interrupts the lower priority task immediately.
  3. Final Answer:

    The higher priority task immediately preempts the lower priority task and starts running. -> Option D
  4. Quick Check:

    Preemptive scheduling = immediate switch to higher priority task [OK]
Hint: Higher priority tasks always interrupt lower ones immediately [OK]
Common Mistakes:
  • Thinking tasks run simultaneously on a single core
  • Believing lower priority tasks finish before switching
  • Confusing preemptive with cooperative scheduling
2. Which of the following is the correct way to enable preemptive scheduling in FreeRTOS configuration?
easy
A. #define configUSE_PREEMPTION 1
B. #define configENABLE_PREEMPTION true
C. #define configPREEMPTIVE_SCHEDULING 1
D. #define configUSE_PREEMPTION 0

Solution

  1. Step 1: Recall FreeRTOS config option for preemption

    The correct macro to enable preemption is configUSE_PREEMPTION.
  2. Step 2: Identify correct value to enable preemption

    Setting configUSE_PREEMPTION to 1 enables preemptive scheduling.
  3. Final Answer:

    #define configUSE_PREEMPTION 1 -> Option A
  4. Quick Check:

    configUSE_PREEMPTION = 1 enables preemption [OK]
Hint: Enable preemption by setting configUSE_PREEMPTION to 1 [OK]
Common Mistakes:
  • Using 0 instead of 1 to enable preemption
  • Using wrong macro names
  • Using boolean true instead of 1
3. Consider two tasks in FreeRTOS: TaskA with priority 2 and TaskB with priority 3. TaskA is running when TaskB becomes ready. What will be the output if both tasks print their names when running?
medium
A. TaskA prints first, then TaskB prints after TaskA finishes.
B. TaskB prints immediately, preempting TaskA.
C. Both tasks print simultaneously.
D. No output because tasks block each other.

Solution

  1. Step 1: Identify task priorities and running state

    TaskA has priority 2 and is running; TaskB has higher priority 3 and becomes ready.
  2. Step 2: Apply preemptive scheduling rules

    TaskB will preempt TaskA immediately and run first, printing its name.
  3. Final Answer:

    TaskB prints immediately, preempting TaskA. -> Option B
  4. Quick Check:

    Higher priority task runs first = TaskB prints first [OK]
Hint: Higher priority task prints first by preempting lower priority [OK]
Common Mistakes:
  • Assuming lower priority task finishes before switching
  • Thinking tasks run at the same time
  • Ignoring task priorities
4. Given this FreeRTOS snippet, what is the error that prevents preemptive scheduling from working?
#define configUSE_PREEMPTION 0

void Task1(void *pvParameters) {
  while(1) {
    // Task code
  }
}

void Task2(void *pvParameters) {
  while(1) {
    // Task code
  }
}
medium
A. configUSE_PREEMPTION is set to 0, disabling preemption.
B. Tasks are missing priority assignments.
C. Task functions must return int, not void.
D. Tasks need to call vTaskDelay to allow switching.

Solution

  1. Step 1: Check configUSE_PREEMPTION value

    The macro is set to 0, which disables preemptive scheduling.
  2. Step 2: Understand effect on scheduling

    With preemption disabled, tasks will not be interrupted automatically by higher priority tasks.
  3. Final Answer:

    configUSE_PREEMPTION is set to 0, disabling preemption. -> Option A
  4. Quick Check:

    configUSE_PREEMPTION = 0 disables preemption [OK]
Hint: Preemption disabled if configUSE_PREEMPTION = 0 [OK]
Common Mistakes:
  • Thinking tasks must return int
  • Assuming missing priorities cause preemption failure
  • Believing vTaskDelay is required for preemption
5. You have three tasks: TaskX (priority 2), TaskY (priority 3), and TaskZ (priority 3). TaskX is running, then both TaskY and TaskZ become ready at the same time. Which task will FreeRTOS run first under preemptive scheduling, and why?
hard
A. TaskY runs first because it was created before TaskZ.
B. TaskZ runs first because it has the same priority as TaskY but higher than TaskX.
C. Either TaskY or TaskZ runs first; FreeRTOS uses time slicing between equal priorities.
D. TaskX continues running until it yields.

Solution

  1. Step 1: Identify task priorities and ready state

    TaskX priority 2 is running; TaskY and TaskZ both have priority 3 and become ready simultaneously.
  2. Step 2: Understand FreeRTOS behavior with equal priority tasks

    FreeRTOS uses time slicing (round-robin) between tasks of equal highest priority.
  3. Step 3: Determine which task runs first

    Either TaskY or TaskZ can run first; the scheduler switches between them fairly.
  4. Final Answer:

    Either TaskY or TaskZ runs first; FreeRTOS uses time slicing between equal priorities. -> Option C
  5. Quick Check:

    Equal priority tasks share CPU time via time slicing [OK]
Hint: Equal priority tasks share CPU time by time slicing [OK]
Common Mistakes:
  • Assuming creation order decides first run
  • Thinking lower priority task continues running
  • Believing one equal priority task always runs first
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