You are on page 1of 28

Chapter

8
Intertask Communication

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

8-1

Introduction

Shared Memory

Message Queues

Pipes

Intertask Communication
8.1 Introduction Shared Memory Message Queues Pipes

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

8-2

Using shared data for communication.

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.

Two common methods:


q q

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

8-3

Shared Memory
tTaskA tTaskB

fooLib.c
LOCAL SEM_ID fooBinSemId, fooMutexId; LOCAL FOO_BUF fooBuffer; fooSet() ... fooGet() ...

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

8-4

Tasks can use shared memory to communicate.


q q

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.

Message Passing Queues


VxWorks pipes and message queues are used for passing messages between tasks. Both pipes and message queues provide:
taskA taskB

FIFO buffer of messages. Synchronization. Mutual exclusion.

More robust than shared data. Can be used from task to task, or from ISR to task.

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

8-5

ISR stands for interrupt service routine.

Intertask Communication
Introduction 8.2 Shared Memory Message Queues Pipes

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

8-6

Using shared memory for intertask communication

Overview of ring buffers and linked lists

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.

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

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

user specic data

user specic data NULL

Mutual exclusion and synchronization are not built-in.


Tornado Training Workshop Copyright Wind River Systems Wind River Systems

8-8

Declare your data type as:


typedef struct my_data { NODE myNode; int myCount; /* your data items go here... */ char * pMyBuf; ...; } MY_DATA;

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.

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

8-9

Intertask Communication
Introduction Shared Memory 8.3 Message Queues Pipes

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

8-10

Message queue creation.

Sending and receiving messages.

Message queue applications.

Message Queues

Used for intertask communication within one CPU. FIFO buffer of variable length messages. Task control is built-in:
q q

Synchronization. Mutual exclusion.

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

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.

Creating a Message Queue


MSG_Q_ID msgQCreate (maxMsgs, maxMsgLength, options)
maxMsgs maxMsgLength options Maximum number of messages on the queue. Maximum size in bytes of a message on the queue. Queue type for pended tasks (MSG_Q_FIFO or MSG_Q_PRIORITY).

Returns an id used to reference this message queue or NULL on error.

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

8-12

MSG_Q_ID, MSG_Q_FIFO

and MSG_Q_PRIORITY dened in msgQLib.h.

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

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

8-13

When queue is full, sending task blocks (until timeout).

Use timeout of NO_WAIT for sending from interrupt level.

Symbolic constants dened in msgQLib.h.

Returns OK on success, or ERROR on timeout, invalid msgQId, or attempt to write more bytes than maximum message size.

Message Sending Examples


char buf[BUFSIZE]; status = msgQSend (msgQId, buf, sizeof(buf), WAIT_FOREVER, MSG_PRI_NORMAL);

status = msgQSend (msgQId, buf, sizeof(buf), NO_WAIT, MSG_PRI_URGENT);


Tornado Training Workshop Copyright Wind River Systems Wind River Systems

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

If queue is empty, receiving task pends (until timeout expires).

Pended tasks wait in FIFO or priority order (specied in msgQCreate( )).

msgQReceive( ) returns at most the number of bytes in the rst message.

Deleting a Message Queue.


STATUS msgQDelete (msgQId)
Deletes message queue. Tasks pended on queue will be unpended; their msgQSend( ) or msgQReceive( ) calls return ERROR. These tasks errno values will be set to S_objLib_OBJ_DELETED.

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

8-16

Gathering Data With Message Queues.


Input device

poll task or ISR

device device device dev devi data data data dat data

consumer

To capture data quickly for future examination:


q

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

See the code example Message Queues: Data Collection in appendix A.

Client-Server Model With Message Queues


Client

this is thisthis is this a is ais a a message message message message

Server Task

Client

@
Tornado Training Workshop Copyright Wind River Systems Wind River Systems

8-18

In the client-server model


q q

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.

See the code example Message Queues: Client - Server in Appendix A.

Client - Server Variations


How would the previous code example change if: The client requires a reply from the server? We wish to simultaneously service several requests? (We may wish to do this if there are multiple clients, and this service requires blocking while I/O completes)

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

8-19

Message-Queue Browser
To examine a message queue, enter the message queue ID in the Browsers Show box, and click on Show.

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

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

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

8-21

Creating a pipe

Reading from 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

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

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.

Returns OK on success, otherwise ERROR.

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

8-23

Example Pipe Creation


-> pipeDevCreate (/pipe/myPipe,10,100)
value = 0 = 0x0

-> devs
drv 0 1 1 4 2 name /null /tyCo/0 /tyCo/1 columbia: /pipe/myPipe

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

8-24

The devs( ) command displays the device list. The drv column indicates the number of the driver managing the device.

Reading and Writing to a Pipe


To access an existing pipe, rst open it with open( ). To read from the pipe, use read( ). To write to the pipe, use write( ).
fd = open (/pipe/myPipe, O_RDWR, 0); write (fd, msg, len);

this is this is this is a this is this is a a a messagea message message message message

read (fd, msg, len); close (fd);


Tornado Training Workshop Copyright Wind River Systems Wind River Systems

8-25

read( ) pends if pipe is empty.

write( ) pends if pipe is full.

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.

UNIX: Differences from UNIX Named Pipes


Message-oriented: preserves message boundaries Faster (no system call overhead). File descriptor table is global in VxWorks. A pipe opened by one task can be read/written by another task.

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

8-26

Message Queues vs. Pipes


Message Queue advantages:
q q

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

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

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

You might also like