Real-Time Operating Systems

What is RTOS?
‡ A RTOS (Real-Time Operating System) ± Is an Operating Systems with the necessary features to support a Real-Time System ± What is a Real-Time System? ‡ A system where correctness depends not only on the correctness of the logical result of the computation, but also on the result delivery time ‡ A system that responds in a timely, predictable way to unpredictable external stimuli arrivals

Real Time System
‡ A system is said to be Real Time if it is required to complete it s work & deliver it s services on time. ‡ Example Flight Control System
± All tasks in that system must execute on time.

‡ Non Example PC system

Deterministic behavior Flat memory architecture Memory Full virtual memory Architecture /Monolithic architecture Size/latency GPOS are non scalable and has RTOS are scalable and has smaller larger footprint and higher footprint and low context switch context switch latency.GPOS (GENERAL PURPOSE OPERATING SYSTEMS) RTOS Time No time bound processing. should not take up too much memory since embedded systems come with tight memory constraints Scheduling Memory priority Priority consideration for each Each task must have a priority task is not as strict as RTOS . task has to has not got any time limit to complete work within given time finish work. priority preemptive scheduling preemption is a must feature Tight memory constraints are not considered. frame. it is not lightweight. task time bound processing. latency and interrupt latency. round robin way of scheduling.

RTS ‡ Tasks or processes attempt to control or react to events that take place in the outside world ‡ These events occur in real time and process must be able to keep up with them .

Examples of RTS ‡ ‡ ‡ ‡ ‡ ‡ ‡ ATM machine updating database Control of laboratory experiments Process control plants Robotics Air traffic control Telecommunications Military command and control systems .

Engine control systemA real time system .

Engine control systemA real time system .

Why an RTOS? .

Why an RTOS? .

Role of an OS in Real Time Systems ‡ Standalone Applications ± Often no OS involved ± Micro controller based Embedded Systems ‡ Some Real Time Applications are huge & complex ± Multiple threads ± Complicated Synchronization Requirements ± Filesystem / Network / Windowing support ± OS primitives reduce the software design time .

Characteristics of RTOS ‡ Deterministic ± Operations are performed at fixed. predetermined times or within predetermined time intervals ± Concerned with how long the operating system delays before acknowledging an interrupt .

Characteristics of RTOS ‡ Responsiveness ± How long. it takes the operating system to service the interrupt ± Includes amount of time to begin execution of the interrupt ± Includes the amount of time to service the interrupt . after acknowledgment.

Characteristics of RTOS ‡ User control ± User specifies priority ± Specify paging ± What processes must always reside in main memory ± Disks algorithms to use ± Rights of processes .

Characteristics of RTOS ‡ Reliability ± Degradation of performance may have catastrophic consequences ± Attempt either to correct the problem or minimize its effects while continuing to run ± Most critical. high priority tasks execute .

and events Use of special sequential files that can accumulate data at a fast rate Preemptive scheduling based on priority (scheduling) Delay tasks for fixed amount of time Special alarms and timeouts.Features of RTOS ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ Fast context switch Small size(kernel should fit within ROM) Ability to respond to external interrupts quickly Multitasking with interprocess communication tools such as semaphores. Resource allocation . signals.

Types of Real-Time Systems ‡ Hard Real-Time ± Missing a deadline has catastrophic results for the system ‡ Soft Real-Time ± Reduction in system quality is acceptable ± Deadlines may be missed and can be recovered from ‡ Non Real-Time ± No deadlines have to be met .

Examples ‡ Soft real time e. ± The emergency valve opens within 20 microsecond after this event occurs.g. ± this parachute automatically opens without fail when you reach 500 feet above ground.g. irrespective of system load. . ± this flight is on time 98 times out of 100 ‡ Hard real time e.

violation of constraints results in degraded quality. ‡ Live audio-video systems are also usually soft real-time. but a single event is neither predictable nor guaranteed.Soft Real Time ‡ Good average case performance ‡ Low deviation from average case performance ‡ Temporally speaking: soft real time systems are statistically predictable. ‡ soft real time systems are not suited for handling mission critical events. The flight plans must be kept reasonably current but can operate to a latency of seconds. . ‡ Soft real-time systems are typically used where there is some issue of concurrent access and the need to keep a number of connected systems up to date with changing situation. but the system can continue to operate. ‡ software that maintains and updates the flight plans for commercial airliners.

