我在named semaphore
中的C
中使用Linux
来控制跨多个进程的共享内存访问。
截至目前,我还没有向信号量sem_close
和sem_unlink
添加任何代码。
所以我的问题是:
命名信号量在使用它的所有进程完成后会自动被销毁吗?
如果是,那么是否可以不致电sem_close
和sem_unlink
?
答案 0 :(得分:4)
http://linux.die.net/man/7/sem_overview
“POSIX命名信号量具有内核持久性:如果未被sem_unlink(3)删除,则在系统关闭之前将存在信号量。”
答案 1 :(得分:1)
从手册页http://pubs.opengroup.org/onlinepubs/7908799/xsh/sem_close.html
sem_close()函数用于指示使用sem指示的命名信号量完成调用过程。调用sem_close()对未命名信号量(由sem_init()创建的信号量)的效果是未定义的。 sem_close()函数解除分配(即,由此过程使后续sem_open()可以重用)系统分配的任何系统资源,供此信号的此进程使用。由sem指示的后续使用信号量的效果是不确定的。如果在成功调用sem_unlink()时尚未删除信号量,则sem_close()对信号量的状态没有影响。
If the sem_unlink() function has been successfully invoked for name after the most recent call to sem_open() with O_CREAT for this semaphore, then when all processes that have opened the semaphore close it, the semaphore is no longer be accessible.
所以从本质上讲,当打开信号量的所有进程成功调用sem_unlink和sem_close时,信号量就会被破坏。