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 #
-
Shared Buffer (
sharedBuffer)- A global memory area that both tasks access.
-
Semaphore (
bufferSem)- Ensures only one task accesses the buffer at a time.
semTake()β lock,semGive()β unlock.
-
Producer Task
- Writes messages into the shared buffer.
-
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.β