1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" Modified Sun Nov 28 17:06:19 1993, Rik Faith (faith@cs.unc.edu)
26 .\" with material from Luigi P. Bai (lpb@softint.com)
27 .\" Portions Copyright 1993 Luigi P. Bai
28 .\" Modified Tue Oct 22 22:04:23 1996 by Eric S. Raymond <esr@thyrsus.com>
29 .\" Modified, 5 Jan 2002, Michael Kerrisk <mtk.manpages@gmail.com>
30 .\" Modified, 19 Sep 2002, Michael Kerrisk <mtk.manpages@gmail.com>
31 .\" Added SHM_REMAP flag description
32 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
33 .\" Added notes on capability requirements
34 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
35 .\" Language and formatting clean-ups
36 .\" Changed wording and placement of sentence regarding attachment
37 .\" of segments marked for destruction
39 .\" FIXME . Add an example program to this page.
40 .\" FIXME Linux 2.6.9 added SHM_EXEC, which should be documented
41 .TH SHMOP 2 2013-02-12 "Linux" "Linux Programmer's Manual"
43 shmat, shmdt \- System V shared memory operations
46 .B #include <sys/types.h>
47 .B #include <sys/shm.h>
49 .BI "void *shmat(int " shmid ", const void *" shmaddr ", int " shmflg );
51 .BI "int shmdt(const void *" shmaddr );
55 attaches the System V shared memory segment identified by
57 to the address space of the calling process.
58 The attaching address is specified by
60 with one of the following criteria:
65 the system chooses a suitable (unused) address at which to attach
75 the attach occurs at the address equal to
77 rounded down to the nearest multiple of
81 must be a page-aligned address at which the attach occurs.
87 the segment is attached for reading and the process must have
88 read permission for the segment.
89 Otherwise the segment is attached for read and write
90 and the process must have read and write permission for the segment.
91 There is no notion of a write-only shared memory segment.
95 flag may be specified in
97 to indicate that the mapping of the segment should replace
98 any existing mapping in the range starting at
100 and continuing for the size of the segment.
103 error would result if a mapping already exists in this address range.)
110 value of the calling process is not altered by the attach.
111 The segment will automatically be detached at process exit.
112 The same segment may be attached as a read and as a read-write
113 one, and more than once, in the process's address space.
117 call updates the members of the
121 associated with the shared memory segment as follows:
124 is set to the current time.
127 is set to the process-ID of the calling process.
130 is incremented by one.
133 detaches the shared memory segment located at the address specified by
135 from the address space of the calling process.
136 The to-be-detached segment must be currently
139 equal to the value returned by the attaching
145 call the system updates the members of the
147 structure associated with the shared memory segment as follows:
150 is set to the current time.
153 is set to the process-ID of the calling process.
156 is decremented by one.
157 If it becomes 0 and the segment is marked for deletion,
158 the segment is deleted.
162 the child inherits the attached shared memory segments.
166 all attached shared memory segments are detached from the process.
170 all attached shared memory segments are detached from the process.
174 returns the address of the attached shared memory segment; on error
178 is set to indicate the cause of the error.
182 returns 0; on error \-1 is returned, and
184 is set to indicate the cause of the error.
190 is set to one of the following:
193 The calling process does not have the required permissions for
194 the requested attach type, and does not have the
199 \fIshmid\fP points to a removed identifier.
204 value, unaligned (i.e., not page-aligned and \fBSHM_RND\fP was not
205 specified) or invalid
207 value, or can't attach segment at
216 Could not allocate memory for the descriptor or for the page tables.
225 There is no shared memory segment attached at
228 .\" The following since 2.6.17-rc1:
230 is not aligned on a page boundary.
233 .\" SVr4 documents an additional error condition EMFILE.
235 In SVID 3 (or perhaps earlier)
236 the type of the \fIshmaddr\fP argument was changed from
239 .IR "const void\ *" ,
240 and the returned type of
246 (Linux libc4 and libc5 have the
248 prototypes; glibc2 has
256 is the preferred, portable way of attaching a shared memory segment.
257 Be aware that the shared memory segment attached in this way
258 may be attached at different addresses in different processes.
259 Therefore, any pointers maintained within the shared memory must be
260 made relative (typically to the starting address of the segment),
261 rather than absolute.
263 On Linux, it is possible to attach a shared memory segment even if it
264 is already marked to be deleted.
265 However, POSIX.1-2001 does not specify this behavior and
266 many other implementations do not support it.
268 The following system parameter affects
271 .\" FIXME A good explanation of the rationale for the existence
272 .\" of SHMLBA would be useful here
274 Segment low boundary address multiple.
275 Must be page aligned.
276 For the current implementation the
280 .\" FIXME That last sentence isn't true for all Linux
281 .\" architectures (i.e., SHMLBA != PAGE_SIZE for some architectures)
284 The implementation places no intrinsic limit on the per-process maximum
285 number of shared memory segments
292 .BR capabilities (7),
293 .BR shm_overview (7),
296 This page is part of release 3.64 of the Linux
299 A description of the project,
300 and information about reporting bugs,
302 \%http://www.kernel.org/doc/man\-pages/.