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.

Rendering diagram...

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

PrimitiveUseMain caution
Queue / message queuetransfer data between tasksdefine queue depth and overflow policy
Semaphoresignal an event or manage a resourcedistinguish binary/counting semantics
Mutexprotect shared resourcepriority inheritance/ceiling and lock ordering
Event flagswait for one or more conditionsdefine clear-on-exit behavior
Mailbox / shared buffertransfer structured dataownership 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.

Rendering diagram...

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.

SymptomLikely causeInvestigation
Task misses deadlineexcessive WCET, blocking, prioritytrace execution and blocking time
High-priority task starves othersrunaway activation or priority assignmentinspect activation rate and CPU budget
Deadlockcyclic lock ordercollect lock ownership and wait graph
Queue overflowconsumer too slow or burst too largemeasure arrival/service rates; define overflow policy
Stack overflowundersized stack or deep call pathstack watermark and static analysis
Watchdog resettask stalled or health supervision wrongcapture 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.

Related articles