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.

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

  1. What is priority inversion and how is it solved?
  2. Queue or global variable: how should two tasks share data?
  3. 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

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.

Download BoardPilot Try the lessons in your browser