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
return0;
}
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.
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.