⏱️RTOS & Real-Time Systems
RTOS questions check two things: whether you know the vocabulary, and whether you can reason about timing and concurrency bugs. Examples use FreeRTOS names, but the ideas apply to Zephyr, ThreadX, embOS, VxWorks and QNX. Each answer gives the one-line version first, then the explanation and the follow-ups.
01What is an RTOS and how is it different from a general-purpose OS?Easy✓
Short answer: A Real-Time Operating System guarantees that tasks respond within a bounded, predictable time. Its goal is determinism (meeting deadlines every time), not maximum average throughput. Compared with Linux or Windows, an RTOS has a small priority-based pre-emptive scheduler, bounded interrupt and context-switch latency, a footprint of a few KB, and usually no MMU-based process isolation: all tasks share one address space.
| RTOS (FreeRTOS, Zephyr) | General-purpose OS (Linux) | |
|---|---|---|
| Scheduling | fixed priorities, highest ready task always runs | fairness (CFS/EEVDF), throughput |
| Latency | bounded, microseconds | usually low, but unbounded worst case (PREEMPT_RT improves it) |
| Footprint | 5–50 KB flash, a few KB RAM | MBs of RAM, storage |
| Memory protection | none or MPU-based | MMU, full process isolation |
| Boot time | milliseconds | seconds |
| Drivers, networking, filesystems | small or add-on | huge ecosystem |
| Typical hardware | Cortex-M, Cortex-R, small RISC-V | Cortex-A, x86 |
What makes it deterministic
- The scheduler's work is O(1) (ready lists per priority, bitmap lookup).
- Kernel critical sections are short and bounded.
- No paging, no swapping, usually no dynamic allocation after start-up.
- Interrupt handling and the API have documented worst-case behaviour.
Examples: FreeRTOS (and SafeRTOS for certification), Zephyr, Eclipse ThreadX (formerly Azure RTOS), embOS, RTX, Micrium µC/OS, NuttX (POSIX-like); and larger commercial ones: VxWorks, QNX (microkernel), INTEGRITY, PikeOS (with partitioning for safety).
Interview line: "Real-time does not mean fast. It means correct results on time, every time, with a provable worst case."
02What is the difference between hard, firm and soft real-time?Easy✓
Short answer: In a hard real-time system, missing a deadline is a system failure (airbag deployment, engine control, pacemaker, motor commutation). In a firm system, a late result is worthless but occasional misses are tolerated (a dropped video frame, a missed sensor sample). In a soft system, late results still have some value, just less (UI responsiveness, audio buffering with retries, network throughput).
| Hard | Firm | Soft | |
|---|---|---|---|
| Missed deadline | failure / hazard | result discarded | degraded quality |
| Analysis | worst-case execution time (WCET), schedulability proof | statistical + worst case | average performance |
| Examples | ABS brakes, flight control, motor current loop | video decode, telemetry sampling | UI, logging, streaming with buffers |
| Typical platform | MCU/RTOS, Cortex-R, lock-step cores | RTOS or Linux with PREEMPT_RT | Linux, Android |
Real systems mix them: a drone has a hard-real-time attitude control loop (e.g. 1 kHz), firm-real-time sensor fusion, and soft-real-time telemetry and video. Good designs isolate the hard parts: highest priorities, dedicated cores, or a separate MCU.
Key terms
- Deadline: latest time by which a result must be produced.
- Jitter: variation in timing (e.g. start times of a periodic task); control loops often care more about jitter than about the absolute delay.
- WCET: worst-case execution time of a piece of code, measured or computed by static analysis tools (aiT, RapiTime), affected by caches, branch predictors and interrupts.
Interview follow-up: "Is Linux real-time?" Standard Linux is soft real-time; with PREEMPT_RT, worst-case latencies of tens of µs are achievable, which suits many firm and some hard requirements, but safety-critical hard real-time still usually runs on an RTOS or a separate core.
03What are the states of a task?Easy✓
Short answer: In FreeRTOS a task is Running (executing on the CPU), Ready (able to run, waiting for the CPU), Blocked (waiting for an event or a delay, usually with a timeout), or Suspended (removed from scheduling until explicitly resumed). A deleted task's memory is freed later by the idle task.
create
│
▼
┌───────► Ready ◄──────────────┐
│ │ ▲ │ event / timeout
│ resume │ │ preempted │
│ ▼ │ │
Suspended ◄─ Running ─── wait ──► Blocked
▲ suspend │
└────────────┘ (from any state)
| State | Meaning | How it gets there |
|---|---|---|
| Running | using the CPU (one task per core) | scheduler picked it |
| Ready | could run but a higher or equal priority task is running | created, unblocked, resumed, or pre-empted |
| Blocked | waiting for time or an event | vTaskDelay, waiting on a queue/semaphore/notification, with a timeout |
| Suspended | not schedulable, no timeout | vTaskSuspend(); leaves only via vTaskResume() |
Key behaviours
- A blocked task uses no CPU; that is why tasks should block instead of polling.
- When an event arrives, the task moves to Ready, not directly to Running; it runs only if it has the highest priority among ready tasks.
- A blocked task with a timeout returns to Ready when the timeout expires, and the API call returns an error code (e.g.
pdFALSE,errQUEUE_EMPTY), which the task must check. vTaskSuspendis rarely the right tool for synchronisation: there is no timeout and it is easy to miss a resume. Prefer blocking on an event.
Zephyr names: running, ready (runnable), pending (waiting on an object), sleeping, suspended, dead.
Inspection: vTaskList() / uxTaskGetSystemState() in FreeRTOS, kernel threads in the Zephyr shell, or RTOS-aware debugger views (Ozone, STM32CubeIDE, Tracealyzer).
04What is pre-emptive versus cooperative scheduling?Easy✓
Short answer: With pre-emptive scheduling, the kernel switches to a higher-priority task as soon as it becomes ready (because of an interrupt, a timer or another task), even in the middle of the current task's code. With cooperative scheduling, a task keeps the CPU until it voluntarily yields or blocks. Pre-emption gives better worst-case response for urgent work; cooperative scheduling is simpler and needs fewer locks, but one task that does not yield stalls everything.
| Pre-emptive | Cooperative | |
|---|---|---|
| When switches happen | any time a higher-priority task is ready | only at taskYIELD(), blocking calls, or task return |
| Response time for urgent work | bounded by kernel latency | bounded by the longest code between yields |
| Shared data protection | needed everywhere data is shared | only needed around yield points (and with ISRs) |
| Risk | race conditions, priority inversion | one long-running task blocks all others |
| FreeRTOS | configUSE_PREEMPTION 1 |
configUSE_PREEMPTION 0 |
How pre-emption happens on Cortex-M: an ISR (or a task) makes a higher-priority task ready → the kernel pends PendSV → when all ISRs finish, PendSV switches context.
Mixed approaches
- FreeRTOS: pre-emptive between priorities, with optional time slicing between equal priorities.
- Zephyr: cooperative threads (negative priorities) cannot be pre-empted by other threads, which is useful for code that must not be interrupted by other threads but still allows ISRs; pre-emptible threads (non-negative priorities).
- Event-driven "run-to-completion" frameworks (QP active objects) are cooperative inside each object but can use pre-emption between objects.
Interview angle: most products use pre-emptive scheduling for responsiveness, plus good discipline: short critical sections, message passing, and correct priorities.
05What is time slicing / round-robin scheduling?Easy✓
Short answer: When several ready tasks share the same highest priority, the scheduler gives each a time slice and rotates between them in round-robin order. In FreeRTOS a slice is one tick (configUSE_TIME_SLICING). Tasks of different priorities are never time-sliced against each other: the higher-priority ready task always runs.
Example (1 ms tick, three tasks at priority 2, one at priority 3)
Priority 3 task blocked: A B C A B C A ... (each runs for one tick in turn)
Priority 3 task ready: H H H ... (A, B, C get nothing until H blocks)
Details
- A task that blocks before its slice ends gives up the rest of it; the next equal-priority task runs immediately.
- A task woken by an event does not necessarily run at once if a task of the same priority is running (it runs at the next tick or yield), unless the port/config says otherwise (
configUSE_PREEMPTIONwith yields on equal priority). - The idle task shares priority 0 with application tasks at priority 0;
configIDLE_SHOULD_YIELDcontrols whether it yields to them.
When to use equal priorities with time slicing: background jobs of similar importance (logging, housekeeping). For time-critical work, give tasks distinct priorities and let them block, rather than relying on slices.
Linux comparison: SCHED_RR is round robin among equal real-time priorities (100 ms default slice); normal tasks use weighted fair scheduling instead.
06What is a context switch and what is saved?Medium✓
Short answer: A context switch saves the state of the running task so it can be resumed later, and restores the state of the next task. On Cortex-M, the hardware automatically stacks R0–R3, R12, LR, PC and xPSR when PendSV is entered; the PendSV handler saves the remaining registers R4–R11 (and the FPU registers S16–S31 if the task used the FPU) onto the task's own stack, stores the stack pointer (PSP) in the task's control block, loads the next task's PSP and registers, and returns, so the hardware unstacks the rest.
FreeRTOS-style PendSV on Cortex-M4F (simplified)
PendSV_Handler:
mrs r0, psp @ current task's stack pointer
tst lr, #0x10 @ did the task use the FPU?
it eq
vstmdbeq r0!, {s16-s31} @ save high FP registers
stmdb r0!, {r4-r11, lr} @ save callee-saved registers + EXC_RETURN
ldr r3, =pxCurrentTCB
ldr r2, [r3]
str r0, [r2] @ TCB->pxTopOfStack = psp
bl vTaskSwitchContext @ choose the next task (with BASEPRI raised)
ldr r2, [r3] @ new current TCB
ldr r0, [r2] @ its saved stack pointer
ldmia r0!, {r4-r11, lr}
tst lr, #0x10
it eq
vldmiaeq r0!, {s16-s31}
msr psp, r0
bx lr @ exception return restores r0-r3, r12, lr, pc, xPSR
What makes up a task's context: all general registers, PC, xPSR (flags), the stack pointer, FP registers and FPSCR if used, and on cores with an MPU the task's MPU regions. No address space switch is needed because tasks share memory.
Cost: on the order of 100–200 cycles (about 1 µs at 168 MHz) on Cortex-M4, more with FP context. Lazy FP stacking avoids saving FP registers for tasks that do not use the FPU.
A new task's first switch: its stack is pre-filled at creation with a fake exception frame (PC = task function, xPSR with the Thumb bit set, LR = an error handler for tasks that return), so "restoring" it starts the task.
Why PendSV at lowest priority: context switches never happen in the middle of an ISR, and multiple switch requests during nested interrupts collapse into one.
07How does an RTOS scheduler pick the next task to run?Medium✓
Short answer: It keeps one ready list per priority and a bitmap with one bit per priority that has ready tasks. To pick the next task, it finds the highest set bit (one CLZ instruction on Cortex-M3+), then takes the task at the head of that priority's list (rotating it to the end for round-robin). This makes scheduling O(1), independent of the number of tasks, which is essential for deterministic timing.
#include <stdint.h>
#define NUM_PRIO 32
typedef struct tcb { struct tcb *next; uint8_t prio; /* saved SP, stack, ... */ } tcb_t;
static tcb_t *ready_head[NUM_PRIO], *ready_tail[NUM_PRIO];
static uint32_t ready_bitmap; /* bit p set = some task of priority p is ready */
void make_ready(tcb_t *t)
{
t->next = NULL;
if (ready_tail[t->prio]) ready_tail[t->prio]->next = t; else ready_head[t->prio] = t;
ready_tail[t->prio] = t;
ready_bitmap |= 1u << t->prio;
}
tcb_t *pick_next(void) /* highest number = highest priority here */
{
if (!ready_bitmap) return NULL; /* only the idle task: handled separately */
unsigned p = 31u - (unsigned)__builtin_clz(ready_bitmap);
tcb_t *t = ready_head[p];
ready_head[p] = t->next; /* rotate for round robin among equals */
if (!ready_head[p]) { ready_tail[p] = NULL; ready_bitmap &= ~(1u << p); }
return t; /* caller re-adds it via make_ready() if still ready */
}
Other pieces of a scheduler
- Delayed list: blocked tasks with timeouts, sorted by wake-up time (FreeRTOS keeps two lists to handle tick-count overflow); the tick handler moves expired tasks to ready.
- Event lists: each queue/semaphore has a list of waiting tasks sorted by priority, so the highest-priority waiter wakes first.
- Scheduling points: tick interrupt, any API call that unblocks a higher-priority task, task blocking or yielding, ISR exit.
- Idle task when nothing else is ready.
FreeRTOS specifics: configUSE_PORT_OPTIMISED_TASK_SELECTION uses CLZ and limits priorities to 32; the generic method scans from the top priority down. Zephyr offers a simple list, a scalable red-black tree, or a multi-queue backend (CONFIG_SCHED_*).
Design-interview extension: "add resource management": combine with priority-inheritance mutexes (next questions) and admission checks for deadlines.
08What is the difference between a mutex and a binary semaphore?Medium✓
Short answer: A mutex is for mutual exclusion: it has an owner, only the task that took it may give it back, it supports priority inheritance, and it can be recursive. A binary semaphore is for signalling: it has no owner, any task or ISR can give it, and it has no priority inheritance. Use a mutex to protect a shared resource; use a semaphore (or a task notification) to signal that an event happened.
| Mutex | Binary semaphore | |
|---|---|---|
| Purpose | protect a resource (critical section) | signal an event / synchronise |
| Ownership | yes: only the holder releases it | no: anyone can give |
| Priority inheritance | yes (FreeRTOS, Zephyr) | no |
| Usable from an ISR | no (an ISR cannot own/block) | give from ISR: yes (xSemaphoreGiveFromISR) |
| Recursive variant | yes (xSemaphoreCreateRecursiveMutex) |
no |
| Initial state | available | usually empty (0) |
Typical patterns
/* mutex: protect a shared I2C bus */
if (xSemaphoreTake(i2c_mutex, pdMS_TO_TICKS(10)) == pdTRUE) {
i2c_read(...);
xSemaphoreGive(i2c_mutex);
}
/* binary semaphore: ISR signals a task */
void EXTI0_IRQHandler(void)
{
BaseType_t woken = pdFALSE;
clear_exti_flag();
xSemaphoreGiveFromISR(button_sem, &woken);
portYIELD_FROM_ISR(woken);
}
void button_task(void *p) { for (;;) { xSemaphoreTake(button_sem, portMAX_DELAY); handle_press(); } }
Why it matters: using a binary semaphore as a lock loses priority inheritance (priority inversion risk), and nothing stops another task from "unlocking" it by mistake. Using a mutex for signalling fails because the giver does not own it.
Modern FreeRTOS advice: for "ISR wakes one task", use a direct-to-task notification (vTaskNotifyGiveFromISR / ulTaskNotifyTake): faster and uses less RAM than a semaphore.
09What is a counting semaphore?Easy✓
Short answer: A semaphore whose count can go above one. Each take decrements the count (blocking at zero) and each give increments it. It is used for resource counting (initialise it to N for N identical resources) and for event counting (an ISR gives once per event, so events that arrive faster than the task handles them are not lost).
Resource counting example: a pool of 4 DMA buffers
SemaphoreHandle_t buf_sem = xSemaphoreCreateCounting(4, 4); /* max 4, initially 4 free */
uint8_t *buf_get(void)
{
if (xSemaphoreTake(buf_sem, pdMS_TO_TICKS(50)) != pdTRUE) return NULL; /* none free */
return pool_alloc(); /* guaranteed to succeed now */
}
void buf_put(uint8_t *b) { pool_free(b); xSemaphoreGive(buf_sem); }
Event counting example: a UART ISR gives the semaphore for each received line; the processing task takes it once per line. With a binary semaphore, two lines arriving before the task runs would count as one.
Limits: the count has a maximum (set at creation); giving beyond it fails. The semaphore carries no data, only a count: if you need the data too, use a queue.
Alternatives: FreeRTOS task notifications can act as a lightweight counting semaphore for a single waiting task (ulTaskNotifyTake(pdFALSE, ...) decrements by one).
10What is priority inversion?Medium✓
Short answer: Priority inversion happens when a high-priority task is blocked waiting for a resource (e.g. a mutex) held by a low-priority task, and a medium-priority task that does not need the resource pre-empts the low-priority task. The high-priority task now effectively waits for the medium-priority task, possibly for an unbounded time, which breaks the priority scheme.
Timeline
L takes mutex M
H becomes ready, pre-empts L, tries to take M → blocks
M (medium) becomes ready, pre-empts L (L holds M but has low priority)
M runs for a long time... H waits for M, although H has the highest priority
L finally runs, releases M → H runs (too late)
Bounded vs unbounded: H waiting for L to finish a short critical section is bounded inversion and acceptable. The problem is the unbounded part caused by medium-priority tasks running instead of L.
Real-world example: NASA's Mars Pathfinder (1997) kept resetting because a high-priority bus-management task waited on a mutex held by a low-priority meteorological task while medium-priority communication tasks ran; a watchdog then reset the system. Engineers enabled priority inheritance on that VxWorks mutex remotely.
Fixes:
- Priority inheritance on mutexes (next question): the default for FreeRTOS and Zephyr mutexes.
- Priority ceiling protocol.
- Keep critical sections short and avoid sharing resources across very different priorities; use message passing to a single owner task.
- Disable pre-emption or interrupts for tiny critical sections (no inversion possible, but adds latency).
Remember: semaphores (no owner) cannot do priority inheritance, so using a binary semaphore as a lock reintroduces the problem.
11How do priority inheritance and priority ceiling solve priority inversion?Hard✓
Short answer: With priority inheritance, a task that holds a mutex temporarily runs at the priority of the highest-priority task waiting for it, so medium-priority tasks cannot pre-empt it; when it releases the mutex, it drops back to its own priority. With the priority ceiling protocol, each mutex has a ceiling equal to the highest priority of any task that uses it, and a task that takes the mutex immediately runs at that ceiling. Inheritance is automatic and needs no configuration; ceiling also prevents deadlock and bounds blocking but requires knowing which tasks use each mutex.
How a mutex with priority inheritance works (sketch)
typedef struct { tcb_t *owner; wait_list_t waiters; } pi_mutex_t;
void pi_lock(pi_mutex_t *m)
{
enter_critical();
while (m->owner != NULL) {
if (current->prio > m->owner->prio)
set_effective_prio(m->owner, current->prio); /* boost the holder (also re-sorts it) */
block_on(&m->waiters, current); /* sorted by priority */
/* critical section re-entered after wake-up; loop re-checks ownership */
}
m->owner = current;
exit_critical();
}
void pi_unlock(pi_mutex_t *m)
{
enter_critical();
m->owner = NULL;
restore_prio(current); /* back to base priority, or the highest of other held mutexes' waiters */
wake_highest(&m->waiters); /* may trigger a context switch */
exit_critical();
}
Details real kernels handle: transitive inheritance (H waits for L1, which waits for L2: L2 must also be boosted), a task holding several mutexes (its priority is the maximum of all their waiters), and timeouts (a waiter giving up must lower the boost).
Comparison
| Priority inheritance | Priority ceiling (immediate) | |
|---|---|---|
| When the holder is boosted | only when a higher task actually waits | as soon as it takes the mutex |
| Configuration | none | a ceiling per mutex |
| Deadlock prevention | no | yes (for single-processor, correct ceilings) |
| Blocking bound | can be chained through several mutexes | at most one critical section |
| Run-time overhead | when contention happens | on every lock/unlock |
| Used by | FreeRTOS, Zephyr, POSIX PTHREAD_PRIO_INHERIT, Linux rt_mutex |
OSEK/AUTOSAR OS (resources), POSIX PTHREAD_PRIO_PROTECT, Ada |
Limits of both: they do not help if the lock holder runs for a long time; keep critical sections short. Neither works for semaphores used as signals.
12What is a deadlock and how do you prevent it?Medium✓
Short answer: A deadlock occurs when two or more tasks each wait for a resource held by another, so none can proceed. It needs all four Coffman conditions: mutual exclusion, hold-and-wait, no pre-emption of resources, and circular wait. Prevent it by breaking at least one condition, most practically by always taking locks in the same global order.
Classic example
/* Task A */ /* Task B */
take(mutex_spi); take(mutex_flash);
take(mutex_flash); /* waits */ take(mutex_spi); /* waits: deadlock */
Prevention strategies
| Strategy | Breaks | Practice |
|---|---|---|
| Global lock ordering | circular wait | document an order (e.g. always SPI before flash); lockdep in Linux checks it at run time |
| Take all locks at once, or none | hold and wait | try each lock; on failure release everything and retry later |
Timeouts on take |
indefinite waiting | detect and recover (log, release, retry); not a full fix |
| Single owner / message passing | mutual exclusion on shared state | one task owns the SPI bus; others send requests via a queue |
| Priority ceiling protocol | circular wait (uniprocessor) | in OSEK/AUTOSAR systems |
| Avoid nested locks | most cases | keep critical sections small and flat |
Detection: watchdogs (tasks stop checking in), tracing tools showing tasks blocked forever on mutexes, FreeRTOS/Zephyr thread analyzers, and in the debugger, the RTOS-aware view listing each task's blocking object and each mutex's owner.
Related problems: the dining philosophers (five philosophers, five forks, each needs two) is the textbook deadlock: fix it by ordering resources (always pick the lower-numbered fork first) or limiting how many can try at once.
Self-deadlock: a task taking a non-recursive mutex it already holds, or an ISR waiting on something a task must release. Recursive mutexes fix the first (but often hide a design problem).
13What is a livelock and what is starvation?Medium✓
Short answer: In a livelock, tasks keep running and changing state but make no progress, for example two tasks that detect a conflict, both back off and retry at the same moment forever. Starvation means a task never gets the CPU or a resource because others always win; in a fixed-priority RTOS, a high-priority task that never blocks starves every lower-priority task, including the idle task.
Livelock examples and fixes
- Two tasks using "try lock A then B; if B fails, release A and retry" in lock-step: add random back-off or a fixed lock order.
- Network devices retransmitting after collisions at the same time: Ethernet's randomised exponential back-off exists to avoid exactly this.
- Two tasks politely yielding to each other forever.
Starvation examples and fixes
| Cause | Fix |
|---|---|
| High-priority task busy-waits/polls | make it block on events; never poll in a high-priority task |
| Too much work at high priority | move work to lower priorities, reduce rates, or a faster CPU |
| Unfair locks (spinlocks, readers starving writers) | fair locks (ticket, FIFO), writer preference |
| Aging not used in priority schedulers | increase the priority of long-waiting tasks over time (general-purpose OSes) |
Symptoms of starvation in an RTOS: the idle task never runs (CPU load 100%), the watchdog starts resetting the system, low-priority logging or communication stops, and vApplicationIdleHook (where you might sleep or kick a watchdog) never executes.
Monitoring: measure CPU load and per-task run time (configGENERATE_RUN_TIME_STATS), and track the stack and execution time of high-priority tasks.
14What is a message queue and when should you use one?Easy✓
Short answer: A message queue is a thread-safe FIFO that copies fixed-size items from senders to receivers, with blocking and timeouts on both ends. Queues decouple producers and consumers, absorb bursts, and avoid shared global data and locks. In FreeRTOS, xQueueSend copies the item into the queue, so for large data send a pointer (and define who owns and frees the buffer).
typedef struct { uint8_t type; int16_t value; uint32_t timestamp; } sensor_msg_t;
QueueHandle_t q = xQueueCreate(16, sizeof(sensor_msg_t));
/* producer task */
sensor_msg_t m = { .type = 1, .value = read_temp(), .timestamp = xTaskGetTickCount() };
if (xQueueSend(q, &m, pdMS_TO_TICKS(5)) != pdTRUE) { /* queue full: count/drop/handle */ }
/* consumer task */
sensor_msg_t r;
if (xQueueReceive(q, &r, portMAX_DELAY) == pdTRUE) process(&r);
When to use a queue
- One or more producers send data/commands to a task that owns a resource (e.g. all display updates go to the display task).
- Interrupts hand data to tasks (
xQueueSendFromISR). - You want back-pressure: a full queue blocks or fails the sender instead of losing data silently.
Design points
- Sizing: depth = worst-case burst rate × worst-case consumer delay, plus margin. Monitor the high-water mark (
uxQueueMessagesWaiting). - Copy vs reference: copying small items (≤ 16–32 bytes) is simple and safe; for large buffers, send pointers from a pool and pass ownership.
- Timeouts: always choose a timeout and handle failure.
- Queue sets (FreeRTOS) or
k_poll(Zephyr) let one task wait on several queues/semaphores.
Related FreeRTOS objects
- Stream buffers: a byte stream (like a pipe) for one writer and one reader, efficient for UART data.
- Message buffers: variable-length messages built on stream buffers.
- Mailbox: a queue of length 1 that is overwritten (
xQueueOverwrite) to always hold the latest value.
15What are stream buffers and message buffers, and when are they better than queues?Medium✓
Short answer: FreeRTOS stream buffers pass a continuous stream of bytes from exactly one writer to exactly one reader (like a pipe), and the reader can block until a trigger level of bytes is available. Message buffers are built on stream buffers and pass variable-length messages (each with a length prefix). Both are lighter than queues because they assume a single writer and single reader (no locking among multiple writers), and they copy only as many bytes as needed rather than fixed-size items.
| Queue | Stream buffer | Message buffer | |
|---|---|---|---|
| Item | fixed size | bytes | variable-length messages |
| Writers/readers | many/many | one/one | one/one |
| Typical use | commands, events | UART/USB byte streams, audio | packets, variable-size records |
| ISR use | yes | yes (one side in an ISR) | yes |
| Overhead | higher | low | low |
StreamBufferHandle_t rx = xStreamBufferCreate(256, 1); /* size, trigger level */
void USART2_IRQHandler(void)
{
BaseType_t woken = pdFALSE;
uint8_t b = (uint8_t)USART2->DR;
xStreamBufferSendFromISR(rx, &b, 1, &woken);
portYIELD_FROM_ISR(woken);
}
void rx_task(void *p)
{
uint8_t buf[64];
for (;;) {
size_t n = xStreamBufferReceive(rx, buf, sizeof buf, portMAX_DELAY);
parse(buf, n);
}
}
Rules: if more than one task (or ISR) writes, protect the writes with a critical section or a mutex, because the implementation assumes one writer. With message buffers, the receive buffer must be large enough for the next message, or the call returns 0 and the message stays.
Zephyr equivalents: k_pipe, k_msgq (fixed size), and ring_buf with semaphores.
16What are event groups / event flags?Medium✓
Short answer: An event group is a set of bits (flags) that tasks can set, clear and wait on with AND ("wait until bits 0 and 2 are both set") or OR ("wait until any of bits 0–3 is set") semantics, optionally clearing them on exit. They synchronise a task with several conditions at once, or several tasks at a barrier (xEventGroupSync). Unlike a counting semaphore, setting a bit that is already set does not count twice.
#define EV_WIFI_UP (1u << 0)
#define EV_TIME_SYNC (1u << 1)
#define EV_CERT_OK (1u << 2)
EventGroupHandle_t sys_events = xEventGroupCreate();
/* cloud task waits until all three are ready */
EventBits_t bits = xEventGroupWaitBits(sys_events,
EV_WIFI_UP | EV_TIME_SYNC | EV_CERT_OK,
pdFALSE, /* do not clear on exit */
pdTRUE, /* wait for ALL bits */
pdMS_TO_TICKS(30000));
if ((bits & (EV_WIFI_UP | EV_TIME_SYNC | EV_CERT_OK)) != (EV_WIFI_UP | EV_TIME_SYNC | EV_CERT_OK)) {
/* timeout: handle */
}
Uses: system start-up readiness (network up, time synced), state broadcasts to several tasks, barriers in multi-step pipelines, and waiting for one of several sources.
Details and pitfalls
- Several tasks can wait on the same group, and setting bits can unblock all of them (a broadcast), which semaphores cannot do.
- Setting bits from an ISR (
xEventGroupSetBitsFromISR) is deferred to the timer service task in FreeRTOS, because waking an unknown number of tasks is not a bounded operation; that adds latency. - Because bits are not counted, two events of the same type before the task runs merge into one. If you must count, use a semaphore or queue.
- The number of usable bits is limited (24 with 32-bit ticks in FreeRTOS).
Zephyr: k_event (event objects) and k_poll for waiting on multiple kernel objects.
17What are FreeRTOS task notifications?Medium✓
Short answer: Every FreeRTOS task has a built-in 32-bit notification value and state (an array of them since v10.4). Another task or an ISR can notify the task by incrementing the value, setting bits in it, or overwriting it, and the task can block waiting for a notification. Notifications are faster and use less RAM than semaphores, queues or event groups because no separate kernel object is needed, but only the owning task can wait on them and they cannot buffer multiple data items.
As a lightweight binary/counting semaphore (ISR → task)
static TaskHandle_t rx_task_handle;
void DMA_IRQHandler(void)
{
BaseType_t woken = pdFALSE;
clear_dma_flags();
vTaskNotifyGiveFromISR(rx_task_handle, &woken); /* increment the notification value */
portYIELD_FROM_ISR(woken);
}
void rx_task(void *p)
{
for (;;) {
uint32_t n = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(100)); /* pdTRUE: clear to 0 (binary) */
if (n == 0) { /* timeout */ continue; }
process_dma_buffer();
}
}
As event bits: xTaskNotify(task, BIT_X, eSetBits) and xTaskNotifyWait(0, ULONG_MAX, &bits, timeout).
As a mailbox for a value: xTaskNotify(task, value, eSetValueWithOverwrite).
Advantages: about 45% faster than a binary semaphore and no extra RAM; ideal for "this ISR wakes that one handler task", which is the most common synchronisation in drivers.
Limitations
- Only one task (the target) can wait; you cannot broadcast to several tasks.
- One 32-bit value per index: it cannot queue several data items.
- The sender cannot block waiting for the receiver to consume a notification (no back-pressure).
- If several unrelated sources use the same notification index, they can interfere: use separate indexes or bits.
18Why must you use the FromISR API variants in an interrupt?Medium✓
Short answer: Normal RTOS API functions may block (wait for space or data), and an ISR must never block because it is not a task and cannot be suspended. The ...FromISR variants never block, use interrupt-safe critical sections, and instead of switching context immediately, they report through a pxHigherPriorityTaskWoken flag that a higher-priority task became ready; the ISR then requests a context switch at its exit with portYIELD_FROM_ISR().
void UART_IRQHandler(void)
{
BaseType_t woken = pdFALSE;
uint8_t b = (uint8_t)UART->DR;
xQueueSendFromISR(rx_queue, &b, &woken); /* never blocks: fails if the queue is full */
portYIELD_FROM_ISR(woken); /* pend PendSV if a higher-priority task was woken */
}
Why the deferred switch: the ISR may be nested inside other ISRs, and it may wake several tasks; requesting a switch once at the end (via PendSV, which runs after all ISRs) is safe and efficient. If you forget portYIELD_FROM_ISR, the woken task only runs at the next tick or scheduling point, adding up to one tick of latency.
Interrupt priority rule (Cortex-M): only interrupts with a priority numerically greater than or equal to configMAX_SYSCALL_INTERRUPT_PRIORITY (i.e. less urgent) may call FreeRTOS APIs. The kernel masks interrupts up to that level in its critical sections; a more urgent ISR could interrupt the kernel mid-update, so it must not call the API. Getting this wrong causes rare, random corruption; enable configASSERT(), which checks it in vPortValidateInterruptPriority().
Other systems: Zephyr uses the same functions from ISRs but requires K_NO_WAIT timeouts (calls with waiting fail in ISRs); CMSIS-RTOS2 functions check __get_IPSR() and behave accordingly; ThreadX allows certain services from ISRs with TX_NO_WAIT.
19What is the tick and what is a tickless idle mode?Medium✓
Short answer: The tick is a periodic timer interrupt (commonly SysTick at 1 kHz, i.e. every 1 ms) that the kernel uses to keep time, wake delayed tasks, handle timeouts, and time-slice equal-priority tasks. In tickless idle mode, when only the idle task is ready, the kernel stops the periodic tick, programs a timer to wake the CPU at the next scheduled event (or on any interrupt), enters a low-power mode, and corrects its tick count on wake-up. This saves a lot of power in battery devices.
Choosing the tick rate
| Tick | Timing resolution | Overhead |
|---|---|---|
| 100 Hz (10 ms) | coarse delays | very low |
| 1000 Hz (1 ms) | typical | moderate |
| 10 kHz | fine, but... | interrupt overhead becomes significant |
Delays are only accurate to ±1 tick: vTaskDelay(1) can return after anything between almost 0 and 1 tick, depending on where in the current tick it was called. For precise timing use hardware timers.
Tickless idle details (FreeRTOS configUSE_TICKLESS_IDLE)
- The idle task sees that the next task wake-up is N ticks away (and N ≥
configEXPECTED_IDLE_TIME_BEFORE_SLEEP). vPortSuppressTicksAndSleep()stops SysTick, programs it (or a low-power timer such as LPTIM/RTC that keeps running in stop mode) for N ticks, and executesWFI.- On wake-up (timer expiry or another interrupt), it computes how many ticks really elapsed and advances the tick count with
vTaskStepTick().
Power impact: a 1 kHz tick wakes the CPU 1000 times per second; for a device that is mostly idle, removing those wake-ups can cut average current from hundreds of µA to a few µA. Low-power ports use a low-power timer clocked from the 32 kHz crystal so the MCU can enter stop mode.
Zephyr is tickless by default (CONFIG_TICKLESS_KERNEL) and uses a timeout queue programmed into the system timer.
20What is the idle task and what is it used for?Easy✓
Short answer: The idle task is created by the kernel at the lowest priority and runs whenever no other task is ready. In FreeRTOS it frees the memory of deleted tasks and calls an optional idle hook that applications use to enter low-power sleep, do background work, or measure CPU load. It must never block or take long, because then deleted tasks are not cleaned up and the system cannot sleep.
Uses of the idle hook (vApplicationIdleHook, configUSE_IDLE_HOOK)
- Power saving: execute
__WFI()so the CPU sleeps until the next interrupt (or use tickless idle for deeper sleep). - CPU load measurement: count how often the idle loop runs, or measure time spent in idle:
load = 1 − idle_time / total_time. - Low-priority background work that must never delay real tasks (e.g. scrubbing memory, non-urgent checks). Keep it short and non-blocking.
What it must not do: call blocking APIs (it must always be ready), or run long jobs (a task of equal priority 0 would wait; and stuck idle means memory from deleted tasks never gets freed).
Diagnostic value: if the idle task never runs, some task is busy-waiting or the system is overloaded, so many teams monitor idle time as a health metric and alarm when it drops below a threshold (e.g. 20%).
Zephyr: has an idle thread too; power management integrates with it automatically (CONFIG_PM).
21How do you choose task priorities? Explain rate-monotonic scheduling.Hard✓
Short answer: For periodic tasks, rate-monotonic scheduling (RMS) assigns higher fixed priorities to tasks with shorter periods (higher rates). For independent periodic tasks with deadlines equal to their periods, RMS is optimal among fixed-priority policies, and a task set is guaranteed schedulable if total utilisation U = Σ Cᵢ/Tᵢ ≤ n(2^(1/n) − 1), which approaches 69.3% for large n. Above that bound a set may still be schedulable, which you check with exact response-time analysis.
Liu & Layland bound values
| n tasks | Bound |
|---|---|
| 1 | 100% |
| 2 | 82.8% |
| 3 | 78.0% |
| 4 | 75.7% |
| ∞ | 69.3% |
Variants
- Deadline-monotonic (DMS): priority by relative deadline (shorter deadline = higher priority); optimal when deadlines are shorter than periods.
- EDF (earliest deadline first): dynamic priorities; schedules any set with U ≤ 100% (deadline = period) but is harder to implement and behaves badly under overload (many tasks can miss). Linux
SCHED_DEADLINEuses EDF.
Practical priority assignment beyond the theory
- Interrupt handlers do minimal work and defer to tasks.
- Hard-deadline control loops get the highest task priorities, by rate.
- Communication handlers (with latency requirements) next.
- User interface, logging and housekeeping lowest.
- Tasks that only react to events get priorities based on how fast they must react (their deadline).
- Avoid many tasks at the same priority unless they are equally important.
Validate with measurement: trace tools (Percepio Tracealyzer, SEGGER SystemView) show response times, CPU load and blocking, which reveals when theory and reality differ (interrupts, blocking on shared resources, cache effects).
22How do you check whether a task set is schedulable? Give a worked example.Hard✓
Short answer: First compute utilisation and compare with the RMS bound; if it is above the bound, use response-time analysis (RTA): for each task, iterate Rᵢ = Cᵢ + Bᵢ + Σⱼ∈hp(i) ⌈Rᵢ / Tⱼ⌉ · Cⱼ (its own execution, blocking by lower-priority tasks, plus interference from every higher-priority task) until it converges. The set is schedulable if every Rᵢ ≤ Dᵢ.
Example (times in ms, RMS priorities: shortest period highest)
| Task | Period T = deadline D | WCET C | Utilisation |
|---|---|---|---|
| T1 | 10 | 3 | 0.300 |
| T2 | 15 | 4 | 0.267 |
| T3 | 40 | 10 | 0.250 |
| Total | 0.817 |
The RMS bound for 3 tasks is 0.780, and 0.817 is above it, so the simple test is inconclusive. Run RTA (no blocking):
- T1: R = 3 (no higher-priority tasks) ≤ 10 ✓
- T2: R₀ = 4 → R = 4 + ⌈4/10⌉·3 = 7 → R = 4 + ⌈7/10⌉·3 = 7 (converged) ≤ 15 ✓
- T3: R₀ = 10
- R = 10 + ⌈10/10⌉·3 + ⌈10/15⌉·4 = 10 + 3 + 4 = 17
- R = 10 + ⌈17/10⌉·3 + ⌈17/15⌉·4 = 10 + 6 + 8 = 24
- R = 10 + ⌈24/10⌉·3 + ⌈24/15⌉·4 = 10 + 9 + 8 = 27
- R = 10 + ⌈27/10⌉·3 + ⌈27/15⌉·4 = 10 + 9 + 8 = 27 (converged) ≤ 40 ✓
All tasks meet their deadlines even though utilisation is above the bound.
Adding reality
- Blocking time
Bᵢ: the longest time a lower-priority task can hold a resource that task i needs (with priority inheritance/ceiling, typically one critical section). - ISR load: treat interrupts as the highest-priority "tasks" with their own C and minimum inter-arrival time.
- Kernel overhead: add context switch time to each C (twice per job: switch in and out).
- Jitter: release jitter adds to the interference terms.
Interview tip: even if you do not remember the formula exactly, explain the idea: "a task's worst-case response is its own work plus the work of every higher-priority job released during that window plus blocking; iterate until stable and compare with the deadline."
23How much stack does a task need and how do you size it?Medium✓
Short answer: Estimate it from the worst-case call depth of the task's code (local variables, saved registers, function arguments, padding) plus the context-switch frame (16 words on Cortex-M, plus 18 more for FP context if the task uses the FPU, and on some ports a little more). Then measure under worst-case conditions with the high-water mark and add a safety margin (20–50%). Interrupts use the main stack on Cortex-M, so they do not need to be included in task stacks.
Step by step
- Static analysis: compile with
-fstack-usageto get each function's frame size, and combine with the call graph (-fcallgraph-info, puncover, or commercial tools) to get the worst path. Recursion and function pointers need manual bounds. - Add the context frame and any library calls that are stack-hungry (
printf/snprintfwith floating point can use 500 bytes to 1 KB or more). - Measure: run stress tests that exercise every path, then check
uxTaskGetStackHighWaterMark(handle)(minimum free stack ever, in words). FreeRTOS fills stacks with0xA5to measure this. - Margin: keep 25% or more free in the worst measured case, since tests rarely hit the true worst case.
Protection during development and in the field
configCHECK_FOR_STACK_OVERFLOW = 2(check pattern at each switch) andvApplicationStackOverflowHook().- MPU guard regions (FreeRTOS-MPU, Zephyr
CONFIG_HW_STACK_PROTECTION) or Armv8-M PSPLIM for immediate faults. - Log the minimum free stack per task in diagnostics.
Reducing stack needs
- Move large buffers out of local variables into static memory or pools.
- Avoid
printf-family functions in small tasks; use a logging task. - Avoid deep call chains and recursion; avoid variable-length arrays and
alloca. - Watch for large structs passed by value.
Typical values: simple tasks 128–256 words (512 B–1 KB) on Cortex-M; tasks using TLS, filesystems or networking several KB.
24What are software timers in an RTOS?Easy✓
Short answer: Software timers run a callback function after a delay, either once (one-shot) or repeatedly (auto-reload), without needing a dedicated task per timer. In FreeRTOS all timer callbacks execute in a single timer service (daemon) task, so callbacks must be short and must never block, otherwise every other timer is delayed.
static void led_timer_cb(TimerHandle_t t) { (void)t; toggle_led(); }
TimerHandle_t led_timer = xTimerCreate("led", pdMS_TO_TICKS(500), pdTRUE /* auto-reload */,
NULL, led_timer_cb);
xTimerStart(led_timer, 0);
How it works
- API calls (
xTimerStart,xTimerStop,xTimerChangePeriod,xTimerReset) send commands through the timer command queue to the daemon task, so they can fail if the queue is full; check the return value and use a block time. - The daemon task keeps active timers in a list sorted by expiry and blocks until the next one expires.
- Its priority (
configTIMER_TASK_PRIORITY) decides how promptly callbacks run relative to other tasks; set it appropriately (often high, with very short callbacks).
Good uses: LED blinking, timeouts for protocols (e.g. "no response in 2 s"), debouncing, periodic housekeeping, retry back-off, watchdog supervision.
Avoid: heavy processing or blocking calls in callbacks (send an event to a worker task instead), and using software timers for precise, jitter-free timing (use a hardware timer interrupt for that).
Resolution: limited to the tick period.
Zephyr: k_timer runs its expiry function in the system timer interrupt context (so it must be ISR-safe and very short); use k_work_delayable for work that should run in a thread.
25What is the difference between vTaskDelay and vTaskDelayUntil?Medium✓
Short answer: vTaskDelay(n) blocks the task for n ticks counted from the moment it is called, so if the task's own work takes variable time, its period drifts. vTaskDelayUntil(&last_wake, period) (or xTaskDelayUntil) blocks until an absolute time last_wake + period and then updates last_wake, giving a fixed-frequency loop with no drift.
/* drifts: period = 10 ms + execution time (variable) */
for (;;) {
control_step();
vTaskDelay(pdMS_TO_TICKS(10));
}
/* fixed 10 ms period, independent of execution time */
TickType_t last = xTaskGetTickCount();
for (;;) {
vTaskDelayUntil(&last, pdMS_TO_TICKS(10));
control_step();
}
Timeline (control step takes 2–4 ms)
vTaskDelay: run(3) wait(10) run(2) wait(10) run(4) wait(10) → wake times 13, 25, 39 ...
vTaskDelayUntil: wake at 10, 20, 30, 40 ... regardless of run time
Details
- If the task overruns (work takes longer than the period),
vTaskDelayUntilreturns immediately and the task runs late;xTaskDelayUntilreturnspdFALSEin that case, so you can detect overruns. - Neither gives better than ±1 tick precision, and higher-priority tasks or interrupts can delay the actual start (jitter). For tight timing (e.g. a 1 kHz control loop with low jitter), trigger the task from a hardware timer interrupt with a notification.
vTaskDelay(0)just yields.
Zephyr equivalents: k_sleep() / k_msleep() (relative) and k_sleep(K_TIMEOUT_ABS_MS(...)) or periodic k_timer + k_timer_status_sync() for absolute timing.
27How do you measure task execution time and CPU load in an RTOS?Medium✓
Short answer: Use the RTOS's run-time statistics (FreeRTOS configGENERATE_RUN_TIME_STATS with a fast timer, then vTaskGetRunTimeStats()), measure idle time to compute CPU load (load = 1 − idle time / total time), toggle GPIOs at task entry/exit and watch them on a logic analyser, or use trace tools (Percepio Tracealyzer, SEGGER SystemView) that record every context switch and interrupt with timestamps.
FreeRTOS run-time stats
/* FreeRTOSConfig.h */
#define configGENERATE_RUN_TIME_STATS 1
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() dwt_init() /* e.g. DWT cycle counter */
#define portGET_RUN_TIME_COUNTER_VALUE() (DWT->CYCCNT)
/* somewhere in a debug command */
char buf[512];
vTaskGetRunTimeStats(buf); /* "Task AbsTime %Time" per task, including IDLE */
The counter should run 10–100× faster than the tick for useful resolution.
Measuring a single function's WCET
uint32_t t0 = DWT->CYCCNT;
filter_update();
uint32_t cycles = DWT->CYCCNT - t0;
if (cycles > max_cycles) max_cycles = cycles; /* keep the worst seen */
Run it under worst-case conditions (all interrupts active, caches cold) and record the maximum, not the average.
GPIO method: set a pin high at task start, low at the end; an oscilloscope or logic analyser shows execution time, period and jitter for several tasks at once, with no software overhead to speak of. Great for checking real-time behaviour and interrupt latency.
Trace tools show the whole timeline: which task ran when, blocking reasons, response times and missed deadlines. They are the fastest way to understand priority problems.
CPU load target: keep peak load below about 70–80% to leave headroom for worst-case bursts and future features, and alarm in diagnostics if idle time falls below a threshold.
28How is memory allocated in an RTOS, and why prefer static allocation?Medium✓
Short answer: RTOS objects (task stacks and control blocks, queues, semaphores, timers) can be allocated dynamically from an RTOS heap or statically from memory you provide. Safety-critical and long-running systems prefer static allocation (or allocation only at start-up) because it cannot fail at run time, cannot fragment, is deterministic, and makes RAM usage visible at link time.
FreeRTOS heap schemes
| Scheme | Free? | Coalescing | Notes |
|---|---|---|---|
heap_1 |
no | – | allocate only; simplest and deterministic; good for "create everything at start-up" |
heap_2 |
yes | no | legacy; fragments; avoid |
heap_3 |
yes | depends | wraps the C library malloc/free with scheduler suspension |
heap_4 |
yes | yes | first-fit with merging of adjacent blocks; most common |
heap_5 |
yes | yes | like heap_4 but spans several non-contiguous RAM regions |
Static allocation
static StackType_t sensor_stack[256];
static StaticTask_t sensor_tcb;
TaskHandle_t h = xTaskCreateStatic(sensor_task, "sensor", 256, NULL, 3, sensor_stack, &sensor_tcb);
static uint8_t q_storage[16 * sizeof(msg_t)];
static StaticQueue_t q_struct;
QueueHandle_t q = xQueueCreateStatic(16, sizeof(msg_t), q_storage, &q_struct);
Enable with configSUPPORT_STATIC_ALLOCATION (and optionally disable dynamic allocation entirely with configSUPPORT_DYNAMIC_ALLOCATION 0).
Guidelines
- Create all tasks and kernel objects during initialisation; never create/delete them repeatedly at run time.
- For variable-size application data, use fixed-size memory pools (Zephyr
k_mem_slab, ThreadX block pools) instead of a general heap. - Monitor the heap (
xPortGetMinimumEverFreeHeapSize()) and definevApplicationMallocFailedHook()if any dynamic allocation remains. - Make sure the C library's
malloc(used byprintf, some drivers) is either thread-safe (newlib locks via__malloc_lock) or not used from multiple tasks.
29How does an RTOS work on a multi-core (SMP) processor?Hard✓
Short answer: In SMP (symmetric multiprocessing) mode, one RTOS kernel schedules tasks across several cores sharing memory: on each core it runs the highest-priority ready task that is allowed to run there. This adds new concerns: kernel data structures need spinlocks instead of only disabling interrupts, tasks of different priorities can run at the same time, core affinity can pin tasks to cores, and caches and memory ordering must be handled. In AMP (asymmetric) mode, each core runs its own independent OS or bare-metal code and they communicate through shared memory and inter-core interrupts.
SMP (FreeRTOS SMP since v11, Zephyr SMP, ThreadX SMP)
- Locks: a task-level critical section on one core does not stop the other core, so the kernel uses spinlocks (with interrupt masking) for its data.
- Priority assumptions break: in single-core code, "a high-priority task never runs at the same time as a low-priority task" was often relied on to avoid locks. On SMP both can run in parallel, so shared data needs real protection.
- Affinity:
vTaskCoreAffinitySet()(FreeRTOS) ork_thread_cpu_mask_*(Zephyr) restricts tasks to cores, e.g. dedicate one core to a time-critical control loop. - Inter-processor interrupts: used to ask another core to reschedule.
- Caches: cores need coherent caches (or uncached shared memory) and atomics with proper barriers.
AMP (common on heterogeneous SoCs)
- e.g. Cortex-M7 + Cortex-M4 in STM32H7 dual-core, Cortex-A + Cortex-M in i.MX 8M / STM32MP1, or two Cortex-R cores in lock-step pairs.
- Each core runs its own RTOS (or Linux on one, RTOS on the other).
- Communication via OpenAMP/RPMsg (shared-memory ring buffers + mailbox interrupts), hardware semaphores (HSEM) for shared peripherals.
- Simpler isolation and certification, but load cannot be balanced automatically.
Lock-step (Cortex-R5/R52 in safety MCUs) is different again: two cores execute the same code in parallel and hardware compares results to detect faults; it is one logical CPU for software.
30What are common RTOS design mistakes?Medium✓
Short answer: The usual mistakes are: too many tasks; busy-waiting instead of blocking; blocking or slow calls in ISRs; wrong interrupt priorities relative to the kernel's syscall priority; holding mutexes during slow I/O or taking them in inconsistent orders; sharing globals without protection; undersized stacks; ignoring API return values and timeouts; and creating/deleting tasks and objects at run time.
| Mistake | Consequence | Better approach |
|---|---|---|
| A task per small job | wasted stacks, more switches and sharing | group related work; use event-driven tasks |
Polling loops (while (!flag);) |
starves lower priorities, burns power | block on notifications/queues |
printf, malloc, blocking APIs in ISRs |
crashes, deadlocks, latency | defer to tasks; use FromISR APIs |
ISR priority above configMAX_SYSCALL_INTERRUPT_PRIORITY calling the API |
rare random corruption | follow the priority rule; enable configASSERT |
| Holding a mutex during flash writes or network calls | priority inversion, latency for others | copy data, release, then do slow work; single-owner tasks |
| Inconsistent lock order | deadlock | global lock ordering; message passing |
| Unprotected shared globals | race conditions | mutex, critical section, or atomics |
| Stack sizes guessed once | random crashes later | measure high-water marks, overflow checks |
portMAX_DELAY everywhere |
silent hangs | finite timeouts and error handling |
Ignoring return values (xQueueSend failed) |
lost data | check and count failures |
| Dynamic creation/deletion at run time | fragmentation, leaks | create at init; static allocation |
Using vTaskDelay for periodic loops |
drift | vTaskDelayUntil or timer-driven tasks |
| Not watching CPU load / idle time | unnoticed overload | run-time stats, trace, watchdog supervision |
Interview tip: pick two or three from your own experience and describe how you found them (trace tools, high-water marks, configASSERT) — that is more convincing than reciting the list.
31How does Zephyr differ from FreeRTOS?Medium✓
Short answer: FreeRTOS is a small, portable kernel (scheduler, queues, semaphores, timers, event groups) with optional add-on libraries; drivers usually come from the silicon vendor's SDK, and configuration is a header file. Zephyr is a full RTOS platform under the Linux Foundation: it uses Kconfig and devicetree (like Linux) for configuration, has a unified driver model and many in-tree drivers, networking (IPv4/IPv6, Bluetooth LE, Thread/Matter), file systems, a shell, logging, power management, MCUboot integration and optional user-mode isolation, built with CMake and the west tool. Zephyr does more out of the box but is larger and has a steeper learning curve.
| FreeRTOS | Zephyr | |
|---|---|---|
| Scope | kernel + optional libraries | complete OS platform |
| Configuration | FreeRTOSConfig.h |
Kconfig (prj.conf) + devicetree + board definitions |
| Drivers | vendor SDK/HAL, or your own | in-tree, generic APIs (gpio_pin_set_dt, i2c_write_read_dt) |
| Build | any (Make, CMake, IDE projects) | CMake + west meta-tool |
| Networking / BLE | separate (FreeRTOS+TCP, vendor stacks, lwIP) | integrated IP stack, Bluetooth host/controller, OpenThread |
| Memory protection | FreeRTOS-MPU port | user mode threads with MPU/MMU (CONFIG_USERSPACE) |
| Footprint | very small (≈ 5–10 KB kernel) | larger, but highly configurable (minimal builds of ~10 KB are possible) |
| Licensing | MIT | Apache 2.0 |
| Learning curve | gentle | steep (devicetree, Kconfig, west) |
| Governance | maintained by Amazon (AWS) | Linux Foundation project with many vendors |
When to choose which: FreeRTOS for small, simple products, existing vendor SDK-based projects, or teams that want minimal abstraction. Zephyr for connected products (BLE, Thread, Matter, IP), portability across many boards, a Linux-like development model, and long-term vendor-neutral support (Nordic's nRF Connect SDK is built on Zephyr).
Others worth knowing: Eclipse ThreadX (formerly Azure RTOS, with safety certifications), NuttX (POSIX-compatible, used by PX4 drones), RIOT (IoT), Mbed OS (end of life), and commercial safety RTOSes (SafeRTOS, VxWorks, QNX, INTEGRITY).