I want to share data between processes using shared memory technique. I can do that using boost libraries on Windows, and on WSL(Windows Subsystem for Linux) seperately. Both work great. My job is to get these scripts working when 1 process in running on Windows, and 1 process is running on WSL Linux. They are running on the same machine.
Sender Script
#include <chrono>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <cstring>
#include <iostream>
#include <thread>
using namespace boost::interprocess;
int main(int argc, char* argv[])
{
//Remove shared memory on construction and destruction
struct shm_remove
{
shm_remove() { shared_memory_object::remove("sharedmem"); }
~shm_remove() { shared_memory_object::remove("sharedmem"); }
} remover;
//Create a shared memory object.
shared_memory_object shm(create_only, "sharedmem", read_write);
//Set size
shm.truncate(1000);
//Map the whole shared memory in this process
mapped_region region(shm, read_write);
//Write all the memory to 2 (to validate in listener script)
std::memset(region.get_address(), 2, region.get_size());
std::cout << "waiting before exit" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(10));
std::cout << "exited with success.." << std::endl;
return 0;
}
Listener Script
#include <chrono>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
#include <thread>
using namespace boost::interprocess;
int main(int argc, char* argv[])
{
std::cout << "start read thread" << std::endl;
//Open already created shared memory object.
shared_memory_object shm(open_only, "sharedmem", read_only);
//Map the whole shared memory in this process
mapped_region region(shm, read_only);
//Check that memory was initialized to 1
char* mem = static_cast<char*>(region.get_address());
for (std::size_t i = 0; i < region.get_size(); ++i)
if (*mem++ != 2)
return 1; //Error checking memory
std::cout << "exited with success.." << std::endl;
return 0;
}
To run in Windows/Linux alone,
./sender
Then run
./listener
A shared memory is created from sender, then listener reads that memory. Tested with boost 1.72.0. Should work with boost 1.54 and higher. Tested on both WSL-1 and WSL-2 Ubuntu-1804.
The question is how do I get sender working on Windows with the listener working on WSL Linux. So that I can share memory between Windows and Linux systems.
Thanks in advance.
/dev/shm WSL2 -docker
led me to itectec.com/ubuntu/ubuntu-opening-ubuntu-20-04-desktop-on-wsl2 which seems to specifically instruct Pulse to--disable-shm=true
. Not a good sign – Lennalennard