shmget Subroutine

Purpose

Gets shared memory segments.

Library

Standard C Library (libc.a)

Syntax

#include <sys/shm.h>

int shmget (Key, Size, SharedMemoryFlag)
key_t  Key;
size_t  Size
int  SharedMemoryFlag;

Description

The shmget subroutine returns the shared memory identifier associated with the specified Key parameter. For information about shared memory limits, see: Interprocess Communication Limits.

Parameters

Item Description
Key Specifies either the IPC_PRIVATE value or an IPC key constructed by the ftok subroutine (or by a similar algorithm).
Size Specifies the number of bytes of shared memory required.
SharedMemoryFlag Constructed by logically ORing one or more of the following values:
IPC_CREAT
Creates the data structure if it does not already exist.
IPC_EXCL
Causes the shmget subroutine to be unsuccessful if the IPC_CREAT flag is also set, and the data structure already exists.
SHM_LGPAGE
Attempts to create the region so it can be mapped through hardware-supported, large-page mechanisms, if enabled. This is purely advisory. For the system to consider this flag, it must be used in conjunction with the SHM_PIN flag and enabled with the vmtune command (-L to reserve memory for the region (which requires a reboot) and -S to enable SHM_PIN). To successfully get large-pages, the user requesting large-page shared memory must have CAP_BYPASS_RAC_VMM capability. This has no effect on shared memory regions created with the EXTSHM=ON environment variable.
SHM_PIN
Attempts to pin the shared memory region if enabled. This is purely advisory. For the system to consider this flag, the system must be enable with vmtune command. This has no effect on shared memory regions created with EXTSHM=ON environment variable.
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.

Values that begin with the S_I prefix are defined in the sys/mode.h file and are a subset of the access permissions that apply to files.

A shared memory identifier, its associated data structure, and a shared memory segment equal in number of bytes to the value of the Size parameter are created for the Key parameter if one of the following is true:
  • The Key parameter is equal to the IPC_PRIVATE value.
  • The Key parameter does not already have a shared memory identifier associated with it, and the IPC_CREAT flag is set in the SharedMemoryFlag parameter.
Upon creation, the data structure associated with the new shared memory identifier is initialized as follows:
  • The shm_perm.cuid and shm_perm.uid fields are set to the effective user ID of the calling process.
  • The shm_perm.cgid and shm_perm.gid fields are set to the effective group ID of the calling process.
  • The low-order 9 bits of the shm_perm.mode field are set to the low-order 9 bits of the SharedMemoryFlag parameter.
  • The shm_segsz field is set to the value of the Size parameter.
  • The shm_lpid, shm_nattch, shm_atime, and shm_dtime fields are set to 0.
  • The shm_ctime field is set to the current time.
    Note: Once created, a shared memory segment is deleted only when the system reboots or by issuing the ipcrm command or using the following shmctl subroutine:
    if (shmctl (id, IPC_RMID, 0) == -1)
     perror ("error in closing segment"),exit (1);

Return Values

Upon successful completion, a shared memory identifier is returned. Otherwise, the shmget subroutine returns a value of -1 and sets the errno global variable to indicate the error.

Error Codes

The shmget subroutine is unsuccessful if one or more of the following are true:

Item Description
EACCES A shared memory identifier exists for the Key parameter, but operation permission as specified by the low-order 9 bits of the SharedMemoryFlag parameter is not granted.
EEXIST A shared memory identifier exists for the Key parameter, and both the IPC_CREAT and IPC_EXCL flags are set in the SharedMemoryFlag parameter.
EINVAL A shared memory identifier does not exist and the Size parameter is less than the system-imposed minimum or greater than the system-imposed maximum.
EINVAL A shared memory identifier exists for the Key parameter, but the size of the segment associated with it is less than the Size parameter, and the Size parameter is not equal to 0.
ENOENT A shared memory identifier does not exist for the Key parameter, and the IPC_CREAT flag is not set in the SharedMemoryFlag parameter.
ENOMEM A shared memory identifier and associated shared memory segment are to be created but the amount of available physical memory is not sufficient to meet the request.
ENOSPC A shared memory identifier will be created, but the system-imposed maximum of shared memory identifiers allowed will be exceeded.