semget
Purpose
Gets a set of semaphores.
Syntax
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
int semget (key, nsems, semflg)
key_t key;
int nsems, semflg;
Description
The semget system call returns the semaphore identifier
associated with the specified key. The key parameter is
either the value IPC_PRIVATE or an IPC key constructed by
the ftok subroutine (or by a similar algorithm). See
"ftok" for details about this subroutine. The nsems
parameter specifies the number of semaphores in the set.
The semflg parameter is constructed by logically OR-ing
one or more of the following values:
IPC_CREAT Creates the data structure if it does not
already exist.
IPC_EXCL Causes the semget system call to fail if
IPC_CREAT is also set and the data structure
already exists.
S_IRUSR Permits the process that owns the data
structure to read it.
S_IWUSR Permits the process that owns the data
structure to modify it.
S_IRGRP Permits the group associated with the data
structure to read it.
S_IWGRP Permits the group associated with the data
structure to modify it.
S_IROTH Permits others to read the data structure.
S_IWOTH Permits others to modify the data structure.
The values that begin with S_I- are defined in the
sys/stat.h header file and are a subset of the access
permissions that apply to files.
The semget system call creates a data structure for the
semaphore ID and an array containing nsems semaphores if
one of the following is true:
o The key parameter is equal to IPC_PRIVATE.
o The key parameter does not already have a semaphore
identifier associated with it, and IPC_CREAT is set.
Upon creation, the data structure associated with the new
semaphore identifier is initialized as follows:
o sem_perm.cuid and sem_perm.uid are set equal to the
effective user ID of the calling process.
o sem_perm.cgid and sem_perm.gid are set equal to the
effective group ID of the calling process.
o The low-order nine bits of sem_perm.mode are set
equal to the low-order nine bits of the semflg param-
eter.
o sem_nsems is set equal to the value of the nsems
parameter.
o sem_otime is set equal to 0 and sem_ctime is set
equal to the current time.
If the key parameter is not IPC_PRIVATE, IPC_EXCL is not
set, and a semaphore identifier already exists for the
specified key, then the value of the nsems parameter
specifies the number of semaphores that the current
process needs. If the nsems parameter is 0, then any
number of semaphores is acceptable. If the nsems param-
eter is not 0, then the semget system call fails if the
set contains fewer than nsems semaphores.
Return Value
Upon successful completion, a semaphore identifier is
returned. If semget fails, a value of -1 is returned and
errno is set to indicate the error.
Diagnostics
The semget system call fails if one or more of the fol-
lowing are true:
EINVAL The nsems parameter is less than 0, equal to
0, or greater than the system-imposed limit.
EACCES A semaphore identifier exists for the key
parameter but operation permission, as speci-
fied by the low-order nine bits of the semflg
parameter, is not granted.
EINVAL A semaphore identifier exists for the key
parameter, but the number of semaphores in the
set associated with it is less than the value
of the nsems parameter and the nsems parameter
is not equal to 0.
ENOENT A semaphore identifier does not exist for the
key parameter and IPC_CREAT is not set.
ENOSPC A semaphore identifier is to be created, but
doing so would exceed the maximum number of
identifiers allowed system wide.
EEXIST A semaphore identifier exists for the key
parameter, and both IPC_CREAT and IPC_EXCL are
set.
Related Information
In this book: "semctl," "semop," and "ftok."