shmat() — Shared memory attach operation

Standards

Standards / Extensions C or C++ Dependencies

XPG4
XPG4.2
Single UNIX Specification, Version 3

both  

Format

#define _XOPEN_SOURCE
#include <sys/shm.h>

void  *shmat(int shmid, const void *shmaddr, int shmflg);

General description

The shmat() function attaches the shared memory segment associated with the shared memory identifier, shmid, to the address space of the calling process. The segment is attached at the address specified by one of the following criteria:
  • If shmaddr is a NULL pointer, the segment is attached at the first available address as selected by the system.
  • If shmaddr is not a NULL pointer, and the flag, SHM_RND was specified, the segment is attached at the address given by (shmaddr-((prtdiff_t)shmaddr%SHMLBA)) where % is the 'C' language remainder operator.
  • If shmaddr is not a NULL pointer, and the flag, SHM_RND was not specified, the segment is attached at the address given by shmaddr.
  • The segment is attached for reading if the flag, SHM_RDONLY, is specified with shmflg and the calling process has read permission. If the flag is not set and the process has both read and write permission, the segment is attached for reading and writing.

    The first attach of newly created __IPC_MEGA segment, as well as subsequent attaches, will have write access to the segment, regardless of the SHM_RDONLY option.

  • All attaches to an __IPC_MEGA shared memory segment have the same Write or Read access authority. If a segment is enabled for writes then all attaches have the ability to read and write to the segment. If the segment is disabled for writes, then all attaches have the ability to read from the segment and cannot write to the segment

    The first attach of newly created __IPC_MEGA segment, as well as subsequent attaches, will have write access to the segment, regardless of the SHM_RDONLY option. Write/Read access can be changed by the shmctl() function, Shared Memory Control Operations.

An __IPC_MEGA shared memory segment is attached as follows:
  • If shmaddr is zero and __IPC_MEGA segment, then the segment will be attached at the first available address selected by the system on a segment boundary.
  • If shmaddr is not zero and SHM_RND is specified and __IPC_MEGA segment, the segment address will be truncated to the segment boundary (last 20 bits zero).
  • If shmaddr is not zero and SHM_RND is not specified and __IPC_MEGA segment, the segment address must be a megabyte multiple (segment boundary).

Returned value

If successful, shmat() increments the value of shm_nattach in the data structure associated with the shared memory ID of the attached shared memory segment and returns the segment's starting address.

If unsuccessful, shmat() returns -1 and sets errno to one of the following values:
Error Code
Description
EACCES
Operation permission is denied to the calling process.
EINVAL
The value of shmid is not a valid shared memory identifier; the shmaddr is not a NULL pointer and the value of (shmaddr-((ptrdiff_t)shmaddr%SHMLBA)) is an illegal address for attaching shared memory segments; or the shmaddr is not a NULL pointer, SHM_RND was specified, and the value of shmaddr is an illegal address for attaching shared memory segments.

The shared memory address, *shmaddr, is not zero, is not on a megabyte boundary, and SHM_RND was not specified.

EMFILE
The number of shared memory segments attached to the calling process would exceed the system-imposed limit.
ENOMEM
The available data space is not large enough to accommodate the shared memory segment.

Related information