Shared Memory Between Windows Process and WSL Linux Process
Asked Answered
P

1

7

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.

Piscine answered 15/12, 2020 at 19:43 Comment(5)
I wanted to add a simple CMakeLists.txt in case someone wanted to test this script. just to save a few minutes. paste.tc/GjxW7GrspJPiscine
Question back: is there any reason you expect it to work? They're different kernels, to an extent. Also, try WSL2 with ubuntu 20.04 as the guest; I've seen it as a fix for GPG agent IPC issues (though it likely does named pipes, so different)Lennalennard
Bc WSL can run/read/write files/programs Windows filesystem. I thought maybe some mapped portions of the memory can be utilized to work. Since there is Boost as middle-stack between kernels, I think there can be solution. possibly. Maybe its not possible. I am not sure. And thanks, your question is really valid, maybe this is impossible.Piscine
Boost is not a middle stack, because Boost doesn't support "WSL2" as a target platform. So, if it works, it will be due to interop that Microsoft has built into the WSL subsystem. I'd ask on MSDN (I'm sorry I don't know off-hand)Lennalennard
Googling /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 signLennalennard
P
-2

It is possible to share memory between a Windows process and a WSL1 process by having them both use the same file to back the shared memory.

Peninsula answered 25/6, 2021 at 6:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.