🚀 Introduction #
Inter-task communication (IPC) is fundamental in real-time operating systems like VxWorks.
One powerful IPC mechanism is the pipe, which allows tasks to send and receive messages using a FIFO (First-In-First-Out) buffer.
Pipes are great when:
- You need stream-like communication between tasks.
- Tasks run asynchronously, but still need to exchange data.
- You want to avoid polling shared memory and prefer message-driven design.
🧩 How Pipes Work in VxWorks #
- A pipe is created with a fixed-size buffer.
- One or more tasks can write messages into it.
- Other tasks can read messages out.
- Supports blocking and non-blocking read/write modes.
💻 Example: Using Pipes in VxWorks #
We’ll implement:
- Writer Task → sends messages into the pipe.
- Reader Task → receives messages from the pipe.
Code Example #
#include <vxWorks.h>
#include <ioLib.h>
#include <pipeDrv.h>
#include <taskLib.h>
#include <stdio.h>
#include <string.h>
#define PIPE_NAME "/pipe/myPipe"
#define PIPE_SIZE 512
#define MSG_SIZE 64
int pipeFd;
// Writer task
void writerTask()
{
char msg[MSG_SIZE];
int count = 0;
while (1)
{
snprintf(msg, MSG_SIZE, "Hello from writer! Count=%d", count++);
write(pipeFd, msg, strlen(msg) + 1);
printf("Writer: Sent message → %s\n", msg);
taskDelay(100); // delay ~1 second
}
}
// Reader task
void readerTask()
{
char msg[MSG_SIZE];
while (1)
{
int n = read(pipeFd, msg, MSG_SIZE);
if (n > 0)
{
printf("Reader: Received message → %s\n", msg);
}
taskDelay(50);
}
}
void usrAppInit(void)
{
// Create a pipe (if not already created)
if (pipeDevCreate(PIPE_NAME, PIPE_SIZE, MSG_SIZE) == ERROR)
{
printf("Pipe creation failed or already exists\n");
}
// Open pipe for reading/writing
pipeFd = open(PIPE_NAME, O_RDWR, 0);
if (pipeFd == ERROR)
{
printf("Failed to open pipe\n");
return;
}
// Spawn writer and reader tasks
taskSpawn("tWriter", 100, 0, 4000, (FUNCPTR)writerTask, 0,0,0,0,0,0,0,0,0,0);
taskSpawn("tReader", 110, 0, 4000, (FUNCPTR)readerTask, 0,0,0,0,0,0,0,0,0,0);
}
📝 Explanation of the Code #
-
Pipe Creation
pipeDevCreatesets up a pipe with sizePIPE_SIZEand max message lengthMSG_SIZE.- The pipe is then opened using
open().
-
Writer Task
- Uses
write()to send formatted strings into the pipe.
- Uses
-
Reader Task
- Uses
read()to receive messages. - Each read extracts one message from the FIFO.
- Uses
⚡ What You’ll See #
Sample console output might look like:
Writer: Sent message → Hello from writer! Count=0
Reader: Received message → Hello from writer! Count=0
Writer: Sent message → Hello from writer! Count=1
Reader: Received message → Hello from writer! Count=1
...
🔍 Key Takeaways #
- Pipes provide FIFO-based message passing between tasks.
- Tasks can be asynchronous, but still exchange data safely.
- Ideal for producer-consumer communication patterns.
✅ Wrap-Up #
In this tutorial, you learned:
- How to create and open a pipe in VxWorks.
- How to implement a writer task and a reader task.
- How pipes simplify task communication in real-time systems.
In the next blog, we’ll dive into Signals in VxWorks: Asynchronous Notifications Between Tasks.