Operating Systems
Introduction
An operating system (OS) manages CPU time, memory, devices, and isolation between programs. On embedded targets you may run bare-metal (no OS), an RTOS (FreeRTOS, Zephyr), or embedded Linux. Understanding processes, scheduling, virtual memory, IPC, and drivers explains why your task starves, why malloc fails, and how a sensor driver fits the stack.
OS roles
| Function | Description |
|---|---|
| Process/thread management | Create, schedule, terminate execution units |
| Memory management | Allocate RAM, virtual memory, protection |
| I/O management | Uniform interface to devices via drivers |
| File systems | Persistent storage abstraction (Linux) |
| Security / isolation | User vs kernel mode, permissions |
Bare-metal firmware handles only what you implement; an OS centralizes these services.
Processes and threads
| Process | Thread | |
|---|---|---|
| Memory | Own address space | Shares process memory |
| Overhead | Higher (context switch) | Lower |
| Communication | IPC mechanisms | Shared variables (with locks) |
| Embedded | Less common on MCU | RTOS tasks are thread-like |
RTOS task ≈ thread with its own stack and priority.
Scheduling
The scheduler picks which ready task runs next.
| Algorithm | Idea | Embedded use |
|---|---|---|
| Round-robin | Equal time slices | Fairness, soft RT |
| Fixed priority | Highest ready priority runs | Most RTOS default |
| RMS | Shorter period → higher priority | Periodic control loops |
| EDF | Earliest deadline first | Dynamic priorities |
| Multilevel feedback | Desktop Linux — balance interactive/batch | Not typical on MCU |
Rate Monotonic Scheduling (RMS)
If tasks are periodic and independent, assign priority inversely to period — shorter period gets higher priority. Utilization bound for n tasks on one CPU: U ≤ n(2^(1/n) − 1) (e.g. ~69% for large n).
Earliest Deadline First (EDF)
Dynamic: task with nearest absolute deadline runs. Can achieve higher utilization than RMS but needs runtime support.
See Real-time systems.
Virtual memory and paging
MMU maps virtual addresses to physical frames using page tables.
| Concept | Purpose |
|---|---|
| Page | Fixed-size block (e.g. 4 KB) |
| Page fault | Access not in RAM — OS loads from disk (Linux) |
| Swap | Disk backing for evicted pages |
| TLB | Cache for page table lookups |
MCU without MMU: flat physical addresses only — no swap, simpler but no process isolation.
Embedded Linux on Cortex-A: paging enabled; firmware on Cortex-M usually not.
Inter-process communication (IPC)
| Mechanism | Use |
|---|---|
| Pipes / FIFOs | Byte streams between processes |
| Message queues | Structured messages (RTOS queues) |
| Shared memory | Fast bulk data — needs synchronization |
| Semaphores / mutexes | Mutual exclusion, signaling |
| Signals | Async notifications (Unix) |
RTOS pattern: queue from ISR to task, mutex around shared sensor buffer.
Device drivers
A driver translates OS requests into hardware register operations:
1 | |
| Layer | Example |
|---|---|
| User app | read("/dev/i2c-1", ...) |
| Kernel driver | Linux i2c-dev |
| HAL | Vendor or CMSIS calls |
| Hardware | I2C peripheral |
On bare-metal, your firmware is the driver stack.
RTOS vs general-purpose OS
| RTOS (FreeRTOS, etc.) | Linux | |
|---|---|---|
| Footprint | KB–few MB | tens–hundreds MB |
| Determinism | Designed for bounded latency | Best-effort + PREEMPT_RT patches |
| API | Tasks, queues, semaphores | POSIX, files, sockets |
| Use case | MCU control loops | Gateways, HMI, vision |
Hybrid: MCU runs RTOS + Linux on application processor via RPMsg — common in automotive/industrial SoCs.
Relevant topics
- Real-time systems
- Virtualization
- Input/output and DMA
- Firmware architecture
- FreeRTOS documentation
- Operating system (OSDev wiki)
Starting points
- Port a blinky superloop to two RTOS tasks with different priorities — observe preemption.
- Measure stack high-water mark per task (
uxTaskGetStackHighWaterMark). - Use a mutex around shared struct — demonstrate deadlock risk with wrong lock order.
- On Linux SBC:
top,strace, and/procfor process view.
Focus points
- Priority inversion — low task holds mutex high task needs; use priority inheritance.
- ISR rules — minimal work; defer to task via queue.
- Stack size — each task needs margin; overflow corrupts silently.
- malloc in RTOS — fragmentation over long run; consider pools.
Key points
- OS manages CPU, memory, I/O, and isolation; RTOS targets deterministic embedded workloads.
- Scheduling (fixed priority, RMS, EDF) assigns CPU to ready tasks.
- Paging provides virtual memory on MMU-equipped systems; MCUs often use flat maps.
- Drivers and IPC connect applications to hardware and to each other safely.