Gets a shared memory segment. Format #include <shm.h> int shmget (key_t key, size_t size, int shmflg);
1 – Arguments
key The key for which the associated shared memory identifier is returned. size Shared memory segment size in bytes. shmflg Flag used to initialize the low order 9 bits of the shm_perm.mode member of the shmid_ds data structure associated with the new shared memory segment. For more information, see the Description.
2 – Description
The shmget() function returns the shared memory identifier associated with the key. A shared memory identifier with its associated shmid_ds data structure is created for a key if one of the following is TRUE: o The key argument is equal to IPC_PRIVATE. o The key argument does not already have a shared memory identifier associated with it and (shmflg and IPC_CREAT) is non-zero. If shmflg specifies both IPC_CREAT and IPC_EXCL and a shared memory segment already exists for a key, shmget() function fails with errno set to EEXIST. When it is created, the shmid_ds data structure associated with the new shared memory identifier is initialized as follows: o The values of shm_perm.cuid, shm_perm.uid, shm_perm.cgid, and shm_perm.gid are set equal to the effective user ID and effective group ID, respectively, of the calling process. o The low order 9 bits of shm_perm.mode are set equal to the low order 9 bits of the shmflg argument. o The variable shm_segsz is set equal to the value of the size argument. o The variables shm_lpid, shm_nattch, shm_atime, and shm_dtime are set equal to zero, the variable shm_cpid is set equal to the Process ID of the segment creator, and the variable shm_ctime is set equal to the current time.
3 – Return Values
n Successful completion. The function returns a non-negative integer shared memory identifier. -1 Indicates an error. The function sets errno to one of the following values: o EACCESS - A shared memory identifier exists for the key, but the operation permission as specified by the low order 9 bits of shmflg are not granted. o EEXIST - A shared memory identifier exists for a key but ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) is non-zero. o EINVAL - The value of size is either less than SHMMIN, greater than the SHMMAX, or a shared memory identifier exists for a key, but the size is greater than the size of that segment. With current implementation SHMMIN is defined to 1 byte and SHMMAX is defined to 512 MB. O ENOENT - A shared memory identifier does not exist for a key and (shmflg & IPC_CREAT) is equal to zero. O ENOSPC - A shared memory identifier is to be created but the system-imposed limit on the maximum number of allowed shared memory segments system-wide is exceeded. o EVMSERR - OpenVMS specific non-translatable error code.