title: Shared Memory Between Windows Process and WSL Linux Process
body: <p>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.</p>
  <p>Sender Script</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;chrono&gt;
  #include &lt;boost/interprocess/shared_memory_object.hpp&gt;
  #include &lt;boost/interprocess/mapped_region.hpp&gt;
  #include &lt;cstring&gt;
  #include &lt;iostream&gt;
  #include &lt;thread&gt;
  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(&quot;sharedmem&quot;); }
        ~shm_remove() { shared_memory_object::remove(&quot;sharedmem&quot;); }
     } remover;
  
     //Create a shared memory object.
     shared_memory_object shm(create_only, &quot;sharedmem&quot;, 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 &lt;&lt; &quot;waiting before exit&quot; &lt;&lt; std::endl;
     std::this_thread::sleep_for(std::chrono::seconds(10));
     std::cout &lt;&lt; &quot;exited with success..&quot; &lt;&lt; std::endl;
     return 0;
  }
  </code></pre>
  <p>Listener Script</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;chrono&gt;
  #include &lt;boost/interprocess/shared_memory_object.hpp&gt;
  #include &lt;boost/interprocess/mapped_region.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;thread&gt;
  using namespace boost::interprocess;
  int main(int argc, char* argv[])
  {
     std::cout &lt;&lt; &quot;start read thread&quot; &lt;&lt; std::endl;
     
     //Open already created shared memory object.
     shared_memory_object shm(open_only, &quot;sharedmem&quot;, 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&lt;char*&gt;(region.get_address());
     for (std::size_t i = 0; i &lt; region.get_size(); ++i)
        if (*mem++ != 2)
           return 1;   //Error checking memory
  
     std::cout &lt;&lt; &quot;exited with success..&quot; &lt;&lt; std::endl;
     return 0;
  }
  </code></pre>
  <p>To run in Windows/Linux alone,</p>
  <pre><code>./sender
  </code></pre>
  <p>Then run</p>
  <pre><code>./listener
  </code></pre>
  <p>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.</p>
  <p>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.</p>
  <p>Thanks in advance.</p>