A real-time operating system (RTOS) provides scheduling, task management, synchronization and communication services. Real-time correctness means meeting required deadlines under defined worst-case conditions—not simply achieving low average CPU utilization.
1. Tasks, states and scheduling
A task is an independently scheduled execution context with its own stack and priority. Typical states include running, ready, blocked/waiting and suspended. A scheduler selects a ready task according to the configured policy.
Common policies include fixed-priority preemptive scheduling, cooperative scheduling and time slicing among equal-priority tasks. The actual RTOS may provide additional policies and restrictions.
2. Priority and response time
Higher-priority tasks can preempt lower-priority tasks in a preemptive fixed-priority system. Assign priorities based on timing requirements and dependencies, not merely perceived importance.
A task's response time can be affected by:
- its own worst-case execution time;
- higher-priority task interference;
- interrupt execution;
- blocking on shared resources;
- scheduler and context-switch overhead;
- non-preemptible sections and hardware bus contention.
A task that executes for 200 µs every 1 ms has a nominal 20% utilization, but that alone does not prove its deadline is met. Response-time analysis and measurement under worst-case load are needed.
3. Periodic and event-driven task design
Periodic tasks run at defined rates, such as sensor sampling or control. Event-driven tasks wait for messages, interrupts or state changes. Avoid using busy loops where a task can block on a meaningful event.
Example conceptual task:
void SensorTask(void *argument)
{
for (;;) {
wait_for_period_or_event();
acquire_sensor_data();
validate_sensor_data();
publish_sample();
}
}
The wait primitive is RTOS-specific. Use the RTOS's absolute-delay or periodic scheduling API when available to avoid drift caused by execution time.
4. Inter-task communication and synchronization
| Primitive | Use | Main caution |
|---|---|---|
| Queue / message queue | transfer data between tasks | define queue depth and overflow policy |
| Semaphore | signal an event or manage a resource | distinguish binary/counting semantics |
| Mutex | protect shared resource | priority inheritance/ceiling and lock ordering |
| Event flags | wait for one or more conditions | define clear-on-exit behavior |
| Mailbox / shared buffer | transfer structured data | ownership and lifetime must be explicit |
A mutex is generally not appropriate inside a conventional ISR because it may block. Use ISR-safe notification/queue APIs provided by the RTOS. A critical section should be short and bounded.
Deadlock and priority inversion
Deadlock can occur when tasks wait cyclically for resources. Establish a global lock order and avoid holding locks across blocking calls. Priority inversion occurs when a high-priority task is blocked by a lower-priority task holding a resource; priority inheritance or ceiling protocols can mitigate it when correctly configured.
5. Interrupt-to-task handoff
Keep the ISR short: acknowledge the source, capture minimal data and notify a task. The task performs heavier processing. Use only APIs explicitly documented as ISR-safe, and follow the required interrupt-priority constraints.
If the queue fills, define whether the newest sample replaces the oldest, the event is dropped, a counter increments, or a fault is raised. Silent overflow is not a design policy.
6. Timing, WCET and schedulability
For each task, document:
- period or minimum inter-arrival time;
- deadline;
- worst-case execution time;
- priority;
- blocking time and shared resources;
- stack requirement;
- activation source and overload behavior.
Measure execution under representative worst-case conditions and use static timing analysis where required. Include interrupts and background communication load. Average CPU load is useful for capacity planning but cannot prove individual deadlines.
7. Watchdogs and health monitoring
A watchdog should detect lack of progress. Avoid feeding it unconditionally from a periodic ISR, because that can hide a deadlocked or failed application task. A supervisory mechanism can check task heartbeats, deadline monitors and health states before authorizing a watchdog service. The safety concept defines which failures trigger reset, degraded operation or another safe response.
8. RTOS configuration and debugging
Review task stack sizes, priorities, tick or timer source, interrupt priorities, queue lengths, mutex policies, memory protection and hook behavior. Enable stack overflow detection and runtime statistics only as appropriate for the target and timing budget.
| Symptom | Likely cause | Investigation |
|---|---|---|
| Task misses deadline | excessive WCET, blocking, priority | trace execution and blocking time |
| High-priority task starves others | runaway activation or priority assignment | inspect activation rate and CPU budget |
| Deadlock | cyclic lock order | collect lock ownership and wait graph |
| Queue overflow | consumer too slow or burst too large | measure arrival/service rates; define overflow policy |
| Stack overflow | undersized stack or deep call path | stack watermark and static analysis |
| Watchdog reset | task stalled or health supervision wrong | capture reset cause and task heartbeat history |
9. Verification checklist
- Task periods, deadlines, priorities and WCET are documented.
- Stack sizes include worst-case call and interrupt behavior.
- ISR-to-task APIs are explicitly ISR-safe.
- Queue capacity and overflow behavior are defined and tested.
- Lock ordering and priority inversion strategy are reviewed.
- Watchdog service depends on application health.
- Overload, simultaneous events and timing stress are tested.
RTOS APIs, scheduling semantics and safety qualification vary by product and version. Confirm all behavior against the selected RTOS documentation and ECU project requirements.
