Summary
A real-time operating system (RTOs) is software that manages the time of a microprocessor or
Microcontroller. This application note is a reprint from an article I published in electronic design
Magazine, June 1994 issue.
Introduction
Real-time systems are characterized by the fact that severe consequences can result if logical as well
Timing correctness properties of the system are not met. A Real-Time Multitasking application is a system
In which several time critical activities must be processed simultaneously. A Real-Time Multitasking
Kernel (also called a real-time operating system, RTOS) is software which ensures that time critical
Events are processed as efficiently as possible. The use of a RTOS generally simplifies the design
Process of a system by allowing the application to be divided into multiple independent elements called
Tasks.
Foreground/background.
Systems which do not use a RTOS are generally designed as shown in Figure 1. These systems are
Called Foreground/background. An application consist of an infinite loop which CILS application modules
To perform the desired operations. The modules are executed sequentially (background) with interrupt
Service routines (ISRs) Handling asynchronous events (foreground). Critical operations must be
Stored med by the ISRs to ensure that they are dealt with in a timely fashion. Because of this, ISRs have
A tendency to take longer than they shocould. Information for a background module made available by
ISR is not processed until the background routine gets its turn to execute. The latency in this case
Depends on how long the background loop takes to execute.
Real-time kernels
Multitasking.
Multitasking is the process of scheduling and switching the CPU between several tasks; A single CPU
Switches its attention between several sequential tasks. Multitasking provides the ability to structure your
Application into a set of small, dedicated tasks that share the processor. One of the most important
Aspects of multitasking is that it allows the application programmer to manage complexity which is
Inherent in real-time applications. Real-time kernels can make application programs easier to design
And maintain. A task is a simple program which 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
Portion of the problem.
Kernel.
The kernel is the part of the multitasking system responsible for the management of tasks and
Communication between tasks. When the kernel decides to run a different task, it simply saves
Current task's context (CPU registers) onto the current task's stack; each task is assigned its own stack
Area in memory. Once this operation is performed med, the new task's context is restored from its stack Area
And then, execution of the new task's code is resumed. This process is called a context switch or a task
Switch. The current top of stack for each task, along with other information, is stored in a Data Structure
Called the task control block (TCB). a tcb is assigned to each task when the task is created and is
Managed by the RTOS.
Interrupts.
An important issue in real-time systems is the time required to respond to an interrupt and to actually
Start executing user code to handle the interrupt. All rtoss disable interrupts when manipulating critical
Sections of code. The longer a RTOS disables interrupts, the higher the interrupt latency. rtoss
Generally disable interrupts for less than about 50 US but obviusly, the lower the better. The code that
Services an interrupt is called an interrupt service routine (ISR ).
Scheduler.
The schedcher, also called the dispatcher, is the part of the kernel which is responsible for determining
Which task will run next and when. Most Real-Time kernels are priority based; each task is assigned
Priority based on its importance. Establishing the priority for each task is application specific. In
Priority based kernel, control of the CPU will always be given to the highest priority task ready to run.
When the highest priority task gets the CPU, however, depends on the type of scheduler used. There
Are two types of schedulers: Non-preemptive and preemptive.
Non-preemptive scheduling.
Non-preemptive schedulers require that each task does something to explicitly give up control of
CPU. To maintain the escalation of concurrency, this process must be done frequently. Non-preemptive
Scheduling is also called cooperative multitasking; tasks cooperate with each other to gain control of
CPU. When a task relinquishes the CPU, the kernel executes the code of the next most important task
That is ready to run. asynchronous events are still handled by ISRs. An ISR can make a higher priority
Task ready to run but the ISR always return to the interrupted task. The new higher priority task will gain
Control of the CPU only when the current task voluntarily gives up the CPU. This is already strated in Figure 2.
Latency of a non-preemptive scheduler is much lower than with Foreground/background; latency is now
Given by the time of the longest task.
Preemptive scheduling.
In a preemptive kernel, when an event makes a higher priority task ready to run, the current task is
Immediately susponded and the higher priority task is given control of the CPU. If an ISR makes
Higher priority task ready to run, the interrupted task is suincluded and the new higher priority task is
Resumed. Most real-time systems employ preemptive schedulers because they are more responsive.
Preemptive scheduling is already strated in figure 3.
Reentrancy.
A reentrant function is a function which can be used by more than one task without fear of data
Upload uption. Conversely, a non-reentrant function is a function which cannot be shared by more than one
Task unless mutual exclusion to the function is ensured by either using a semaphore (described later) or,
By disabling interrupts during critical sections of code. A reentrant function can be interrupted at any
Time and resumed at a later time without loss of data. reentrant functions either use local variables
(CPU registers or variables on the stack) or protect their data when global variables are used. Compilers
Specifically designed for embedded software will generally provide reentrant run-time libraries. nonpreemptive
Schedulers do not require reentrant functions unless a function is shared between a task and
An ISR. preemptive schedulers requires you to have reentrant functions if the functions are shared
More than one task.
Here a lot of things incomplete: the original please see: http://www.ucos-ii.com/downloads/appnotes/an1004.pdf
This is good. It is about uC/OS _ii.