DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

sem_init(3pthread)


sem_init -- initialize an unnamed semaphore

Synopsis

   cc [options] -Kthread file
   

#include <semaphore.h>

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

Description

sem_init initializes the unnamed semaphore sem. Following a successful call to sem_init, the semaphore may be used in subsequent calls to sem_wait, sem_trywait, sem_post, and sem_destroy. This semaphore remains usable until the semaphore is destroyed.

sem is a pointer to an unnamed semaphore to be initialized.

If pshared has a non-zero value, then the semaphore is shared between processes; in this case, any process that can access the semaphore sem can use sem for performing sem_wait, sem_trywait, sem_post, and sem_destroy operations.

If the pshared argument is zero, then the semaphore is shared between threads of the process; any thread in this process can use sem for performing sem_wait, sem_trywait, sem_post, and sem_destroy operations.

value specifies the value of the initialized semaphore value must be greater than or equal to zero.

Return values

sem_init returns zero for success. Otherwise it returns -1 and sets errno to indicate the error.

Diagnostics

sem_init returns the following values if the corresponding condition is detected:

EINVAL
value exceeds SEM_VALUE_MAX or is less than zero.

Warnings

Attempting to initialize an already initialized semaphore results in undefined behavior.

sem_init does not examine the sem argument before initializing it. If sem_init is called more than once for the same semaphore, it will overwrite its state. It is the user's responsibility to ensure that sem_init is only called once for each semaphore.

Standards compliance

The Single UNIX Specification, Version 2; The Open Group.

References

Intro(3pthread), sem_destroy(3pthread), sem_post(3pthread), sem_trywait(3pthread), sem_wait(3pthread), semaphore(4)
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 25 April 2004