Unit II Part 1
Unit II Part 1
Unit – II
Introduction to Real Time Operating Systems
T. Ramakrishnaiah
Associate Professor
Contents
Tasks and Task States
Tasks and Data
Semaphores and Shared Data
Message Queues
Mailboxes and Pipes
Timer Functions
Events
Memory Management
Interrupt Routines in an RTOS environment
2
Real Time Embedded Systems (A7422)
3
Real Time Embedded Systems (A7422)
Introduction
A real-time operating system (RTOS) is an operating
system (OS) intended to serve real-time application process
data as it comes in, typically without buffering delays.
The Real Time Operating System consists time constraints to
4
Real Time Embedded Systems (A7422)
Introduction Cont..
Hard Real Time RTOS is an RTOS that meets a deadline with in given time limit
given time limit and the amount of time it takes to complete task is not
deterministic.
The most common RTOS designs are:
the task.
Tasks are very simple to write; under most RTOSs a task is
simply a subroutine.
ES application makes calls to the RTOS functions to start tasks,
6
Real Time Embedded Systems (A7422)
1. Running - This means that the microprocessor is executing the instructions of a task.
Unless yours is a multiprocessor system, there is only one microprocessor, and hence
only one task will be in the running state at any given time.
2. Ready - This means that some other task is in the running state but that this task has
things that it could do if the microprocessor becomes available. Any number of tasks can
be in this state.
3. Blocked -This means that this task hasn’t got anything to do right now, even if the
microprocessor becomes available. Tasks get into this state because they are waiting for
some external event. For example, a task that handles data coming in from a network
9
Real Time Embedded Systems (A7422)
unblocked?
The RTOS provides a collection of functions
does the task that is running get stopped and moved to the ready state
right away?
1
0
Real Time Embedded Systems (A7422)
1
1
Real Time Embedded Systems (A7422)
having high priority .The vLevelsTask task uses for computing how
much gasoline is in the tanks.
If user pushes the button, then vButtonTask task unblocks, The RTOS
will stop low priority vLevelsTask task in its tracks, move it to the
ready state, and run the high priority vButtonTask task.
When vButtonTask task is finished responding, it blocks and RTOS
1
3
Real Time Embedded Systems (A7422)
1
4
Real Time Embedded Systems (A7422)
1
5
Real Time Embedded Systems (A7422)
1
6
Real Time Embedded Systems (A7422)
1
7
Real Time Embedded Systems (A7422)
1
8
Real Time Embedded Systems (A7422)
You can easily accomplish this by having the two tasks in the
1
9
Real Time Embedded Systems (A7422)
2
0
Real Time Embedded Systems (A7422)
2
1
Real Time Embedded Systems (A7422)
2
2
Real Time Embedded Systems (A7422)
2
3
Real Time Embedded Systems (A7422)
The difficulty with the program is that both task1 and Task2
2
4
Real Time Embedded Systems (A7422)
2
5
Real Time Embedded Systems (A7422)
2
6
Real Time Embedded Systems (A7422)
variable. The value of the variable may differ from the one
expected if the earlier operation had been completed.
2
7
Real Time Embedded Systems (A7422)
always work correctly, even if the RTOS switches from one task to another in the
middle of executing the function.
Three rules to decide if a function is reentrant
1. A reentrant function may not use variables in a non-atomic way unless they are
stored on the stack of the task that called. The function or are otherwise the
private variables of that task.
2. A reentrant function may not call any other functions that are not themselves
reentrant.
3. A reentrant function may not use the hardware in a non atomic way.
2
8
Real Time Embedded Systems (A7422)
2
9
Real Time Embedded Systems (A7422)
3
0
Real Time Embedded Systems (A7422)
3
1
Real Time Embedded Systems (A7422)
3
2
Real Time Embedded Systems (A7422)
3
3
Real Time Embedded Systems (A7422)
Rule1 (Violates)
The variable fError is in a fixed location in memory and is
tasks between the time that it is tested and the time that it is
set.
Rule2 (Violates)
For this function to be reentrant, printf must also be reentrant.
3
4
Real Time Embedded Systems (A7422)
functions.
The code here shows a very simple function in the gray area.
3
6
Real Time Embedded Systems (A7422)
3
7
Real Time Embedded Systems (A7422)
the microprocessor from task to task and like interrupts, changing the
flow of execution.
The RTOS also gives you new tools with which to deal with shared-
3
9
Real Time Embedded Systems (A7422)
semaphore and he stops his train and waits for the semaphore to rise.
When the first train leaves that section of track, the semaphore rises,
and the engineer on the second train knows that it is safe to proceed
on.
The general idea of a semaphore in an RTOS is similar to the idea of a
railroad semaphore.
4
0
Real Time Embedded Systems (A7422)
not called ReleaseSemaphore to release it, then any other task that
calls TakeSemaphore will block until first task calls ReleaseSemaphore.
Only one task can have the semaphore at a time.
4
1
Real Time Embedded Systems (A7422)
4
2
Real Time Embedded Systems (A7422)
4
3
Real Time Embedded Systems (A7422)
data and still has the semaphore, then the following sequence of
events occurs:
1. The RTOS will switch to the button task, just as before moving the
levels task to ready state.
2. When the button task tries to get semaphore by calling
TakeSemaphore, it will block because the levels task already has the
semaphore.
4
4
Real Time Embedded Systems (A7422)
4
5
Real Time Embedded Systems (A7422)
4
6
Real Time Embedded Systems (A7422)
4
7
Real Time Embedded Systems (A7422)
4
8
Real Time Embedded Systems (A7422)
4
9
Real Time Embedded Systems (A7422)
semaphore.
The OSSemCreate function initializes the semaphore and it must be
5
0
Real Time Embedded Systems (A7422)
that the task making the call is willing to wait forever for the
semaphore.
The OSTimeDly function causes vReadTemperatureTask to block
5
2
Real Time Embedded Systems (A7422)
5
3
Real Time Embedded Systems (A7422)
In above figure the code that modifies the static variable cErrors is surrounded by
semaphore.
In the language of reentrancy we have made the use of cErrors atomic and
5
4
Real Time Embedded Systems (A7422)
5
5
Real Time Embedded Systems (A7422)
5
6
Real Time Embedded Systems (A7422)
5
7
Real Time Embedded Systems (A7422)
5
8
Real Time Embedded Systems (A7422)
that accesses the shared data, for read or for write, uses the
semaphore. If anybody forgets, then the RTOS may switch away from
the code that forgot to take the semaphore and cause an ugly shared-
data bug.
Forgetting to release the semaphore: If any task fails to release the
semaphore, then every other task that ever uses the semaphore will
sooner or later block waiting to take that semaphore and will be
blocked forever.
5
9
Real Time Embedded Systems (A7422)
6
0
Real Time Embedded Systems (A7422)
state and miss its real-time deadline, this situation is called a priority
inversion problem.
In priority inversion a high priority task waits because a low priority task
has a semaphore, but the lower priority task is not given CPU time to
finish its work.
A typical solution is to have the task that owns a semaphore run at, or
6
1
Real Time Embedded Systems (A7422)
6
2
Real Time Embedded Systems (A7422)
6
3
Real Time Embedded Systems (A7422)
released only by the task that took them. These semaphores are useful
for the shared - data problem, but they cannot be used to
communicate between two tasks. Such semaphores are sometimes
called resource semaphores or resources.
6
4
Real Time Embedded Systems (A7422)
automatically deal with the priority inversion problem and another that
will not. The former kind of semaphore commonly called a mutex
semaphore or mutex. (Other RTOSs offer semaphores that they call
mutexes but that do not deal with priority inversion.)
6
5
Real Time Embedded Systems (A7422)
6
6
Real Time Embedded Systems (A7422)
6
7
Real Time Embedded Systems (A7422)
6
8
Real Time Embedded Systems (A7422)
Message Queues
Simple Example
Suppose that we have two tasks, Task1 and Task2, each of which has a
number of high-priority, urgent things to do. Suppose also that from
time to time these two tasks discover error conditions that must be
reported on a network, a time consuming process.
In order not to delay Task1 and Task2, it makes sense to have a separate
task, ErrorsTask that is responsible for reporting the error conditions on
the network.
Whenever Task1 or Task2 discovers an error, it reports that error to
ErrorsTask and then goes on about its own business.
The error reporting process undertaken by ErrorsTask does not delay
the other tasks.
An RTOS queue is the way to implement this design.
6
9
Real Time Embedded Systems (A7422)
7
0
Real Time Embedded Systems (A7422)
7
1
Real Time Embedded Systems (A7422)
7
2
Real Time Embedded Systems (A7422)
7
3
Real Time Embedded Systems (A7422)
7
4
Real Time Embedded Systems (A7422)
7
5
Real Time Embedded Systems (A7422)
7
6
Real Time Embedded Systems (A7422)
7
7
Real Time Embedded Systems (A7422)
7
8
Real Time Embedded Systems (A7422)
7
9
Real Time Embedded Systems (A7422)
8
0
Real Time Embedded Systems (A7422)
8
1
Real Time Embedded Systems (A7422)
8
2
Real Time Embedded Systems (A7422)
Mailboxes
In general, mailboxes are much like queues.
The typical RTOS has functions to create, to write to, and to read from
mailboxes, and perhaps functions to check whether the mailbox
contains any messages and to destroy the mailbox if it is no longer
needed.
The details of mailboxes are different in different RTOSs.
Here are some of the variations that you might see:
Although some RTOSs allow a certain number of messages in each
mailbox, a number that you can usually choose when you create the
mailbox, others allow only one message in a mailbox at a time. Once
one message is written to a mailbox under these systems, the mailbox
is full; no other message can be written to the mailbox until the first
one is read.
8
3
Real Time Embedded Systems (A7422)
Mailboxes Cont..
In some RTOSs, the number of messages in each mailbox is unlimited.
There is a limit to the total number of messages that can be in all of the
mailboxes in the system, but these messages will be distributed into the
individual mailboxes as they are needed.
In some RTOSs, you can prioritize mailbox messages. Higher-priority
messages will be read before lower-priority messages, regardless of the
order in which they are written into the mailbox.
For example, in multitask system each message is a void pointer. you
must create all of the mailboxes you need when you configure the
system, after which you can use these three functions
Int sndmsg(unsigned int uMbId, void *p_vmsg, unsigned int
uPriority);
Void *rcvmsg(unsigned int uMbId, unsigned uTimeout);
Void *chkmsg(unsigned int uMbId)
8
4
Real Time Embedded Systems (A7422)
Mailboxes Cont..
In the above all three functions uMbId parameter identifies the mailbox
on which to operate.
The sndmsg function adds p_vmsg into the queue of messages held by
the uMbId mailbox with the priority indicated by uPriority .it returns an
error if uMbId is invalid or if too many messages are already pending in
mailboxes.
The rcmsg function returns the highest-priority message from the
specified mailbox. It blocks the task that called it if the mailbox is
empty. The task use uTimeout parameter to limit how long it will wait if
there are no messages.
The chkmsg function returns the first message in the mailbox. It returns
a NULL immediately if the mailbox is empty.
8
5
Real Time Embedded Systems (A7422)
Pipes
Pipes are also much like queues. The RTOS can create them, write to
them, read from them, and so on. The details of pipes, however, like
the details of mailboxes and queues, vary from RTOS to RTOS.
Some variations you might see include the following:
Some RTOSs allow you to write messages of varying lengths onto pipes
(unlike mailboxes and queues, in which the message length is typically
fixed).
Pipes in some RTOSs are entirely byte-oriented: if Task A writes 11 bytes
to the pipe and then Task B writes 19 bytes to the pipe, then if Task C
reads 14 bytes from the pipe, it will get the 11 that Task A wrote plus
the first 3 that Task B wrote. The other 16 that task B wrote remain in
the pipe for whatever task reads from it next.
Some RTOSs use the standard C library functions fread and fwrite to
read from and write to pipes.
8
6
Real Time Embedded Systems (A7422)
Pitfalls
Most RTOSs do not restrict which tasks can read from or write to any
given queue, mailbox, or pipe. Therefore, you must ensure that tasks
use the correct one each time. If some task writes temperature data
onto a queue read by a task expecting error codes, your system will not
work very well. This is obvious, but it is easy to mess up.
The RTOS cannot ensure that data written onto a queue, mailbox, or
pipe will be properly interpreted by the task that reads it. If one task
writes an integer onto the queue and another task reads it and then
treats it as a pointer, your product will not ship until the problem is
found and fixed.
8
7
Real Time Embedded Systems (A7422)
Pitfalls Cont..
Running out of space in queues, mailboxes, or pipes is usually a disaster
for embedded software. When one task needs to pass data to another,
it is usually not optional. For example, if the RTOS fail to report errors if
its queue filled, the data will be stored in garbage location which
cannot be identified, Good solutions to this problem is to make your
queues, mailboxes, and pipes large enough in the first place.
Passing pointers from one task to another through a queue, mailbox, or
pipe is one of several ways to create shared data is not necessary in all
cases.
8
8
Real Time Embedded Systems (A7422)
Timers
Timers are used to measure the elapsed time of events. For
instance, the kernel has to keep track of different times.
A particular task may need to be executed periodically, say,
every 10 msec. A timer is used to keep track of this periodicity.
A task may be waiting in a queue for an event to occur. If the
event does occur for a specified time, it has to take an
appropriate action.
A task may be waiting in a queue for a shared resource. If the
resource is not available for a specified time, it has to take an
appropriate action.
8
9
Real Time Embedded Systems (A7422)
Timer Functions
Most embedded systems must keep track of the passage of time.
To extend its battery life, the cordless bar-code scanner must turn itself
off after a certain number of seconds.
Systems with network connections must wait for acknowledgements to
data that they have sent and retransmit the data if an
acknowledgement doesn’t show up on time.
One simple service that most RTOSs offer is a function that delays a
task for a period of time; that is, blocks it until the period of time
expires.
9
0
Real Time Embedded Systems (A7422)
9
1
Real Time Embedded Systems (A7422)
9
2
Real Time Embedded Systems (A7422)
Timer Functions
Questions
How do I know that the taskDelay function takes a number of
milliseconds as its parameter?
You don’t. In fact, it doesn’t. The taskDelay function in VxWorks, like
the equivalent delay function in most RTOSs, takes the number of
system ticks as its parameter. The length of time represented by each
system tick is something you can usually control when you set up the
system.
9
3
Real Time Embedded Systems (A7422)
Timer Functions
Questions
How accurate are the delays produced by taskDelay function?
They are accurate to the nearest system tick. The RTOS works by setting
up a single hardware timer to interrupt periodically, say, every
millisecond, and bases all timings on that interrupt. This timer is often
called the heartbeat timer. For example, if one of your tasks passes 3 to
taskDelay, that task will block until the heartbeat timer interrupts three
times. The first timer interrupt may come almost immediately after the
call to taskDelay or it may come after just under one tick time or after
any amount of time between those two extremes.(Note that the task
will unblock when the delay time expires; when it will run depends as
always upon what other, higher-priority tasks are competing for the
microprocessor at that time.).
9
4
Real Time Embedded Systems (A7422)
Timer Functions
9
5
Real Time Embedded Systems (A7422)
Timer Functions
How does the RTOS know how to set up the timer hardware on my
particular hardware?
It is common for microprocessors used in embedded systems to have
timers in them. Since RTOSs, like other operating systems, are
microprocessor-dependent, the engineers writing the RTOS know what
kind of microprocessor the RTOS will run on and can therefore program
the timer on it. If you are using nonstandard timer hardware, then you
may have to write your own timer setup software and timer interrupt
routine. The RTOS will have an entry point for your interrupt routine to
call every time the timer expires. Many RTOS vendors provide board
support packages or BSPs, which contain driver software for common
hardware components – such as timers - and instructions and model
code to help you write driver software for any special hardware you are
using.
9
6
Real Time Embedded Systems (A7422)
Timer Functions
What is a “normal" length for the system tick?
There really isn’t one. The advantage of a short system tick is that you
get accurate timings. The disadvantage is that the microprocessor must
execute the timer interrupt routine frequently. a short system tick can
decrease system throughput quite considerably by increasing the
amount of microprocessor time spent in the timer interrupt routine.
Real - time system designers must make this trade-off.
9
7
Real Time Embedded Systems (A7422)
Timer Functions
What if my system needs extremely accurate timing?
You have two choices. One is to make the system tick short enough that
RTOS timings fit your definition of “extremely accurate”. The second is
to use a separate hardware timer for those timings that must be
extremely accurate. The advantage of the RTOS timing functions is that
one hardware timer times any number of operations simultaneously.
9
8
Real Time Embedded Systems (A7422)
Timer Functions
Other Timing Services
Most RTOSs offer an array of other timing services, all of them based on
the system tick.
For example, most allow you to limit how long a task will wait for a
message from a queue or a mailbox, how long a task will wait for a
semaphore, and so on. Although these services are occasionally useful .
For example, if you set a time limit when your high - priority task
attempts to get a semaphore and if that time limit expires, then your
task does not have the semaphore and cannot access the shared data.
Then you’ll have to write code to allow your task to recover. Before
writing this code which is likely to be difficult, since your task needs to
use the data but can’t - it may make sense to ask whether there might
not be a better design.
9
9
Real Time Embedded Systems (A7422)
Timer Functions
Other Timing Services
If your high-priority task is in such a hurry that it cannot wait for the
semaphore, perhaps it would make more sense to send instructions
about using the shared data through a mailbox to a lower-priority task
and let the higher-priority task get on with its other work.
A rather more useful service offered by many RTOSs is to call the
function of your choice after a given number of system ticks. Depending
upon the RTOS, your function may be called directly from the timer
interrupt service routine, or it may be called from a special, high-
priority task within the RTOS.
1
0
Real Time Embedded Systems (A7422)
Events
Another service many RTOSs offer is the management of events within the system.
An event is essentially a Boolean flag that tasks can set or reset and that other
task that turns on the laser scanning mechanism and tries to recognize the bar-
code must start.
Some standard features of events are listed below:
More than one task can block waiting for the same event, and the RTOS will
unblock all of them (and run them in priority order) when the event occurs.
For example, if the radio task needs to start warming up the radio when the user
pulls the trigger, then that task can also wait on the trigger-pull event.
1
0
Real Time Embedded Systems (A7422)
Events Cont..
RTOSs typically form groups of events, and tasks can wait for any subset
event after it has occurred and tasks that were waiting for it have been
unblocked.
Some RTOSs reset events automatically; others require that your task
software do this.
It is important to reset events: if the trigger-pull event is not reset, for
example, then tasks that need to wait for that event to be set will never
again wait.
1
0
Real Time Embedded Systems (A7422)
1
0
Real Time Embedded Systems (A7422)
1
0
Real Time Embedded Systems (A7422)
Memory Management
Most RTOSs have some kind of memory management
subsystem.
some offer the equivalent of the C library functions malloc and
free, real-time systems engineers often avoid these two
functions because they are typically slow and because their
execution times are unpredictable.
They favor instead functions that allocate and free fixed-size
buffers, and most RTOSs offer fast and predictable functions
for that purpose.
1
0
Real Time Embedded Systems (A7422)
The reqbuf and getbuf functions allocate memory buffer from a pool
1
0
Real Time Embedded Systems (A7422)
which the buffer is allocated, since all the buffers in anyone pool are
the same size. The tasks that call, these functions must know the sizes
of the buffers in each pool.
The relbuf function frees a memory buffer.
1
0
Real Time Embedded Systems (A7422)
int init_mem_pool (unsigned int uPoolId, void *p_vMemory, unsigned int uBufSize,
1
0
Real Time Embedded Systems (A7422)
Therefore, interrupt routines must not get semaphores, read from queues or
to the interrupt routine, the task that was running when the interrupt occurred
will be blocked, even if that task is the highest- priority task.
Also, most interrupt routines must run to completion to reset the hardware to be
RTOS to switch tasks unless the RTOS knows that an interrupt routine, and
not a task, is executing.
This means that interrupt routines may not write to mailboxes or queues on
which tasks may be waiting, set events, release semaphores, and so on - unless
the RTOS knows it is an interrupt routine that is doing these things.
If an interrupt routine breaks this rule, the RTOS might switch control away
from the interrupt routine (which the RTOS think is a task) to run another task,
and the interrupt routine may not complete for a long time, blocking at least all
lower-priority interrupts and possibly all interrupts.
1
1
Real Time Embedded Systems (A7422)
1
1
Real Time Embedded Systems (A7422)
This time, the task code and the interrupt routine share the temperature data with a
semaphore, then when the interrupt routine called GetSemaphore, the RTOS would
notice that the semaphore was already taken and block.
This will stop both the interrupt routine and vTaskTestTemperatures (the task that was
interrupted), after which the system would grind to a halt in a sort of one-armed
deadly embrace.
With both the interrupt routine and vTaskTestTemperatures blocked, no code will ever
1
1
Real Time Embedded Systems (A7422)
1
1
Real Time Embedded Systems (A7422)
1
1
Real Time Embedded Systems (A7422)
interrupt routine writes to the mailbox, the RTOS unblocks the higher-
priority task.
Then the RTOS (knowing nothing about the interrupt routine) notices
thinks is part of the lower priority task), the RTOS switches to the
higher-priority task. The interrupt routine doesn’t get to finish until
later.
1
1
Real Time Embedded Systems (A7422)
1
1
Real Time Embedded Systems (A7422)
By doing this, the RTOS finds out when an interrupt routine has started.
When the interrupt routine later writes to mailbox, the RTOS knows to return to
the interrupt routine and not to switch tasks, no matter what task is unblocked by
the write to the mailbox.
When the interrupt routine is over, it returns, and the RTOS gets control again.
The RTOS scheduler then figures out what task should now get the microprocessor.
If your RTOS uses this method, then you will need to call some function within the
RTOS that tells the RTOS where your interrupt routines are and which hardware
interrupts correspond to which interrupt routines.
1
1
Real Time Embedded Systems (A7422)
1
1
Real Time Embedded Systems (A7422)
the interrupt routines call to let the RTOS know that an interrupt routine is
running.
After the call to that function, the RTOS knows that an interrupt routine is in
progress, and when the interrupt routine writes to the mailbox the RTOS always
returns to the interrupt routine, no matter what task is ready, as in the figure.
When the interrupt routine is over, it jumps to or calls some other function in
RTOS, which calls the scheduler to figure out what task should now get the
microprocessor. Essentially, this procedure disables the scheduler for the
duration of the interrupt routine.
1
2
Real Time Embedded Systems (A7422)
returns to the interrupt routine that calls it, never to some other
task.
In this method, the RTOS also has a function the interrupt routine
can interrupt a lower-priority interrupt routine, then another consideration comes into
play.
If the higher-priority interrupt routine makes any calls to RTOS functions, then the
lower-priority interrupt routine must let the RTOS know when the lower-priority
interrupt occurs.
Otherwise, when the higher-priority interrupt routine ends, the RTOS scheduler may
run some other task rather than let the lower-priority interrupt routine complete.
Obviously, the RTOS scheduler should not run until all interrupt routines are complete.
1
2
Real Time Embedded Systems (A7422)
1
2