QNX.. not as average ‡ Low Latency response to events ‡ Precise scheduling of periodic tasks ‡ No real time event is ever missed ‡ System response is load-independent ‡ e. VxWORKS. ECOS..Hard Real Time ‡ Predictable performance at each moment of time. RTLinux..g. .

RTOS Concepts .

interrupts are typically disabled before the critical code is executed and enabled when the critical code is finished. . also called a critical region. Once the section of code starts executing. is code that needs to be treated indivisibly. it must not be interrupted. To ensure this.Critical Section of Code ‡ A critical section of code.

Critical Sections .

an array. a structure. a keyboard. a display. . or a variable. ‡ A resource can be an I/O device such as a printer.Resource ‡ A resource is any entity used by a task. etc.

This is called Mutual Exclusion .Shared Resource ‡ A shared resource is a resource that can be used by more than one task. Each task should gain exclusive access to the shared resource to prevent data corruption.

‡ Multitasking is the process of scheduling and switching the CPU (Central Processing Unit) between several tasks; a single CPU switches its attention between several sequential tasks.


‡ A task, also called a thread, is a simple program that thinks it has the CPU all to itself. ‡ The design process for a real-time application involves splitting the work to be done into tasks which are responsible for a portion of the problem. ‡ Each task is assigned a priority, its own set of CPU registers, and its own stack area


State Diagram ‡ Task states Blocked Ready task delete task resume task create Dormant task delete Ready Waiting Running task suspend task start Running task is preempted intExit interrupt ISR task delete .

Priority .

All the tasks and their timing constraints are known at compile time in a system where priorities are static. .Task Priority ‡ Static Priorities: ‡ Task priorities are said to be static when the priority of each task does not change during the application's execution. ‡ Each task is thus given a fixed priority at compile time.

This is a desirable feature to have in a real-time kernel to avoid priority inversion problem.‡ Dynamic Priorities ‡ Task priorities are said to be dynamic if the priority of tasks can be changed during the application's execution. . each task can change its priority at run-time.

by lower priority tasks. ‡ Example: ‡ Let T1 . and T3 be the three periodic tasks with decreasing order of priorities. ‡ Let T1 and T3 share a resource S .Priority Inversion Problem ‡ Priority inversion is an undesirable situation in which a higher priority task gets blocked (waits for CPU) for more time than that it is supposed to. . T2 .


‡ T3-lock on semaphore S enters critical section ‡ T1 ready-preempts T3-T1 tries to lock semaphore Slocked by T3-T1 is blocked ‡ T2 ready-preempts T3 while T3 is in critical section ‡ T1(high priority) is blocked for longer duration of time as T2 got executed in between .

. T2 preempts T3 while T3 is in its critical section. ‡ T1 becomes ready to run and preempts T3. unpredictable because task T2 got executed in between. However. ‡ Ideally.Example ‡ T3 obtains a lock on the semaphore S and enters its critical section to use a shared resource. S is already locked by T3 and hence T1 is blocked. But. Then. Since only T2 and T3 are ready to run. in fact.Priority Inversion -. ‡ T2 becomes ready to run. the duration of blocking is. T1 tries to enter its critical section by first trying to lock S. one would prefer that the highest priority task (T1) be blocked no longer than the time for T3 to complete its critical section.

.‡ Dynamic Priorities ‡ Task priorities are said to be dynamic if the priority of tasks can be changed during the application's execution. each task can change its priority at run-time. This is a desirable feature to have in a real-time kernel to avoid priority inversion problem.

