Skip to main content

Shared Memory Communication in VxWorks

·466 words·3 mins
VxWorks RTOS IPC Shared Memory Embedded Systems Programming Tutorial
Table of Contents

Shared Memory Communication in VxWorks
#

πŸš€ Introduction
#

In the previous blog Inter-Task Communication with Message Queues in VxWorks, we explored Message Queues for inter-task communication.
Now, let’s move to an even faster IPC method: Shared Memory.

Shared memory allows multiple tasks to directly read and write into the same memory region.
It’s ideal when:

  • Tasks need to exchange large data blocks.
  • Performance and low latency are critical.

But shared memory also comes with challenges:

  • Requires synchronization to prevent race conditions.
  • Data consistency must be managed carefully.

🧩 Why Use Shared Memory?
#

  • Fastest IPC since no copying is required.
  • Useful for high-throughput systems (e.g., video/audio data).
  • Requires synchronization primitives (e.g., semaphores, mutexes).

πŸ’» Example: Shared Buffer with Semaphore Synchronization
#

We’ll create:

  • A Producer Task β†’ writes data into shared memory.
  • A Consumer Task β†’ reads data from shared memory.
  • A Semaphore β†’ ensures safe access.

Code Example
#

#include <vxWorks.h>
#include <taskLib.h>
#include <semLib.h>
#include <stdio.h>
#include <string.h>

#define BUFFER_SIZE 128

char sharedBuffer[BUFFER_SIZE];
SEM_ID bufferSem;  // Binary semaphore for synchronization

// Consumer Task
void consumerTask()
{
    while (1)
    {
        semTake(bufferSem, WAIT_FOREVER);  // Lock buffer

        printf("Consumer: Read -> %s\n", sharedBuffer);

        semGive(bufferSem);  // Release buffer
        taskDelay(100);
    }
}

// Producer Task
void producerTask()
{
    char *messages[] = {
        "Shared Memory Message 1",
        "Shared Memory Message 2",
        "Shared Memory Message 3"
    };

    for (int i = 0; i < 3; i++)
    {
        semTake(bufferSem, WAIT_FOREVER);  // Lock buffer

        strncpy(sharedBuffer, messages[i], BUFFER_SIZE);
        printf("Producer: Wrote -> %s\n", messages[i]);

        semGive(bufferSem);  // Release buffer
        taskDelay(50);
    }
}

// Init function
void usrAppInit(void)
{
    bufferSem = semBCreate(SEM_Q_PRIORITY, SEM_FULL);

    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
#

  1. Shared Buffer (sharedBuffer)

    • A global memory area that both tasks access.
  2. Semaphore (bufferSem)

    • Ensures only one task accesses the buffer at a time.
    • semTake() β†’ lock, semGive() β†’ unlock.
  3. Producer Task

    • Writes messages into the shared buffer.
  4. Consumer Task

    • Reads messages from the shared buffer.

⚑ What You’ll See
#

When running, the output looks like:

Producer: Wrote -> Shared Memory Message 1
Consumer: Read -> Shared Memory Message 1
Producer: Wrote -> Shared Memory Message 2
Consumer: Read -> Shared Memory Message 2
Producer: Wrote -> Shared Memory Message 3
Consumer: Read -> Shared Memory Message 3

πŸ” Key Takeaways
#

  • Shared memory provides fast IPC but requires synchronization.
  • Use semaphores or mutexes to avoid race conditions.
  • Great for high-performance, data-intensive systems.

βœ… Wrap-Up
#

In this tutorial, you learned:

  • How to implement shared memory communication in VxWorks.
  • How to synchronize access with semaphores.
  • Why shared memory is the fastest IPC mechanism.

In the next blog, we’ll cover Timers in VxWorks β€” essential for periodic tasks and timeouts in real-time applications.


πŸ‘‰ Stay tuned for Blog 12: β€œTimers in VxWorks: Periodic and One-Shot Timers.”

Related

Inter-Task Communication with Message Queues in VxWorks
·459 words·3 mins
VxWorks RTOS IPC Message Queues Embedded Systems Programming Tutorial
UDP Networking in VxWorks: Sending and Receiving Packets
·574 words·3 mins
VxWorks RTOS Networking Sockets UDP Embedded Systems Programming Tutorial
Handling Multiple Clients in VxWorks with select
·648 words·4 mins
VxWorks RTOS Networking Sockets Select() Multi-Client Embedded Systems Programming Tutorial