C++ Boost Creating Shared-Memory for two different processes

So, I'm trying to create a shared-memory segment in a C++ program, so I can for example write a simple character in it, and read that character from another C++ program.

I've downloaded the Boost libraries, as I read it simplifies this process. Basically I have two questions: First of all, how do I write to it after its created? Then what should I write in the second program in order to identify the segment and read the info in it?

This is what I've got so far. It's not a lot, but I'm still new to this (first program):

#include "stdafx.h"
#include <boost/interprocess/windows_shared_memory.hpp>
#include <boost/interprocess/mapped_region.hpp>

int main(int argc, char *argv[])
{
   using namespace boost::interprocess;

   windows_shared_memory shared (create_only, "shm", read_write, 65536); 
   //created shared memory using the windows native library
   mapped_region region (shared, read_write, 0 , 0 , (void*)0x3F000000); 
   //mapping it to a region using HEX

   //Here I should write to the segment

   return 0;
}

Thanks in advance. Any information I will be more than happy to provide, in order to receive the appropriate help.

Upvotes: 3

Views: 9945

Answers (1)

djf
djf

Reputation: 6757

The following is a slightly modified example which is based on the Boost documentation on Shared Memory

Note: When using windows_shared_memory keep in mind that the shared memory block will automatically be destroyed when the last process that uses it exists. In the example below that means, if the server exists before the client has a change to open the shared memory block, the client will throw an exception.

Server side:

#include <boost/interprocess/windows_shared_memory.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <cstring>
#include <cstdlib>
#include <string>

int main(int argc, char *argv[])
{
   using namespace boost::interprocess;

   //Create a native windows shared memory object.
   windows_shared_memory shm (create_only, "shm", read_write, 65536);

   //Map the whole shared memory in this process
   mapped_region region(shm, read_write);

   //Write a character to region
   char myChar = 'A';
   std::memset(region.get_address(), myChar , sizeof(myChar));

   ... it's important that the server sticks around, otherwise the shared memory 
       block is destroyed and the client will throw exception when trying to open

   return 0;
}

Client side:

#include <boost/interprocess/windows_shared_memory.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <cstring>
#include <cstdlib>
#include <string>

int main(int argc, char *argv[])
{
   using namespace boost::interprocess;

   //Open already created shared memory object.
   windows_shared_memory shm (open_only, "shm", read_only);

   //Map the whole shared memory in this process
   mapped_region region(shm, read_only);

   //read character from region
   char *myChar= static_cast<char*>(region.get_address());


   return 0;
}

Instead of memsetting raw bytes in shared memory, you'll probably be better off using Boost.Interprocess. It's designed to simplify the use of common interprocess communication and synchronization mechanisms and offers a wide range of them - including shared memory. For example you could create a vector in shared memory.

Upvotes: 4

Related Questions