priority inheritance may lead to deadlock. TL resumes its original priority. TL temporarily inherits the priority of T . because TL is currently executing critical section needed by T . TL exits the critical section).Priority Inheritance Protocol ‡ Priority inheritance protocol solves the problem of priority inversion. ‡ Under this protocol. ‡ Unfortunately. if a higher priority task T is blocked by a lower priority task TL. .e. ‡ When blocking ceases (i..


Priority Inheritance Protocol Deadlock ‡ Assume T2 > T1 (i.e.. T2 has high priority) .

and ‡ c) release the resources in the reverse order. The simplest way to avoid a deadlock is for tasks to: ‡ a) acquire all resources before proceeding. neither task can continue. They are deadlocked. then ‡ if T1 needs exclusive access to R2 and T2 needs exclusive access to R1.Deadlock ‡ If task T1 has exclusive access to resource R1 and task T2 has exclusive access to resource R2. . ‡ b) acquire the resources in the same order.

‡ Deadlock conditions don't always show up easily during software testing.Deadlock ‡ Avoiding deadlock conditions requires careful attention to the way in which multiple tasks share semaphores and other RTOS resources. .

the use of demand paging (swapping pages to disk) is prohibited for real-time processes. ‡ As a direct consequence. ‡ This is why systems providing a virtual memory mechanism should have the ability to lock the process into the main memory so swapping will not occur (Swapping is a mechanism that cannot be made predictable.) .Memory ‡ The fundamental requirement for memory in a real-time system is that its access time should be bound (or in other words predictable).

Memory ‡ Virtual memory is another technique that cannot be made predictable. ‡ A simple solution is to allocate all memory for all objects you need during the life of the system and never de-allocate them. and therefore should not be used in real-time systems. . ‡ Another solution is always to allocate and deallocate blocks of memory with a fixed size. (Introducing internal fragmentation = never using some parts of memory internal to the blocks).

Memory Management .

Memory ‡ Static memory allocation ± All memory allocated to each process at system startup ‡ Expensive ‡ Desirable solution for Hard-RT systems ‡ Dynamic memory allocation ± Memory requests are made at runtime ‡ Should know what to do upon allocation failure ‡ Some RTOSs support a timeout function .

. ‡ In non-RT you may want virtual memory and compaction.Memory ‡ In HRT static memory allocation is used. and no compaction. ‡ In SRT you have the option of dynamic memory allocation. no virtual memory.

semaphore management. and timeouts should require about 1 to 3 Kbytes of code space.Memory Requirements ‡ A minimal kernel for an 8-bit CPU that provides only scheduling. ‡ Application code size + Kernel code size . context switching. ‡ Code space needed when a kernel is used. delays.

it simply saves the current task's context (CPU registers) in the current task's context storage area it s stack Once this operation is performed. . the new task's ‡ context is restored from its storage area and then resumes execution of the new task's code.Context Switch (or Task Switch) ‡ When a multitasking kernel decides to run a different task. This process is called a context switch or a task switch.

Context Switches .

Example PIC24F .

queues.Kernel ‡ The kernel is the part of a multitasking system responsible for the management of tasks (that is. . The fundamental service provided by the kernel is context switching. for managing the CPU's time) and communication between tasks. time delays etc. ‡ The use of a real-time kernel will generally simplify the design of systems by allowing the application to be divided into multiple tasks managed by the kernel. ‡ A kernel can allow you to make better use of your CPU by providing you with indispensible services such as ‡ semaphore management.

Scheduling .

Scheduling Tasks .

Scheduler ‡ The scheduler. When the highest-priority task gets the CPU. Each task is assigned a priority based on its importance. is the part of the kernel responsible for determining which task will run next. . ‡ Most real-time kernels are priority based. control of the CPU will always be given to the highest priority task ready-to-run. also called the dispatcher. In a priority-based kernel. however. is determined by the type of kernel used. ‡ There are two types of priority-based kernels: ‡ non-preemptive and preemptive. The priority for each task is application specific.

tasks cooperate with each other to share the CPU.Non-Preemptive Kernel ‡ Non-preemptive kernels require that each task does something to explicitly give up control of the CPU. This is done to maintain illusion of concurrency. ‡ Non-preemptive scheduling is also called ‡ cooperative multitasking. .

-System performance can be optimized Tasks can be given priorities .Co-Operative Multitasking Tasks execute until they pause or yield Tasks execute until they pause or yield -Must be written to explicitly yield.

Pre-emptive Multitasking .

Priority .

Round Round-Robin Scheduling .

the kernel will allow one task to run for a predetermined amount of time. This is also called time slicing.Round Robin Scheduling ‡ When two or more tasks have the same priority. The kernel gives control to the next task in line if: ‡ a) the current task doesn't have any work to do during its time slice or ‡ b) the current task completes before the end of its time slice. and then selects another task. . called a quantum.

