简体   繁体   English

从另一个进程访问mmap内存

[英]Access mmap memory from another process

I've started playing with mmap. 我开始玩mmap了。 I'm trying to create an example workspace that will be then extended to the real case. 我正在尝试创建一个示例工作区,然后将其扩展到实际案例。

This is what I want to achieve: 这就是我想要实现的目标:

PROCESS 1: 过程1:

  • mmap a file (actually a device, but it's okay to generate an example with a text file) mmap一个文件(实际上是一个设备,但可以用文本文件生成一个例子)

PROCESS 2: (not foked from process 1; just an independent process) 流程2 :(不是从流程1中获取;只是一个独立的流程)

  • read the memory mapped by process 1 读取进程1映射的内存
  • change some bits 改变一些比特
  • write it to a new file 将其写入新文件

I've read several examples and documentations, but I still didn't find how to achieve this. 我已经阅读了几个示例和文档,但我仍然没有找到如何实现这一点。 What I'm missing is: 我缺少的是:

  • how can process 2 access the memory mapped by process 1, without knowing anything about the opened file? 进程2如何在不知道打开文件的情况下访问进程1映射的内存?
  • how can I put the mmap content in a new file? 如何将mmap内容放在新文件中? I suppose I have to ftruncate a new file, mmap this file and memcpy the content of process 1 memory map to process 2 memory map (then msync) 我想我必须ftruncate一个新文件,mmap这个文件并memcpy进程1内存映射的内容到进程2内存映射(然后是msync)

Side info, I have a message queue opened between the two processes, so they can share some messages if needed (ex. the memory address/size, ...). 侧面信息,我在两个进程之间打开了一个消息队列,因此如果需要,它们可以共享一些消息(例如内存地址/大小,......)。

Any hints? 任何提示?

Thanks in advance! 提前致谢!

MIX 混合

This answer considers you are trying to do this stuff on linux/unix. 这个答案认为你正试图在linux / unix上做这个东西。

how can process 2 access the memory mapped by process 1, without knowing anything about the opened file? 进程2如何在不知道打开文件的情况下访问进程1映射的内存?

Process 1 passes to mmap[1] the flag MAP_SHARED. 进程1将标志MAP_SHARED传递给mmap [1]。

You can: 您可以:

  • A) Share the file descriptor using unix domain sockets[2]. A)使用unix域套接字共享文件描述符[2]。
  • B) Send the name of the file using the queues you mentioned at the end of your message. B)使用您在邮件末尾提到的队列发送文件的名称。

Process 2 opens mmap with the flag MAP_SHARED. 进程2使用标志MAP_SHARED打开mmap。 Modifications to the mmaped memory in Process 1 will be visible for Process 2. If you need fine control of when the changes from process 1 are shown to process 2 you should control it with msync[3] 进程2中将显示对进程1中的mmaped内存的修改。如果需要对进程1的更改何时显示为进程2进行精确控制,则应使用msync控制它[3]

how can I put the mmap content in a new file? 如何将mmap内容放在新文件中? I suppose I have to ftruncate a new file, mmap this file and memcpy the content of process 1 memory map to process 2 memory map (then msync) 我想我必须ftruncate一个新文件,mmap这个文件并memcpy进程1内存映射的内容到进程2内存映射(然后是msync)

Why just don't write the mmaped memory as regular memory with write? 为什么只是不将写入的mmaped内存写为常规内存?

[1] http://man7.org/linux/man-pages/man2/mmap.2.html [1] http://man7.org/linux/man-pages/man2/mmap.2.html

[2] Portable way to pass file descriptor between different processes [2] 在不同进程之间传递文件描述符的便携方式

[3] http://man7.org/linux/man-pages/man2/msync.2.html [3] http://man7.org/linux/man-pages/man2/msync.2.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM