sem_init()

initialize an unnamed semaphore 

Realtime Function


SYNOPSIS

#include <semaphore.h>

int sem_init(sem_t *sem, int pshared, unsigned int value);


DESCRIPTION

The sem_init() function initializes the unnamed semaphore specified by the sem argument and assigns that semaphore the value specified by the value argument. Once the semaphore has been successfully initializes, you can use it in subsequent calls to sem_wait(), sem_trywait(), sem_post(), and sem_destroy(). You can continue to use the semaphore until it is destroyed with the sem_destroy() function.

If you attempt to initialize a semaphore that has already been initialized, the original semaphore is overwritten by the newly initialized one.

You can only use sem itself to perform synchronization. Referring to copies of sem in calls to the sem_wait(), sem_trywait(), sem_post(), and sem_destroy() functions is not recommended. While the current implementation of the semaphore functions supports the use of copies, there is no guarantee that future implementations will have this support.

Semaphores created with sem_init() should only be destroyed with the sem_destroy() function and semaphores created with sem_open() should only be closed with the sem_close() function.


PARAMETERS

sem 

Specifies the semaphore to be initialized.

pshared 

Is ignored. In other implementations, this argument specifies whether or not the newly initialized semaphore is shared between processes or between threads. A non-zero value means the semaphore is shared between processes; a value of zero means it is shared between threads. On the NuTCRACKER Platform, semaphores are always shared.

value 

Specifies the value to assign to the newly initialized semaphore.


RETURN VALUES

When successful, the sem_init() function initializes the specified semaphore . Otherwise, it returns -1 and sets errno to one of the following values:

EACCES 

The process lacks the appropriate privileges to initialize the semaphore.

EEXIST 

The specified semaphore already exists.

EINVAL 

The value argument exceeds SEM_VALUE_MAX.

ENFILE 

Too many semaphores are currently open in the system.

ENOSPC 

A resource required to initialize the semaphore has been exhausted, or the limit on semaphores (SEM_NSEMS_MAX) has been reached.


EXAMPLES

None.


CONFORMANCE

UNIX 03. Derived from the POSIX Realtime Extension (1003.1b-1993/1003.1i-1995)


MULTITHREAD SAFETY LEVEL

MT-Safe.


PORTING ISSUES

None.


AVAILABILITY

PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Functions:
sem_destroy(), sem_post(), sem_trywait(), sem_wait()

Miscellaneous:
semaphores


PTC MKS Toolkit 10.3 Documentation Build 39.