Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, Am writing a program using a named semaphore. Just wanted to know the significance of sem_unlink() function. Isn't sem_close() enough to clean up the resources allocated? What will happen ...
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    13

    Question Question on Named semaphore

    Hi,

    Am writing a program using a named semaphore.

    Just wanted to know the significance of sem_unlink() function.

    Isn't sem_close() enough to clean up the resources allocated?

    What will happen if sem_unlink() is not used at all in my program?

    Thanks and Regards,
    krishna

  2. #2
    Just Joined!
    Join Date
    Nov 2006
    Location
    Harrisburg, PA, USA
    Posts
    56
    When you use sem_close() it closes the semaphore in the current process. The semaphore still exists and other processes can still use it.

    When you use sem_unlink(), it will close and destroy the semaphore after all other processes close this semaphore. I would suggest to use sem_close() and then sem_unlink() function to be used in the source code.

    Use 'ipcs' linux command to see the difference.

    Regards,
    Sumit

  3. #3
    Just Joined!
    Join Date
    Oct 2008
    Posts
    13

    Question

    Lets say if I have only one process and many threads. Even then, sem_unlink() is needed?

  4. #4
    Just Joined!
    Join Date
    Nov 2006
    Location
    Harrisburg, PA, USA
    Posts
    56
    It depends on your program requirements. If your program is OK in creating named semaphore for the first time and use it again for next run cycle then no need to use sem_unlink() function. In this case sem_open() will return an error code EEXIST and your program should be intelligent enough to handle this error.

    Regards,
    Sumit

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...