semget()

NAME

semget() - get or create a set of semaphores

SYNOPSIS

#include <sys/sem.h>

int semget(key_t key, int nsems, int semflg)

DESCRIPTION

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 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:

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
T}

The header file <sys/sem.h> includes <sys/ipc.h>.

RETURN VALUE

On success, semget(2) returns the semaphore identifier, a non-negative integer; on failure, it returns -1 and sets errno to indicate the error.

ERRORS

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.

SEE ALSO

semctl(2)

semop(2)