When to call sem_unlink()?

假如想象 提交于 2019-12-04 23:55:29

Response to your questions:

  1. In comparison to the semaphore behavior for windows you describe, POSIX semaphores are Kernel persistent. Meaning that the semaphore retains it's value even if no process has the semaphore opened. (the semaphore's reference count would be 0)

  2. If process A calls sem_unlink() while process B has the semaphore locked. This means the semaphore's reference count is not 0 and will not be destructed.

Basic operation of sem_close vs sem_unlink, I think will help overall understanding:

sem_close: close's a semaphore, this also done when a process exits. the semaphore still remains in the system.

sem_unlink: will be removed from the system only when the reference count reaches 0 (that is after all processes that have it open, call sem_close or are exited).

References: Book - Unix Networking Programming-Interprocess Communication by W.Richard Stevens, vol 2, ch10

Abhishek

The sem_unlink() function removes the semaphore identified by name and marks the semaphore to be destroyed once all processes cease using it (this may mean immediately, if all processes that had the semaphore open have already closed it).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!