STARTCOUNTER

Saturday, August 6, 2011

Inter task communication in vxWorks

We can use the following mechanisms for inter task communication in vxWorks.

1) shared memory
2) semaphores
3) Message Queues
4) pipes
5) events
6) signals

Let's discuss about these inter task communication mechanisms in some posts.

Let's have a look at message queues.

  VxWorks applications are constructed using cooperating tasks. These tasks have high speed inter task communication mechanism called Message queues. Remember you can use semaphores only for mutual execution or synchronization of tasks.
   Each task and ISRs can send messages to msgQ and tasks can receive messages from a msgQ. Message queues allow a variable number of messages, each of variable length, to be queued. For full duplex communication between two tasks you need two msg  Queues.

Using Message Queues in your application is very simple. You need to know only four functions to use messages queues in your application. They are
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1) msgQCreate( ) ---> Allocates and initializes a message queue.
       
        Message Queues can be created with the call to msgQCreate. It has three parameters.
       
MSG_Q_ID msgQCreate
    (
    int maxMsgs,              /* max messages that can be queued */
    int maxMsgLength,         /* max bytes in a message */
    int options               /* message queue options */
    )

       

    The options parameter can have three values.
   
    MSG_Q_FIFO (0x00)
            queue pended tasks in FIFO order. If there are two are more tasks are waiting on this message queue then these tasks are queued in FIFO order. That is the fast task that calls the msgQReceive with this msgQID will receive the message that sent to the message Queue.
    MSG_Q_PRIORITY (0x01)
           queue pended tasks in priority order. If there are two or more tasks waiting for messages on a queue then thetasks are queued in priority order. The message sent to the queue will be received by the maximum priority task that is waiting on the queue for messages.
    MSG_Q_EVENTSEND_ERR_NOTIFY (0x02)
            When a message is sent, if a task is registered for events and the actual sending of events fails, a value of ERROR is returned and the errno is set accordingly. This option is off by default.
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2. msgQDelete( ) - delete a message queue
  
STATUS msgQDelete
    (
    MSG_Q_ID msgQId           /* message queue to delete */
    )


    This routine deletes a message queue. All tasks pending on either msgQSend( ), msgQReceive( ) or pending for the reception of events meant to be sent from the message queue will unblock and return ERROR. When this function returns, msgQId is no longer a valid message queue ID.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3) msgQSend( ) - send a message to a message queue

STATUS msgQSend
    (
    MSG_Q_ID msgQId,          /* message queue on which to send */
    char *   buffer,          /* message to send */
    UINT     nBytes,          /* length of message */
    int      timeout,         /* ticks to wait */
    int      priority         /* MSG_PRI_NORMAL or MSG_PRI_URGENT */
    )

    The timeout parameter specifies the number of ticks to wait for free space if the message queue is full. The timeout parameter can also have the following special values:

    NO_WAIT (0)
            return immediately, even if the message has not been sent.
    WAIT_FOREVER (-1)
            never time out.

    The priority parameter specifies the priority of the message being sent. The possible values are:

    MSG_PRI_NORMAL (0)
            normal priority; add the message to the tail of the list of queued messages.
    MSG_PRI_URGENT (1)
            urgent priority; add the message to the head of the list of queued messages.

     Note that msgQSend can send a message to only one task. that is a message that is sent to a message queue (using msgQSend) can be received by only one task even though there are two or more tasks waiting for messages on this message queue.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4) msgQReceive( ) - receive a message from a message queue
   
   
int msgQReceive
    (
    MSG_Q_ID msgQId,          /* message queue from which to receive */
    char *   buffer,          /* buffer to receive message */
    UINT     maxNBytes,       /* length of buffer */
    int      timeout          /* ticks to wait */
    )

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     You can also use the msgQNumMsgs( ) to get the number of messages queued to a message queue

int msgQNumMsgs
    (
    MSG_Q_ID msgQId           /* message queue to examine */
    )
 
returns the number of messages queued.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~






No comments:

Post a Comment