从另一个进程访问mmap内存 [英] Access mmap memory from another process

查看:94
本文介绍了从另一个进程访问mmap内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始玩mmap.我正在尝试创建一个示例工作空间,然后将其扩展到实际情况.

I've started playing with mmap. I'm trying to create an example workspace that will be then extended to the real case.

这是我想要实现的:

过程1:

  • 映射文件(实际上是设备,但是可以生成带有文本文件的示例)

过程2 :(不属于过程1;只是一个独立的过程)

PROCESS 2: (not foked from process 1; just an independent process)

  • 读取进程1映射的内存
  • 更改一些位
  • 将其写入新文件

我已经阅读了一些示例和文档,但是我仍然没有找到实现该目标的方法.我所缺少的是:

I've read several examples and documentations, but I still didn't find how to achieve this. What I'm missing is:

  • 进程2如何在不了解打开文件的情况下访问进程1映射的内存?
  • 如何将mmap内容放入新文件中?我想我必须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, ...).

有任何提示吗?

提前谢谢!

MIX

推荐答案

此答案认为您正在尝试在linux/unix上执行此操作.

This answer considers you are trying to do this stuff on linux/unix.

进程2如何在不了解有关打开的文件的情况下访问进程1映射的内存?

how can process 2 access the memory mapped by process 1, without knowing anything about the opened file?

进程1将标志MAP_SHARED传递给mmap [1].

Process 1 passes to mmap[1] the flag MAP_SHARED.

您可以:

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

进程2使用标志MAP_SHARED打开mmap.对于进程2,将看到对进程1中的mmaped内存的修改.如果需要精细控制何时显示从进程1到进程2的更改,则应使用msync [3]

Process 2 opens mmap with the flag MAP_SHARED. 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]

如何将mmap内容放入新文件中?我想我必须 ftruncate一个新文件,mmap这个文件并memcpy进程的内容 1个内存映射来处理2个内存映射(然后是msync)

how can I put the mmap content in a new file? 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)

为什么不通过写操作将mmaped内存写为常规内存?

Why just don't write the mmaped memory as regular memory with write?

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

[2] 在不同对象之间传递文件描述符的便携式方法进程

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

这篇关于从另一个进程访问mmap内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