Sie sind auf Seite 1von 4

Software MILITARY EMBEDDED SYSTEMS Resource Guide

What is real time and


why do I need it?
By Steve Furr

Not all systems need an RTOS; but of with a basic definition of a real-time sys- the effects of a late computation may be
those that do, does the designer know tem, as defined in the Frequently Asked catastrophic. Simply put, a hard real-time
what about the system is “real time”? Questions for the comp.realtime news- system is one where all activities must be
An RTOS can play a key role in deter- group (news://comp.realtime or groups- completed on time. A flight control sys-
mining how a system runs, and it’s beta.google.com/group/comp.realtime): tem is a good example.
critical to choose an RTOS based upon
system requirements. “A real-time system is one in which the On the other hand, soft real time is a prop-
correctness of the computations not only erty of the timeliness of a computation
Real time is a sometimes misunderstood depends upon the logical correctness of where the value diminishes according to
and misapplied property of operating sys- the computation but also upon the time at its tardiness. A soft real-time system can
tems. Moreover, there is often disagree- which the result is produced. If the timing tolerate some late answers to soft real-
ment as to when a Real-Time Operating constraints of the system are not met, sys- time computations, as long as the value
System (RTOS) is needed. For instance, tem failure is said to have occurred.” hasn’t diminished to zero. Deadlines may
when designing an industrial control be missed, but the number and frequency
system or medical instrument, most engi- Real time, then, is a property of systems of such misses must typically comply
neers and system designers would con- where time is literally of the essence. In a with Quality of Service (QoS) metrics.
cur that an RTOS is necessary. However, real-time system, the value of a computa-
questions arise, when it comes to other tion depends on how timely the answer is. Frequently, soft real time is erroneously
applications, such as tracking systems For example, a computation that is com- applied to OSs that cannot guarantee
and a variety of in-vehicle devices. Is an pleted late has a diminishing value, or computations will be completed on time.
RTOS needed here? Or would a general- no value whatsoever, and a computation Such OSs are best described as quasi real
purpose OS such as Linux or Windows do completed early is of no extra value. Real time or pseudo real time OSs in that they
the job? Often, such systems do require time is always a matter of degree, since execute real-time activities in preference
an RTOS, but the issue isn’t recognized even batch computing systems have a real to others whenever necessary, but don’t
until later in the design phase. time aspect to them. Nobody wants to get adequately account for non-schedulable
their payroll deposit two weeks late! activities in the system. Put simply, soft
Therefore, it’s important to understand real time shouldn’t be confused with non
why the real-time capabilities provided Problems arise when many activities com- real-time.
by an RTOS are not only beneficial, but pete for a system’s resources; in fact, this
necessary for a wide variety of embedded is where we begin to apply the real-time Positive impact
systems. For instance, consider a system property to operating systems. In imple- Traditionally, RTOSs have been used in
where users expect or need immediate menting any real-time system, a critical hard real-time environments where fail-
feedback to input. With an RTOS, a devel- step in the process will be the determina- ure to perform activities in a timely man-
oper can ensure that the system always tion of a schedule of activities that enables ner can result in harm to persons or prop-
provides feedback in a timely fashion, all activities to be completed on time. erty. But an RTOS can be just as useful
even when the system is handling many for applications that must meet QoS guar-
other compute-intensive activities. The Any real-time system will comprise dif- antees, particularly when failure to do
user is never left wondering whether the ferent types of activities: those that can be so could result in financial penalty. This
system has, in fact, accepted the button scheduled, those that cannot be scheduled covers obvious service scenarios, such
push or recognized the voice command. (for example, operating system facilities as “30 minutes or it’s free,” but it also
and interrupt handlers), and non real-time includes intangible penalties, such as lost
In a nutshell, an RTOS allows develop- activities. If non-schedulable activities opportunities or loss of market share.
ers to control how long a system will take can execute in preference to schedulable
to perform a task or respond to critical activities, they will affect the ability of the Moreover, real-time technology can be
events. Deadlines can be met within pre- system to handle time constraints. applied to conventional systems in ways
dictable, and wholly consistent, timelines, that positively impact the user experi-
even under heavy system loads. Hard vs. soft real time ence, either by improving the perceived
Often, a distinction is made between hard response to certain events or by ensuring
What, exactly, is real time? and soft real time. A hard real-time con- that important activities execute preferen-
To appreciate what real time is, what it straint is one for which there is no value tially with respect to others in the system.
isn’t, and why it’s beneficial, let us start to a computation if it is late and where For instance, consider a device that pres-

12 / 2005 Reprinted From MILITARY EMBEDDED SYSTEMS Resource Guide


Software MILITARY EMBEDDED SYSTEMS Resource Guide

ents live video, such as MPEG movies. If ensures that OS services themselves, inversions. Consequently, a high-priority
this device depends on software for any which are internal factors, don’t introduce thread that becomes ready to run can pre-
part of its content delivery, it may experi- non-schedulable activities that could vio- empt a lower-priority thread.
ence dropped frames at a rate that the user late basic requirement 3.
perceives as unacceptable. With an RTOS, Within this framework all device driv-
however, the developer can precisely con- RTOS vs. GPOS ers and operating system services apart
trol the order in which software processes The key characteristic that separates from basic scheduling and Interprocess
execute and thereby ensure that playback an RTOS from a General-Purpose OS Communication (IPC) exist as separate
occurs at an appropriate and consistent (GPOS) is the predictability inherent in processes within the system. All services
media rate. all of the requirements specified above. are accessed through a synchronous mes-
A GPOS such as Linux attempts to use a sage-passing IPC mechanism that allows
RTOS: A working definition “fairness” policy when scheduling threads the receiver to inherit the priority of the
What, exactly, constitutes a hard real- and processes to the CPU. This gives all client. This priority-inheritance scheme
time operating system? No universally applications in the system a chance to allows OSR 5 to be met by carrying the
accepted definition exists, but here is a make progress, but doesn’t establish the priority of the original real-time activity
good working definition based on real- supremacy of real-time threads in the into all service requests and subsequent
time scheduling theory and consistent system or preserve their relative priori- device driver requests.
with industry practice: A hard RTOS ties, as is required to guarantee that they Figure 1
must guarantee that a feasible schedule finish on time. Likewise, all priority There is an attendant flexibility available
can be executed given sufficient compu- information is usually lost when a system as well. Since OSR 1 and OSR 5 (refer
tational capacity if external factors are service, usually performed in a kernel back to Table 1) stress that device-driver
discounted. External factors, in this case, call, is executing on behalf of the cli- requests need to operate in priority order,
are devices that may generate interrupts, ent thread. This results in unpredictable at the priority of the client, throughput for
including network interfaces that gener- delays and thus prevents an activity from normal operations can be substantially
ate interrupts in response to network traf- completing on time. reduced. Using this model, an operating
fic. In other words, if a system designer service or device driver can be swapped
controls the environment, the operating By contrast, the microkernel architecture out in favor of a real-time version that sat-
system itself will not be the cause of any used in an OS like the QNX Neutrino isfies these requirements.
tardy computations. To provide such guar- RTOS is designed to deal directly with all
antees, the OS must satisfy the following of these requirements (Figure 2). Because A closer look
basic conditions: of its modular design, a microkernel A closer examination of OSR 2 (the OS
RTOS can employ granular synchroniza- must provide priority inheritance or pri-
1. Higher-priority tasks always execute tion mechanisms, ensuring that latencies ority-ceiling emulation) showcases the
in preference to lower-priority tasks are unaffected by system services. value of having services execute at a pri-
(see Figure 1). ority determined by the client of the ser-
2. Priority inversions, which may result The microkernel itself simply manages vice. To begin, we must look at how task
when a higher-priority task needs a processes and threads within the system, synchronization can result in blocking,
resource allocated to a lower-priority and allows them to communicate with and how this blocking can, in turn, cause
one, are bounded. each other. Scheduling is always per- priority inversion.
3. Non-schedulable activities, includ- formed at the thread level, and threads are
ing both non real-time activities and always scheduled according to their fixed Let’s say two jobs are running, Job 1 and
operating system activities, don’t priority – in the case of priority inver- Job 2, and that Job 1 has the higher prior-
exceed the remaining capacity in any sion, by the priority as adjusted by the ity. If Job 1 is ready to execute, but must
particular division. microkernel to compensate for priority wait for Job 2 to complete an activity,

In Figure 1, the RTOS scheduler deter- Operating System Requirements for Real Time
mines which thread should run by look-
OSR 1 The OS must support fixed-priority preemptive scheduling for tasks. (Both
ing at the priority assigned to every thread threads and processes, as applicable)
ready for execution. The thread with
the highest priority is selected to run. OSR 2 The OS must provide priority inheritance or priority-ceiling emulation for
Because of condition 3, we must discount synchronization primitives. This prevents cases of unbounded priority
inversion, where a higher-priority task cannot obtain a resource from a
those activities outside of the control of
lower-priority task.
the operating system, yielding the exter-
nal factors provision above. OSR 3 The OS kernel must be preemptible.
OSR 4 Interrupts must have a fixed upper bound on latency. By extension, support
From these conditions, we can derive the for nested interrupts is required.
Operating System Requirements (OSRs)
listed in Table 1. OSR 5 Operating system services must execute at a priority determined by the
client of the service. All services on which the client depends must inherit
that priority. Priority inversion avoidance must be applied to all shared
OSR 3 and OSR 4 impose a fixed upper resources used by the service.
bound on the latency that may occur on
the onset of any real-time activity. OSR 5 Table 1

Reprinted From MILITARY EMBEDDED SYSTEMS Resource Guide 2005 / 13


Software MILITARY EMBEDDED SYSTEMS Resource Guide

If you are writing an application for


deployment on an RTOS, it is important
to consider the effect that the RTOS’s
characteristics have on the execution of
the application, and to understand how
these can be used to your benefit.

For instance, applications conventionally


