sem_init

initialize a semaphore

Synopsis:

#include <semaphore.h>
int sem_init(sem_t *sem, 
             int pshared, 
             unsigned value);

Description:

The sem_init() function initializes the unnamed semaphore referred to by the sem argument. The initial counter value of this semaphore is specified by the value argument.

The initialized semaphore can be used in subsequent calls to sem_wait(), sem_trywait(), sem_post() and sem_destroy(). An initialized semaphore is valid until it is destroyed by the sem_destroy() function, or until the memory where the semaphore resides is released.

If the pshared argument is non-zero, then the semaphore can be shared between processes via shared memory. Any process can then use sem with the sem_wait(), sem_trywait(), sem_post() and sem_destory() functions.

Returns:

If successful, the sem_init() function initializes the semaphore referred to by sem. Otherwise, it returns -1 and errno is set to indicate the error.

Errors:

EINVAL
The value argument exceeds SEM_VALUE_MAX.
ENOSPC
A resource required to initialize the semaphore has been exhausted.

The limit on semaphores (-e num-semaphores argument to Proc32) has been reached.

ENOSYS
The sem_init() function is not supported.
EPERM
The process lacks the appropriate privileges to initialize the semaphore.

See also:

errno, sem_destroy(), sem_post(), sem_trywait(), sem_wait()

Examples:

See /usr/demo/source/semaphores.

Classification:

POSIX 1003.1b-1993