Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intertask Communication

Similar presentations


Presentation on theme: "Intertask Communication"— Presentation transcript:

1 Intertask Communication
8.1 Introduction Shared Memory Message Queues Pipes Using shared data for communication. Message passing.

2 Overview Multitasking systems need communication between tasks.
Intertask communication made up of three components: 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). Two common methods: Shared memory. Message passing.

3 Shared Memory Tasks can use shared memory to communicate.
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.

4 Message Passing Queues
VxWorks pipes and message queues are used for passing messages between tasks. Both pipes and message queues provide: FIFO buffer of messages. Synchronization. Mutual exclusion. More robust than shared data. Can be used from task to task, or from ISR to task. ISR stands for interrupt service routine.

5 Intertask Communication
Introduction 8.2 Shared Memory Message Queues Pipes Using shared memory for intertask communication Overview of ring buffers and linked lists

6 Overview All tasks reside in a common address space.
User-defined data structures may be used for intertask communication: 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.

7 Linked Lists lstLib contains routines to manipulate doubly linked lists. Mutual exclusion and synchronization are not built-in. Declare your data type as: typedef struct my_data { NODE myNode; int myCount; /* your data items go here... */ char * pMyBuf; ...; } MY_DATA;

8 Ring Buffers rngLib contains routines to manipulate ring buffers (FIFO data streams). Mutual exclusion is not required if there is only one reader and one writer. Otherwise, user must provide. Synchronization is not built-in.

9 Intertask Communication
Introduction Shared Memory 8.3 Message Queues Pipes Message queue creation. Sending and receiving messages. Message queue applications.

10 Message Queues Used for intertask communication within one CPU.
FIFO buffer of variable length messages. Task control is built-in: Synchronization. Mutual exclusion. 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.

11 Creating a Message Queue
MSG_Q_ID msgQCreate (maxMsgs, maxMsgLength, options) maxMsgs Maximum number of messages on the queue. maxMsgLength Maximum size in bytes of a message on the queue. options 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. MSG_Q_ID, MSG_Q_FIFO and MSG_Q_PRIORITY defined in msgQLib.h.

12 STATUS msgQSend (msgQId, buffer, nBytes, timeout, priority)
Sending Messages STATUS msgQSend (msgQId, buffer, nBytes, timeout, priority) msgQId MSG_Q_ID returned by msgQCreate( ). buffer Address of data to put on queue. nBytes Number of bytes to put on queue. timeout Maximum time to wait (if queue is full). Values can be tick count, WAIT_FOREVER, or NO_WAIT. priority “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. When queue is full, sending task blocks (until timeout). Use timeout of NO_WAIT for sending from interrupt level. Symbolic constants defined in msgQLib.h. Returns OK on success, or ERROR on timeout, invalid msgQId, or attempt to write more bytes than maximum message size.

13 Message Sending Examples

14 int msgQReceive (msgQId, buffer, maxNBytes, timeout)
Receiving Messages int msgQReceive (msgQId, buffer, maxNBytes, timeout) msgQId Returned from msgQCreate( ). buffer Address to store message. maxNBytes Maximum size of message to read from queue. timeout 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. If queue is empty, receiving task pends (until timeout expires). Pended tasks wait in FIFO or priority order (specified in msgQCreate( )). msgQReceive( ) returns at most the number of bytes in the first message.

15 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.

16 Gathering Data With Message Queues.
To capture data quickly for future examination: 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. See the code example Message Queues: Data Collection in appendix A. @

17 Client-Server Model With Message Queues
In the client-server model Clients send requests to a server task. The server task services these requests. Some server tasks send replies to the clients. Examples: Server handles requests to manipulate an actuator (output hardware). Some VxWorks routines can’t 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. @

18 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)

19 Message-Queue Browser
To examine a message queue, enter the message queue ID in the Browser’s Show box, and click on Show. Under Messages Queued, the item address is the address of the message, and value lists the first several bytes of the message in hexadecimal and ascii.

20 Intertask Communication
Introduction Shared Memory Message Queues 8.4 Pipes Creating a pipe Reading from a pipe Writing to a pipe

21 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)

22 STATUS pipeDevCreate (name, nMessages, nBytes)
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.

23 Example Pipe Creation -> pipeDevCreate (“/pipe/myPipe”,10,100)
value = 0 = 0x0 -> devs drv name 0 /null 1 /tyCo/0 1 /tyCo/1 4 columbia: 2 /pipe/myPipe The devs( ) command displays the device list. The drv column indicates the number of the driver managing the device.

24 Reading and Writing to a Pipe
To access an existing pipe, first open it with open( ). To read from the pipe, use read( ). To write to the pipe, use write( ). read( ) pends if pipe is empty. write( ) pends if pipe is full. You should call close() when you have finished using a pipe, or else you will eventually exhaust the supply of available file descriptors. See the I/O chapter for details.

25 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.

26 Message Queues vs. Pipes
Message Queue advantages: Timeouts capability. Message prioritization. Faster. show(). Can be deleted. Pipe advantages: 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( ). Select allows pending (with a timeout) until one or more file 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.

27 Summary Shared Memory Message Queues Pipes
Often used in conjunction with semaphores. lstLib and rngLib can help. Message Queues msgQCreate( ) msgQSend( ) msgQReceive( ) Pipes pipeDevCreate( ) Access pipe via file descriptor returned from open( ). Use write( )/read( ) to send/receive messages from a pipe. Free file descriptor by calling close( ).


Download ppt "Intertask Communication"

Similar presentations


Ads by Google