sem_getvalue()--Get Semaphore Value


  Syntax
 #include <semaphore.h>

 int sem_getvalue(sem_t * sem, int * value); 

  Service Program Name: QP0ZPSEM

  Default Public Authority: *USE

  Threadsafe: Yes

The sem_getvalue() function retrieves the value of a named or unnamed semaphore. If the current value of the semaphore is zero and there are threads waiting on the semaphore, a negative value is returned. The absolute value of this negative value is the number of threads waiting on the semaphore.

Parameters

sem
(Input) A pointer to an initialized unnamed semaphore or an opened named semaphore.

value
(Output) A pointer to the integer that contains the value of the semaphore.

Authorities

None


Return Value

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


Error Conditions

If sem_getvalue() 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.


Error Messages

None.


Related Information


Example

See Code disclaimer information for information pertaining to code examples.

The following example retrieves the value of a semaphore before and after it is decremented by sem_wait().

#include <stdio.h>
#include <semaphore.h>
main() {
  sem_t my_semaphore;
  int value;

  sem_init(&my_semaphore, 0, 10);
  sem_getvalue(&my_semaphore, &value);
  printf("The initial value of the semaphore is %d\n", value);
  sem_wait(&my_semaphore);
  sem_getvalue(&my_semaphore, &value);
  printf("The value of the semaphore after the wait is %d\n", value);

}

Output:

The initial value of the semaphore is 10
The value of the semaphore after the wait is 9


API introduced: V4R4
Top | UNIX-Type APIs | APIs by category