Priority-based Pre-emptive emptive Multi-tasking .

. The new higher priority task will gain control of the CPU only when the current task gives up the CPU.Non-Preemptive Kernel ‡ Asynchronous events are still handled by ‡ ISRs ‡ An ISR can make a higher priority task ready to run. but the ISR always returns to the interrupted task.


advantages of a non-preemptive kernel
‡ interrupt latency is typically low ‡ lesser need to guard shared data through the use of semaphores. ‡ Each task owns the CPU and you don't have to fear that a task will be preempted. ‡ semaphores should still be used. Shared I/O devices may still require the use of mutual exclusion semaphores; for example, a task might still need exclusive access to a printer.

drawback of a non-preemptive kernel
‡ responsiveness ‡ A higher priority task that has been made ‡ ready to run may have to wait a long time to run, because the current task must give up the CPU when it is ready.

Preemptive Kernel
‡ A preemptive kernel is used when system responsiveness is important ‡ The highest priority task ready to run is always given control of the CPU. ‡ If an ISR makes a higher priority task ready, when the ISR completes, the interrupted task is suspended and the new higher priority task is resumed. ‡ With a preemptive kernel, execution of the highest priority task is deterministic


..Reentrancy ‡ A reentrant function is a function that can be used by more than one task without fear of data corruption. A reentrant ‡ function can be interrupted at any time and resumed at a later time without loss of data. CPU registers or variables on the stack) or protect data when global variables are used.e. Reentrant functions either use ‡ local variables (i.

copies of the arguments to strcpy() are placed on the task's stack. strcpy() can be invoked by multiple tasks without fear that the tasks will corrupt each other's pointers. .

Non-Reentrant function .


Figure 2-6 shows what could happen if a low priority task is interrupted while swap() F2-6(1) is executing. z is 4 and t is 3). The high priority task sets Temp to 3 and swaps the contents of its variables correctly (that is. the kernel (assuming µC/OSII) is invoked to switch to this task F2-6(3). . Note that at this point. and thus. at the completion of the ISR F2-6(2). Note that at this point Temp contains 1. it sets y ‡ to 3 instead of 1. The lower priority task is thus ‡ resumed F2-6(5). The high priority task eventually relinquishes control to the low priority task ‡ F2-6(4) by calling a kernel service to delay itself for 1 clock tick (described later). Temp is still set to 3! When the low-priority task resumes execution.‡ The programmer intended to make swap() usable by any task. The ISR makes the higher priority task ready to run.

. tasks with the highest rate of execution are given the highest priority.Assigning Task Priorities ‡ An interesting technique called Rate Monotonic Scheduling (RMS) has been established to assign task priorities based on how often tasks execute. Simply put.

RMS makes a number of assumptions ‡ 1. share resources. In other words. All tasks are periodic (they occur at regular intervals). ‡ 3. The CPU must always execute the highest priority task that is ready to run. preemptive scheduling must be used. . ‡ 2. Tasks do not synchronize with one another. or exchange data.

Rate Monotonic Scheduling ‡ Promising method of scheduling for periodic tasks ‡ Assign priorities based on their periods ‡ Task with the shortest period has the highest priority .

Periodic Task Timing .

‡ For practical reasons this bound is less than 1. so to meet all possible deadlines following expression must hold: ‡ Sum of the processor utilization cannot exceed 1 that is the maximum utilization of processor.. for RMS it is: C1 Cn 1/ n --.< U(n) = n(2 . this is infact for a perfect scheduling algorithm.. + --.RMS ‡ Suppose we have n tasks with a fixed period and computation time.1) T1 Tn .+ ..

U.69 npg ª º .RMS Theorem (RMA Bound) Any set o n periodic tasks is RM-schedulable i the processor utilization. ª º This means that henever U is at or belo the given utilization bound. a schedule can be constructed ith RM. the maximum utilization limit is ¨ 1 ¸ limn© 2n 1¹ ! ln2 } 0. is no greater ¨ 1 ¸ than n© 2n 1¹ . In the limit hen the number o tasks n !g .

