sem_init()--Initialize Unnamed Semaphore
Syntax
#include <semaphore.h> int sem_init(sem_t * sem, int shared, unsigned int value);
Service Program Name: QP0ZPSEM
Default Public Authority: *USE
Threadsafe: Yes
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.
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.
Authorities
None.
Return Value
0 | sem_init() was successful. |
-1 | sem_init() was not successful. The errno variable is set to indicate the error. |
Error Conditions
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.
- [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 SEM_VALUE_MAX.
- [ENOSPC]
-
No space available.
System semaphore resources have been exhausted.
Error Messages
None.
Related Information
- The <semaphore.h> file (see )
- sem_destroy()--Destroy Unnamed Semaphore
- sem_getvalue()--Get Semaphore Value
- sem_init_np()--Initialize Unnamed Semaphore with
Maximum Value
- sem_post()--Post to Semaphore
- sem_post_np()--Post Value to Semaphore
- sem_trywait()--Try to Decrement Semaphore
- sem_wait()--Wait for Semaphore
- sem_wait_np()--Wait for Semaphore with Timeout
Example
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.
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; int rc; rc = sem_init(&my_semaphore, 0, 10); }
API introduced: V4R4