Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

semctl(2)

semop(2)

semid_ds(4)

semget(2)  —  System Calls

OSF

NAME

semget − Returns (and possibly creates) a semaphore ID

SYNOPSIS

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
int semget(
key_t key,
int nsems,
int semflg):

PARAMETERS

keySpecifies the key that identifies the semaphore set. The IPC_PRIVATE key can be used to assure the return of a new, unused, entry in the semaphore table. 

nsemsSpecifies the number of semaphores to create in the semaphore set. 

semflgSpecifies the creation flags. Possible values are:

IPC_CREAT
If the key does not exist, the semget() function creates a semaphore set using the given key. If the key does exist, forces an error notification. 

IPC_EXCLIf the key already exists, the semget() function fails and returns an error notification. 

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

DESCRIPTION

The semget() function returns (and possibly creates) the ID for a semaphore set identified by the key parameter.  Semaphores are used primarily for synchronization between processes. 

The sets of semaphores are implemented collectively as a system-wide table, with each set being an entry in the table. The returned ID identifies the semaphore set’s entry in the table. Each set of semaphores is implemented using the semid_ds data structure. This structure defines an array whose members are the individual semaphores in the set. 

Each individual semaphore within a set is implemented using the sem structure. 

The semget() function creates a semaphore ID, its associated semid_ds data structure, and nsems individual semaphores if one of the following is true:

       •The key parameter is IPC_PRIVATE. 

       •The key parameter does not already exist as an entry in the semaphore table and the IPC_CREAT flag is set. 

After creating a semaphore ID, the semget() function initializes the semid_ds structure associated with the ID as follows:

       •The sem_perm.cuid and sem_perm.uid fields are set equal to the effective user ID of the calling process. 

       •The sem_perm.cgid and sem_perm.gid fields are set equal to the effective group ID of the calling process. 

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

       •The sem_nsems field is set equal to the value of nsems. 

       •The sem_otime field is set equal to 0 (zero) and the sem_ctime field is set equal to the current time. 

The semget() function does not initialize the sem structure associated with each semaphore in the set. The individual semaphores are initialized by using the semctl() function with the SETVAL or SETALL command. 

RETURN VALUES

Upon successful completion, a semaphore identifier is returned.  If the semget() function fails, a value of -1 is returned and errno is set to indicate the error. 

ERRORS

If the semget() function fails, errno may be set to one of the following values:

[EACCES]A semaphore ID already exists for the key parameter, but operation permission as specified by the low-order nine bits of the semflg parameter was not granted. 

[EINVAL]The value of the nsems parameter is less than or equal to 0 (zero) or greater than the system-defined limit. Or, a semaphore ID already exists for the key parameter, but the number of semaphores in the set is less than the nsems parameter, and the nsems parameter is not equal to 0 (zero). 

[ENOENT]A semaphore ID does not exist for the key parameter and IPC_CREAT was not set. 

[ENOSPC]An attempt to create a new semaphore ID exceeded the system-wide limit on the size of the semaphore table. 

[EEXIST]A semaphore ID already exists for the key parameter, but IPC_CREAT and IPC_EXCL were used for the semflg parameter. 

RELATED INFORMATION

Functions: semctl(2), semop(2)

Data structures: semid_ds(4)

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026