gpt4 book ai didi

c - 将已经分配的内存映射到共享内存

转载 作者:太空狗 更新时间:2023-10-29 11:13:03 26 4
gpt4 key购买 nike

如果已经分配了一些内存(例如使用 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 内存。
  • 在另一个进程中打开同一个文件。
  • 享受共享内存。

关于c - 将已经分配的内存映射到共享内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33741155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com