OSDN Git Service

LDP: Update original to LDP v3.67
[linuxjm/LDP_man-pages.git] / original / man2 / shmop.2
1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
2 .\"
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.
7 .\"
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.
12 .\"
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
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
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
38 .\"
39 .\" FIXME . Add an example program to this page.
40 .\"
41 .TH SHMOP 2 2014-04-28 "Linux" "Linux Programmer's Manual"
42 .SH NAME
43 shmat, shmdt \- System V shared memory operations
44 .SH SYNOPSIS
45 .nf
46 .B #include <sys/types.h>
47 .B #include <sys/shm.h>
48
49 .BI "void *shmat(int " shmid ", const void *" shmaddr ", int " shmflg );
50
51 .BI "int shmdt(const void *" shmaddr );
52 .fi
53 .SH DESCRIPTION
54 .SS shmat()
55 .BR shmat ()
56 attaches the System\ V shared memory segment identified by
57 .I shmid
58 to the address space of the calling process.
59 The attaching address is specified by
60 .I shmaddr
61 with one of the following criteria:
62 .IP * 3
63 If
64 .I shmaddr
65 is NULL,
66 the system chooses a suitable (unused) address at which to attach
67 the segment.
68 .IP *
69 If
70 .I shmaddr
71 isn't NULL
72 and
73 .B SHM_RND
74 is specified in
75 .IR shmflg ,
76 the attach occurs at the address equal to
77 .I shmaddr
78 rounded down to the nearest multiple of
79 .BR SHMLBA .
80 .IP *
81 Otherwise,
82 .I shmaddr
83 must be a page-aligned address at which the attach occurs.
84 .PP
85 In addition to
86 .BR SHM_RND ,
87 the following flags may be specified in the
88 .I shmflg
89 bit-mask argument:
90 .TP
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.
94 .TP
95 .BR SHM_RDONLY
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.
102 .TP
103 .BR SHM_REMAP " (Linux-specific)"
104 This flag specifies
105 that the mapping of the segment should replace
106 any existing mapping in the range starting at
107 .I shmaddr
108 and continuing for the size of the segment.
109 (Normally, an
110 .B EINVAL
111 error would result if a mapping already exists in this address range.)
112 In this case,
113 .I shmaddr
114 must not be NULL.
115 .PP
116 The
117 .BR brk (2)
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.
122 .PP
123 A successful
124 .BR shmat ()
125 call updates the members of the
126 .I shmid_ds
127 structure (see
128 .BR shmctl (2))
129 associated with the shared memory segment as follows:
130 .IP
131 .I shm_atime
132 is set to the current time.
133 .IP
134 .I shm_lpid
135 is set to the process-ID of the calling process.
136 .IP
137 .I shm_nattch
138 is incremented by one.
139 .\"
140 .SS shmdt()
141 .BR shmdt ()
142 detaches the shared memory segment located at the address specified by
143 .I shmaddr
144 from the address space of the calling process.
145 The to-be-detached segment must be currently
146 attached with
147 .I shmaddr
148 equal to the value returned by the attaching
149 .BR shmat ()
150 call.
151 .PP
152 On a successful
153 .BR shmdt ()
154 call, the system updates the members of the
155 .I shmid_ds
156 structure associated with the shared memory segment as follows:
157 .IP
158 .I shm_dtime
159 is set to the current time.
160 .IP
161 .I shm_lpid
162 is set to the process-ID of the calling process.
163 .IP
164 .I shm_nattch
165 is decremented by one.
166 If it becomes 0 and the segment is marked for deletion,
167 the segment is deleted.
168 .SH RETURN VALUE
169 On success,
170 .BR shmat ()
171 returns the address of the attached shared memory segment; on error,
172 .I (void\ *)\ \-1
173 is returned, and
174 .I errno
175 is set to indicate the cause of the error.
176
177 On success,
178 .BR shmdt ()
179 returns 0; on error \-1 is returned, and
180 .I errno
181 is set to indicate the cause of the error.
182 .SH ERRORS
183 When
184 .BR shmat ()
185 fails,
186 .I errno
187 is set to one of the following:
188 .TP
189 .B EACCES
190 The calling process does not have the required permissions for
191 the requested attach type, and does not have the
192 .B CAP_IPC_OWNER
193 capability.
194 .TP
195 .B EIDRM
196 \fIshmid\fP points to a removed identifier.
197 .TP
198 .B EINVAL
199 Invalid
200 .I shmid
201 value, unaligned (i.e., not page-aligned and \fBSHM_RND\fP was not
202 specified) or invalid
203 .I shmaddr
204 value, or can't attach segment at
205 .IR shmaddr ,
206 or
207 .B SHM_REMAP
208 was specified and
209 .I shmaddr
210 was NULL.
211 .TP
212 .B ENOMEM
213 Could not allocate memory for the descriptor or for the page tables.
214 .PP
215 When
216 .BR shmdt ()
217 fails,
218 .I errno
219 is set as follows:
220 .TP
221 .B EINVAL
222 There is no shared memory segment attached at
223 .IR shmaddr ;
224 or,
225 .\" The following since 2.6.17-rc1:
226 .I shmaddr
227 is not aligned on a page boundary.
228 .SH CONFORMING TO
229 SVr4, POSIX.1-2001.
230 .\" SVr4 documents an additional error condition EMFILE.
231
232 In SVID 3 (or perhaps earlier),
233 the type of the \fIshmaddr\fP argument was changed from
234 .I "char\ *"
235 into
236 .IR "const void\ *" ,
237 and the returned type of
238 .BR shmat ()
239 from
240 .I "char\ *"
241 into
242 .IR "void\ *" .
243 (Linux libc4 and libc5 have the
244 .I "char\ *"
245 prototypes; glibc2 has
246 .IR "void\ *" .)
247 .SH NOTES
248 .PP
249 After a
250 .BR fork (2),
251 the child inherits the attached shared memory segments.
252
253 After an
254 .BR execve (2),
255 all attached shared memory segments are detached from the process.
256
257 Upon
258 .BR _exit (2),
259 all attached shared memory segments are detached from the process.
260
261 Using
262 .BR shmat ()
263 with
264 .I shmaddr
265 equal to NULL
266 is the preferred, portable way of attaching a shared memory segment.
267 Be aware that the shared memory segment attached in this way
268 may be attached at different addresses in different processes.
269 Therefore, any pointers maintained within the shared memory must be
270 made relative (typically to the starting address of the segment),
271 rather than absolute.
272 .PP
273 On Linux, it is possible to attach a shared memory segment even if it
274 is already marked to be deleted.
275 However, POSIX.1-2001 does not specify this behavior and
276 many other implementations do not support it.
277 .LP
278 The following system parameter affects
279 .BR shmat ():
280 .TP
281 .\" FIXME A good explanation of the rationale for the existence
282 .\" of SHMLBA would be useful here
283 .B SHMLBA
284 Segment low boundary address multiple.
285 Must be page aligned.
286 For the current implementation, the
287 .B SHMLBA
288 value is
289 .BR PAGE_SIZE .
290 .\" FIXME That last sentence isn't true for all Linux
291 .\" architectures (i.e., SHMLBA != PAGE_SIZE for some architectures)
292 .\" -- MTK, Nov 04
293 .PP
294 The implementation places no intrinsic per-process limit on the
295 number of shared memory segments
296 .RB ( SHMSEG ).
297 .SH SEE ALSO
298 .BR brk (2),
299 .BR mmap (2),
300 .BR shmctl (2),
301 .BR shmget (2),
302 .BR capabilities (7),
303 .BR shm_overview (7),
304 .BR svipc (7)
305 .SH COLOPHON
306 This page is part of release 3.67 of the Linux
307 .I man-pages
308 project.
309 A description of the project,
310 information about reporting bugs,
311 and the latest version of this page,
312 can be found at
313 \%http://www.kernel.org/doc/man\-pages/.