使用 C 在 Linux 中共享内存

Share Memory in Linux using C

即使当前有其他进程打开共享内存,使用shm_unlink是否安全?

例如:

进程 B shm_open,然后是进程 A shm_unlink。流程 B 可以吗?

是的,如果另一个进程已打开,调用 shm_unlink 是安全的。根据 shm_unlink 的手册页:

The operation of shm_unlink() is analogous to unlink(2): it removes a shared 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 will fail (unless O_CREAT was specified, in which case a new, distinct object is created).