Gets a set of semaphores.
Standard C Library (libc.a)
#include <sys/sem.h>
int semget (Key, NumberOfSemaphores, SemaphoreFlag)
key_t Key;
int NumberOfSemaphores, SemaphoreFlag;
The semget subroutine returns the semaphore identifier associated with the Key parameter value.
The semget subroutine creates a data structure for the semaphore ID and an array containing the NumberOfSemaphores parameter semaphores if one of the following conditions is true:
Upon creation, the data structure associated with the new semaphore identifier is initialized as follows:
The data structure associated with each semaphore in the set is not initialized. The semctl (semctl Subroutine) subroutine (with the Command parameter values SETVAL or SETALL) can be used to initialize each semaphore.
If the Key parameter value is not IPC_PRIVATE, the IPC_EXCL value is not set, and a semaphore identifier already exists for the specified Key parameter, the value of the NumberOfSemaphores parameter specifies the number of semaphores that the current process needs.
If the NumberOfSemaphores parameter has a value of 0, any number of semaphores is acceptable. If the NumberOfSemaphores parameter is not 0, the semget subroutine is unsuccessful if the set contains fewer than the value of the NumberOfSemaphores parameter.
The following limits apply to semaphores:
Item | Description |
---|---|
Key | Specifies either the IPC_PRIVATE value or an IPC key constructed by the ftok subroutine (or a similar algorithm). |
NumberOfSemaphores | Specifies the number of semaphores in the set. |
SemaphoreFlag |
Constructed by logically ORing one or more of the following
values:
Values that begin with the S_I prefix are defined in the sys/mode.h file and are a subset of the access permissions that apply to files. |
Upon successful completion, the semget subroutine returns a semaphore identifier. Otherwise, a value of -1 is returned and the errno global variable is set to indicate the error.
The semget subroutine is unsuccessful if one or more of the following conditions is true:
Item | Description |
---|---|
EACCES | A semaphore identifier exists for the Key parameter but operation permission, as specified by the low-order 9 bits of the SemaphoreFlag parameter, is not granted. |
EINVAL | A semaphore identifier does not exist and the NumberOfSemaphores parameter is less than or equal to a value of 0, or greater than the system-imposed value. |
EINVAL | A semaphore identifier exists for the Key parameter, but the number of semaphores in the set associated with it is less than the value of the NumberOfSemaphores parameter and the NumberOfSemaphores parameter is not equal to 0. |
ENOENT | A semaphore identifier does not exist for the Key parameter and the IPC_CREAT value is not set. |
ENOSPC | Creating a semaphore identifier would exceed the maximum number of identifiers allowed systemwide. |
EEXIST | A semaphore identifier exists for the Key parameter, but both the IPC_CREAT and IPC_EXCL values are set. |
ENOMEM | There is not enough memory to complete the operation. |