Skip to content
systemcoding.dev
RTOS

What Makes an Operating System Real-Time

Real-time means a bounded, predictable response to an event, not raw speed. Learn what determinism, deadlines and jitter mean and how an RTOS guarantees them.

6 min read
Summarize in AI
Flat illustration of RTOS tasks taking turns on a timeline, with the C, Espressif and Arm logos.

A real-time operating system is not a fast operating system. Plenty of general-purpose kernels move more data per second than any RTOS on a microcontroller. What an RTOS offers is a promise about the worst case: when an event happens, the right piece of code runs within a time you can state in advance, every time. This post defines the terms, shows the promise in a few lines of FreeRTOS code, and explains which parts of the kernel have to be built a particular way to keep it. If you want the whole picture of RTOS design for small chips, the cornerstone guide to real-time operating systems for microcontrollers covers it end to end.

Real-time means bounded, not fast

Picture a motor controller that must update a PWM duty cycle every 1 ms. If the update runs in 20 microseconds on average but occasionally takes 3 ms, the system is fast and wrong. If the update always runs somewhere between 40 and 60 microseconds after the timer fires, the system is slower on average and correct. Real-time engineering cares about the second property.

That property has a name: determinism. A system is deterministic when you can put an upper bound on the time between an event and the response to it, and the system never exceeds that bound under any load it is specified to handle. The bound is the latency. The distance between the best and worst observed latency is the jitter. The point in time by which the response must be finished is the deadline.

TermMeaningExample
LatencyTime from event to start of the responseTimer fires, control task starts running
Response timeTime from event to end of the responseTimer fires, duty cycle is written
DeadlineThe latest acceptable response time1 ms after the timer fires
JitterVariation in latency from one event to the nextLatency ranges from 8 to 14 microseconds
DeterminismA provable upper bound on latencyWorst case never exceeds 20 microseconds

An ordinary desktop kernel optimizes the average. It will happily delay your process for 40 ms to run a disk flush, because on average that gives better throughput. An RTOS gives up some average performance to keep the tail short.

What an RTOS has to do differently

Four design choices separate an RTOS from a time-sharing kernel.

Priority-based preemption. The scheduler always runs the highest-priority task that is ready. If a higher-priority task becomes ready, it takes the CPU away from the current one at the next opportunity, instead of waiting for a time slice to end. A time-sharing scheduler tries to be fair between tasks; an RTOS scheduler tries to be right about which one matters most.

Bounded kernel operations. Every kernel call has a documented worst case, and the kernel avoids work whose cost grows with load. The FreeRTOS ready lists, for example, are kept per priority, so picking the next task does not scan every task in the system.

Short, bounded interrupt-disabled sections. Whenever the kernel disables interrupts to update its own data structures, it does so for a handful of instructions. Long critical sections show up directly as interrupt latency.

No hidden waiting. Memory allocators that may walk a long free list, filesystems that stall on flash erases, and locks that can be held indefinitely are the usual sources of surprise delays. A real-time design keeps them out of the time-critical path or bounds them explicitly.

Real-time is a property of the whole system, not of the kernel alone. An RTOS makes it possible to meet deadlines. Your interrupt handlers, your task priorities and the hardware (flash wait states, cache, bus contention) decide whether you do.

The promise in code

The smallest useful demonstration is a periodic task that checks its own deadline. This sketch uses FreeRTOS Kernel V11.3.3 on a Cortex-M4 part with a 1 kHz tick, which means one tick is one millisecond:

deadline.cc
#include <stdio.h>
#include "FreeRTOS.h"
#include "task.h"

#define SAMPLE_PERIOD_MS   10
#define DEADLINE_MS         4

/* Stand-in for real work. A real task would read a sensor and filter it. */
static void process_sample(void)
{
    vTaskDelay(pdMS_TO_TICKS(2));
}

static void vSensorTask(void *pvParameters)
{
    TickType_t xLastWake = xTaskGetTickCount();

    (void) pvParameters;

    for (;;)
    {
        xTaskDelayUntil(&xLastWake, pdMS_TO_TICKS(SAMPLE_PERIOD_MS));

        TickType_t xStart = xTaskGetTickCount();
        process_sample();
        TickType_t xElapsed = xTaskGetTickCount() - xStart;

        if (xElapsed > pdMS_TO_TICKS(DEADLINE_MS))
        {
            printf("deadline missed: %u ms\n", (unsigned) (xElapsed * portTICK_PERIOD_MS));
        }
        else
        {
            printf("sample ok: %u ms\n", (unsigned) (xElapsed * portTICK_PERIOD_MS));
        }
    }
}

int main(void)
{
    xTaskCreate(vSensorTask, "sensor", 256, NULL, 2, NULL);
    vTaskStartScheduler();
    for (;;) { }
}

With nothing else running, the task wakes every 10 ms and finishes in 2 ticks:

Text
sample ok: 2 ms
sample ok: 2 ms
sample ok: 2 ms

Two details matter here. xTaskDelayUntil wakes the task at fixed instants measured from xLastWake, so the period does not drift by the execution time of the loop body. And the deadline check measures in ticks, which means it can only see overruns of a whole tick or more. Tick-level checks catch gross misses. Catching microsecond-level jitter takes a hardware timer or a GPIO and a logic analyzer.

What a missed deadline costs

Not every deadline carries the same penalty, and the penalty decides how much engineering you owe the design. There are three common classes.

  • Hard real-time: a missed deadline is a system failure. An airbag controller that fires 30 ms late has failed, however useful its late answer would have been.

  • Firm real-time: a late result is worthless, but a single miss does not break the system. A video frame decoded too late is dropped.

  • Soft real-time: a late result still has some value that decays. A UI that responds in 120 ms instead of 50 ms feels sluggish but works.

The distinction between these classes gets its own treatment in the post on hard, soft and firm requirements. For now, notice that "real-time" does not tell you how short the deadline is. A weather logger with a 10 second deadline can be hard real-time if missing it loses the data. A 100 microsecond deadline on a best-effort display refresh can be soft.

Where the guarantee stops

An RTOS cannot give you determinism you do not design for. Three things quietly erase it.

First, priority inversion. If a low-priority task holds a mutex that a high-priority task needs, and a medium-priority task keeps preempting the low one, the high-priority task is stuck behind work that matters less. Kernels counter this with priority inheritance, and FreeRTOS mutexes implement it, but a plain binary semaphore does not.

Second, unbounded calls in the critical path. Calling malloc, printf or a flash write from your 1 ms control loop imports the worst case of those routines into your deadline. The printf calls in the sample above are fine for a demonstration and wrong for a production control task.

Third, interrupt storms. A noisy GPIO line that fires thousands of times a second steals CPU from every task regardless of priority, because interrupts outrank the scheduler. Rate-limit interrupt sources and keep handlers short.

The honest boundary of this post: it describes the concept at the level a beginner needs to read RTOS documentation. It does not prove schedulability. For that you need worst-case execution times for each task and an analysis such as rate-monotonic response-time analysis, which belongs to a later rung of the ladder.

Taggedcbeginner
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.