我在Linux的C中使用named semaphore来控制跨多个进程的共享内存的访问。到目前为止,我还没有向sem_close和sem_unlink信号量添加任何代码。所以我的问题是:
当所有使用它的进程结束时,命名信号量会自动销毁吗?
如果是,那么完全不调用sem_close和sem_unlink是可以的吗?
发布于 2013-06-12 18:50:57
http://linux.die.net/man/7/sem_overview
"POSIX命名信号量具有内核持久性:如果没有被sem_unlink(3)删除,信号量将一直存在,直到系统关闭。“
发布于 2013-06-12 18:51:18
从手册页http://pubs.opengroup.org/onlinepubs/7908799/xsh/sem_close.html
sem_close()函数用于指示调用进程已使用由sem指示的已命名信号量完成。为未命名信号量(由sem_init()创建的信号量)调用sem_close()的效果未定义。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时,信号量就被销毁了。
https://stackoverflow.com/questions/17063479
复制相似问题