Chapter 7.
More Operating System
Services
The other features offered by
commercial RTOSs.
Contents
Message Queues, Mailboxes and
Pipes
Timer Functions
Events
Memory Management
Interrupt Routines in an RTOS Environment
Message Queues, Mailboxes
and Pipes
Task must be able to communicate
with one another to coordinate their
activities or to share data.
queues, mailboxes, and pipes
Message Queues, Mailboxes
and Pipes
void Task1(void) void vLogError(int iErrorType)
{ {
… AddToQueue(iErrorType);
if (!!problem arises) }
vLogError(ERROR_TYPE_X);
static int cErrors;
…
!! Other things that need to be done soon. void ErrorTask(void)
} {
int iErrorType;
void Task2(void)
{ while(FOREVER)
… {
if (!!problem arises) ReadFromQueue(&iErrorType);
vLogError(ERROR_TYPE_Y); ++cErrors;
!! Send cErrors and iErrorType out on network
…
}
!! Other things that need to be done soon. }
}
Details with Message Queues
Initialize your queues before using
queue.
Have as many queues as you want
the identity of the queue
When queue is full, the RTOS must
return an error
block
Details with Message Queues
A function that will read from a queue
if there is any data
and will return error code and block
if not.
Write onto a queue the number of bytes
taken up by a void pointer
Pointer and Queues
Casting the data as a void pointer
One task can pass data to one other
task
Put the data into buffer
And write a pointer to the buffer onto
queue
Pointer and Queues
void *apvQueue[SIZEOF_ QUEUE]; void vLogError(int iErrorType) {
byReturn = OSQPost(pOseQueue,
void main(void) (void*)iErorType);
{ …
… }
pOseQueue = OSQCreate(apvQueue,
SIZEOF_ QUEUE); void ErrorTask(void)
… {
!!Start Task1 while(FOREVER)
!!Start Task2 {
} iErrorType = (int) OSQPend(pOseQueue,
void Task1(void) { W AIT_ FOREVER, &byErr);
… }
} …
void Task2(void) { }
…
}
Fig. 7.2 More Realistic Use of a Queue
Pointer and Queues
void vMainT
ask(void)
{
static OS_ EVENT *pOseQueueT
emp; int *pT
emperatures;
BYTE byErr;
void vReadT
emperaturesT
ask(void)
{ while(TRUE)
int *pT
emperatures; {
pTemperatures = (int*)OSQPend(pOseQueueT emp,
W hile(TRUE) { WAIT_ FOREVER, &byErr);
… if (pTemperatures[0] != pTemperatures[1])
pTemperatures = !! Set Off howling alarm;
(int*)malloc(2*sizeof*pT
emperatures); }
pT
emperatures[0] = }
pT
emperatures[1] =
OSQPost(pOseQueueTemp,
(void*)pT
emperatures);
}
}
Fig. 7.3 Passing Pointer on Queues
Mailboxes
Much like queues.
RTOS can
Create, write ,check and read from mail boxes.
The number of messages in a mailbox is limited.
User can prioritize mailbox messages.
MultiT
ask!
sndmsg, rcvmsg, chkmsg
Pipes
Much like queues.
RTOS can
Create, write ,check and read from pipes.
Byte-oriented.
Use fread(), fwrite()
Which should I use?
Trade-offs among
Flexibility, speed, memory space, disabled
interrupt time length
Refer to documents
Pitfalls
•Passing Pointers through a queue may create
shared data bugs
void vReadT
emperaturesT
ask(void) void vReadTemperaturesTask(void)
{ {
int *pT
emperatures; int iT
emperatures[2];
W hile(TRUE) { W hile(TRUE) {
… …
pTemperatures =
(int*)malloc(2*sizeof*pT
emperatures); iT
emperatures[0] =
iT
emperatures[1] =
pT
emperatures[0] =
pT
emperatures[1] = OSQPost(pOseQueueT emp,
(void*)iT
emperatures);
OSQPost(pOseQueueTemp, }
(void*)pT
emperatures); }
}
}
Timer Functions
Must Keep track of the passage of time
Offer taskDelay() functions.
void vMakePhoneCallT ask(void)
{
…
taskDelay(100);
oneOn(*p_ chPhoneNumber –’0’);
vDialingT
taskDelay(100);
vDialingT
oneOff();
…
}
Events
• Procedure
1. More than task can block waiting
2. The events occurs
3. The RTOS will unblock all of tasks
Form group of events and any subset of
events within the group
Need to reset events automatically or
manually
Comparison for Intertask Comm.
Semaphore : the fastest and simplest
not much information
Events :
more complicated than semapohre
can wait several events at the same time
take more CPU time
Queues : much information
CPU-intensive, bugs-opportunity.
Memory Management
Avoid malloc and free function
Slow and unpredictable
Use the functions that allocate and
free fixed-size buffers.
Predictable
At MultiT
ask! system
void *getbuf(PoolID, timeout);
void *reqbuf(PoolID);
void reluf(PoolID, *p_vBuffer);
Interrupt Routines in an RTOS
Environment
Interrupt Routines must not call any
RTOS functions that-
1. Might block the caller.
2. Might cause the RTOS to switching tasks
without fair warning
Interrupt Routines in an RTOS
Environment
ISR
RTOS
TaskHigh Send message
to mailbox
TaskLow
Time
Fig. 7.14 How Interrupt Routines Should Work
Interrupt Routines in an RTOS
Environment
ISR
RTOS
TaskHigh Send Message
to mailbox
TaskLow
Time
Fig. 7.15 What Would Really Happen
Interrupt Routines in an RTOS
Environment
1. The RTOS intercepts all the interrupts and
then call your interrupt routine.
ISR
Call Return
RTOS
Send Message
TaskHigh to mailbox
TaskLow
Time
Fig. 7.16 How Interrupt Routines Do Work
Interrupt Routines in an RTOS
Environment
2. Disabling the scheduler for the
duration of the interrupt routine.
ISR
Jump or call
RTOS
Enter Send Message
TaskHigh interrupt to mailbox
routine.
TaskLow
Time
Fig. 7.17 How Interrupt Routines Do Work:Plan B
Interrupt Routines in an RTOS
Environment
3. Aseparate set of functions for
interrupt routines specially
OSSemPost
OSISRSemPost for interrupt routines
Nested Interrupts
• The RTOS must know
when the lower-priority interrupt routine.
RTOS schedler goes to TaskHigh
instead of finishing low-priority ISR.
High-priority
ISR
Low-priority
ISR
RTOS
TaskHigh Send Message
to mailbox
High-priority
interrupt
TaskLow occurs.
Time
Fig. 7.18 Nested Interrupts and the RTOS