How an RTOS schedules tasks

A real-time operating system (RTOS) organizes firmware into tasks, each with a priority and a state such as ready, running or blocked. Most RTOSes use preemptive priority scheduling: when an interrupt or a message makes a higher-priority task ready, the kernel saves the running task's context and switches immediately, and the interrupted task later resumes where it stopped. A periodic tick from a hardware timer, often 1 ms, drives timeouts and delays; for example, 16 MHz divided by a prescaler of 16 gives 1 µs counts, and an interrupt every 1000 counts gives the 1 ms tick.

Real-time means predictable, not just fast

The goal is a bounded, predictable response time for critical work such as motor control or communication protocols, not maximum average throughput. A general-purpose OS optimizes fairness and throughput and may delay a task unpredictably; an RTOS keeps its kernel small and its scheduling decisions deterministic, which also lets it run on microcontrollers with little memory. Without any OS, firmware often uses a super loop: a while (1) loop calls each job in turn while interrupts handle urgent events and set flags. It is simple and lean, but one slow job delays everything, which is when an RTOS becomes worthwhile.

Communication between tasks

An RTOS supplies primitives so tasks can cooperate without busy-waiting. A message queue lets a sensor task post readings while a display task sleeps in the blocked state, using no CPU, until data arrives; buffering several items absorbs temporary speed differences so nothing is lost, as long as the queue's capacity is not exceeded. Mutexes protect shared resources such as a bus or a buffer, semaphores signal events, often from interrupt handlers, and timed waits let a task block with a timeout. Using these instead of shared variables and polled flags avoids data races and wasted cycles.

Priority inversion and inheritance

Priority inversion occurs when a high-priority task waits for a mutex held by a low-priority task, and an unrelated medium-priority task preempts the low one. The low task cannot finish and release the mutex, so the high task is effectively blocked by the medium task for an unbounded time. This caused repeated system resets on the Mars Pathfinder lander. Priority inheritance fixes it by temporarily raising the mutex holder to the waiting task's priority until it releases the resource, and many RTOS mutexes provide it as a built-in option.

Try it: questions about Real-time operating system (RTOS)

What is the main purpose of using an RTOS (real-time operating system) in embedded devices?

  1. ATo display attractive windows on a screen
  2. BTo make average processing speed always faster than a PC OS
  3. CTo switch among multiple tasks by priority so the system can respond within set time limits
Show answer and explanation

Answer: C. To switch among multiple tasks by priority so the system can respond within set time limits

An RTOS divides work into tasks and switches to a higher-priority task as soon as it becomes ready to run. Its purpose is to make sure time-critical work, such as motor control or communication responses, happens reliably within set time limits. It also provides queues for passing data between tasks, mutexes for protecting resources and timed waits. Examples include FreeRTOS, Zephyr and μITRON.

In a preemptive RTOS, while a low-priority task is running, a high-priority task receives the data it was waiting for and becomes ready. What generally happens?

  1. AThe high-priority task waits until the low-priority task finishes on its own
  2. BBoth tasks stop and the microcontroller reboots
  3. CThe low-priority task is suspended midway and the high-priority task runs immediately
Show answer and explanation

Answer: C. The low-priority task is suspended midway and the high-priority task runs immediately

In a preemptive RTOS, when an interrupt or similar event makes a higher-priority task ready, the kernel saves and suspends the running low-priority task and switches to the high-priority one. When the high-priority task finishes or blocks, the low-priority task resumes where it left off. This keeps response times for high-priority work short and predictable.

FAQ

What is the difference between an RTOS and a general-purpose OS like Linux?

An RTOS is designed for predictable worst-case response times with strict priority-based preemption and a small footprint. A general-purpose OS optimizes average throughput and fairness and offers richer services but less timing determinism.

What is priority inversion in an RTOS?

A high-priority task is stuck waiting for a resource held by a low-priority task, which is itself starved by a medium-priority task. Priority inheritance temporarily boosts the holder to prevent this.

When should I use an RTOS instead of a super loop?

When there are several activities with different timing requirements and a long job in a super loop would delay time-critical work. Simple, lightly loaded firmware is often fine with a super loop and interrupts.

Questions that mention Real-time operating system (RTOS) (5)

Real-time operating system (RTOS) appears in 7 practice questions across 1 topic, including Embedded Systems & Firmware.

  1. Which describes priority inversion in an RTOS?
  2. In an RTOS, what is the advantage of using a queue (message queue) to pass data from a sensor-reading task to a display task?
  3. A timer counts a 16 MHz clock divided by 16 with a prescaler and raises an interrupt every 1000 counts. What is the interrupt interval?
  4. Which describes the "super loop" structure often seen in microcontroller programs without an OS?
  5. Which describes priority inheritance, used to prevent priority inversion?

Books on Embedded Systems & Firmware Ad

Learn hands-on: parts and kits

Building real circuits is a good way to check what you learned in the questions.

As an Amazon Associate I earn from qualifying purchases.

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary