๐ Introduction #
In the previous blog UDP Networking in VxWorks: Sending and Receiving Packets, we explored UDP networking in VxWorks.
Now, letโs switch gears to Inter-Task Communication (IPC) inside the RTOS itself.
VxWorks offers several IPC mechanisms:
- Semaphores (synchronization).
- Message Queues (data transfer).
- Shared Memory (fast, but more complex).
In this tutorial, weโll focus on Message Queues โ a reliable way to pass data between tasks.
๐งฉ Why Use Message Queues? #
- Allows safe communication between tasks.
- FIFO order ensures predictable message handling.
- Can prioritize messages if needed.
- Decouples sender and receiver tasks.
๐ป Example: Two Tasks Communicating with a Message Queue #
Weโll build:
- Producer Task โ sends messages into a queue.
- Consumer Task โ receives and processes them.
Code Example #
#include <vxWorks.h>
#include <taskLib.h>
#include <msgQLib.h>
#include <stdio.h>
#include <string.h>
#define MAX_MSGS 10
#define MAX_LEN 64
MSG_Q_ID msgQId; // Message Queue ID
// Consumer Task
void consumerTask()
{
char buffer[MAX_LEN];
int len;
while (1)
{
len = msgQReceive(msgQId, buffer, MAX_LEN, WAIT_FOREVER);
if (len > 0)
{
buffer[len] = '\0';
printf("Consumer: Received -> %s\n", buffer);
}
}
}
// Producer Task
void producerTask()
{
char *messages[] = {
"Message 1: Hello",
"Message 2: VxWorks",
"Message 3: IPC Rocks!"
};
for (int i = 0; i < 3; i++)
{
msgQSend(msgQId, messages[i], strlen(messages[i])+1, WAIT_FOREVER, MSG_PRI_NORMAL);
printf("Producer: Sent -> %s\n", messages[i]);
taskDelay(100); // Delay between messages
}
}
// Init function
void usrAppInit(void)
{
// Create a message queue
msgQId = msgQCreate(MAX_MSGS, MAX_LEN, MSG_Q_FIFO);
// Spawn tasks
taskSpawn("tConsumer", 100, 0, 4000, (FUNCPTR)consumerTask,
0,0,0,0,0,0,0,0,0,0);
taskSpawn("tProducer", 110, 0, 4000, (FUNCPTR)producerTask,
0,0,0,0,0,0,0,0,0,0);
}
๐ Explanation of the Code #
-
msgQCreate()
- Creates a message queue that can hold 10 messages of up to 64 bytes.
-
Producer Task
- Sends three messages into the queue using
msgQSend(). - Uses
MSG_PRI_NORMAL(default priority).
- Sends three messages into the queue using
-
Consumer Task
- Waits on the queue using
msgQReceive(). - Processes and prints each message.
- Waits on the queue using
-
Synchronization
WAIT_FOREVERensures producer/consumer wait until resources are available.
โก What Youโll See #
When running, the output looks like:
Producer: Sent -> Message 1: Hello
Consumer: Received -> Message 1: Hello
Producer: Sent -> Message 2: VxWorks
Consumer: Received -> Message 2: VxWorks
Producer: Sent -> Message 3: IPC Rocks!
Consumer: Received -> Message 3: IPC Rocks!
๐ Key Takeaways #
- Message Queues provide reliable IPC in VxWorks.
- They support FIFO and priority-based delivery.
- Ideal for producer-consumer patterns in embedded systems.
โ Wrap-Up #
In this tutorial, you learned:
- How to create and use a message queue in VxWorks.
- How producer and consumer tasks communicate.
- How message queues help decouple tasks in real-time systems.
In the next blog, weโll explore Shared Memory in VxWorks โ the fastest IPC method when tasks need to exchange large amounts of data.
๐ Stay tuned for Blog 11: โShared Memory Communication in VxWorks.โ