]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/shmop.2
Use bold for function name, not underline.
[thirdparty/man-pages.git] / man2 / shmop.2
1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .\" Modified Sun Nov 28 17:06:19 1993, Rik Faith (faith@cs.unc.edu)
24 .\" with material from Luigi P. Bai (lpb@softint.com)
25 .\" Portions Copyright 1993 Luigi P. Bai
26 .\" Modified Tue Oct 22 22:04:23 1996 by Eric S. Raymond <esr@thyrsus.com>
27 .\" Modified, 5 Jan 2002, Michael Kerrisk <mtk-manpages@gmx.net>
28 .\" Modified, 19 Sep 2002, Michael Kerrisk <mtk-manpages@gmx.net>
29 .\" Added SHM_REMAP flag description
30 .\" Modified, 27 May 2004, Michael Kerrisk <mtk-manpages@gmx.net>
31 .\" Added notes on capability requirements
32 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk-manpages@gmx.net>
33 .\" Language and formatting clean-ups
34 .\" Changed wording and placement of sentence regarding attachment
35 .\" of segments marked for destruction
36 .\"
37 .TH SHMOP 2 2004-11-10 "Linux 2.6.9" "Linux Programmer's Manual"
38 .SH NAME
39 shmop, shmat, shmdt \- shared memory operations
40 .SH SYNOPSIS
41 .nf
42 .B
43 #include <sys/types.h>
44 .B
45 #include <sys/shm.h>
46 .fi
47 .sp
48 .BI "void *shmat(int " shmid ,
49 .BI "const void *" shmaddr ,
50 .BI "int " shmflg );
51 .sp
52 .BI "int shmdt(const void *" shmaddr );
53 .SH DESCRIPTION
54 .BR shmat ()
55 attaches the 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 .B 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 .SH "SYSTEM CALLS"
160 .TP 11
161 .BR fork (2)
162 After a
163 .BR fork (2)
164 the child inherits the attached shared memory segments.
165 .TP
166 .BR execve (2)
167 After an
168 .BR execve (2)
169 all attached shared memory segments are detached from the process.
170 .TP
171 .BR _exit (2)
172 Upon
173 .BR _exit (2)
174 all attached shared memory segments are detached from the process.
175 .SH "RETURN VALUE"
176 On success
177 .BR shmat ()
178 returns the address of the attached shared memory segment; on error
179 .I (void *) \-1
180 is returned, and
181 .I errno
182 is set to indicate the cause of the error.
183
184 On success
185 .BR shmdt ()
186 returns 0; on error \-1 is returned, and
187 .I errno
188 is set to indicate the cause of the error.
189 .SH ERRORS
190 When
191 .BR shmat ()
192 fails,
193 .I errno
194 is set to one of the following:
195 .TP
196 .B EACCES
197 The calling process does not have the required permissions for
198 the requested attach type, and does not have the
199 .B CAP_IPC_OWNER
200 capability.
201 .TP
202 .B EINVAL
203 Invalid
204 .I shmid
205 value, unaligned (i.e., not page-aligned and \fBSHM_RND\fP was not
206 specified) or invalid
207 .I shmaddr
208 value, or failing attach at
209 .BR brk (2),
210 .\" FIXME What does "failing attach at brk" mean? (Is this phrase
211 .\" just junk?)
212 or
213 .B SHM_REMAP
214 was specified and
215 .I shmaddr
216 was NULL.
217 .TP
218 .B ENOMEM
219 Could not allocate memory for the descriptor or for the page tables.
220 .PP
221 When
222 .BR shmdt ()
223 fails,
224 .I errno
225 is set as follows:
226 .TP
227 .B EINVAL
228 There is no shared memory segment attached at
229 .IR shmaddr ;
230 or,
231 .\" The following since 2.6.17-rc1:
232 .I shmaddr
233 is not aligned on a page boundary.
234 .SH NOTES
235 Using
236 .BR shmat ()
237 with
238 .I shmaddr
239 equal to NULL
240 is the preferred, portable way of attaching a shared memory segment.
241 Be aware that the shared memory segment attached in this way
242 may be attached at different addresses in different processes.
243 Therefore, any pointers maintained within the shared memory must be
244 made relative (typically to the starting address of the segment),
245 rather than absolute.
246 .PP
247 On Linux, it is possible to attach a shared memory segment even if it
248 is already marked to be deleted.
249 However, POSIX.1-2001 does not specify this behaviour and
250 many other implementations do not support it.
251 .LP
252 The following system parameter affects
253 .BR shmat ():
254 .TP 11
255 .\" FIXME A good explanation of the rationale for the existence
256 .\" of SHMLBA would be useful here
257 .B SHMLBA
258 Segment low boundary address multiple.
259 Must be page aligned.
260 For the current implementation the
261 .B SHMLBA
262 value is
263 .BR PAGE_SIZE .
264 .\" FIXME That last sentence isn't true for all Linux
265 .\" architectures (i.e., SHMLBA != PAGE_SIZE for some architectures)
266 .\" -- MTK, Nov 04
267 .PP
268 The implementation places no intrinsic limit on the per\-process maximum
269 number of shared memory segments
270 .RB ( SHMSEG ).
271 .SH "CONFORMING TO"
272 SVr4, POSIX.1-2001.
273 .\" SVr4 documents an additional error condition EMFILE.
274
275 In SVID 3 (or perhaps earlier)
276 the type of the \fIshmaddr\fP argument was changed from
277 .I "char *"
278 into
279 .IR "const void *" ,
280 and the returned type of
281 .BR shmat ()
282 from
283 .I "char *"
284 into
285 .IR "void *" .
286 (Linux libc4 and libc5 have the
287 .I "char *"
288 prototypes; glibc2 has
289 .IR "void *" .)
290 .SH "SEE ALSO"
291 .BR brk (2),
292 .BR mmap (2),
293 .BR shmctl (2),
294 .BR shmget (2),
295 .BR capabilities (7),
296 .BR svipc (7)