Q&A-1Design and validate RTOS behavior for thread states.
Answer
Assumptions. Single core, preemptive priority scheduling. FreeRTOS 11.x on Cortex-M4F; Zephyr 3.x where it differs. configTICK_RATE_HZ = 1000 (1 ms tick), configMAX_PRIORITIES = 5, configUSE_PREEMPTION = 1, configUSE_TIME_SLICING = 1, configUSE_MUTEXES = 1, INCLUDE_vTaskSuspend = 1.
The model. A thread is in exactly one state, and in both kernels that state is literally which list its TCB sits on: the ready list for its priority, a blocked list owned by the object it waits on, the delayed list (a timeout), or the suspended list. The scheduler runs the highest-priority TCB on a ready list; equal priorities round-robin. Blocking is a list move, not a spin: that is why a blocked thread costs no CPU, and it is the invariant to validate against. Design around three rules: every block has a bounded wakeup, no unbounded priority inversion, and state is never read without a critical section.
| State | FreeRTOS | Zephyr | Entered by | Left by |
|---|---|---|---|---|
| Running | eRunning | _THREAD_RUNNING | scheduler picks it | preemption, block, yield |
| Ready | eReady | _THREAD_READY | create, unblock, preempt | scheduler picks it |
| Blocked | eBlocked | _THREAD_PENDING | xQueueReceive, xSemaphoreTake | event or timeout |
| Sleeping | eBlocked + timeout | _THREAD_SLEEPING | vTaskDelay, k_sleep | tick expiry |
| Suspended | eSuspended | _THREAD_SUSPENDED | vTaskSuspend, k_thread_suspend | vTaskResume, k_thread_resume |
| Deleted | eDeleted (TCB awaiting idle-task free) | _THREAD_DEAD | vTaskDelete, thread return | idle task frees TCB, k_thread_join |
FreeRTOS has no separate sleeping state: vTaskDelay blocks the task with a timeout, so eTaskGetState() reports eBlocked. Read state with eTaskGetState() (INCLUDE_eTaskGetState = 1) or uxTaskGetSystemState() (configUSE_TRACE_FACILITY = 1); in Zephyr, k_thread_state_str() (CONFIG_THREAD_MONITOR).
Worked trace.
/* FreeRTOS 11.x, Cortex-M4F */
#define PRIO_FEEDER 3
#define PRIO_WORKER 2
static QueueHandle_t q; /* xQueueCreate(4, sizeof(uint32_t)) */
static void worker(void *arg)
{
uint32_t item;
for (;;) {
if (xQueueReceive(q, &item, portMAX_DELAY) == pdTRUE) {
process(item);
}
}
}
static void feeder_isr(void)
{
BaseType_t woken = pdFALSE;
uint32_t item = read_hw();
xQueueSendFromISR(q, &item, &woken);
portYIELD_FROM_ISR(woken);
}| t (ms) | Event | worker TCB | ready list | queue count |
|---|---|---|---|---|
| 0.00 | xQueueReceive, queue empty | eBlocked | idle (prio 0) | 0 |
| 0.40 | ISR: xQueueSendFromISR | eBlocked → eReady | worker (2), idle (0) | 1 |
| 0.40 | ISR exit, PendSV | eRunning | idle (0) | 0 |
At t = 0.40 the ISR copies the item into the queue storage, then xTaskRemoveFromEventList() moves the worker TCB from the queue's xTasksWaitingToReceive list to ready list[2] and returns pdTRUE because the worker's priority (2) beats the interrupted task's (0). portYIELD_FROM_ISR pends PendSV; on ISR exit the worker resumes inside xQueueReceive, copies the item out, and returns pdTRUE. The item was latched in the queue, so nothing is lost. If the ISR fires before the worker blocks, the count is already 1 and xQueueReceive returns without blocking: same outcome.
A lost wakeup needs a signal that does not latch. Queues and semaphores latch, so the check-then-block race is safe with them. It bites when a binary semaphore counts events: two xSemaphoreGiveFromISR calls before the worker runs leave the count at 1, and the second event is gone. Use xSemaphoreCreateCounting or a queue of length N.
Trade-offs. A mutex (xSemaphoreCreateMutex) has priority inheritance; a binary semaphore does not. With H(3), M(2), L(1): L takes the mutex, H preempts and blocks on it, M becomes ready and preempts L. L never releases, so H waits out all of M. Inheritance raises L to 3 so it finishes and releases; the holder takes the priority of the highest-priority waiter. Zephyr's k_mutex inherits too; CONFIG_PRIORITY_CEILING switches to the ceiling protocol. Use a mutex for a shared resource, a semaphore for signalling. Tickless idle (configUSE_TICKLESS_IDLE, Zephyr CONFIG_TICKLESS_IDLE) stops the tick when idle; the cost is that every timeout now depends on reprogramming the wake timer correctly.
Failure modes and validation.
| Failure | Symptom | Detection | Cost |
|---|---|---|---|
| Lost wakeup | task blocked forever | watchdog heartbeat, block timeout | missed deadline |
| Priority inversion | H never runs | runtime stats show H starved | deadline miss |
| Suspended mutex holder | all waiters block | xSemaphoreTake timeout | deadlock |
| Stale handle | crash, corruption | configASSERT, MPU, handle validation | memory corruption |
| Torn state read | wrong state in debugger | eTaskGetState() (takes a critical section) | bad diagnosis |
ISR above configMAX_SYSCALL_INTERRUPT_PRIORITY | configASSERT or corruption | check NVIC priority vs the config value | kernel corruption |
Validate with Percepio Tracealyzer or SEGGER SystemView, then check the recorded transition sequence matches the table above. Toggle a GPIO in the ISR and at the worker's first instruction after wakeup; a scope gives wakeup latency end to end. At 100 MHz, 1 µs is 100 cycles, and a PendSV switch is roughly 100–200 cycles including tail-chaining, so expect low single-digit microseconds: measure it, don't assume it. If the delta is near a full tick instead, the ISR is not preempting: check its NVIC priority against configMAX_SYSCALL_INTERRUPT_PRIORITY. Inject faults: vTaskSuspend the mutex holder and assert the waiter times out. Stress with a 10 kHz ISR giving the semaphore and compare gives against wakeups. Note vTaskDelay(1) unblocks at the next tick boundary, so the elapsed delay is anywhere from just over zero to a full tick period depending on where in the tick period the call lands.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.