gtpc2m6l | C/C++ Language Support User's Guide |
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 key returned by the ftok function if the shared memory can be
accessed by more than one process.
- The value IPC_PRIVATE to indicate that the shared memory cannot be
accessed by other processes.
A shared memory identifier, associated data structure, and the shared
memory are created for the key parameter if one of the following is
true:
- The key parameter has a value of IPC_PRIVATE.
- The key parameter does not already have a shared memory
identifier associated with it and IPC_CREAT is specified for the
shmflg parameter.
- 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:
- A shared memory identifier does not exist for the specified key
parameter and the value of the size parameter is less than the
minimum size or greater than the maximum size imposed by the TPF
system.
- A shared memory identifier exists for the specified key
parameter, but the size of the shared memory is less than what is specified by
the size parameter.
- A value of 0 was specified for the shmflg parameter.
- 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
- Programs require restricted authorization to use this function.
- The shmid_ds data structure is defined in the
sys/shm.h header file.
- The following fields are initialized when a shmid_ds data
structure is created for the allocated shared memory:
- The shm_perm.cuid and
shm_perm.uid fields are set to the effective UID of the
calling process.
- The shm_perm.cgid and
sem_perm.gid fields are set to the effective GID of the
calling process.
- The low-order 9 bits of the shm_perm.mode field are
set to the value in the low-order 9 bits of the shmflg
parameter.
- The shm_segsz field is set to the value of the size
parameter.
- The shm_lpid, shm_nattach, shm_atime, and
shm_dtime fields are set to zero.
- The shm_ctime field is set to the current time.
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