sem_post()--Post to Semaphore


  Syntax
 #include <semaphore.h>

 int sem_post(sem_t * sem); 

  Service Program Name: QP0ZPSEM 

  Default Public Authority: *USE

  Threadsafe: Yes

The sem_post() function posts to a semaphore, incrementing its value by one. If the resulting value is greater than zero and if there is a thread waiting on the semaphore, the waiting thread decrements the semaphore value by one and continues running.


Parameters

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

Authorities

None


Return Value

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

Error Conditions

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

Posting to the semaphore would cause its value to exceed its maximum value. The maximum value is SEM_VALUE_MAX or was set using sem_open_np() or sem_init_np().


Error Messages

None.


Related Information


Example

See Code disclaimer information for information pertaining to code examples.

The following example initializes an unnamed semaphore and posts to it, incrementing its value by 1.

#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_post(&my_semaphore);
  sem_getvalue(&my_semaphore, &value);
  printf("The value of the semaphore after the post is %d\n", value);

}

Output:

The initial value of the semaphore is 10
The value of the semaphore after the post is 11


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