shmget()

NAME

shmget() - get shared memory segment

SYNOPSIS

#include <sys/shm.h>

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

DESCRIPTION

The shmget(2) function creates a shared memory identifier based upon the argument key. The function creates an identifier, data structure, and shared memory segment if:

The new shared memory segment is at least size bytes large. The permission mode (the low-order nine bits of the member shm_perm.mode) are set to the low-order nine bits of shmflg.

If there is already a shared memory segment associated with key, the function returns the identifier for that shared memory segment if the permissions on that shared memory segment allow the calling process to make use of it. If the permissions do not allow it, the function fails. See the errors [EACCES] and [EEXIST].

The members of the data structure associated with the shared memory identifier (described in <sys/shm.h>) are initialized as:

shm_atime Zero
shm_ctime Current time
shm_dtime Zero
shm_lpid Zero
shm_nattch Zero
shm_perm.cgid Effective group ID of calling process
shm_perm.cuid Effective user ID of calling process
shm_perm.gid Effective group ID of calling process
shm_perm.uid Effective user ID of calling process
Low-order nine bits of shm_perm.mode Low-order nine bits of shflg
shm_segsz Value of size

The header file <sys/shm.h> includes <sys/ipc.h>.

RETURN VALUE

On success, the shmget(2) function returns a shared memory identifier (a non-negative integer).

On failure, shmget(2) returns -1 and sets errno

ERRORS

The shmget(2) function can fail for the following reasons:

[EACCES]
A shared memory identifier already exists for key but the permissions specified by the low-order nine bits of shmflg wouldn't be granted.
[EEXIST]
A shared memory identifier exists for key but the value
(shmflg&IPC_CREAT)&&(shmflg&IPC_EXCL)
is non-zero.
[EINVAL]
The value of size is invalid (outside system-imposed bounds), or a shared memory identifier already exists for key but the segment's size is less than size, and size is not zero.
[ENOENT]
Although shmflg is 0, no shared memory identifier exists for key.
[ENOMEM]
There is not enough physical memory available to to create the identifier and the memory segment.
[ENOSPC]
Creating the shared memory identifier would exceed the system-wide maximum.
[ENOSYS]
the funtion is not implemented.

SEE ALSO

shmat(2)

shmctl(2)

shmdt(2)