简体   繁体   English

如何使用 mmap 和 shm_open 在多个独立进程之间共享内存

[英]how to use mmap and shm_open to shared memory among multiple independent processes

I want to share memory among processes, which run independently instead of fork .我想在独立运行而不是fork进程之间共享内存。

I've read the man page for mmap and shm_open , and still confused about the usage.我已经阅读了mmapshm_open的手册页,但仍然对用法感到困惑。

  1. shared memory, in my mind, should be the mechanism of mapping virtual memory space among different processes, but Why mmap has the fd argument?共享内存,在我看来,应该是不同进程之间映射虚拟内存空间的机制,但是为什么mmapfd参数呢? Does it mean the memory is actually shared through file?这是否意味着内存实际上是通过文件共享的?
  2. Also, the shm_open seems to accept a filename as its argument, so it actually opens a file?此外, shm_open似乎接受一个文件名作为它的参数,所以它实际上打开了一个文件?
  3. Since I have multiple independent processes, how should one inform other processes the physical memory address to share?既然我有多个独立的进程,应该如何通知其他进程共享的物理内存地址?

Could anyone give some example code for sharing memory among two process?谁能给出一些在两个进程之间共享内存的示例代码? Say, we have process producer and a consumer process, how would they communicate through the memory segment shared through mmap ?比如说,我们有进程producerconsumer进程,他们如何通过mmap共享的内存段进行通信?

  1. Essentially, yes - on unix "everything is a file".本质上,是的 - 在 unix 上“一切都是文件”。 Well not quite, and it need not be an on-disk file, but in fact you can use an on-disk file if you want.不完全是,它不必是磁盘文件,但实际上,如果需要,您可以使用磁盘文件。

  2. Formally shm_open accepts a name in its own namespace of named shared memory objects.正式shm_open接受在其自己的命名共享内存对象命名空间中的名称。 They need to start with a single slash and should not contain another slash.它们需要以一个斜杠开头,并且不应包含另一个斜杠。 You can name them however you want, but since it's a global namespace, it's preferable to generate random names (properly using O_CREAT|O_EXCL to check for collisions) and communicate the name via some other channel (like a state directory agreed upon in the configuration file used by the instance of your application) so that you don't make it a system-wide singleton.您可以随意命名它们,但由于它是一个全局命名空间,因此最好生成随机名称(正确使用O_CREAT|O_EXCL来检查冲突)并通过其他渠道(如配置中商定的状态目录)传达名称应用程序实例使用的文件),这样您就不会使其成为系统范围的单例。 In reality, on Linux and perhaps other common systems, the shared memory namespace is just a directory /dev/shm that's normally mounted as a non-persistent (in-memory) filesystem type to avoid wasteful writes to a disk.实际上,在 Linux 或其他常见系统上,共享内存命名空间只是一个目录/dev/shm ,它通常作为非持久(内存中)文件系统类型挂载,以避免浪费地写入磁盘。

  3. There is no physical memory address;没有物理内存地址; it can vary and at times might not exist at all (if swapped out or when a page is clean and untouched).它可能会有所不同,有时可能根本不存在(如果换出或当页面干净且未触及时)。 Nor are the virtual addresses of the mapped shared memory object common to different processes mapping it.映射的共享内存对象的虚拟地址对于映射它的不同进程也不是通用的。 What identifies the memory is the shared memory object file (referenced by an open file descriptor to it) an offset within it.识别内存的是共享内存对象文件(由打开的文件描述符引用)其中的偏移量。 So in order to use shared memory that's independently mapped in different processes' address spaces, you need to work with offsets from the base, not absolute pointers, adding the offset to the base whenever you need a pointer (eg to pass to synchronization primitives for mutexes, condition variables, semaphores, etc. in the shared memory, or for your own use).因此,为了使用独立映射在不同进程地址空间中的共享内存,您需要使用基址的偏移量,而不是绝对指针,只要需要指针就将偏移量添加到基址(例如传递给同步原语)共享内存中的互斥体、条件变量、信号量等,或供您自己使用)。

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

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