semget

Creates or gets a semaphore set.

Library:LibC
Classification:System-V
Service:Synchronization

Syntax

  #include <sys/sem.h>
   
  int semget (
     key_t   key,
     int     nsems,
     int     semflag );
  

Parameters

key

(IN) Specifies an existing key created with the ftok function or IPC_PRIVATE, a flag which allows a key to be created.

nsems

(IN) Specifies the number of semaphores for the set. When accessing an existing semaphore set, this parameter can be set to 0.

semflag

(IN) Specifies how to create the semaphore set with the following flags which can be ORed together:

Flag

Value

Description

IPC_CREAT

0x00000200

If the key doesn't already exist, allows a key to be created.

IPC_EXCL

0x00000400

If the key already exists, prevents the semaphore set from being created

Return Values

If successful, returns a semaphore idenifier for the semaphore set; otherwise, returns -1 and sets errno to one of the following:

Decimal

Constant

Description

1

ENOENT

The key parameter does not specify an existing key and the semflag parameter is set to 0.

6

EACCES

The calling thread has insufficient rights.

7

EEXIST

A semaphore set already exists for the key and the IPC_CREAT flag was set.

12

ENOSPC

The system does not have enough semaphore resources to create the semaphore set.

105

ENOCONTEXT

The calling thread has no context.

Remarks

When a semaphore set is created, the array of sem structures is not initialized.

When accessing an existing semaphore set, the key parameter must specify the key associated with the set and the IPC_EXCL flag cannot be set in the semflg parameter.

A semaphore set created with a key value of IPC_PRIVATE is private to the process that created the key. Semaphores created with keys generated with the ftok function can be shared.

When you are finished with the semaphore set, you should remove it by calling the semctl function.

See Also