sem_close()--Close Named Semaphore


  Syntax
 #include <semaphore.h>

 int sem_close(sem_t * sem); 

  Service Program Name: QP0ZPSEM 

  Default Public Authority: *USE

  Threadsafe: Yes

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.


Parameters

sem
(Input) A pointer to an opened named semaphore. This semaphore is closed for this process.

Authorities

No authorization is required. Authorization is verified during sem_open().


Return Value

0 sem_close() was successful.
-1 sem_close() was not successful. The errno variable is set to indicate the error.


Error Conditions

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.

[EINVAL]

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.


Error Messages

None.


Related Information


Example

The following example opens a named semaphore with an initial value of 10 and then closes it.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

#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);

}


API introduced: V4R4

[ Back to top | UNIX-Type APIs | APIs by category ]