When to call sem_unlink()? When to call sem_unlink()? linux linux

When to call sem_unlink()?


Response to your questions:

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

  2. If process A calls sem_unlink() while process B has the semaphorelocked. This means the semaphore's reference count is not 0 and willnot 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


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