将已分配的内存映射到共享内存中

如果已经分配了一些内存(例如使用malloc ),是否有可能将该内存与另一个进程共享,例如将页面标记为共享?

要清楚的是,这与将内存初始分配为共享内存不同,例如使用shmget等。 显然这是可以做到这一点与memcpy ,但有没有办法直接做到这一点?

 mmap() creates a new mapping in the virtual address space of the calling process. The starting address for the new mapping is specified in addr. The length argument specifies the length of the mapping. 

所以我想象:

  • 在捐赠过程中打开一个文件进行编写。
  • mmap()你现有的malloc'd内存。
  • 在另一个进程中打开同一文件。
  • 享受共享内存。