Foundations · Lesson 8 of 11 · about 15 min
RTOS: many jobs on one chip
Tasks, priorities and queues
A device may need to read a sensor every 10 ms, update a slow screen and send data, all at once. In a simple loop, a slow job makes the others late. A real-time operating system (RTOS) solves this.
- A task is a function with its own endless loop, like a thread.
- The scheduler runs the most important task that is ready, even if it must pause another one.
- A delay puts a task to sleep so the others can use the CPU.
- A queue passes data safely from one task to another.
- A mutex protects something shared, such as the UART, so two tasks do not use it at the same time.
void SensorTask(void *p) {
for (;;) {
float t = read_temperature();
xQueueSend(tempQueue, &t, 0); // hand the value to the display task
vTaskDelay(pdMS_TO_TICKS(10)); // sleep 10 ms, the CPU is free
}
}
xTaskCreate(SensorTask, "sensor", 256, NULL, 3, NULL); // high priority
xTaskCreate(DisplayTask, "display", 256, NULL, 1, NULL); // low priority
vTaskStartScheduler(); // never returns
Real-time does not mean fast. It means predictable: the important task always runs within a known time. The scheduler itself is driven by a timer interrupt.
Interview questions
- What is priority inversion and how is it solved?
- Queue or global variable: how should two tasks share data?
- When is a simple loop better than an RTOS?
No answers here on purpose: try answering out loud first. In the BoardPilot app, the interview coach reads your own answer and tells you what was right, what is missing and what a senior interviewer would ask next (it uses the AI provider you choose in the app). Embedded systems interview questions →
Key terms
RTOS (real-time operating system) Task Scheduler Queue Mutex Semaphore Priority inversion
In the app, the lessons come alive
The lessons have interactive demos and hands-on labs checked on your board in the app. These pages are the readable version: the demos are described in words, and the labs need the app.