1SHMOP(2)                   Linux Programmer's Manual                  SHMOP(2)
2
3
4

NAME

6       shmat, shmdt - System V shared memory operations
7

SYNOPSIS

9       #include <sys/types.h>
10       #include <sys/shm.h>
11
12       void *shmat(int shmid, const void *shmaddr, int shmflg);
13
14       int shmdt(const void *shmaddr);
15

DESCRIPTION

17       shmat() attaches the System V shared memory segment identified by shmid
18       to the address space of the calling process.  The attaching address  is
19       specified by shmaddr with one of the following criteria:
20
21       If  shmaddr  is NULL, the system chooses a suitable (unused) address at
22       which to attach the segment.
23
24       If shmaddr isn't NULL and SHM_RND is specified in  shmflg,  the  attach
25       occurs at the address equal to shmaddr rounded down to the nearest mul‐
26       tiple of SHMLBA.  Otherwise shmaddr must be a page-aligned  address  at
27       which the attach occurs.
28
29       If SHM_RDONLY is specified in shmflg, the segment is attached for read‐
30       ing and the process must have read permission for the segment.   Other‐
31       wise  the  segment  is attached for read and write and the process must
32       have read and write permission for the segment.  There is no notion  of
33       a write-only shared memory segment.
34
35       The (Linux-specific) SHM_REMAP flag may be specified in shmflg to indi‐
36       cate that the mapping of the segment should replace any  existing  map‐
37       ping  in  the  range starting at shmaddr and continuing for the size of
38       the segment.  (Normally an EINVAL  error  would  result  if  a  mapping
39       already  exists in this address range.)  In this case, shmaddr must not
40       be NULL.
41
42       The brk(2) value of the calling process is not altered by  the  attach.
43       The  segment  will automatically be detached at process exit.  The same
44       segment may be attached as a read and as a  read-write  one,  and  more
45       than once, in the process's address space.
46
47       A successful shmat() call updates the members of the shmid_ds structure
48       (see shmctl(2)) associated with the shared memory segment as follows:
49
50              shm_atime is set to the current time.
51
52              shm_lpid is set to the process-ID of the calling process.
53
54              shm_nattch is incremented by one.
55
56       shmdt() detaches the shared memory segment located at the address spec‐
57       ified  by  shmaddr  from the address space of the calling process.  The
58       to-be-detached segment must be currently attached with shmaddr equal to
59       the value returned by the attaching shmat() call.
60
61       On  a  successful  shmdt()  call  the system updates the members of the
62       shmid_ds structure associated with the shared memory  segment  as  fol‐
63       lows:
64
65              shm_dtime is set to the current time.
66
67              shm_lpid is set to the process-ID of the calling process.
68
69              shm_nattch  is decremented by one.  If it becomes 0 and the seg‐
70              ment is marked for deletion, the segment is deleted.
71
72       After a fork(2) the child inherits the attached shared memory segments.
73
74       After an execve(2) all attached shared  memory  segments  are  detached
75       from the process.
76
77       Upon _exit(2) all attached shared memory segments are detached from the
78       process.
79

RETURN VALUE

81       On success shmat() returns the address of the  attached  shared  memory
82       segment; on error (void *) -1 is returned, and errno is set to indicate
83       the cause of the error.
84
85       On success shmdt() returns 0; on error -1 is returned, and errno is set
86       to indicate the cause of the error.
87

ERRORS

89       When shmat() fails, errno is set to one of the following:
90
91       EACCES The  calling  process does not have the required permissions for
92              the requested attach type, and does not have  the  CAP_IPC_OWNER
93              capability.
94
95       EIDRM  shmid points to a removed identifier.
96
97       EINVAL Invalid  shmid  value,  unaligned  (i.e.,  not  page-aligned and
98              SHM_RND was not specified) or invalid shmaddr  value,  or  can't
99              attach  segment  at  shmaddr,  or  SHM_REMAP  was  specified and
100              shmaddr was NULL.
101
102       ENOMEM Could not allocate memory for the descriptor  or  for  the  page
103              tables.
104
105       When shmdt() fails, errno is set as follows:
106
107       EINVAL There  is  no  shared  memory  segment  attached at shmaddr; or,
108              shmaddr is not aligned on a page boundary.
109

CONFORMING TO

111       SVr4, POSIX.1-2001.
112
113       In SVID 3 (or perhaps earlier) the type of  the  shmaddr  argument  was
114       changed from char * into const void *, and the returned type of shmat()
115       from char * into void *.  (Linux libc4 and libc5 have the char * proto‐
116       types; glibc2 has void *.)
117

NOTES

119       Using shmat() with shmaddr equal to NULL is the preferred, portable way
120       of attaching a shared memory segment.  Be aware that the shared  memory
121       segment  attached in this way may be attached at different addresses in
122       different processes.  Therefore, any  pointers  maintained  within  the
123       shared  memory must be made relative (typically to the starting address
124       of the segment), rather than absolute.
125
126       On Linux, it is possible to attach a shared memory segment even  if  it
127       is  already marked to be deleted.  However, POSIX.1-2001 does not spec‐
128       ify this behavior and many other implementations do not support it.
129
130       The following system parameter affects shmat():
131
132       SHMLBA Segment low boundary address multiple.  Must  be  page  aligned.
133              For the current implementation the SHMLBA value is PAGE_SIZE.
134
135       The implementation places no intrinsic limit on the per-process maximum
136       number of shared memory segments (SHMSEG).
137

SEE ALSO

139       brk(2),  mmap(2),  shmctl(2),  shmget(2),  capabilities(7),   shm_over‐
140       view(7), svipc(7)
141

COLOPHON

143       This  page  is  part of release 3.53 of the Linux man-pages project.  A
144       description of the project, and information about reporting  bugs,  can
145       be found at http://www.kernel.org/doc/man-pages/.
146
147
148
149Linux                             2013-02-12                          SHMOP(2)
Impressum