Professional Documents
Culture Documents
8
Intertask Communication
8-1
Introduction
Shared Memory
Message Queues
Pipes
Intertask Communication
8.1 Introduction Shared Memory Message Queues Pipes
8-2
Message passing.
Overview
Multitasking systems need communication between tasks. Intertask communication made up of three components:
q q
Data/information being shared. Mechanism to inform task that data is available to read or write. Mechanism to prevent tasks from interfering with each other (e.g., if there are two writers). Shared memory. Message passing.
8-3
Shared Memory
tTaskA tTaskB
fooLib.c
LOCAL SEM_ID fooBinSemId, fooMutexId; LOCAL FOO_BUF fooBuffer; fooSet() ... fooGet() ...
8-4
tTaskA calls fooSet( ) to set the global structure fooBuffer. tTaskB calls fooGet( ) to examine the value of fooBuffer.
This would not work on UNIX or Windows NT because each task has its own private copy of global and static variables.
Shared data is often combined with semaphores, which provide mutual exclusion and/or synchronization.
More robust than shared data. Can be used from task to task, or from ISR to task.
8-5
Intertask Communication
Introduction 8.2 Shared Memory Message Queues Pipes
8-6
Overview
All tasks reside in a common address space. User-dened data structures may be used for intertask communication:
q
Write a library of routines to access these global or static data-structures. All tasks which use these routines manipulate the same physical memory. Semaphores may be used to provide mutual exclusion and synchronization.
VxWorks provides libraries to manipulate common data structures such as linked lists and ring buffers.
8-7
Linked Lists
lstLib contains routines to manipulate doubly linked lists. List Descriptor head tail count = 2 NULL user node1
NODE
user node2
NODE
8-8
Ring Buffers
rngLib contains routines to manipulate ring buffers (FIFO data streams).
reader
writer
Mutual exclusion is not required if there is only one reader and one writer. Otherwise, user must provide. Synchronization is not built-in.
8-9
Intertask Communication
Introduction Shared Memory 8.3 Message Queues Pipes
8-10
Message Queues
Used for intertask communication within one CPU. FIFO buffer of variable length messages. Task control is built-in:
q q
8-11
Message queues are normally FIFO; however, the msgQSend( ) function does allow an urgent message to be placed at the front, rather than the rear, of the queue, enabling LIFO operation also.
8-12
MSG_Q_ID, MSG_Q_FIFO
Sending Messages
STATUS msgQSend (msgQId, buffer, nBytes, timeout, priority)
msgQId buffer nBytes timeout
MSG_Q_ID returned by msgQCreate( ).
Address of data to put on queue. Number of bytes to put on queue. Maximum time to wait (if queue is full). Values can be tick count, WAIT_FOREVER, or NO_WAIT. Priority of message to put on queue. If MSG_PRI_URGENT, message put at head of queue; if MSG_PRI_NORMAL, message put at end of queue.
priority
8-13
Returns OK on success, or ERROR on timeout, invalid msgQId, or attempt to write more bytes than maximum message size.
8-14
Receiving Messages
int msgQReceive (msgQId, buffer, maxNBytes, timeout)
msgQId buffer maxNBytes timeout Returned from msgQCreate( ). Address to store message. Maximum size of message to read from queue. Maximum time to wait (if no messages available). Values can be clock ticks, WAIT_FOREVER, or NO_WAIT.
Returns number of bytes read on success, ERROR on timeout or invalid msgQId. Unread bytes in a message are lost.
Tornado Training Workshop Copyright Wind River Systems Wind River Systems
8-15
8-16
device device device dev devi data data data dat data
consumer
Have a poll task or ISR place device data in a message queue. Have a lower priority task read data from this queue for processing.
@
Tornado Training Workshop Copyright Wind River Systems Wind River Systems
8-17
Server Task
Client
@
Tornado Training Workshop Copyright Wind River Systems Wind River Systems
8-18
Clients send requests to a server task. The server task services these requests. Some server tasks send replies to the clients.
Examples:
q q
Server handles requests to manipulate an actuator (output hardware). Some VxWorks routines cant be called at interrupt time (see the interrupt chapter). Server can perform such routines for ISR clients.
8-19
Message-Queue Browser
To examine a message queue, enter the message queue ID in the Browsers Show box, and click on Show.
8-20
Under Messages Queued, the item address is the address of the message, and value lists the rst several bytes of the message in hexadecimal and ascii.
Intertask Communication
Introduction Shared Memory Message Queues 8.4 Pipes
8-21
Creating a pipe
Writing to a pipe
Pipes
Virtual I/O device managed by pipeDrv. Built on top of message queues. Standard I/O system interface (read/write). Similar to named pipes in UNIX. (UNIX Host)
this is this is this is a this is this is a a a messagea message message message message
8-22
Creating a Pipe
STATUS pipeDevCreate (name, nMessages, nBytes)
name Name of pipe device; by convention use /pipe/yourName.
nMessages Maximum number of messages in the pipe. nBytes Maximum size in bytes of each message.
8-23
-> devs
drv 0 1 1 4 2 name /null /tyCo/0 /tyCo/1 columbia: /pipe/myPipe
8-24
The devs( ) command displays the device list. The drv column indicates the number of the driver managing the device.
this is this is this is a this is this is a a a messagea message message message message
8-25
You should call close() when you have nished using a pipe, or else you will eventually exhaust the supply of available le descriptors. See the I/O chapter for details.
8-26
Timeouts capability. Message prioritization. Faster. show(). Can be deleted. Use standard I/O interface i.e. open( ), close( ), read( ), write( ), etc. Can perform redirection via ioTaskStdSet( ) (see I/O chapter). File descriptor can be used in select( ).
8-27
Pipe advantages:
q
Select allows pending (with a timeout) until one or more le descriptors become ready to read from or write to.
Both message queues and pipes may be written to from an ISR. The pipe driver translates a write to a pipe from interrupt level into a NO_WAIT msgQSend( ) on the underlying message queue.
Summary
Shared Memory
q q
Often used in conjunction with semaphores. lstLib and rngLib can help.
Message Queues
msgQCreate( ) msgQSend( ) msgQReceive( )
Pipes
pipeDevCreate( ) Access pipe via le descriptor returned from open( ). Use write( )/read( ) to send/receive messages from a pipe. Free le descriptor by calling close().
Tornado Training Workshop Copyright Wind River Systems Wind River Systems
8-28