OSDN Git Service

(split) LDP: Update original to LDP v3.65
[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 .\" FIXME Linux 2.6.9 added SHM_EXEC, which should be documented
41 .TH SHMOP 2 2013-02-12 "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 .BR shmat ()
55 attaches the System V shared memory segment identified by
56 .I shmid
57 to the address space of the calling process.
58 The attaching address is specified by
59 .I shmaddr
60 with one of the following criteria:
61 .LP
62 If
63 .I shmaddr
64 is NULL,
65 the system chooses a suitable (unused) address at which to attach
66 the segment.
67 .LP
68 If
69 .I shmaddr
70 isn't NULL
71 and
72 .B SHM_RND
73 is specified in
74 .IR shmflg ,
75 the attach occurs at the address equal to
76 .I shmaddr
77 rounded down to the nearest multiple of
78 .BR SHMLBA .
79 Otherwise
80 .I shmaddr
81 must be a page-aligned address at which the attach occurs.
82 .PP
83 If
84 .B SHM_RDONLY
85 is specified in
86 .IR shmflg ,
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.
92 .PP
93 The (Linux-specific)
94 .B SHM_REMAP
95 flag may be specified in
96 .I shmflg
97 to indicate that the mapping of the segment should replace
98 any existing mapping in the range starting at
99 .I shmaddr
100 and continuing for the size of the segment.
101 (Normally an
102 .B EINVAL
103 error would result if a mapping already exists in this address range.)
104 In this case,
105 .I shmaddr
106 must not be NULL.
107 .PP
108 The
109 .BR brk (2)
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.
114 .PP
115 A successful
116 .BR shmat ()
117 call updates the members of the
118 .I shmid_ds
119 structure (see
120 .BR shmctl (2))
121 associated with the shared memory segment as follows:
122 .IP
123 .I shm_atime
124 is set to the current time.
125 .IP
126 .I shm_lpid
127 is set to the process-ID of the calling process.
128 .IP
129 .I shm_nattch
130 is incremented by one.
131 .PP
132 .BR shmdt ()
133 detaches the shared memory segment located at the address specified by
134 .I shmaddr
135 from the address space of the calling process.
136 The to-be-detached segment must be currently
137 attached with
138 .I shmaddr
139 equal to the value returned by the attaching
140 .BR shmat ()
141 call.
142 .PP
143 On a successful
144 .BR shmdt ()
145 call the system updates the members of the
146 .I shmid_ds
147 structure associated with the shared memory segment as follows:
148 .IP
149 .I shm_dtime
150 is set to the current time.
151 .IP
152 .I shm_lpid
153 is set to the process-ID of the calling process.
154 .IP
155 .I shm_nattch
156 is decremented by one.
157 If it becomes 0 and the segment is marked for deletion,
158 the segment is deleted.
159 .PP
160 After a
161 .BR fork (2)
162 the child inherits the attached shared memory segments.
163
164 After an
165 .BR execve (2)
166 all attached shared memory segments are detached from the process.
167
168 Upon
169 .BR _exit (2)
170 all attached shared memory segments are detached from the process.
171 .SH RETURN VALUE
172 On success
173 .BR shmat ()
174 returns the address of the attached shared memory segment; on error
175 .I (void\ *)\ \-1
176 is returned, and
177 .I errno
178 is set to indicate the cause of the error.
179
180 On success
181 .BR shmdt ()
182 returns 0; on error \-1 is returned, and
183 .I errno
184 is set to indicate the cause of the error.
185 .SH ERRORS
186 When
187 .BR shmat ()
188 fails,
189 .I errno
190 is set to one of the following:
191 .TP
192 .B EACCES
193 The calling process does not have the required permissions for
194 the requested attach type, and does not have the
195 .B CAP_IPC_OWNER
196 capability.
197 .TP
198 .B EIDRM
199 \fIshmid\fP points to a removed identifier.
200 .TP
201 .B EINVAL
202 Invalid
203 .I shmid
204 value, unaligned (i.e., not page-aligned and \fBSHM_RND\fP was not
205 specified) or invalid
206 .I shmaddr
207 value, or can't attach segment at
208 .IR shmaddr ,
209 or
210 .B SHM_REMAP
211 was specified and
212 .I shmaddr
213 was NULL.
214 .TP
215 .B ENOMEM
216 Could not allocate memory for the descriptor or for the page tables.
217 .PP
218 When
219 .BR shmdt ()
220 fails,
221 .I errno
222 is set as follows:
223 .TP
224 .B EINVAL
225 There is no shared memory segment attached at
226 .IR shmaddr ;
227 or,
228 .\" The following since 2.6.17-rc1:
229 .I shmaddr
230 is not aligned on a page boundary.
231 .SH CONFORMING TO
232 SVr4, POSIX.1-2001.
233 .\" SVr4 documents an additional error condition EMFILE.
234
235 In SVID 3 (or perhaps earlier)
236 the type of the \fIshmaddr\fP argument was changed from
237 .I "char\ *"
238 into
239 .IR "const void\ *" ,
240 and the returned type of
241 .BR shmat ()
242 from
243 .I "char\ *"
244 into
245 .IR "void\ *" .
246 (Linux libc4 and libc5 have the
247 .I "char\ *"
248 prototypes; glibc2 has
249 .IR "void\ *" .)
250 .SH NOTES
251 Using
252 .BR shmat ()
253 with
254 .I shmaddr
255 equal to NULL
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.
262 .PP
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.
267 .LP
268 The following system parameter affects
269 .BR shmat ():
270 .TP
271 .\" FIXME A good explanation of the rationale for the existence
272 .\" of SHMLBA would be useful here
273 .B SHMLBA
274 Segment low boundary address multiple.
275 Must be page aligned.
276 For the current implementation the
277 .B SHMLBA
278 value is
279 .BR PAGE_SIZE .
280 .\" FIXME That last sentence isn't true for all Linux
281 .\" architectures (i.e., SHMLBA != PAGE_SIZE for some architectures)
282 .\" -- MTK, Nov 04
283 .PP
284 The implementation places no intrinsic limit on the per-process maximum
285 number of shared memory segments
286 .RB ( SHMSEG ).
287 .SH SEE ALSO
288 .BR brk (2),
289 .BR mmap (2),
290 .BR shmctl (2),
291 .BR shmget (2),
292 .BR capabilities (7),
293 .BR shm_overview (7),
294 .BR svipc (7)
295 .SH COLOPHON
296 This page is part of release 3.65 of the Linux
297 .I man-pages
298 project.
299 A description of the project,
300 and information about reporting bugs,
301 can be found at
302 \%http://www.kernel.org/doc/man\-pages/.