Linux中的shm_unlink

问题描述

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,de-allocates and destroys the contents of the associated region

所以,这是否意味着当调用shm_unlink时,将自动对所有进程进行取消映射,然后销毁将发生,或者销毁销毁销毁销毁销毁销毁销毁销毁销毁销毁销毁销毁销毁销路图,该销毁销毁销毁销毁销毁销路后取消映射该空间(根据他们将来的便利)是否会被销毁?

上面的描述是手册页有点描述性的,可以通过以上两种方式来解释,因此我对此表示怀疑。

解决方法

罪魁祸首在这里

[...] 所有进程都取消映射对象,取消分配并销毁关联区域的内容。

关键字未映射

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

如果该区域与多个进程共享,则内核将等待它们的所有全部 取消映射该区域并 close shm_open()获得,只有这样它才会取消分配并销毁关联内存区域的内容。即使调用shm_unlink(),内核也必须在每个进程关闭其描述符或终止之前等待(此时描述符由内核本身自动关闭)。