Skip to main content

Message Queues in VxWorks: Passing Data Between Tasks

·480 words·3 mins
VxWorks RTOS Message Queues Inter-Task Communication Embedded Systems Programming Tutorial
Table of Contents
VxWorks Programming Tutorial for Beginners - This article is part of a series.
Part 4: This Article

πŸš€ Introduction
#

In the previous blog Using Semaphores for Task Synchronization in VxWorks, we used semaphores to synchronize tasks.
But what if tasks need to exchange data, not just signals?

That’s where message queues come in.

In this tutorial, you’ll learn:

  • What message queues are in VxWorks.
  • How to create and use a message queue.
  • A practical producer-consumer example.

🧩 What is a Message Queue?
#

A message queue is an RTOS object that allows tasks to:

  • Send messages (data) asynchronously.
  • Receive messages in order (FIFO or priority-based).
  • Communicate safely without race conditions.

VxWorks provides the API:

  • msgQCreate() β†’ create a queue.
  • msgQSend() β†’ send a message.
  • msgQReceive() β†’ receive a message.

πŸ’» Example: Producer and Consumer with Message Queue
#

We’ll create two tasks:

  • Producer Task β†’ sends data into the queue.
  • Consumer Task β†’ receives data from the queue.

Code Example
#

#include <vxWorks.h>
#include <taskLib.h>
#include <msgQLib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define MAX_MSG_LEN 64
#define MAX_MSGS    10

MSG_Q_ID msgQueue;  // Message Queue ID

// Producer Task
void producerTask()
{
    int counter = 1;
    char msg[MAX_MSG_LEN];

    while (1)
    {
        snprintf(msg, MAX_MSG_LEN, "Message %d from Producer", counter++);
        printf("Producer: sending -> %s\n", msg);

        // Send message to queue
        msgQSend(msgQueue, msg, strlen(msg) + 1, WAIT_FOREVER, MSG_PRI_NORMAL);

        sleep(2); // Produce every 2 seconds
    }
}

// Consumer Task
void consumerTask()
{
    char buffer[MAX_MSG_LEN];

    while (1)
    {
        // Wait for a message
        msgQReceive(msgQueue, buffer, MAX_MSG_LEN, WAIT_FOREVER);
        printf("Consumer: received -> %s\n", buffer);
    }
}

void usrAppInit(void)
{
    // Create a message queue with 10 messages, each up to 64 bytes
    msgQueue = msgQCreate(MAX_MSGS, MAX_MSG_LEN, MSG_Q_PRIORITY);

    // Spawn Producer Task
    taskSpawn("tProducer", 100, 0, 2000, (FUNCPTR)producerTask,
              0,0,0,0,0,0,0,0,0,0);

    // Spawn Consumer Task
    taskSpawn("tConsumer", 150, 0, 2000, (FUNCPTR)consumerTask,
              0,0,0,0,0,0,0,0,0,0);
}

πŸ“ Explanation of the Code
#

  1. msgQCreate()

    • Creates a message queue.
    • MAX_MSGS = 10 β†’ maximum number of messages stored.
    • MAX_MSG_LEN = 64 β†’ maximum length per message.
    • MSG_Q_PRIORITY β†’ messages are dequeued in priority order if set.
  2. Producer Task

    • Builds a string message with a counter.
    • Sends it using msgQSend().
  3. Consumer Task

    • Uses msgQReceive() to wait for incoming messages.
    • Prints each received message.

⚑ What You’ll See
#

When running this program, the output will look like:

Producer: sending -> Message 1 from Producer
Consumer: received -> Message 1 from Producer
Producer: sending -> Message 2 from Producer
Consumer: received -> Message 2 from Producer
...

πŸ” Key Takeaways
#

  • Message queues are ideal for task-to-task communication.
  • They allow safe data transfer between tasks without shared memory issues.
  • You can choose FIFO or priority-based message handling.

βœ… Wrap-Up
#

In this tutorial, you learned:

  • How to create and use message queues in VxWorks.
  • How tasks exchange data safely.
  • A producer-consumer example with msgQSend() and msgQReceive().

In the next blog, we’ll explore device drivers in VxWorks β€” writing your first simple driver.

πŸ‘‰ Stay tuned for Blog 5: β€œWriting a Simple Device Driver in VxWorks.”

VxWorks Programming Tutorial for Beginners - This article is part of a series.
Part 4: This Article

Related

Using Semaphores for Task Synchronization in VxWorks
·434 words·3 mins
VxWorks RTOS Semaphores Task Synchronization Embedded Systems Programming Tutorial
Understanding Tasks and Scheduling in VxWorks
·423 words·2 mins
VxWorks RTOS Tasks Scheduling Embedded Systems Programming Tutorial
Getting Started with VxWorks Programming: Hello World for Beginners
·510 words·3 mins
VxWorks RTOS Embedded Systems Programming Tutorial Beginner Guide