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.
41 .TH SHMOP 2 2014-07-08 "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 );
56 attaches the System\ V shared memory segment identified by
58 to the address space of the calling process.
59 The attaching address is specified by
61 with one of the following criteria:
66 the system chooses a suitable (unused) address at which to attach
76 the attach occurs at the address equal to
78 rounded down to the nearest multiple of
83 must be a page-aligned address at which the attach occurs.
87 the following flags may be specified in the
91 .BR SHM_EXEC " (Linux-specific; since Linux 2.6.9)"
92 Allow the contents of the segment to be executed.
93 The caller must have execute permission on the segment.
96 Attach the segment for read-only access.
97 The process must have read permission for the segment.
98 If this flag is not specified,
99 the segment is attached for read and write access,
100 and the process must have read and write permission for the segment.
101 There is no notion of a write-only shared memory segment.
103 .BR SHM_REMAP " (Linux-specific)"
105 that the mapping of the segment should replace
106 any existing mapping in the range starting at
108 and continuing for the size of the segment.
111 error would result if a mapping already exists in this address range.)
118 value of the calling process is not altered by the attach.
119 The segment will automatically be detached at process exit.
120 The same segment may be attached as a read and as a read-write
121 one, and more than once, in the process's address space.
125 call updates the members of the
129 associated with the shared memory segment as follows:
132 is set to the current time.
135 is set to the process-ID of the calling process.
138 is incremented by one.
142 detaches the shared memory segment located at the address specified by
144 from the address space of the calling process.
145 The to-be-detached segment must be currently
148 equal to the value returned by the attaching
154 call, the system updates the members of the
156 structure associated with the shared memory segment as follows:
159 is set to the current time.
162 is set to the process-ID of the calling process.
165 is decremented by one.
166 If it becomes 0 and the segment is marked for deletion,
167 the segment is deleted.
171 returns the address of the attached shared memory segment; on error,
175 is set to indicate the cause of the error.
179 returns 0; on error \-1 is returned, and
181 is set to indicate the cause of the error.
187 is set to one of the following:
190 The calling process does not have the required permissions for
191 the requested attach type, and does not have the
196 \fIshmid\fP points to a removed identifier.
201 value, unaligned (i.e., not page-aligned and \fBSHM_RND\fP was not
202 specified) or invalid
204 value, or can't attach segment at
213 Could not allocate memory for the descriptor or for the page tables.
222 There is no shared memory segment attached at
225 .\" The following since 2.6.17-rc1:
227 is not aligned on a page boundary.
230 .\" SVr4 documents an additional error condition EMFILE.
232 In SVID 3 (or perhaps earlier),
233 the type of the \fIshmaddr\fP argument was changed from
236 .IR "const void\ *" ,
237 and the returned type of
247 the child inherits the attached shared memory segments.
251 all attached shared memory segments are detached from the process.
255 all attached shared memory segments are detached from the process.
262 is the preferred, portable way of attaching a shared memory segment.
263 Be aware that the shared memory segment attached in this way
264 may be attached at different addresses in different processes.
265 Therefore, any pointers maintained within the shared memory must be
266 made relative (typically to the starting address of the segment),
267 rather than absolute.
269 On Linux, it is possible to attach a shared memory segment even if it
270 is already marked to be deleted.
271 However, POSIX.1-2001 does not specify this behavior and
272 many other implementations do not support it.
274 The following system parameter affects
278 Segment low boundary address multiple.
279 When explicitly specifying an attach address in a call to
281 the caller should ensure that the address is a multiple of this value.
282 This is necessary on some architectures,
283 in order either to ensure good CPU cache performance or to ensure that
284 different attaches of the same segment have consistent views
285 within the CPU cache.
287 is normally some multiple of the system page size
288 (on many Linux architectures, it is the same as the system page size).
290 The implementation places no intrinsic per-process limit on the
291 number of shared memory segments
298 .BR capabilities (7),
299 .BR shm_overview (7),