Skip to main content

Message Passing with Pipes in VxWorks

·460 words·3 mins
VxWorks RTOS Pipes Message Passing IPC Embedded Systems Programming Tutorial
Table of Contents
VxWorks Programming Tutorial for Beginners - This article is part of a series.
Part 14: This Article

🚀 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:

  1. Writer Task → sends messages into the pipe.
  2. 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
#

  1. Pipe Creation

    • pipeDevCreate sets up a pipe with size PIPE_SIZE and max message length MSG_SIZE.
    • The pipe is then opened using open().
  2. Writer Task

    • Uses write() to send formatted strings into the pipe.
  3. Reader Task

    • Uses read() to receive messages.
    • Each read extracts one message from the FIFO.

⚡ 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.

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

Related

Shared Memory Communication in VxWorks
·460 words·3 mins
VxWorks RTOS IPC Shared Memory Embedded Systems Programming Tutorial
Inter-Task Communication with Message Queues in VxWorks
·459 words·3 mins
VxWorks RTOS IPC Message Queues Embedded Systems Programming Tutorial
Semaphores in VxWorks: Binary, Counting, and Mutexes
·536 words·3 mins
VxWorks RTOS Semaphores Synchronization Mutex Embedded Systems Programming Tutorial