📖 Scenario: You are working on a small embedded system that controls two tasks: one that blinks an LED and another that reads a sensor. Both tasks need to run repeatedly, but the sensor reading must happen more frequently to keep the system responsive.
🎯 Goal: Build a simple FreeRTOS program that creates two tasks with different priorities and observe how the scheduler decides which task runs first, showing how scheduling affects real-time behavior.
📋 What You'll Learn
Create two tasks named vTaskBlink and vTaskSensor.
Set the priority of vTaskSensor higher than vTaskBlink.
Use vTaskDelay to simulate task work and periodic execution.
Print messages from each task to show which runs when.
Observe the output to understand scheduling impact.
💡 Why This Matters
🌍 Real World
Embedded systems often run multiple tasks that must respond quickly. Scheduling ensures critical tasks get CPU time first.
💼 Career
Understanding task scheduling is key for embedded software engineers working with real-time operating systems like FreeRTOS.
Progress0 / 4 steps
1
Create two FreeRTOS tasks
Write code to create two tasks named vTaskBlink and vTaskSensor using xTaskCreate. Use empty task functions for now.
FreeRTOS
Hint
Use xTaskCreate to make tasks. Give vTaskSensor a higher priority number than vTaskBlink.
2
Add print statements and delays
Inside vTaskBlink and vTaskSensor, add printf statements to print "Blink task running" and "Sensor task running" respectively. Keep the vTaskDelay calls to simulate work.
FreeRTOS
Hint
Use printf inside each task's infinite loop before the delay.
3
Explain scheduling effect with priorities
Add comments in the main function explaining that vTaskSensor has higher priority and will run more often, showing how scheduling affects real-time behavior.
FreeRTOS
Hint
Add comments in main explaining the priority difference and its effect on scheduling.
4
Run and observe output
Run the program and observe the printed output. Write a printf statement in main before starting the scheduler that says "Starting scheduler".
FreeRTOS
Hint
Print "Starting scheduler" before calling vTaskStartScheduler() to confirm program start.
Practice
(1/5)
1. Why is scheduling important in FreeRTOS for real-time behavior?
easy
A. It decides which task runs and when, affecting response time.
B. It controls the memory allocation for tasks.
C. It manages the power consumption of the system.
D. It handles the input/output operations automatically.
Solution
Step 1: Understand scheduling role
Scheduling in FreeRTOS controls task execution order and timing.
Step 2: Connect scheduling to real-time
By deciding which task runs and when, scheduling affects how fast the system reacts to events.
Final Answer:
It decides which task runs and when, affecting response time. -> Option A
Quick Check:
Scheduling controls task timing = It decides which task runs and when, affecting response time. [OK]
Hint: Scheduling controls task timing and response speed [OK]
Common Mistakes:
Confusing scheduling with memory management
Thinking scheduling manages power or I/O directly
Ignoring the impact on response time
2. Which FreeRTOS function is used to start the scheduler?
easy
A. vTaskDelay()
B. xTaskCreate()
C. vTaskStartScheduler()
D. xQueueSend()
Solution
Step 1: Identify scheduler start function
The function to start the FreeRTOS scheduler is vTaskStartScheduler().
Step 2: Differentiate from other functions
xTaskCreate() creates tasks, vTaskDelay() delays tasks, xQueueSend() sends to queues.
Final Answer:
vTaskStartScheduler() -> Option C
Quick Check:
Start scheduler function = vTaskStartScheduler() [OK]
Hint: Scheduler starts with vTaskStartScheduler() function [OK]
Common Mistakes:
Confusing task creation with scheduler start
Using delay or queue functions to start scheduler
Misspelling function names
3. Given two tasks with priorities 3 and 5, which task will FreeRTOS schedule first?
medium
A. Task with priority 5
B. Task created first runs first
C. Both run simultaneously
D. Task with priority 3
Solution
Step 1: Understand priority meaning
In FreeRTOS, higher numeric priority means higher importance.
Step 2: Compare priorities 3 and 5
Priority 5 is higher than 3, so the task with priority 5 runs first.
Final Answer:
Task with priority 5 -> Option A
Quick Check:
Higher priority runs first = Task with priority 5 [OK]
Hint: Higher numeric priority runs before lower [OK]
Common Mistakes:
Assuming lower number means higher priority
Thinking tasks run simultaneously
Believing creation order affects scheduling
4. What is wrong with this FreeRTOS code snippet?
void task1(void *pvParameters) {
while(1) {
// do work
}
}
int main() {
xTaskCreate(task1, "Task1", 1000, NULL, 1, NULL);
// Missing scheduler start
return 0;
}
medium
A. Task stack size is too large.
B. Scheduler is not started, so tasks never run.
C. Task priority is invalid.
D. Task function has wrong signature.
Solution
Step 1: Check for scheduler start
The code creates a task but does not call vTaskStartScheduler(), so no tasks run.
Step 2: Verify other parts
Stack size 1000 and priority 1 are valid; task signature is correct.
Final Answer:
Scheduler is not started, so tasks never run. -> Option B
Quick Check:
Missing scheduler start = Scheduler is not started, so tasks never run. [OK]
Hint: Always call vTaskStartScheduler() after creating tasks [OK]
Common Mistakes:
Forgetting to start the scheduler
Thinking stack size or priority causes no run
Assuming task signature is incorrect
5. How does using task priorities in FreeRTOS help meet real-time deadlines?
hard
A. Priorities automatically optimize CPU clock speed.
B. Priorities reduce memory usage for tasks.
C. Priorities allow tasks to run in parallel on a single core.
D. Higher priority tasks preempt lower ones, ensuring timely execution.