Hard real-time vs Soft real-time Task Handling in FreeRTOS
📖 Scenario: You are working on a small embedded system using FreeRTOS. Your system needs to handle two types of tasks:A hard real-time task that must run exactly every 100 milliseconds without fail.A soft real-time task that runs every 200 milliseconds but can tolerate some delays.This is like having a fire alarm that must sound immediately (hard real-time) and a temperature logger that can update less strictly (soft real-time).
🎯 Goal: Build a FreeRTOS program that creates two tasks: one hard real-time task that runs precisely every 100 ms, and one soft real-time task that runs every 200 ms but can skip some cycles if delayed.You will set up the tasks, configure their timing, implement their behavior, and print messages to show their execution.
📋 What You'll Learn
Create a hard real-time task function called HardRealTimeTask that runs every 100 ms using vTaskDelayUntil.
Create a soft real-time task function called SoftRealTimeTask that runs every 200 ms using vTaskDelay.
Create task handles and start the FreeRTOS scheduler.
Print messages inside each task to show when they run.
💡 Why This Matters
🌍 Real World
Embedded systems often need to handle tasks with strict timing (hard real-time) and others with flexible timing (soft real-time), such as controlling motors and logging data.
💼 Career
Understanding how to implement and differentiate hard and soft real-time tasks is essential for embedded software engineers working with FreeRTOS or similar real-time operating systems.
Progress0 / 4 steps
1
Setup Task Functions
Create two task functions called HardRealTimeTask and SoftRealTimeTask. Each should take a void *pvParameters argument and have an infinite loop with empty bodies for now.
FreeRTOS
Hint
Define two functions with the exact names and infinite loops inside.
2
Configure Task Timing Variables
Create two variables: TickType_t xLastWakeTime initialized to 0 for the hard real-time task, and const TickType_t xSoftDelay = pdMS_TO_TICKS(200) for the soft real-time task delay.
FreeRTOS
Hint
Use TickType_t type and initialize variables exactly as shown.
3
Implement Task Timing Logic
In HardRealTimeTask, use vTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(100)) inside the loop to run every 100 ms. In SoftRealTimeTask, use vTaskDelay(xSoftDelay) inside the loop to run every 200 ms. Add printf statements inside each loop to print "Hard task running" and "Soft task running" respectively.
FreeRTOS
Hint
Use vTaskDelayUntil for the hard task and vTaskDelay for the soft task with the exact delays and print statements.
4
Create Tasks and Start Scheduler
In main(), create two tasks using xTaskCreate: one for HardRealTimeTask with priority 2, and one for SoftRealTimeTask with priority 1. Then start the scheduler with vTaskStartScheduler(). Print "Scheduler started" before starting.
FreeRTOS
Hint
Use xTaskCreate with the exact parameters and start the scheduler. The output should show the scheduler start message and task prints.
Practice
(1/5)
1. Which statement best describes a hard real-time system in FreeRTOS?
easy
A. Tasks can miss deadlines occasionally without major issues.
B. Tasks run only when the CPU is idle.
C. Tasks must meet deadlines strictly without any misses.
D. Tasks have no timing constraints.
Solution
Step 1: Understand hard real-time definition
Hard real-time means deadlines are strict and must never be missed.
Step 2: Compare options with definition
The statement 'Tasks must meet deadlines strictly without any misses' matches the definition of hard real-time.
Final Answer:
Tasks must meet deadlines strictly without any misses. -> Option C
Quick Check:
Hard real-time = strict deadlines [OK]
Hint: Hard real-time means no deadline misses allowed [OK]
Common Mistakes:
Confusing hard real-time with soft real-time
Thinking tasks run only when CPU is idle
Ignoring timing constraints
2. Which FreeRTOS API function is used to delay a task for a specific time period?
easy
A. vTaskDelay()
B. xTaskCreate()
C. vTaskStartScheduler()
D. xQueueSend()
Solution
Step 1: Identify delay function in FreeRTOS
vTaskDelay() pauses a task for a given tick count, controlling timing.
Step 2: Check other options
xTaskCreate() creates tasks, vTaskStartScheduler() starts the OS, xQueueSend() sends to queues.
Final Answer:
vTaskDelay() -> Option A
Quick Check:
Delay task = vTaskDelay() [OK]
Hint: Delay tasks with vTaskDelay() in FreeRTOS [OK]
Common Mistakes:
Confusing task creation with delay
Using scheduler start as delay
Mixing queue functions with delays
3. Consider this FreeRTOS task priority setup:
Task A priority = 3
Task B priority = 2
Task C priority = 1
If Task A blocks for 100ms using vTaskDelay(), which task runs next?
medium
A. Task A again immediately
B. Task B
C. Task C
D. No task runs until Task A finishes delay
Solution
Step 1: Understand task priorities and blocking
Task A has highest priority (3) but blocks for 100ms, so it is not ready.
Step 2: Identify next highest priority ready task
Task B has priority 2 and is ready, so it runs next.
Final Answer:
Task B -> Option B
Quick Check:
Next task after blocking = highest ready priority [OK]
Hint: Next runs highest priority ready task after delay [OK]
Common Mistakes:
Assuming blocked task runs immediately
Ignoring task priorities
Thinking no task runs during delay
4. Identify the error in this FreeRTOS code snippet for a hard real-time task:
A. vTaskDelay should be replaced with vTaskDelete.
B. The task function lacks a return statement.
C. The infinite loop will crash FreeRTOS.
D. Using vTaskDelay causes deadline misses in hard real-time tasks.
Solution
Step 1: Analyze vTaskDelay in hard real-time context
vTaskDelay pauses the task, which can cause missing strict deadlines in hard real-time.
Step 2: Check other options for correctness
Return statement is not needed in infinite loop, infinite loops are normal, vTaskDelete deletes task, not delay.
Final Answer:
Using vTaskDelay causes deadline misses in hard real-time tasks. -> Option D
Quick Check:
Hard real-time tasks avoid vTaskDelay [OK]
Hint: Hard real-time tasks avoid delays that cause misses [OK]
Common Mistakes:
Thinking infinite loops crash FreeRTOS
Confusing vTaskDelay with task deletion
Expecting return in infinite loops
5. You need to design a FreeRTOS system where some tasks must never miss deadlines (hard real-time), and others can miss occasionally (soft real-time). Which approach best achieves this?
hard
A. Assign highest priorities to hard real-time tasks and use vTaskDelay carefully for soft real-time tasks.
B. Use vTaskDelay for all tasks to simplify timing.
C. Assign equal priorities to all tasks and rely on round-robin scheduling.
D. Run hard real-time tasks only when soft real-time tasks are idle.
Solution
Step 1: Understand priority and delay roles in FreeRTOS
Hard real-time tasks need highest priority to avoid preemption and deadline misses.
Step 2: Use vTaskDelay carefully for soft real-time tasks
Soft real-time tasks can tolerate delays, so vTaskDelay helps manage their timing without affecting hard real-time tasks.
Step 3: Evaluate other options
Equal priorities or running hard real-time tasks only when soft are idle do not guarantee deadlines.
Final Answer:
Assign highest priorities to hard real-time tasks and use vTaskDelay carefully for soft real-time tasks. -> Option A
Quick Check:
Priority + careful delay = mixed real-time system [OK]
Hint: Prioritize hard real-time tasks highest, delay soft tasks [OK]
Common Mistakes:
Giving equal priority to all tasks
Using delays in hard real-time tasks
Running hard real-time tasks only after soft tasks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.