📖 Scenario: You are working on a simple embedded system using FreeRTOS. You want to understand how preemptive scheduling works by creating two tasks with different priorities. The higher priority task should preempt the lower priority task when it becomes ready.
🎯 Goal: Build a FreeRTOS program with two tasks: TaskHigh and TaskLow. TaskHigh has higher priority and preempts TaskLow. You will observe the order of task execution through printed messages.
📋 What You'll Learn
Create two tasks named TaskHigh and TaskLow
TaskHigh must have higher priority than TaskLow
Use vTaskDelay to simulate work in each task
Print messages to show when each task runs
Start the FreeRTOS scheduler to run the tasks
💡 Why This Matters
🌍 Real World
Embedded systems often use FreeRTOS to manage multiple tasks with different priorities, such as sensor reading, communication, and user interface.
💼 Career
Understanding preemptive scheduling is essential for embedded software engineers working with real-time operating systems to ensure timely and predictable task execution.
Progress0 / 4 steps
1
Create two tasks with different priorities
Write code to create two tasks named TaskHigh and TaskLow using xTaskCreate. Set TaskHigh priority to 2 and TaskLow priority to 1. Use empty task functions for now.
FreeRTOS
Hint
Use xTaskCreate to create tasks. The priority is the fifth argument.
2
Add print statements to tasks
Add printf statements inside TaskHigh and TaskLow to print "High priority task running" and "Low priority task running" respectively. Keep the vTaskDelay(pdMS_TO_TICKS(1000)) calls to simulate work.
FreeRTOS
Hint
Use printf inside each task's infinite loop before the delay.
3
Add a counter to observe preemption
Inside TaskLow, add an integer counter variable count initialized to 0 before the loop. Increment count each time the task runs and print it with the message. This will help observe how often TaskLow runs compared to TaskHigh.
FreeRTOS
Hint
Declare count before the loop and increment it inside the loop before printing.
4
Run the scheduler and observe output
Run the program and observe the printed output. The output should show "High priority task running" and "Low priority task running, count = X" messages. Because of preemptive scheduling, TaskHigh runs as soon as it is ready, preempting TaskLow. Write a printf statement in main before starting the scheduler to print "Starting scheduler".
FreeRTOS
Hint
Print "Starting scheduler" before calling vTaskStartScheduler().
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
Step 1: Understand preemptive scheduling behavior
In preemptive scheduling, the system always runs the highest priority task that is ready.
Step 2: Apply to the scenario
When a higher priority task becomes ready, it interrupts the lower priority task immediately.
Final Answer:
The higher priority task immediately preempts the lower priority task and starts running. -> Option D
Quick Check:
Preemptive scheduling = immediate switch to higher priority task [OK]
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
Step 1: Recall FreeRTOS config option for preemption
The correct macro to enable preemption is configUSE_PREEMPTION.
Step 2: Identify correct value to enable preemption
Setting configUSE_PREEMPTION to 1 enables preemptive scheduling.
Final Answer:
#define configUSE_PREEMPTION 1 -> Option A
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
Step 1: Identify task priorities and running state
TaskA has priority 2 and is running; TaskB has higher priority 3 and becomes ready.
Step 2: Apply preemptive scheduling rules
TaskB will preempt TaskA immediately and run first, printing its name.
Final Answer:
TaskB prints immediately, preempting TaskA. -> Option B
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?
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
Step 1: Check configUSE_PREEMPTION value
The macro is set to 0, which disables preemptive scheduling.
Step 2: Understand effect on scheduling
With preemption disabled, tasks will not be interrupted automatically by higher priority tasks.
Final Answer:
configUSE_PREEMPTION is set to 0, disabling preemption. -> Option A
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
Step 1: Identify task priorities and ready state
TaskX priority 2 is running; TaskY and TaskZ both have priority 3 and become ready simultaneously.
Step 2: Understand FreeRTOS behavior with equal priority tasks
FreeRTOS uses time slicing (round-robin) between tasks of equal highest priority.
Step 3: Determine which task runs first
Either TaskY or TaskZ can run first; the scheduler switches between them fairly.
Final Answer:
Either TaskY or TaskZ runs first; FreeRTOS uses time slicing between equal priorities. -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.