Embedded Systems Linux Memory Management

 Mastering Real-Time Operating Systems(RTOS)

Mubeen J August 16, 2024
Read more
Home Embedded Systems  Mastering Real-Time Operating Systems(RTOS)
Table of Contents
    Add a header to begin generating the table of contents

    Introduction to Real-Time Operating System (RTOS)

    The Real Time Operating System (RTOS) is an Operating System with task designed to respond to input within the predetermined time frame. RTOS is gaining popularity with increase in real time applications such as Robots, Air traffic control system, Autonomous vehicles, live video streaming and many more. Real-Time operating system is different from a general Operating system in many ways, such as for generating predictable responses, scheduling policy, inter task communication mechanism, and many more.

     

    There are various factors that optimize the real-time performance of operating systems, from their scheduling capabilities to their memory managing ways. The main feature of real time operating system in os includes multitasking, interrupt management, task synchronisation and communication, memory management, and a real time clock. The RTOS gets a new dimension when it is run in a customized hardware and software called Embedded Systems. Giving real-time responses in a resources-constrained rtos in embedded system is all the more challenging.

     

    In this blog let us deeply understand about RTOS, its features and how does in work.

    Key Features of Real-Time Operating System

    real time operating system in os
    Fig 1: RTOS- Key Features Overview

    Priority-Based Scheduling

    Priority-based scheduling is a cornerstone of Real-Time Operating Systems, ensuring that critical tasks meet their deadlines by prioritising them over less critical ones. This scheduling strategy is crucial for applications where timing and responsiveness are paramount. Priority-based scheduling assigns a priority level to each task. The real time operating system in os scheduler selects the task with the highest priority to execute next. If a higher-priority task becomes ready, it can pre-empt a currently running lower-priority task.

     

    Example – FreeRTOS follows the Priority-based scheduling, making it suitable for Real Time Applications mentioned above.

    Minimal Footprint

    One of the standout features of Real-Time Operating systems is their minimal footprint, especially when they are used in Embedded Systems. The minimal footprint of an RTOS refers to its ability to operate using a very small amount of system resources, particularly memory. This includes both the size of the real time operating system kernel itself and the additional overhead it imposes on applications. This characteristic is crucial for embedded systems, which often operate with limited resources such as memory and processing power. A minimal footprint ensures that the RTOS can run efficiently on small, resource constrained devices without compromising performance.

     

    Example – embOS follows the Minimal Footprint, this RTOS has a small memory footprint, making it suitable for resource-constrained devices. it also provides fast context switching and low interrupt latency

    Fault Tolerance and Reliability

    Fault tolerance and reliability are critical features of the Real-Time Operating System, especially in applications where system failures can lead to significant consequences, like the Air bag system of an Automotive car. Fault Tolerance is the ability of a system to continue functioning correctly even when one or more components fail. Reliability is the probability that a system will perform its intended function correctly over a specified period of time. These features part of the real time operating system ensure that the system can continue to operate correctly even in the presence of faults, thereby enhancing the overall robustness and dependability of the system.

     

    Example – Integrity RTOS follows the Fault Tolerance and Reliability, This RTOS uses separation kernel technology to ensure that each application runs in its own protected memory space thereby they not affecting each others, improving overall reliability of the system.

    Inter-Task Communication

    Inter-Task Communication is a critical feature of RTOS that enables tasks to exchange information with other tasks and applications. Effective inter-task communication mechanisms ensure that tasks can coordinate their operations, share data, and signal events in a predictable and efficient manner, which is essential for the functionality and reliability of real-time system. Message Queues is one such mechanism that allows tasks to send and receive messages. Each message is placed in queue, and tasks can read from the queue in a First-In-First-Out(FIFO) manner.  

     

    There are other mechanisms like Pipes, Signals, and Shared Memory that help in enabling inter task communications.

    real time operating system in os
    Fig 2: Illustrative Examples of Real-Time Systems

    Key Functionalities of RTOS

    The above mentioned and some more features could be part of the RTOS. Assuming that they are part of the system, let us take a look into how the real time operating system functions. Primarily task management and interrupt management are the functionalities are the key. Let us take a deeper look into them.  

    Task Management in RTOS

    Inside the kernel is the scheduler. It is a set of algorithms that manage the task running order. Multitasking definition comes from the ability of the kernel to control multiple tasks that must run within time deadlines. Multitasking may give the impression that multiple threads are running concurrently; as a matter of fact, the processor runs task by task, according to the task scheduling. Scheduler and scheduling algorithm plays an important role in RTOS.

    1. Tasks

    In an real time operating system, the basic unit of execution is a task, also known as a thread or process. Tasks are independent functions running in infinite loops, usually each responsible for one feature. Tasks are running independently in their own time (temporal isolation) and memory stack (spatial isolation). Spatial isolation between tasks can be guaranteed using effective Memory Management Units (MMU), which restricts accessible memory region and triggers fault exceptions on access violation. Normally, internal peripherals are memory-mapped, so an MMU can be used to restrict access to peripherals as well.

    2. Task Control Block(TCB)

    A Task Control Block (TCB) is a data structure used by the operating system to manage and keep track of information about a process or thread. It contains important details such as: Process State is Whether the process is ready, running, waiting, etc. Process Privileges is the Access levels of the process. Process ID is a Unique identifier for the process. Memory Pointers is the Information about the process’s memory allocation. I/O Status Information is the Details about the input/output operations of the process.

    In a way TCB can be looked a database where each entry in the database with fields mentioned above. This TCP helps the RTOS to manage the task effectively including the scheduling.

    3. Task Scheduling

    Task scheduling in a real-time operating system (RTOS) is the process of determining which task should be executed next by the CPU. This is based on the system’s resources and the tasks’ priorities and deadlines. RTOS task scheduling is critical for ensuring that real-time tasks meet their deadlines, as even a small delay in executing a task can have serious consequences example being Automomous Vehciles.

    There are two main types of RTOS task scheduling algorithms:

    • Pre-emptive scheduling algorithms:  This allow the RTOS to interrupt a running task and switch to a higher-priority task. This is important for ensuring that critical tasks meet their deadlines, even if lower-priority tasks are already running. Priority based scheduling, Round Robin (RR), Shortest Remaining Time First (SRTF) are examples of this category.

    • Non-preemptive scheduling algorithms: This do not allow the RTOS to interrupt a running task. This can be useful for tasks that need to run to completion without being interrupted, but it can also lead to missed deadlines for higher-priority tasks if a lower-priority task takes a long time to complete. Shortest Job First (SJF) is an example of non-preemptive scheduling algorithm

    Typically in real time operating system pre-emptive algorithms are preferred as they need to guarantee the completion of Real Time Tasks.

    Interrupt Handling in RTOS

    Interrupt handling is another key function of the real time operating system, and comprises interrupts and their handlers. A Process is like a task except that it executes in its own virtual address space and has a stack and heap located within that virtual space. Processes can be implemented on embedded systems that include a special device that changes address space and paging (Memory Management Unit). Threads are similar to processes but can easily be assigned to be executed on a different processor. For instance, a Symmetric Multi-Processing (SMP) system can have different threads running on different processors.

    Interrupt Routine Rules

    Interrupt routines in RTOS must follow two rules that do not apply to task code:

    • An interrupt routine must not call any RTOS functions that might block. could block the highest priority task might not reset the hardware or allow further interrupts

    • An interrupt routine must not call any RTOS function that might cause the RTOS to switch tasks causing a higher priority task to run may cause the interrupt routine to take a very long time to complete. 

    Interrupt Service Routines(ISRs)

     

    An interrupt service routine (ISR) is a software routine that hardware invokes in response to an interrupt. ISR examines an interrupt and determines how to handle it. The ISR handles the interrupt, and then returns a logical interrupt value. If no further handling is required because the device is disabled or data is buffered, the ISR notifies the kernel with a return value. A basic example of an ISR is a routine that handles keyboard events, such as pressing or releasing a key. Each time a key is pressed, the ISR processes the input. For example, if you press and hold the right arrow key in a text file, the ISR will signal to the CPU that the right arrow key is depressed.

     

    An ISR must perform very fast to avoid slowing down the operation of the device and the operation of all lower priority ISRs. Although an ISR might move data from a CPU register or a hardware port into a memory buffer, in general it relies on a dedicated interrupt thread, called the Interrupt Service Thread (IST), to do most of the required processing. If additional processing is required, the ISR returns a logical interrupt value to the kernel. It then maps a physical interrupt number to a logical interrupt value.

    Advanced Topic in RTOS

    Fig 3: Limitations and Constraints in Real-Time Operating Systems

    Jitter Constraints: Jitter refers to the variability in the timing of task execution or interrupt handling. Minimizing jitter is important for tasks requiring precise timing, such as audio processing and control systems. For example, Audio Processing System – In an audio processing system, tasks must be executed at precise intervals to maintain audio quality. Jitter in this context could lead to glitches, pops, or audio distortion.

     

    Throughput Constraints: Throughput is the number of tasks or operations completed in a given time period. In real-time systems, it is crucial to maintain a consistent and predictable throughput. For example, Industrial Automation – In an industrial automation system, a conveyor belt system must handle and process a certain number of items per minute to keep the production line running smoothly.

     

    Priority Inversion Constraints: Priority inversion occurs when a lower-priority task holds a resource needed by a higher-priority task, causing the higher-priority task to be delayed. RTOS should provide mechanisms like priority inheritance or priority ceiling to mitigate priority inversion. For example, The Mars Pathfinder Incident – A real-world example of priority inversion occurred with the Mars Pathfinder mission.

     

    Resource Constraints: Real-time systems often operate with limited resources (CPU, memory, I/O). The real time operating system must manage these resources efficiently to ensure that real-time tasks meet their deadlines. For example, Embedded Control System – In an embedded control system, such as an automotive engine control unit (ECU), resources are limited, and the system must manage tasks efficiently to ensure timely responses.

    Conclusion

    Real-Time Operating System play a important role in the design and implementation of embedded systems, providing the necessary framework to meet stringent real-time requirements. From ensuring predictable performance to improving system responsiveness and reliability, RTOS offers a suite of benefits that make it indispensable in a wide range of applications. As technology continues to advance, real time operating system will likely evolve to address emerging challenges and cater to the ever-growing demands of real-time applications.

    Serial No. Related Blogs Links
    1. Selecting the Right Operating System for Your next Embedded Design Click Here
    2. Embedded Linux Projects| Kernel Optimization Click Here

    People Also Ask (PAA)

    Common scheduling algorithms in RTOS includes: Priority-based scheduling, Round-robin scheduling, Rate-monotonic scheduling.

    Priority Inheritance is a technique used to prevent task priority inversion. When a lower priority task holds a resource needed by a higher priority task, the priority of the lower priority task is temporarily boosted to that of the higher priority task. This ensures that the higher priority task can proceed without undue delay caused by lower priority tasks.

    RTOS employs priority-based scheduling to ensure that tasks with higher priorities are executed before lower priority tasks. Task starvation, where a low-priority task never gets CPU time due to continuous arrival of high-priority tasks, is mitigated through techniques such as aging or priority boosting.

    Share this material with your friend:

    Start Your
    Embedded Systems Journey Today!

    Register Now!