The semget(2) function returns the semaphore
identifier associated with key. If the following are true,
semget(2) creates the semaphore identifier, its
associated semid_ds data structure and the associated set of
semaphores (the number of semaphores in the set is determined by
nsems):
The key is IPC_PRIVATE.
The key doesn't already have a semaphore associated with
it, and the value of (semflg & IPC_CREAT) is not
zero.
The semid_ds data structure has the following
members:
struct semid_ds {
struct ipc_perm sem_perm /* operation permission structure */
unsigned short int sem_nsems /* number of semaphores in set */
time_t sem_otime /* time of last semop() */
time_t sem_ctime /* time of last change by semctl() */
}
The new semid_ds data structure is initialized to:
The IDs in the operation permissions structure are set to those
of the effective IDs of the calling process. That is, the members
sem_perm.cuidsem_perm.uidsem_perm.cgid and
sem_perm.gid are set to the value of the calling process'
effective user ID and effective group ID. (On INTERIX systems,
these are the user ID and the group ID, respectively.)
The low-order 9 bits of sem_perm.mode are set to the
low-order 9 bits of semflg.
The value of sem_nsems is set to the value of
nsems.
The value of sem_otime is set to 0.
The value of sem_ctime is set to the current time.
Note:
The data structure associated with each semaphore in the set is
not initialized. Use the semctl(2) function with the
command SETVAL or SETALL to initialize each semaphore.
A semaphore contains the following members (the data structure
is anonymous):
unsigned short int
semval
semaphore value
pid_t
sempid
process ID of last operation
unsigned short int
semncnt
number of processes waiting for semval to become greater
than current value
unsigned short int
semzcnt
number of processes waiting for semval to become 0
The semget(2) function can fail for these
reasons:
[EACCES]
Although a semaphore identifier exists for key, its
permissions are not compatible with the low-order 9 bits of
semflg.
[EEXIST]
Although a semaphore identifier exists for key, the
value of ((semflg & IPC_CREAT) && (semflg
& IPC_EXCL)) is non-zero.
[EINVAL]
The value of nsems is outside the system boundaries
(less than or equal to 0 or greater than the limit), or an
identifier exists for key but the number of semaphores in
the set associated with it is less than nsems and
nsems isn't equal to 0.
[ENOENT]
No semaphore identifier exists for key, and
(semflg & IPC_CREAT) equals 0.
[ENOSPC]
Creating the identifier would exceed the system-wide maximum
number of semaphores allowed.