Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

semget - get semaphore ID

&pagelevel(4)&pagelevel

Syntax

#include <sys/sem.h>

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

Description

semget() creates a semaphore identifier with its associated semid_ds data structure and its associated set of nsems semaphores (see sys/sem.h) for the argument key if one of the following is true:

  • key has the value IPC_PRIVATE.

  • No semaphore ID has been created yet for key and (semflg & IPC_CREAT) is not equal to 0.

When the new semaphore ID key is created, the corresponding data structure semid_ds is initialized as follows:

  • The effective user ID and the effective group ID of the calling process are entered for the structure components sem_perm.cuid, sem_perm.uid, sem_perm.cgid and sem_perm.gid.

  • The 9 low-order bits of sem_perm.mode are set equal to the 9 low-order bits of semflg.

  • sem_nsems is set to the value of nsems.

  • sem_otime is set to 0 and sem_ctime is set equal to the current time.

  • The data structures associated with the individual semaphores are not initialized. The semctl() function with the command SETVAL or SETALL can be used to initialize each semaphore.

Return val.

Semaphore ID


if successful. The semaphore ID is a non-negative integer.

-1

if unsuccessful. errno is set to indicate the error.

Errors

semget() will fail if:

 

 

EACCES

There already exists a semaphore ID for key, but the permission specified in the 9 low-order bits of semflg was not granted.

 

EEXIST

A semaphore ID exists for the key, but (( semflg & IPC_CREAT) && ( semflg & IPC_EXCL)) is not equal to 0.

 

EINVAL

The value of nsems is either less than or equal to 0 or exceeds the maximum value specified by the system, or a semaphore ID exists for the argument key, but the corresponding semaphore set contains less than nsems semaphores and nsems is not equal to 0.


ENOENT

No semaphore ID exists for key and ( semflg & IPC_CREAT) is equal to 0.


ENOSPC

A semaphore ID is to be created, but this would exceed the maximum number of semaphores permitted in the system.

See also

semctl(), semop(), sys/sem.h, section “Interprocess communication”.