Windows 进程和 WSL Linux 进程之间的共享内存

Posted

技术标签:

【中文标题】Windows 进程和 WSL Linux 进程之间的共享内存【英文标题】:Shared Memory Between Windows Process and WSL Linux Process 【发布时间】:2021-03-26 11:40:51 【问题描述】:

我想使用共享内存技术在进程之间共享数据。我可以分别在 Windows 和 WSL(Linux 的 Windows 子系统)上使用 boost 库来做到这一点。两者都工作得很好。 我的工作是让这些脚本在 1 个进程在 Windows 上运行并且 1 个进程在 WSL Linux 上运行时运行。它们在同一台机器上运行。

发送者脚本

#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;

监听器脚本

#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;

要单独在 Windows/Linux 中运行,

./sender

然后运行

./listener

从发送者创建一个共享内存,然后侦听器读取该内存。使用 boost 1.72.0 测试。应该与 boost 1.54 及更高版本一起使用。在 WSL-1 和 WSL-2 Ubuntu-1804 上测试。

问题是如何让发送者在 Windows 上工作,而监听者在 WSL Linux 上工作。这样我就可以在 Windows 和 Linux 系统之间共享内存。

提前致谢。

【问题讨论】:

我想添加一个简单的 CMakeLists.txt 以防有人想测试这个脚本。只是为了节省几分钟。 paste.tc/GjxW7GrspJ 回问:您有什么理由期望它起作用吗?在某种程度上,它们是不同的内核。另外,尝试使用 ubuntu 20.04 作为访客的 WSL2;我已经将它视为 GPG 代理 IPC 问题的修复(尽管它可能确实命名管道,所以不同) Bc WSL 可以运行/读取/写入文件/程序 Windows 文件系统。我想也许内存的一些映射部分可以用来工作。由于内核之间有 Boost 作为中间堆栈,我认为可以有解决方案。可能。也许是不可能的。我不知道。谢谢,您的问题确实有效,也许这是不可能的。 Boost 不是中间栈,因为 Boost 不支持“WSL2”作为目标平台。因此,如果它有效,那将是由于 Microsoft 已内置到 WSL 子系统中的互操作。我会在 MSDN 上问(对不起,我不知道) 谷歌搜索/dev/shm WSL2 -docker 将我带到itectec.com/ubuntu/ubuntu-opening-ubuntu-20-04-desktop-on-wsl2,这似乎专门指示Pulse 到--disable-shm=true。不是一个好兆头 【参考方案1】:

可以在 Windows 进程和 WSL1 进程之间共享内存,方法是让它们都使用同一个文件来备份共享内存。

【讨论】:

以上是关于Windows 进程和 WSL Linux 进程之间的共享内存的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Rider 调试 WSL2 中的进程?

WSL 上的齐柏林飞艇。 java.io.IOException:无法启动解释器进程

Windows之wsl简单配置

docker-compose 用于将 VS Code 中的 node.js 调试器附加到 WSL docker 中的节点进程

Linux 进程管理命令之top

Linux之ps命令基本使用