shmget()--Get ID of Shared Memory Segment with Key


  Syntax
 #include <sys/shm.h>
 #include <sys/stat.h>

 int shmget(key_t key, size_t size, int shmflg);  

  Service Program Name: QP0ZUSHR

  Default Public Authority: *USE

  Threadsafe: Yes

The shmget() function either creates a new shared memory segment or returns the shared memory identifier associated with the key parameter for an existing shared memory segment. A new shared memory segment is created if one of the following is true:

The system maintains status information about a shared memory segment which can be retrieved with the shmctl() function. When a new shared memory segment is created, the system initializes the members of the shmid_ds structure as follows:

There are two types of shared memory segments: teraspace shared memory segments and nonteraspace shared memory segments. A teraspace shared memory segment is accessed by adding the shared memory segment to a job's teraspace. A nonteraspace shared memory segment creates shared memory using i5/OS space objects.

Shared memory segments larger than 16 773 120 bytes (16 MB minus 4096 bytes) should be created as teraspace shared memory segments. The maximum size of a teraspace shared memory segment is 4 294 967 295 bytes (4GB - 1). The maximum size of a resizeable teraspace shared memory segment is 268 435 456 bytes (256 MB).

The maximum size of a nonteraspace shared memory segments is 16 776 960 bytes (16 MB - 256 bytes). When the operating system accesses a nonteraspace shared memory segment that has a size in the range 16 773 120 bytes (16 MB minus 4096 bytes) to 16 776 960 bytes (16 MB minus 256 bytes), a performance degradation will be observed.

The size of the shared memory segment can be changed after it is created using the shmctl() function. The size can only be changed if it is nonteraspace shared memory segment or if it is a teraspace shared memory segment and SHM_RESIZE_NP is specified in the shmflg parameter.


Parameters

key
(Input) The key associated with the shared memory segment. A key of IPC_PRIVATE (0x00000000) guarantees that a unique shared memory segment is created. A key can also be specified by the caller or generated by the ftok() function.

size
(Input) The size of the shared memory segment being created. If an existing shared memory segment is being accessed, size may be zero.

shmflg
(Input) Operation and permission flags. The value of the shmflg parameter is either zero or is obtained by performing an OR operation on one or more of the following constants:

S_IRUSR (0x00000100)
Allow the owner of the shared memory segment to attach to it in read mode.

S_IWUSR (0x00000080)
Allow the owner of the shared memory segment to attach to it in write mode.

S_IRGRP (0x00000020)
Allow the group of the shared memory segment to attach to it in read mode.

S_IWGRP (0x00000010)
Allow the group of the shared memory segment to attach to it in write mode.

S_IROTH (0x00000004)
Allow others to attach to the shared memory segment in read mode.

S_IWOTH (0x00000002)
Allow others to attach to the shared memory segment in write mode.

IPC_CREAT (0x00000200)
Create the shared memory segment if it does not exist.

IPC_EXCL (0x00000400)
Return an error if the IPC_CREAT flag is set and the shared memory segment already exists.

SHM_TS_NP (0x00010000)
If creating a new shared memory segment, make the shared memory segment a teraspace shared memory segment. When a job attaches to the shared memory segment, the shared memory segment will be added to the job's teraspace. Some compilers permit the user to indicate that the teraspace versions of storage functions should be used. For example, if a C module is compiled using CRTCMOD TERASPACE(*YES *TSIFC), this flag will be set automatically.

If accessing an existing shared memory segment, only specify this constant if it was specified when the shared memory segment was created.

SHM_RESIZE_NP (0x00040000)
If creating a new teraspace shared memory segment, allow the size of the shared memory segment to be changed with the shmctl() function. The maximum size of this teraspace shared memory segment is 268 435 456 bytes (256 MB). This flag is ignored for nonteraspace shared memory segments. A nonteraspace shared memory segment may always be resized up to 16 773 120 bytes (16 MB - 4096 bytes).

