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 ...
- 11-29-2010 #1Just Joined!
- Join Date
- Oct 2008
- Posts
- 13
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
- 11-29-2010 #2Just 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
- 11-29-2010 #3Just Joined!
- Join Date
- Oct 2008
- Posts
- 13
Lets say if I have only one process and many threads. Even then, sem_unlink() is needed?
- 11-29-2010 #4Just 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


Reply With Quote