#include <semaphore.h> int sem_init(sem_t * sem, int shared, unsigned int value);
The sem_init() function initializes an unnamed semaphore and sets its initial value. The maximum value of the semaphore is set to SEM_VALUE_MAX. The title for the semaphore is set to the character representation of the address of the semaphore. If an unnamed semaphore already exists at sem, then it will be destroyed and a new semaphore will be initialized.
None
0 | sem_init() was successful. |
-1 | sem_init() was not successful. The errno variable is set to indicate the error. |
If sem_init() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.
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 SEM_VALUE_MAX.
No space available.
System semaphore resources have been exhausted.
None.
See Code disclaimer information for information pertaining to code examples.
The following example initializes an unnamed semaphore, my_semaphore, that will be used by threads of the current process. Its value is set to 10.
#include <semaphore.h> main() { sem_t my_semaphore; int rc; rc = sem_init(&my_semaphore, 0, 10); }
Top | UNIX-Type APIs | APIs by category |