?? real-time experiment #4 message queues.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0069)http://www.rt.db.erau.edu/experiments/vx/mesgQueues/Experiment-4.html -->
<HTML><HEAD><TITLE>Real-Time Experiment #4: Message Queues</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2900.2873" name=GENERATOR></HEAD>
<BODY>ticks
<CENTER>
<H1>Embry-Riddle Real-Time Laboratory Experiment<BR>Experiment #4<BR>Message
Queues </H1></CENTER>
<HR SIZE=3>
<H2>Introduction</H2>
<P></P>In VxWorks, the primary intertask communication mechanism within a single
CPU is <EM>message queues</EM>. Message queues allow a variable number of
messages, each of variable length, to be queued(FIFO or priority based). Any
task can send a message to a message queue and any task can receive a message
from a message queue. Multiple tasks can send to and receive from the same
message queue. Two way communication between two tasks generally requires two
message queues, one for each direction.
<P></P>
<HR SIZE=3>
<H2>Objectives</H2>The following are the primary objectives of this experiment:
<UL>
<LI>To demonstrate the use of VxWorks message queues. </LI></UL>
<P></P>
<HR SIZE=3>
<H2>Description</H2>Wind message queues are created and deleted with the
following routines:
<UL>
<LI><EM>msgQCreate(int maxMsgs, int maxMsgLength, int options)</EM>: Allocate
and initialize a message queue. </LI></UL>
<UL>
<LI><EM>msgQDelete(MSG_Q_ID msgQId)</EM>: Terminate and free a message queue.
</LI></UL>
<UL>
<LI><EM>msgQSend(MSG_Q_ID msgQId, char *Buffer, UINT nBytes, int timeout, int
priority)</EM>: Send a message to a message queue. </LI></UL>
<UL>
<LI><EM>msgQReceive(MSG_Q_ID msgQId, char *Buffer, UINT nBytes, int
timeout)</EM> Send a message to a message queue. </LI></UL>This library provides
messages that are queued in FIFO order, with a single exception: there are two
priority levels, and messages marked as high priority are attached to the head
of the queue.
<P></P>A message queue is created with <EM>msgQCreate()</EM>. Its parameters
specify the maximum number of messages that can be queued in the message queue
and the maximum length in bytes of each message.
<P></P>A task sends a message to a message queue with <EM>msgQSend()</EM>. If no
tasks are waiting for messages on that queue, the message is added to the
queue's buffer of messages. If any tasks are waiting for a message from that
message queue, the message is immediately delivered to the first waiting task.
<P></P>A task receives a message from a message queue with
<EM>msgQReceive()</EM>. If messages are already available in the queue's buffer,
the first message is immediately dequeued and returned to the caller. If no
messages are available, then the calling task blocks and is added to a queue of
tasks waiting for messages. The queue of waiting tasks can be ordered either by
task priority or FIFO, as specified when the queue is created.
<P></P>
<UL>
<LI><EM><B>Timeouts</B></EM>: Both <EM>msgQSend()</EM> and
<EM>msgQReceive()</EM> take timeout parameters. The timeout parameter
specifies how many ticks(clock ticks per second) to wait for space to be
available when sending a message and to wait for a message to be available
when receiving a message. </LI></UL>
<UL>
<LI><EM><B>Urgent Messages</B></EM>: The <EM>msgQSend()</EM> function can
specify the priority of a message either as normal <B>MSG_PRI_NORMAL</B> or
urgent <B>MSG_PRI_URGENT</B>. Normal priority messages are added to the tail
of the message queue, while urgent priority messages are added to the head of
the message queue. </LI></UL>
<P></P><B>1. Example</B>
<P></P><PRE>-------------------------------------------------------------------------------------
/* includes */
#include "vxWorks.h"
#include "msgQLib.h"
/* function prototypes */
void taskOne(void);
void taskTwo(void);
/* defines */
#define MAX_MESSAGES 100
#define MAX_MESSAGE_LENGTH 50
/* globals */
MSG_Q_ID mesgQueueId;
void message(void) /* function to create the message queue and two tasks */
{
int taskIdOne, taskIdTwo;
/* create message queue */
if ((mesgQueueId = msgQCreate(MAX_MESSAGES,MAX_MESSAGE_LENGTH,MSG_Q_FIFO))
== NULL)
printf("msgQCreate in failed\n");
/* spawn the two tasks that will use the message queue */
if((taskIdOne = taskSpawn("t1",90,0x100,2000,(FUNCPTR)taskOne,0,0,0,0,0,0,0,
0,0,0)) == ERROR)
printf("taskSpawn taskOne failed\n");
if((taskIdTwo = taskSpawn("t2",90,0x100,2000,(FUNCPTR)taskTwo,0,0,0,0,0,0,0,
0,0,0)) == ERROR)
printf("taskSpawn taskTwo failed\n");
}
void taskOne(void) /* task that writes to the message queue */
{
char message[] = "Received message from taskOne";
/* send message */
if((msgQSend(mesgQueueId,message,MAX_MESSAGE_LENGTH, WAIT_FOREVER,
MSG_PRI_NORMAL)) == ERROR)
printf("msgQSend in taskOne failed\n");
}
void taskTwo(void) /* tasks that reads from the message queue */
{
char msgBuf[MAX_MESSAGE_LENGTH];
/* receive message */
if(msgQReceive(mesgQueueId,msgBuf,MAX_MESSAGE_LENGTH, WAIT_FOREVER)
== ERROR)
printf("msgQReceive in taskTwo failed\n");
else
printf("%s\n",msgBuf);
msgQDelete(mesgQueueId); /* delete message queue */
}
-------------------------------------------------------------------------------------
</PRE>
<P></P>
<HR SIZE=3>
<H2>Procedures</H2>1. Copy the source code in the example and compile it.
<P></P>2. Load the object file onto the target machine.
<P></P>3. Run the examples by executing the main routine("message") of the
example on WindSh terminal.
<P></P>Note: Make sure you have redirected I/O, otherwise you won't see the
results of the printf commands.
<HR SIZE=3>
<H2>Follow On Experiment</H2>Experiment 1. Modify the above program so that
taskTwo can send a string message ("Received message from taskTwo") to taskOne.
Have taskOne print the message sent from taskTwo.
<P></P>Experiment 2. Are there any advantages or disadvantages to using message
queues as opposed to global variables as a message passing scheme? Explain your
answer.
<P></P>
<HR SIZE=3>
<H2>Additional Information</H2>Refer to VxWorks User's Manual and Reference
Manual.
<P></P>
<HR SIZE=3>
<CENTER>
<H4><A
href="http://www.rt.db.erau.edu/experiments/vx/toc/TableOfContents.html">Return
to Primary Table of Contents </A></H4></CENTER>
<HR SIZE=3>
<CENTER>Last Updated: 17 March 1997<BR><EM>Created by: Dan Eyassu</EM><BR><A
href="mailto:eyassud@db.erau.edu">eyassud@db.erau.edu</A><BR></CENTER></BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -