shmget(2) — System Calls
NAME
shmget − Returns (and possibly creates) the ID for a shared memory region
SYNOPSIS
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int shmget (
key_t key,
size_t size,
int flags );
PARAMETERS
keySpecifies the key that identifies the shared memory region. The value for the key parameter can be IPC_PRIVATE or a random number other than zero (0). If the value of key is IPC_PRIVATE, it can be used to assure the return of a new, unused shared memory region.
sizeSpecifies the minimum number of bytes to allocate for the region.
flagsSpecifies the creation flags. Possible values are:
IPC_CREAT
If the key specified does not exist, the shmget function creates an ID using the specified key.
If the specified key does exist, and IPC_EXCL is not set, the ID for the specified key is returned.
If the specified key does exist and IPC_EXCL is set, the shmget function fails and returns an error.
IPC_EXCLIf the key already exists, the shmget function fails and returns an error notification.
The low-order nine bits of shm_perm.mode are set equal to the low-order nine bits of flags.
DESCRIPTION
The shmget function returns (and possibly creates) the ID for the shared memory region identified by the key parameter. If IPC_PRIVATE is used for the key parameter, the shmget function returns the ID for a private (that is, newly created) shared memory region. The flags parameter supplies creation options for the shmget function. If the key parameter does not already exist, the IPC_CREAT flag instructs the shmget function to create a new shared memory region for the key and return the kernel-assigned ID for the region.
After creating a new shared memory region ID, the shmget function initializes the shmid_ds structure associated with the ID as follows:
•The shm_perm.cuid and shm_perm.uid fields are set equal to the effective user ID of the calling process.
•The shm_perm.cgid and shm_perm.gid fields are set equal to the effective group ID of the calling process.
•The low-order nine bits of the shm_perm.mode field are set equal to the low-order nine bits of flags.
•The shm_segsz field is set equal to size.
•The shm_lpid, shm_nattch, shm_atime, and shm_dtime fields are all set equal to 0 (zero).
•The shm_ctime field is set equal to the current time.
•The shm_cpid field is set to the process ID of the calling process.
RETURN VALUES
Upon successful completion, a shared memory identifier is returned. If the shmget function fails, a value of -1 is returned and errno is set to indicate the error.
ERRORS
If the shmget function fails, errno may be set to one of the following values:
[EINVAL]The value of the size parameter is less than the system-defined minimum or greater than the system-defined maximum. Or, a shared memory region ID already exists for the key parameter, but the number of bytes allocated for the region is less than size and size is not equal to 0 (zero).
[EACCES]A shared memory region ID already exists for the key parameter, but operation permission as specified by the low-order nine bits of the flags parameter was not granted.
[ENOENT]A shared memory region ID does not exist for the key parameter, and IPC_CREAT was used for the flags parameter.
[ENOSPC]An attempt to create a new shared memory region ID exceeded the system-wide limit on the maximum number of IDs allowed.
[ENOMEM]An attempt was made to create a shared memory region ID and its associated shmid_ds structure, but there was not enough physical memory available.
[EEXIST]A shared memory region ID already exists for the key parameter, but IPC_CREAT and IPC_EXCL were used for the flags parameter.
RELATED INFORMATION
Functions: shmat(2), shmctl(2), shmdt(2), table(2).
Data structures: shmid_ds(4).