RTOS (Real-Time Operating System)
Brief
- Simple programs run in one big loop (eg:
while(1)). An RTOS (Real-Time Operating System) lets you run multiple, indepen fdent functions (called Tasks) that seem to run at the same time. - There are four key concepts in RTOS:
- Tasks
- Independent C-functions.
- Eg: one task blinks LED every second and another task reads a sensor every 100 milliseconds. The RTOS "scheduler" switches between them.
- Queues
- Primary way tasks send data to each other.
- Eg: sensor task reads a temperature and "sends" the value to a queue. A separate "display" task "recieves" the data from the queue and shows it on the screen.
- This prevents tasks from corrupting each other's data.
- Semaphores
- Simple signal used to either synchronise tasks or protect a resource.
- Synchronisation:
- Sensor task can "give" a semaphore to signal that new data is ready.
- Processing task "takes" the semaphore, does its work, and then waits for the next signal.
- Resource Protection (Mutex):
- With resources like Serial/UART port that two tasks may try to use at the same time, a Mutex acts like a "key".
- A task must "take" the key before using the port and "give" it back when finished. This prevents garbled data.
- Scheduler
- This is the heart of the RTOS.
- It is the code that decides which task to run at any given millisecond.
- The call
vTaskDelay()is the most important tool as it tells the scheduler, "My task is done for now, you can run other tasks for the next X milliseconds."
- Tasks
Introduction
- Real-time operating systems (RTOS) are used in environments where a large number of events, mostly external to the computer system, must be accepted and processed in a short time or within certain deadlines.
- Such applications are industrial control, telephone switching equipment, flight control, and real-time simulations.
- RTOS processing time is measured in tenths-of-a-second.
- System is time-bound and has a fixed deadline.
- Processing in this type of system must occur within specified constraints, otherwise it will lead to system failure.
Types of RTOS
- Hard RTOS
- Guarantees that critical tasks are completed within a strict, specified time range.
- The system must meet the deadline, every single time. These systems are deterministic.
- Missing a deadline is considered a total system failure. The value of the task's result instantly drops to zero (or negative), and the entire system is considered to have failed.
- Examples:
- Airbag Deployment: Must deploy within a specific millisecond window. Too late, and it's useless.
- Robotic Surgery: A command to stop a cut must be immediate.
- Flight Control Systems: Adjusting a wing flap must happen precisely when calculated.
- Firm RTOS
- Deadlines must be followed.
- Missing a deadline has a small impact, such as a reduction in the product's quality, but is not catastrophic.
- The result of a late task has zero value and is discarded. The system doesn't fail, but it loses that specific result.
- Examples:
- Assembly Line Quality Control: A camera must photograph a part at a specific point. If the photo is late, the part has already moved on. That photo is now useless (value=0), so it's discarded. The assembly line continues running.
- Financial Ticker: A stock price must be updated by a deadline. If the data arrives late, it's stale and worthless. The system discards it and waits for the next update.
- Soft RTOS
- Provides relaxation in time limits. Completing the task on time is preferred but not mandatory.
- The result of a late task still has value, but its value decays over time. The system's performance or quality is degraded, but it continues to function acceptably.
- Examples:
- Video Streaming (Multimedia): If a video frame arrives late, you might see a brief stutter or "lag." The frame is still useful (it's shown late), but the quality of the user experience is reduced.
- Online Gaming: A late data packet causes lag, which degrades the experience but doesn't crash the game.
- Digital Audio: A late audio packet might cause a small pop or click, but the music continues.
Note
How RTOS Manages Tasks?
- Priority-Based Preemptive Scheduling: This is the scheduling method used by real-time systems.
- How it works:
- Each task (or process) is assigned a priority level based on the importance of the event it handles.
- The processor is always allocated to the highest-priority process that is ready to run.
- If a high-priority task becomes ready while a low-priority task is running, the system will preempt (interrupt) the low-priority task to run the more critical one.