T1 = 100.286 ‡ Total Utilization: =0. U1 = 20/100 = 0.RMS Example ‡ Example: ‡ Task P1: C1 = 20. U2 = 40/150 = 0.2 ‡ Task P2: C2 = 40. T2 = 150.267 ‡ Task P3: C3 = 100. T3 = 350.753 . U3 = 100/350 = 0.

. not early neither late.Deadline Scheduling ‡ Real time is not just about sheer speed ‡ It is the completion of a task at a specified time.

or may have constraints on both start and finish.Types of RT Tasks ‡ An aperiodic real time task is the one which has a dead line by that it must finish or start. ‡ A periodic real time task is the one which has a dead line once every period T or exactly T units apart .

system collects and processes data from two sensors A and B. ‡ A takes 10 ms to process the sample including operating system overhead and B takes 25 ms.Example Periodic Tasks ‡ Consider an example of scheduling periodic tasks with completion deadlines. . ‡ Deadline for A to collect data is 20 ms and 50 ms for B.

Example Periodic Tasks .

Example Periodic Tasks .

Example Aperiodic tasks .

Example Aperiodic tasks .

Mutual Exclusion ‡ The most common methods to obtain exclusive access to shared resources are: ‡ a) Disabling interrupts ‡ b) Test-And-Set ‡ c) Disabling scheduling ‡ d) Using semaphores .

‡ When the I/O operation is complete. an ISR (or another task) signals the semaphore and the task is resumed. to indicate that it is used to signal the occurrence of an event . ‡ the semaphore is drawn as a flag.Synchronization ‡ A task can be synchronized with an ISR. or another task when no data is being exchanged. by using a semaphore ‡ A task initiates an I/O operation and then waits for the semaphore.



‡ Similarly. it signals the second task and then waits for a signal from the second task. it signals the first task and then waits for a signal from the first task At this point. both tasks are synchronized with each other. . when the second task reaches a certain point.‡ When the first task reaches a certain point.


Events .

This is called conjunctive synchronization (logical AND).Event Flags ‡ Event flags are used when a task needs to synchronize with the occurrence of multiple events. ‡ A task can also be synchronized when all events have occurred. This is called disjunctive synchronization (logical OR). ‡ The task can be synchronized when any of the events have occurred. .


Intertask Communication ‡ ‡ ‡ ‡ ‡ ‡ Message queues Pipes Fifos Mailboxes Semaphore Shared memory .

Inter-task Communication .

