Knowledge / Electronics and Communication Engineering / Real-time operating systems

Real-time operating systems

Electronics and Communication Engineering (B.E./B.Tech) · Microprocessors and embedded · free study notes for exams, placements and interviews

In short

A real-time operating system is a small, deterministic kernel that guarantees a task will run within a known, bounded time — correctness depends not only on the computed result but on *when* it appears. It replaces the single while(1) superloop with multiple prioritised tasks, a scheduler, and safe mechanisms (semaphores, mutexes, queues) for them to share a single CPU. RTOS is a standard unit of the embedded-systems paper in most AICTE/CBCS B.E. ECE curricula; GATE EC carries no operating-systems section, so scheduling, synchronisation and deadlock are examined in GATE CS and in university and placement papers. Indian employers — Bosch, Continental, KPIT, Tata Elxsi, L&T Technology Services, HCL, Wipro, Qualcomm, ISRO/DRDO labs and automotive Tier-1s — test FreeRTOS APIs, scheduling maths and priority inversion in almost every embedded placement interview.

Explained

What makes an operating system "real-time"

Tasks, states and the scheduler

Inter-task communication and synchronisation

Priority inversion, deadlock and the classic hazards

Memory, interrupts and kernel internals

Common RTOSes, standards and Indian practice

Key points

Formulas and examples

CPU utilisation U = Σ (Cᵢ / Tᵢ) — Cᵢ = worst-case execution time, Tᵢ = period (same units, usually ms); U is dimensionless (×100 for %).
Liu–Layland RMS bound U ≤ n (21/n − 1) — n = number of periodic tasks; the bound falls from 1.000 (n=1) to 0.828 (2), 0.780 (3), 0.757 (4) → ln 2 ≈ 0.693 as n → ∞. Sufficient only.
EDF / dynamic-priority bound U ≤ 1 for deadlines equal to periods (necessary and sufficient, uniprocessor, independent tasks).
Response-time analysis (fixed priority) Rᵢ = Cᵢ + Bᵢ + Σ_(j ∈ hp(i)) ⌈Rᵢ / Tⱼ⌉ Cⱼ — solved by iteration from Rᵢ⁰ = Cᵢ; Bᵢ = worst blocking by a lower-priority task; schedulable if Rᵢ ≤ Dᵢ.
Task response time (practical) R = interrupt latency + ISR execution + scheduler/context-switch time + preemption time
Scheduler (tick) overhead Overhead % = (t_tick_handler / T_tick) × 100
Timer tick resolution T_tick = 1 / f_tick ; worst-case delay error for vTaskDelay(n) ≈ one full tick.
Hyperperiod H = LCM(T₁, T₂, … Tₙ) — the length of the repeating schedule used in timeline drawing.
Context-switch time t_cs = (cycles to save + restore context) / f_CPU

Example 1 — RMS schedulability test. Tasks: T1 (C = 1 ms, T = 4 ms), T2 (C = 2 ms, T = 6 ms), T3 (C = 1 ms, T = 12 ms).

Example 2 — a set that fails the bound but is still schedulable (why the test is only sufficient). Tasks: T1 (C = 2, T = 5), T2 (C = 3, T = 8), T3 (C = 1, T = 10) ms.

R⁰ = 1 → R¹ = 1 + ⌈1/5⌉×2 + ⌈1/8⌉×3 = 1 + 2 + 3 = 6 R² = 1 + ⌈6/5⌉×2 + ⌈6/8⌉×3 = 1 + 4 + 3 = 8 R³ = 1 + ⌈8/5⌉×2 + ⌈8/8⌉×3 = 1 + 4 + 3 = 8 ms (converged)

Example 3 — interrupt latency and response time budget. A 72 MHz Cortex-M3 reads an encoder. Longest interrupt-disabled critical section = 8 µs; NVIC + stacking latency = 12 cycles; ISR body = 300 cycles; context switch = 120 cycles; the handling task is the highest priority.

Example 4 — tick overhead and timing resolution. configTICK_RATE_HZ = 1000 on the same 72 MHz MCU; the tick handler (including scheduler run) takes 90 cycles.

Example 5 — priority inversion, with and without inheritance. Tasks H (priority 3), M (2), L (1). L takes mutex X and needs 5 ms inside it; H needs X; M needs 40 ms of CPU and uses no mutex. L enters X at t = 0; H becomes ready at t = 1 ms; M becomes ready at t = 2 ms.

MechanismOwner?Use forISR-safePriority inheritance
Binary semaphoreNoEvent signalling, ISR→taskYes (...FromISR)No
Counting semaphoreNoCounting resources/eventsYesNo
MutexYesProtecting shared dataNoYes
Recursive mutexYesNested access by same taskNoYes
Queue—Passing data + ownershipYesN/A
Task notification—Fast 1-to-1 signalYesN/A
FeatureSuperloop (bare metal)RTOSGPOS (desktop Linux/Windows)
Worst-case responseWhole loop passBounded by priority analysisUnbounded
SchedulingCode orderPriority / deadline basedFairness, throughput
Memory overheadNegligible~5-10 kB kernel code + per-task stacksMegabytes
ScalabilityPoor beyond ~5 activitiesGoodExcellent
Typical useSimple sensor nodeMotor control, ABS, droneInfotainment, servers
CriterionRMS (static)EDF (dynamic)
Priority assignmentFixed, by periodChanges at run time, by deadline
Utilisation bound (D = T)n(21/n − 1), ~69 % for large n100 %
Run-time costLowHigher (deadline queue)
Overload behaviourPredictable — low-priority tasks miss firstUnpredictable cascade
Industry useVery common (AUTOSAR, FreeRTOS)Research, some Linux SCHED_DEADLINE
/* Deferred interrupt handling: short ISR + prioritised task (FreeRTOS) */
void EXTI0_IRQHandler(void) {
    BaseType_t xHigherPriorityTaskWoken = pdFALSE;
    clear_exti_flag();
    vTaskNotifyGiveFromISR(xSensorTask, &xHigherPriorityTaskWoken);
    portYIELD_FROM_ISR(xHigherPriorityTaskWoken);   /* switch now if needed */
}

void vSensorTask(void *pv) {
    TickType_t xLast = xTaskGetTickCount();
    for (;;) {
        ulTaskNotifyTake(pdTRUE, portMAX_DELAY);    /* block, 0 % CPU */
        xSemaphoreTake(xBusMutex, pdMS_TO_TICKS(10));  /* inheritance applies */
        read_adc_and_filter();
        xSemaphoreGive(xBusMutex);
        xQueueSend(xDataQueue, &sample, 0);
        xTaskDelayUntil(&xLast, pdMS_TO_TICKS(10)); /* drift-free 10 ms period */
    }
}

Where it is used

Interview questions

Common mistakes

Related topics

ARM and embedded C · Microcontroller peripherals: timers, ADC, PWM and interrupts · Embedded Linux and single-board computers · Operating systems: process scheduling and synchronisation · Internet of Things basics · Embedded interfaces: UART, SPI and I2C

Open in the Knowledge reader →Find jobs →