SHM_MAP_FIXED_NP (0x00100000)
If creating a new teraspace shared memory segment, make all jobs that successfully attach to the shared memory segment attach to the shared memory segment at the same address. The shared memory segment may not be attached in read-only mode. This flag is ignored for nonteraspace shared memory segments.

If accessing an existing shared memory segment, only specify this constant if it was specified when the shared memory segment was created.



Authorities

Authorization Required for shmget()

Object Referred to Authority Required errno
Shared memory segment to be created None None
Existing shared memory segment to be accessed See Note EACCES

Note: If the thread is accessing a shared memory segment that already exists, the mode specified in the last 9 bits of the shmflg parameter must be a subset of the mode of the existing shared memory segment.


Return Value

value shmget()was successful. The value returned is the shared memory identifier associated with the key parameter.
-1 shmget() was not successful. The errno variable is set to indicate the error.


Error Conditions

If shmget() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.

[EACCES]

Permission denied.

An attempt was made to access an object in a way forbidden by its object access permissions.

The thread does not have access to the specified file, directory, component, or path.

A shared memory identifier exists for the parameter key, but permissions specified in the low-order 9 bits of semflg are not a subset of the current permissions.

Shared memory operations are not permitted because the QSHRMEMCTL system value is set to 0.

[EDAMAGE]

A damaged object was encountered.

The shared memory segment has been damaged by a previous shared memory operation.

[EEXIST]

File exists.

The file specified already exists and the specified operation requires that it not exist.

The named file, directory, or path already exists.

A shared memory identifier exists for the key parameter and both the IPC_CREAT and IPC_EXCL flags are set in the shmflg parameter.

[EINVAL]

The value specified for the argument is not correct.

A function was passed incorrect argument values, or an operation was attempted on an object and the operation specified is not supported for that type of object.

An argument value is not valid, out of range, or NULL.

One of the following has occurred:


[ENOENT]

No such path or directory.

The directory or a component of the path name specified does not exist.

A named file or directory does not exist or is an empty string.

A shared memory identifier does not exist for the key parameter and the IPC_CREAT flag is not set in the shmflg parameter.

[ENOMEM]

Storage allocation request failed.

A function needed to allocate storage, but no storage is available.

A new shared memory segment is being created and the amount of available physical memory is not sufficient to fulfill the request.

[ENOSPC]

No space available.

The requested operations required additional space on the device and there is no space left. This could also be caused by exceeding the user profile storage limit when creating or transferring ownership of an object.

Insufficient space remains to hold the intended file, directory, or link.

A shared memory identifier cannot be created because the system limit on the maximum number of allowed shared memory identifiers would be exceeded.

[EUNKNOWN]

Unknown system state.

The operation failed because of an unknown system state. See any messages in the job log and correct any errors that are indicated, then retry the operation.


Error Messages

None.


Usage Notes

  1. The best way to generate a unique key is to use the ftok() function.

  2. When the operating system accesses a nonteraspace shared memory segment that has a size in the range 16 773 120 bytes (16 MB minus 4096 bytes) to 16 776 960 bytes (16 MB minus 256 bytes), a performance degradation will be observed. Use a teraspace shared memory segment if the size of the segment is larger than 16 773 120 bytes.

  3. Use the shmat() function to get addressability to the shared memory segment after the shared memory identifier is obtained.

  4. The storage for a shared memory segment is not allocated until it is attached to a job. A job will not be able to attach to a shared memory segment that is larger than the amount of storage available on the system.

  5. Jobs cannot attach a nonteraspace shared memory segment in read-only or write-only mode. Consequently, permissions that specify read-only or write-only will always result in shmat() returning with a return value of -1 and errno set to EOPNOTSUPP. Jobs are permitted to attach a teraspace shared memory segment in read-only mode.


Related Information


Example

See Code disclaimer information for information pertaining to code examples.

For an example of using this function, see Using Semaphores and Shared Memory in Examples: APIs.



API introduced: V3R6
Top | UNIX-Type APIs | APIs by category