registers) and jumps to a special subroutine called an Interrupt Service Routine. or ISR. the CPU saves part (or all) of its context (i.e. .Interrupts ‡ An interrupt is a hardware mechanism used to inform the CPU that an asynchronous event has occurred. When an interrupt is recognized.

Interrupts ‡ RT systems respond to external events ± External events are translated by the hardware and interrupts are introduced to the system ‡ Interrupt Service Routines (ISRs) handle system interrupts ± May be stand alone or part of a device driver ‡ RTOSs should allow lower level ISRs to be preempted by higher lever ISRs ± ISRs must be completed as quickly as possible .

Interrupts .

Interrupts ± Interrupt Dispatch Time ‡ Time the hardware needs to bring the interrupt to the processor ± Interrupt Routine ‡ ISR execution time ± Other Interrupt ‡ Time needed for managing each simultaneous pending interrupt ± Pre-emption ‡ Time needed to execute critical code during which no preemption may happen .

.Interrupts ± Scheduling ‡ Time needed to make the decision on which thread to run ± Context Switch ‡ Time to switch from one context to another ± Return from System Call ‡ Extra time needed when the interrupt occurred while a system call was being executed ‡ System calls cause software interrupts (SWIs) ± Portable Operating System Interface (POSIX) defines the syntax of many of the library calls that execute the SWIs.

The longer interrupts are disabled.Interrupt Latency ‡ the most important specification of a realtime kernel is the amount of time interrupts are disabled. ‡ All real-time systems disable interrupts to manipulate critical sections of code and reenable interrupts when the critical ‡ section has executed. the higher the interrupt latency .

‡ Maximum amount of time interrupts are disabled ‡ + ‡ Time to start executing the first instruction in the ISR .Interrupt latency.

the processor's context (CPU registers) is saved on the stack before the user code is executed. ‡ Typically. The interrupt response time accounts for all the overhead involved in handling an interrupt. .Interrupt Response ‡ Interrupt response is defined as the time between the reception of the interrupt and the start of the user code which will handle the interrupt.

Interrupt Response ‡ Interrupt latency ‡ + ‡ Time to save the CPU's context .

Non-preemptive kernel. the user ISR code is executed immediately after the processor's context is saved. The ‡ response time to an interrupt for a nonpreemptive kernel is ‡ Interrupt latency ‡ + ‡ Time to save the CPU's context .Interrupt response. ‡ For a non-preemptive kernel.

.Interrupt response. a special function provided by the kernel needs to be called. Preemptive kernel. This function notifies the kernel that an ISR is in progress and allows the kernel to keep track of interrupt nesting. ‡ For a preemptive kernel.

Interrupt response. Preemptive kernel ‡ ‡ ‡ ‡ ‡ Interrupt latency + Time to save the CPU's context + Execution time of the kernel ISR entry function .

Interrupt Recovery ‡ Interrupt recovery is defined as the time required for the processor to return to the interrupted code. .

Non-preemptive kernel.Interrupt recovery. ‡ Time to restore the CPU's context ‡ + ‡ Time to execute the return from interrupt instruction .

Interrupt recovery. preemptive kernel. ‡ Time to determine if a higher priority task is ready ‡ + ‡ Time to restore the CPU's context of the highest priority task ‡ + ‡ Time to execute the return from interrupt instruction .

and Recovery . Response.Interrupt Latency.

and recovery (Preemptive kernel) .Interrupt latency. response.

± Time to execute longest instruction + ± Time to start executing the NMI ISR ‡ Interrupt response for an NMI. ± Time to restore the CPU's context + ± Time to execute the return from interrupt instruction .Non-Maskable Interrupts (NMIs) ‡ Interrupt latency for an NMI. ± Interrupt latency + ± Time to save the CPU's context ‡ Interrupt recovery of an NMI.

larger variables) to and from the ISR and a task.e.use this feature to pass parameters (i. .

e.The total RAM required if the kernel does not support a separate interrupt stack ‡ ‡ ‡ ‡ ‡ ‡ Data space needed when a kernel is used. Application code requirements + Data space (i. RAM) needed by the kernel + SUM(task stacks + MAX(ISR nesting)) .

the kernel supports a separate stack for interrupts ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ Data space needed when a kernel is used. RAM) needed by the kernel + SUM(task stacks) + MAX(ISR nesting) .e. Application code requirements + Data space (i.

e. subroutine) nesting c) interrupt nesting d) library functions stack usage e) function calls with many arguments .To reduce the amount of RAM needed in an application.. carefulness about how you use each task's stack for ‡ ‡ ‡ ‡ ‡ ‡ a) large arrays and structures declared locally to functions and ISRs b) function (i.

‡ This especially the case when running an RTOS and wanting to share relatively small amounts of RAM amongst two or three or four tasks' stacks. .

‡ a multitasking system will require more code space (ROM) and data space (RAM) ‡ The amount of extra ROM depends only on the size of the kernel. and the amount of ‡ RAM depends on the number of tasks in your system. .

time delays.Advantages and Disadvantages of Real-Time Kernels ‡ An RTOS allow you to make better use of ‡ your resources by providing you with precious services such as semaphores. ‡ Disdvantage: cost . Products are available for 8-. queues. and 32-bit microprocessors. mailboxes. 16-. timeouts ‡ With a preemptive RTOS. ‡ There are currently about 80+ RTOS vendors. all time-critical events are handled as quickly and as efficiently as possible.

Real-Time Systems Summary .

When is an RTOS appropriate? .

Why an RTOS? .

Why an RTOS?

Concepts and Terminology

Memory Management

RTOS Configuration .

Example Configuration .

How to Choose/Factors influencing .

RTOS Vendors .

RTOS Vendor Websites .

Sign up to vote on this title
UsefulNot useful