Real-Time Operating System (RTOS) Based Embedded System Design
1. A real-time operating system (RTOS) is an OS that ensures deterministic timing behavior by providing minimal services and prioritizing tasks based on timing constraints.
2. The RTOS kernel contains only essential services like task management, scheduling, synchronization, and memory management to provide predictable performance.
3. Key aspects of an RTOS kernel include task control blocks (TCBs) that store task information, a scheduler that shares CPU time between tasks, and exception handling for errors and timeouts.
Real-Time Operating System (RTOS) Based Embedded System Design
1. A real-time operating system (RTOS) is an OS that ensures deterministic timing behavior by providing minimal services and prioritizing tasks based on timing constraints.
2. The RTOS kernel contains only essential services like task management, scheduling, synchronization, and memory management to provide predictable performance.
3. Key aspects of an RTOS kernel include task control blocks (TCBs) that store task information, a scheduler that shares CPU time between tasks, and exception handling for errors and timeouts.
OPERATING SYSTEM BASICS • The operating system acts as a bridge between the user applications/tasks and the underlying system resources through a set of system functionalities and services. • The OS manages the system resources and makes them available to the user applications/tasks on a need basis. • A normal computing system is a collection of different I/O subsystems, working, and storage memory. • The primary functions of an operating system is 1. Make the system convenient to use 2. Organise and manage the system resources efficiently and correctly The Operating System Architecture The Kernel • The kernel is the core of the operating system and is responsible for managing the system resources and the communication among the hardware and other system services. • Kernel acts as the abstraction layer between system resources and user applications. • Kernel contains a set of system libraries and services. • For a general purpose OS, the kernel contains different services for handling the following. 1 Process Management 2 Primary Memory Management 3 File System Management 4 I/O System (Device) Management • Secondary Storage Management • Protection Systems • Interrupt Handler Kernel Space and User Space • the applications /services are classified into two categories, namely: 1 user applications 2 kernel applications. The program code corresponding to the kernel applications/services are kept in a contiguous area of primary (working) memory and is protected from the unauthorized access by user programs/applications. • The memory space at which the kernel code is located is known as ‘Kernel Space'. • All user applications are loaded to a specific area of primary memory and this memory area is referred as ‘User Space'. • User space is the memory area where user applications are loaded and executed. Monolithic Kernel and Microkernel • Based on the kernel design, kernels can be classified into ‘Monolithic’ and ‘Micro’. Monolithic Kernel • all kernel services run in the kernel space. • all kernel modules run within the same memory space under a single kernel thread. • The tight internal integration of kernel modules in monolithic kernel architecture allows the effective utilization of the low- level features of the underlying system. • The major drawback of monolithic kernel is that any error or failure in any one of the kernel modules leads to the crashing of the entire kernel application. • LINUX, SOLARIS, MS-DOS kernels are examples of monolithic kernel. The Monolithic Kernel The Microkernel • The microkernel design incorporates only the essential set of Operating System services into the kernel. • The rest of the Operating System services are implemented in programs known as ‘Servers’ which runs in user space. • This provides a’highly modular design and OS-neutral abstraction to the kernel. • Memory management, process management, timer systems and interrupt handlers are the essential services, which forms the part of the microkernel. • Mach, QNX, Minix 3 kernels are examples for microkernel. Benefits 1 Robustness 2 Configurability The Microkernel Model Types of Operating Systems General Purpose Operating System (GPOS) • The operating systems, which are deployed in general computing systems, are referred as General Purpose Operating Systems (GPOS). • The kernel of such an OS is more generalised and it contains all kinds of services required for executing generic applications. • General-purpose operating systems are often quite non- deterministic in behaviour. • Their services can inject random delays into application software and may cause slow responsiveness of an application at unexpected times. • GPOS are usually deployed in computing systems where deterministic behavior is not an important criterion. • Personal Computer/Desktop system is a typical example for a system where GPOSs are deployed. Windows XP/MS-DOS are examples for General Purpose Operating Systems Real-Time Operating System (RTOS) • ‘Real-Time’ implies deterministic timing behaviour. Deterministic timing behavior in RTQS context means the OS services consumes only known and expected amounts of time regardless the number of services. • A RTOS implements policies and rules concerning time- critical allocation of a system’s resources. • The RTOS decides which applications should run in which order and how much time needs to be allocated for each application. • Predictable performance is the hallmark of a well-designed RTOS. • Windows CE, QNX, VxWorks MicroC/OS-II, etc. are examples of Real-Time Operating Systems (RTOS). The Real-Time Kernel • The kernel of a Real-Time Operating System is referred as Real Time kernel. • In complement to the conventional OS kernel, the Real-Time kernel is highly specialised and it contains only the minimal set of services required for running the user applications/tasks. • The basic functions of a Real-Time kernel are listed below: 1.Task/Process management 2. Task/Process scheduling 3. Task/Process synchronisation 4.Error/Exception handling 5. Memory management 6. Interrupt handling 7.Time management Task/Process management • Deals with setting up the memory space for the tasks, loading the task’s code into the memory space, allocating system resources, setting up a Task Control Block (TCB) for the task and task/process termination/deletion. • A Task Control Block (TCB) is used for holding the information corresponding to a task. • TCB usually contains the following set ofinformation: a) Task ID: Task Identification Number b) Task State: The current state ofthe task (e.g. State = ‘Ready’ for a task which is ready to execute) c) Task Type: Task type. Indicates what is the type for this task. The task can be a hard real time or soft real time or background task. d) Task Priority: Task priority (e.g. Task priority = 1 for task with priority - 1) e) Task Context Pointer: Context pointer. Pointer for context saving f) Task Memory Pointers: Pointers to the code memory, data memory and stack memory for the task g) Task System Resource Pointers: Pointers to system resources (semaphores, mutex, etc.) used by the task h) Task Pointers: Pointers to other TCBs (TCBs for preceding, next and waiting tasks) i) Other Parameters Other relevant task parameters Task/Process Scheduling • Deals with sharing the CPU among various tasks/processes. • A kernel application called ‘Scheduler’ handles the task scheduling. • Scheduler is nothing but an algorithm implementation, which performs the efficient and optimal scheduling of tasks to provide a deterministic behaviour. Task/Process Synchronisation • Deals with synchronising the concurrent access of a resource,which is shared across multiple tasks and the communication between various tasks. Error/Exception Handling • Deals with registering and handling the errors occurred/exceptions raised during the execution of tasks. • Insufficient memory, timeouts, deadlocks, deadline missing, bus error, divide by zero, unknown instruction are examples of errors/exceptions. • Errors/Exceptions can happen at the kernel level services or at task level. • Deadlock is an example for kernel level exception, whereas timeout is an example for a task level exception. • The OS kernel gives the information about the error in the form of a system call (API).\ • GetLastError() API provided by Windows CE RTOS is an example for such a system call. • Watchdog timer is a mechanism for handling the timeouts for tasks. Cont’d • Certain tasks may involve the waiting of external events from devices. • These tasks will wait infinitely when the external device is not responding and the task will generate a hang-up behaviour. • In order to avoid these types of scenarios, a proper timeout mechanism should be implemented.A watch dog is normally used in such situations. • The watchdog will be loaded with the maximum expected wait time for the event and if the event is not triggered within this wait time, the same is informed to the task and the task is timed out. • If the event happens before the timeout, the watchdog is resetted. MemoryManagement • The memory allocation time increases depending on the size of the block of memory needs to be allocated and the state of the allocated memory block. • RTOS makes use of ‘block‘ based memory allocation technique • RTOS kernel uses blocks of fixed size of dynamic memory and the block is allocated for a task on a need basis. • The blocks are stored in a ‘'Free Buffer Queue‘ • To achieve predictable timing and avoid the timing overheads, most of the RTOS kernels allow tasks to access any of the memory block's without any memory protection. • RTOS kernels assume that the whole design is proven correct and protection is unnecessary. • Some commercial RTOS kernels allow memory protection as optional and the kernel enters a fail-safe mode when an illegal memory access occurs. Cont’d • A few RTOS kernels implement Virtual Memory concept for memory allocation if the system supports secondary memory storage (like HDD and FLASH memory) • In the ‘block' based memory allocation, a block of fixed memory is always allocated for tasks on need basis and it is taken as a unit. • The memory allocation can be implemented as constant functions and thereby it consumes fixed amount of time for memory allocation. • The ‘block’ based memory allocation achieves deterministic behaviour with the trade-of limited choice of memory chunk size and suboptimal memory usage. Interrupt Handling • Interrupts provide Real Time behaviour to systems. • Interrupts inform the processor that an external device or an associated task requires immediate attention of the CPU. • Interrupts can be either Synchronous or Asynchronous. • Interrupts which occurs in sync with the currently executing task is known as Synchronous interrupts. • The software interrupts fall under the Synchronous Interrupt category. • Divide by zero, memory segmentation error, etc. are examples of synchronous interrupts. • For synchronous interrupts, the interrupt handler runs in the same context of the interrupting task. • Asynchronous interrupts are interrupts, which occurs at any point of execution of any task, and are not in sync with the currently executing task. • The interrupts generated by external devices connected to the processor/controller, timer overflow interrupts, serial data reception/ transmission interrupts, etc. are examples for asynchronous interrupts. Cont’d • For asynchronous interrupts, the interrupt handler is usually written as separate task and it runs in a different context. • Hence, a context switch happens while handling the asynchronous interrupts. • Priority levels can be assigned to the interrupts and each interrupts can be enabled or disabled individually.
• Most of the RTOS kernel implements ‘Nested Interrupts’ architecture .
• Interrupt nesting allows the pre-emption (interruption) of an Interrupt Service Routine (ISR), servicing an interrupt, by a high priority interrupt. • The ‘Timer tick’ interval may vary depending on the hardware timer. • Usually the ‘Timer tick’ varies in the microseconds range. • The time parameters for tasks are expressed as the multiples of the ‘Timer tick'. Time Management • Accurate time management is essential for providing precise time reference for all applications. • The time reference to kernel is provided by a high-resolution Real-Time Clock (RTC) hardware chip (hardware timer). • The hardware timer is programmed to interrupt the processor/controller at a fixed rate. • This timer interrupt is referred as ‘Timer tick’. The ‘Timer tick’ is taken as the timing reference by the kernel. • The System time is updated based on the ‘Timer tick’. Ifthe System time register is 32 bits wide and the ‘ Timer tick’ interval is 1 microsecond, the System time register will reset in 232 * 10-6/ (24 * 60 * 60) = 49700 Days = ~ 0.0497 Days =1.19 Hours If the ‘Timer tick’ interval is 1 millisecond, the system time register will reset in 232 * 10-3 / (24 * 60 * 60) = 497 Days = 49.7 Days = ~ 50 Days The ‘Timer tick’ interrupt is handled by the ‘Timer Interrupt’ handler of kernel. • The ‘Timer tick’ interrupt can be utilised for implementing the following actions. • Save the current context (Context ofthe currently executing task). • Increment the System time register by one. Generate timing error and reset the System time register if the timer tick count is greater than the maximum range available for System time register. • Update the timers implemented in kernel (Increment or decrement the timer registers for each timer depending on the count direction setting for each register. Increment registers with count direction setting = ‘count up’ and decrement registers with count direction setting = ‘count down’). • Activate the periodic tasks, which are in the idle state. • Invoke the scheduler and schedule the tasks again based on the scheduling algorithm. • Delete all the terminated tasks and their associated data structures (TCBs) • Load the context for the first task in the ready queue. Due to the re- scheduling, the ready task might be changed to a new one from the task, which was preempted by the ‘Timer Interrupt’ task. Cont’d • Apart from these basic functions, some RTOS provide other functionalities also (Examples are file management and network functions). • Some RTOS kernel provides options for selecting the required kernel functions at the time of building a kernel . • The user can pick the required functions from the set of available functions and compile the same to generate the kernel binary. • Windows CE is a typical example for such an RTOS. While building the target, the user can select the required components for the kernel. Hard Real-Time • RTOS that strictly adhere to the timing constraints for a task is referred as ‘Hard Real-Time’ systems. • A Hard Real-Time system must meet the deadlines for a task without any slippage. • Missing any deadline may produce catastrophic results for Hard Real-Time Systems, including permanent data lose and irrecoverable damages to the system /users. • A system can have several such tasks and the key to their correct operation lies in scheduling them so that they meet their time constraints. • Air bag control systems and Anti-locking Brake Systems (ABS) of vehicles are typical examples for Hard Real-Time Systems. Soft Real-Time • Real-Time Operating System that does not guarantee meeting deadlines, but offer the best effort to meet the deadline are referred as ‘Soft Real- Time’ systems. • Missing deadlines for tasks are acceptable for a Soft Real-time system if the frequency of deadline missing is within the compliance limit ofthe Quality of Service (QoS). • A Soft Real-Time system emphasises the principle ‘A late answer is an acceptable answer, but it could have done bit faster. • Soft Real-Time systems most often have a ‘human in the loop , • Automatic Teller Machine (ATM) is a typical example for Soft- Real-Time System. TASKS, PROCESS AND THREADS • The term ‘task' refers to something that needs to be done. • A task is defined as the program in execution and the related information maintained by the operating system program • Task is also known as ‘Job’ in the operating system context. • A program or part of it in execution is also called a ‘Process'. • The terms ‘Task', ‘Job' and ‘Process' refer to the same entity in the operating system context Process • A ‘Process' is a program, or part ofit, in execution. • Process is also known as an instance of a program in execution. • Multiple instances of the same program can execute simultaneously. • A process requires various system resources like CPU for executing the process; memory for storing the code corresponding to the process and associated variables, I/O devices for information exchange, etc. • A process is sequential in execution. The Structure of a Process • The concept of ‘Process' leads to concurrent execution (pseudo parallelism) of tasks and thereby the efficient utilisation of the CPU-and other system resources. • Concurrent execution is achieved through the sharing of CPU among the processes . • A process which inherits all the properties of the CPU can be considered as a virtual processor, awaiting its turn to have its properties switched into the physical processor. • When the process gets its turn, its registers arid the program counter register becomes mapped to the physical registers of the CPU. • From a memory perspective, the memory occupied by the process is segregated into three regions, namely, Stack memory, Data memory and Code memory • The ‘Stack’ memory holds all temporary data such as variables local to the process. • Data memory holds all global data for the process. • The code memory contains the program code (instructions) cor¬ • responding to the process. Structure of a Process Process States and State Transition • The creation of a process to its termination is not a single step operation. • The process traverses through a series of states during its transition from the newly created state to the terminated state. • The cycle through which a process changes its state from ‘newly created’ to ‘execution completed' is known as ‘Process Life Cycle'. • The various states through which a process traverses through during a Process Life Cycle indicates the current status of the process with respect to time and also provides information on what it is allowed to do next. Process States and State Transition Process Management • Process management deals with the creation of a process, setting up the memory space for the process, loading the process’s code into the memory space, allocating system resources, setting up a Process Control Block (PCB) for the process and process termination/deletion. Threads • A thread is the primitive that can execute code. • A thread is a single sequential flow of control within a process. • ‘Thread’ is also known as light weight process. • A process can have many threads of execution. • Different threads, which are part of a process, share the same address space; meaning they share the data memory, code memory and heap memory area. • Threads maintain their own thread status (CPU register values), Program Counter (PC) and stack. • The memory model for a process and its associated threads are given in fig. The Concept of Multithreading • A process/task in embedded application may be a complex or lengthy one and it may contain various sub operations like getting input from I/O devices connected to the processor, performing some internal calculations/operations, updating some I/O devices etc. • If all the subfunctions of a task are executed in sequence, the CPU utilisation may not be efficient. • For example, if the process is waiting for a user input, the CPU enters the wait state for the event, and the process execution also enters a wait state. • Instead of this single sequential execution of the whole process, if the task/process is split into different threads carrying out the different sub functionalities of the process, the CPU can be effectively utilised and when the thread corresponding to the I/O operation enters the wait state, another threads which do not require the I/O event for their operation can be switched into execution. • This leads to more speedy execution of the process and the efficient utilisation of the processor time and resources. • The multithreaded architecture of a process can be better visualized with the thread-process diagram. shown in Fig. Thread Pre-emption • Thread pre-emption is the act of pre-empting the currently running thread (stopping the currently running thread temporarily). • Thread pre-emption ability is solely dependent on the Operating System. • Thread pre-emption is performed for sharing the CPU time among all the threads. • The execution switching among threads are known as ‘ Thread context switching'. Thread context switching is dependent on the Operating system’s scheduler and the type of the thread. • When we say ‘Thread’, it falls into any one of the following types. 1 User Level Thread 2 Kernel/System Level Thread 3 Many-to-One Model 4 One-to-One Model 5 Many-to-Many Model Thread v/s Process Threads Process Thread is a single unit of execution and is Process is a program in execution and part of process contains one or more threads A thread does not have its own data Process has its own code memory ,data memory and heap memory,it shares the memory and stack memory data memory and heap memory with other threads of the same process A thread cannot live independently ,it A Process contains atleast one thread lives within the process There can be multiple threads in a Threads within process.The first thread calls the main function and occupies the stack memory of the process