sem_post_np()--Post Value to Semaphore


  Syntax
 #include <semaphore.h>

 int sem_post_np(sem_t * sem,
                sem_post_options_np_t *options); 

  Service Program Name: QP0ZPSEM

  Default Public Authority: *USE

  Threadsafe: Yes

The sem_post_np() function posts to a semaphore, incrementing its value by the increment specified in the options parameter. If the resulting value is greater than zero and if there are threads waiting on the semaphore, the waiting threads decrement the semaphore and continue running.


Parameters

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

options
(Input) Post options.

The members of the sem_post_options_np_t structure are as follows.

unsigned int reserved1[1] A reserved field that must be set to zero.
unsigned int increment The value, greater than zero, used to increment the semaphore. If the value specified causes the value of a semaphore to exceed its maximum value, sem_post_np() will fail by returning [EINVAL].
unsigned int reserved2[2] A reserved field that must be set to zero.

Authorities

None.


Return Value

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


Error Conditions

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

[EOVERFLOW]

Maximum value exceeded.

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

The reserved fields of the attr argument are not set to zero.


Error Messages

None.


Related Information


Example

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

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

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

  sem_init(&my_semaphore, 0, 10);
  sem_getvalue(&my_semaphore, &value);
  printf("The initial value of the semaphore is %d.\n", value);
  memset(&options, 0, sizeof(options));
  options.increment=2;
  sem_post_np(&my_semaphore,&options);
  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 12.


API introduced: V4R4

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