sem_init_np()--Initialize Unnamed Semaphore with Maximum Value


  Syntax
 #include <semaphore.h>

 int sem_init_np(sem_t * sem, int shared, 
                unsigned int value,
                sem_attr_np_t * attr);

  Service Program Name: QP0ZPSEM

  Default Public Authority: *USE

  Threadsafe: Yes

The sem_init_np() function initializes an unnamed semaphore and sets its initial value. The sem_init_np() function uses the attr parameter to set the maximum value and title of the semaphore. If an unnamed semaphore already exists at sem, then it will be destroyed and a new semaphore will be initialized.


Parameters

sem
(Input) A pointer to the storage of an uninitialized unnamed semaphore. The pointer must be aligned on a 16-byte boundary. This semaphore is initialized.

shared
(Input) An indication to the system of how the semaphore is going to be used. A value of zero indicates that the semaphore will be used only by threads within the current process. A nonzero value indicates that the semaphore may be used by threads from other processes.

value
(Input) The value used to initialize the value of the semaphore.

attr
(Input) Attributes for the semaphore.

The members of the sem_attr_np_t structure are as follows.

unsigned int reserved1[1] A reserved field that must be set to zero.
unsigned int maxvalue The maximum value that the semaphore may obtain. maxvalue must be greater than zero. If a sem_post() or sem_post_np() operation would cause the value of a semaphore to exceed its maximum value, the operation will fail, returning EINVAL.
unsigned int reserved2[2] A reserved field that must be set to zero.
char title[16] The title of the semaphore. The title is a null-terminated string that contains up to 16 bytes. Any bytes after the null character are ignored. The title is retrieved using the Open List of Interprocess Communication Objects (QP0ZOLIP) API.
void * reserved3[2] A reserved field that must be set to zero.

Authorities

None.


Return Value

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


Error Conditions

If sem_init_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.

The value parameter is greater than the maxvalue field of the attr parameter.

The maxvalue field of the attr parameter is greater than SEM_VALUE_MAX.

The maxvalue field of the attr parameter is equal to zero.

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

[EFAULT]

The address used for an argument is not correct.

In attempting to use an argument in a call, the system detected an address that is not valid.

While attempting to access a parameter passed to this function, the system detected an address that is not valid.

[ENOSPC]

No space available.

The requested operations required additional space on the device and there is no space left. This could also be caused by exceeding the user profile storage limit when creating or transferring ownership of an object.

Insufficient space remains to hold the intended file, directory, or link.

System semaphore resources have been exhausted.


Error Messages

None.


Related Information


Example

The following example initializes an unnamed semaphore, my_semaphore, that will be used by threads of the current process and sets its value to 10. The maximum value and title of the semaphore are set to 10 and "MYSEM".

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;
  sem_attr_np_t attr;
  int rc;

  memset(&attr, 0, sizeof(attr));
  attr.maxvalue = 10;
  strcpy(attr.title, "MYSEM");
  rc = sem_init_np(&my_semaphore, 0, 10, &attr);

}


API introduced: V4R4

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