#include <semaphore.h> int sem_close(sem_t * sem);
The sem_close() function closes a named semaphore that was previously opened by a thread of the current process using sem_open() or sem_open_np(). The sem_close() function frees system resources associated with the semaphore on behalf of the process. Using a semaphore after it has been closed will result in an error. A semaphore should be closed when it is no longer used. If a sem_unlink() was performed previously for the semaphore and the current process holds the last reference to the semaphore, then the named semaphore will be deleted and removed from the system.
No authorization is required. Authorization is verified during sem_open().
0 | sem_close() was successful. |
-1 | sem_close() was not successful. The errno variable is set to indicate the error. |
If sem_close() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.
The value specified for the argument is not correct.
A function was passed incorrect argument values, or an operation was attempted on an object and the operation specified is not supported for that type of object.
An argument value is not valid, out of range, or NULL.
The sem parameter is not a valid semaphore.
None.
See Code disclaimer information for information pertaining to code examples.
The following example opens a named semaphore with an initial value of 10 and then closes it.
#include <semaphore.h> main() { sem_t * my_semaphore; int rc; my_semaphore = sem_open("/mysemaphore", O_CREAT, S_IRUSR | S_IWUSR, 10); sem_close(my_semaphore); }
Top | UNIX-Type APIs | APIs by category |