gtpc2m6lC/C++ Language Support User's Guide

shmget-Allocate Shared Memory

This function allocates shared memory for the specified key and returns its associated shared memory identifier. The shared memory identifier is used as the shmid parameter for the shared memory attach (shmat) and shared memory control (shmctl) functions.

Format

#include  <sys/shm.h>
int       shmget(  key_t  key,
                  size_t  size,
                     int  shmflg);

key
One of the following:

A shared memory identifier, associated data structure, and the shared memory are created for the key parameter if one of the following is true:

size
The size of the shared memory to be created, in bytes.

shmflg
A flag field specified with any combination of the following constants defined in the sys/shm.h and modes.h header files:

IPC_CREAT
Create a shared memory segment if a shared memory identifier does not exist for the specified key parameter.

IPC_CREAT is ignored when IPC_PRIVATE is specified for the key parameter.

IPC_EXCL
Causes the shmget function to fail if the specified key parameter has an associated shared memory identifier.

IPC_EXCL is ignored when IPC_CREAT is not specified for the shmflg parameter or IPC_PRIVATE is specified for the key parameter.

S_IRUSR
Permits read access when the effective user ID (UID) of the caller is equal to either the shm_perm.cuid or the shm_perm.uid field in the shmid_ds data structure associated with the shared memory identified by the key parameter.

S_IWUSR
Permits write access when the effective UID of the caller is equal to either the shm_perm.cuid or the shm_perm.uid field in the shmid_ds data structure associated with the shared memory identified by the key parameter.

S_IRGRP
Permits read access when the effective group ID (GID) of the caller is equal to either the shm_perm.cgid or the shm_perm.gid field in the shmid_ds data structure associated with the shared memory identified by the key parameter.

S_IWGRP
Permits write access when the effective GID of the caller is equal to either the shm_perm.cgid or the shm_perm.gid field in the shmid_ds data structure associated with the shared memory identified by the key parameter.

S_IROTH
Permits other read access to the shared memory.

S_IWOTH
Permits other write access to the shared memory.

Normal Return

If successful, the shmget function returns the shared memory identifier associated with the specified key as a nonnegative integer and a shmid_ds data structure is created for the allocated shared memory.

Error Return

If unsuccessful, the shmget function returns a value of -1 and sets errno to one of the following:

EACCES
A shared memory identifier exists for the key parameter, but operation permission as specified by the low-order 9 bits of the shmflg parameter could not be granted because the UID of the calling process does not have the appropriate privileges to access the shared memory.

EEXIST
A shared memory identifier exists for the key parameter and both IPC_CREAT and IPC_EXCL are specified for the shmflg parameter.

EINVAL
One of the following conditions occurred:

ENOENT
A shared memory identifier does not exist for the key parameter and IPC_CREAT is not specified for the shmflg parameter.

ENOMEM
A shared memory identifier and associated shared memory segment are to be created, but there is not enough system storage available to satisfy the request.

ENOSPC
A shared memory identifier is to be created, but the limit (imposed by the TPF system) on the maximum number of shared memory identifiers that are allocated systemwide would be exceeded.

Programming Considerations

Examples

The following example allocates shared memory and returns its shared memory identifier after calling the ftok function to return a key that is used as input to the shmget function.

#include <sys/ipc.h>
#include <sys/shm.h>
int main()
 
{
 key_t key;
 int   i;
 
 key = ftok("/usr/testfile",3);
 i= shmget(key,100,IPC_CREAT);
 }

Related Information