Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to call sem_unlink()?

I'm a little confused by the Linux API sem_unlink(), mainly when or why to call it. I've used semaphores in Windows for many years. In Windows once you close the last handle of a named semaphore the system removes the underlying kernel object. But it appears in Linux you, the developer, needs to remove the kernel object by calling sem_unlink(). If you don't the kernel object persists in the /dev/shm folder.

The problem I'm running into, if process A calls sem_unlink() while process B has the semaphore locked, it immediately destroys the semaphore and now process B is no longer "protected" by the semaphore when/if process C comes along. What's more, the man page is confusing at best:

"The semaphore name is removed immediately. The semaphore is destroyed once all other processes that have the semaphore open close it."

How can it destroy the object immediately if it has to wait for other processes to close the semaphore?

Clearly I don't understand the proper use of semaphore objects on Linux. Thanks for any help. Below is some sample code I'm using to test this.

int main(void)
{
    sem_t *pSemaphore = sem_open("/MyName", O_CREAT, S_IRUSR | S_IWUSR, 1);
    if(pSemaphore != SEM_FAILED)
    {
        if(sem_wait(pSemaphore) == 0)
        {
            // Perform "protected" operations here

            sem_post(pSemaphore);
        }

        sem_close(pSemaphore);
        sem_unlink("/MyName");
    }

    return 0;
}
like image 334
user2124642 Avatar asked Mar 01 '13 18:03

user2124642


People also ask

What does sem_ unlink do?

DESCRIPTION. The sem_unlink() function shall remove the semaphore named by the string name. If the semaphore named by name is currently referenced by other processes, then sem_unlink() shall have no effect on the state of the semaphore.

What is the difference between named and unnamed semaphores?

Unnamed semaphores are either private, inherited through fork() , or are protected by access protections of the regular file in which they are allocated and mapped. Named semaphores are like process-shared semaphores, except that named semaphores are referenced with a pathname rather than a pshared value.

How do you unlink a named semaphore?

The sem_unlink() function unlinks a named semaphore. The name of the semaphore is removed from the set of names used by named semaphores. If the semaphore is still in use, the semaphore is not deleted until all processes using the semaphore have ended or have called sem_close().


2 Answers

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

like image 91
rg_sw Avatar answered Sep 20 '22 17:09

rg_sw


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).

like image 31
Abhishek Avatar answered Sep 22 '22 17:09

Abhishek