run in a round robin execution, compet-
ing with each other for a proportion of the
CPU capacity. With an RTOS, however,
you can manipulate the priorities of pro-
cesses to have certain activities run pref-
erentially to others in the system. Applied
judiciously, this priority manipulation can
dramatically improve response in areas
Figure 2 important to the user, without potentially
starving other processes in the system.
blocking is said to occur. The blocking in. If we return to our scenario and make
may occur because of synchronization; for Job 2 run at the priority of Job 1 during the The key to ensuring that higher-priority
instance, Job 1 and Job 2 share a resource synchronization period, then Job 3 won’t processes and threads do not starve out
controlled by a lock or semaphore, and be able to preempt Job 2, and the resulting other processes is to be certain of the
Job 1 is waiting for Job 2 to unlock the priority inversion is avoided (Figure 4). limits imposed on their execution. By
resource. Or, it may occur because Job 1 pacing the execution, or by throttling it
is requesting a service currently used by Manipulating priorities in response to load, you can limit the pro-
Job 2.

The blocking allows Job 2 to run until the


condition that Job 1 is waiting for occurs
(for instance, Job 2 unlocks the resource
that both jobs share). At that point, Job 1
gets to execute. The total time that Job 1
must wait may vary, with a minimum,
average, and maximum time. This inter-
val is known as the blocking factor.
If Job 1 is to meet any of its timeliness
constraints, this factor can’t vary accord-
ing to any parameter, such as the number
of threads or an input into the system. In
other words, the blocking factor must be
bounded.

Now let’s introduce a third job. Job 3 Figure 3


that has a higher priority than Job 2 but
a lower priority than Job 1 (Figure 3). If
Job 3 becomes ready to run while Job 2 is
executing, it will preempt Job 2, and Job 2
won’t be able to run again until Job 3
blocks or completes. This will, of course,
increase the blocking factor of Job 1; that
is, it will further delay Job 1 from execut-
ing. The total delay introduced by the pre-
emption is a priority inversion.

In fact, multiple jobs can preempt Job 2 in


this way, resulting in an effect known as
chain blocking. Under these circumstances,
Job 2 might be preempted for an indefinite
period of time, yielding an unbounded pri-
ority inversion and causing Job 1 to fail to
meet any of its timeliness constraints.

This is where priority inheritance comes Figure 4

14 / 2005 Reprinted From MILITARY EMBEDDED SYSTEMS Resource Guide


MILITARY EMBEDDED SYSTEMS Resource Guide
Software
portion of CPU consumed by these activi- how reliable a system will run, how well it
ties so that user processes get their share will perform, and how easily it will support
of the CPU. new or enhanced functionality. It’s critical,
therefore, to choose an RTOS and an RTOS
Priority manipulation can benefit a variety vendor that can meet project requirements,
of applications, including, for example, both now and in the future. O
media players (MP3, WAV, MPEG-2, and
so on). The operation of a media player Steve Furr is
can be tied to the media rate required for senior OS product
proper playback (44 kHz audio, 30 fps manager for QNX
video). So, within this constraint, a reader Software Systems and
thread and a rendering thread can both be a coauthor of the Real-
designed to wake up on a programmable time Specification
timer, to buffer or render a single frame, for Java (RTSJ). He
and then go to sleep until the next timer currently serves as a
trigger. This provides the pacing that technical member on several industry
allows the priority to be assigned above forums, including the Open Group’s
normal user activities, but below critical Real-time and Embedded Systems
system functions. Forum, which is dedicated to expanding
the marketplace for standardized real-
With well-chosen priorities, playback time and embedded systems. In his 13
will occur consistently at the given years with QNX, Steve has held various
media rate. A well-written media player engineering positions, including software
will also take into account quality of ser- architect. He holds a Bachelor’s degree
vice, so that if it doesn’t receive adequate in computer science.
CPU time, it can reduce its requirements
by selectively dropping samples or fol- For further information, contact
low an appropriate fallback strategy. Steve at:
This will then prevent it from starving
other processes as well. QNX Software Systems Ltd.
175 Terence Matthews Crescent
A strategic decision Ottawa, Ontario
An RTOS can help make complex appli- Canada, K2M 1W8
cations both predictable and reliable. Tel: 613-591-0931
In fact, the precise control over timing Fax: 613-591-3579
made possible by an RTOS adds a form E-mail: furr@qnx.com
of reliability that cannot be achieved with Website: www.qnx.com
a GPOS. If a system based on a GPOS
doesn’t behave correctly due to incorrect
timing behavior, then we can justifiably
say that the system is unreliable.

Still, choosing the right RTOS can itself


be a complex task. The underlying archi-
tecture of an RTOS is an important crite-
rion, but so are other factors. For instance,
does the RTOS support standard APIs,
such as POSIX.1? A flexible choice of
scheduling algorithms? Protocol stacks
such as IPv4, IPv6, and IPsec? What
about support for distributed or symmet-
ric multiprocessing? Diagnostic tools for
system profiling, memory analysis, and
application profiling? And what of the
RTOS vendor? Do they offer a full range
of engineering services and well-docu-
mented source and customization kits?
How about any direct experience serving
embedded developers?

On one point, there is no question: An


RTOS can play a key role in determining

Reprinted From MILITARY EMBEDDED SYSTEMS Resource Guide 2005 / 15

Das könnte Ihnen auch gefallen