shm_unlink 在 linux

shm_unlink in linux

来自 shm_unlink 的手册页:

 The operation of shm_unlink() is analogous to unlink(2): it removes a
 memory object name, and, once all processes have unmapped the object,
 de-allocates and destroys the contents of the associated memory
 region.  After a successful shm_unlink(), attempts to shm_open()
 an object with the same name fail (unless O_CREAT was specified,
 in which case a new, distinct object is created).

这个的工作由句子指定

it removes a memory object name, and, once all processes have unmapped the object, de-allocates and destroys the contents of the associated region

那么,这是否意味着当 shm_unlink 被调用时,将自动对所有进程取消映射,然后销毁将发生,或者它是否仍然存在,一旦所有进程使用它space 取消了这个 space 的映射(根据他们将来的方便),它会被销毁吗?

上面的描述是man pages描述的有点不够详细,这可以用上面两种方式解释,所以我怀疑。

罪魁祸首在这里:

[...] once all processes have unmapped the object, de-allocates and destroys the contents of the associated region.

关键字未映射

调用shm_unlink()只会影响调用进程,不会影响其他已经映射区域的进程。如果其他进程已经将该共享内存区域映射到它们的地址 space,它对它们仍然有效。这与处理文件的方式相同。如果一个进程打开(映射)一个文件,第二个进程通过 unlink 删除它,第一个进程仍然能够从映射的内存页面读取文件内容。

如果区域与多个进程共享,内核将等待 所有 个进程 unmap 区域和 关闭shm_open()获得的文件描述符,只有这样它才会de-allocate并销毁相关内存区域的内容。即使调用 shm_unlink(),内核也必须在每个进程关闭其描述符或终止之前等待(此时描述符会由内核本身自动关闭)。