]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/shmop.2
mmap.2: srcfix: note kernel commit that caused MAP_POPULATE | MAP_NONBLOCK to be...
[thirdparty/man-pages.git] / man2 / shmop.2
1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
2 .\" and Copyright 2020 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" Modified Sun Nov 28 17:06:19 1993, Rik Faith (faith@cs.unc.edu)
27 .\" with material from Luigi P. Bai (lpb@softint.com)
28 .\" Portions Copyright 1993 Luigi P. Bai
29 .\" Modified Tue Oct 22 22:04:23 1996 by Eric S. Raymond <esr@thyrsus.com>
30 .\" Modified, 5 Jan 2002, Michael Kerrisk <mtk.manpages@gmail.com>
31 .\" Modified, 19 Sep 2002, Michael Kerrisk <mtk.manpages@gmail.com>
32 .\" Added SHM_REMAP flag description
33 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
34 .\" Added notes on capability requirements
35 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
36 .\" Language and formatting clean-ups
37 .\" Changed wording and placement of sentence regarding attachment
38 .\" of segments marked for destruction
39 .\"
40 .TH SHMOP 2 2020-04-11 "Linux" "Linux Programmer's Manual"
41 .SH NAME
42 shmat, shmdt \- System V shared memory operations
43 .SH SYNOPSIS
44 .nf
45 .B #include <sys/types.h>
46 .B #include <sys/shm.h>
47 .PP
48 .BI "void *shmat(int " shmid ", const void *" shmaddr ", int " shmflg );
49 .PP
50 .BI "int shmdt(const void *" shmaddr );
51 .fi
52 .SH DESCRIPTION
53 .SS shmat()
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 .IP \(bu 2
62 If
63 .I shmaddr
64 is NULL,
65 the system chooses a suitable (unused) page-aligned address to attach
66 the segment.
67 .IP \(bu
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 .IP \(bu
80 Otherwise,
81 .I shmaddr
82 must be a page-aligned address at which the attach occurs.
83 .PP
84 In addition to
85 .BR SHM_RND ,
86 the following flags may be specified in the
87 .I shmflg
88 bit-mask argument:
89 .TP
90 .BR SHM_EXEC " (Linux-specific; since Linux 2.6.9)"
91 Allow the contents of the segment to be executed.
92 The caller must have execute permission on the segment.
93 .TP
94 .BR SHM_RDONLY
95 Attach the segment for read-only access.
96 The process must have read permission for the segment.
97 If this flag is not specified,
98 the segment is attached for read and write access,
99 and the process must have read and write permission for the segment.
100 There is no notion of a write-only shared memory segment.
101 .TP
102 .BR SHM_REMAP " (Linux-specific)"
103 This flag specifies
104 that the mapping of the segment should replace
105 any existing mapping in the range starting at
106 .I shmaddr
107 and continuing for the size of the segment.
108 (Normally, an
109 .B EINVAL
110 error would result if a mapping already exists in this address range.)
111 In this case,
112 .I shmaddr
113 must not be NULL.
114 .PP
115 The
116 .BR brk (2)
117 value of the calling process is not altered by the attach.
118 The segment will automatically be detached at process exit.
119 The same segment may be attached as a read and as a read-write
120 one, and more than once, in the process's address space.
121 .PP
122 A successful
123 .BR shmat ()
124 call updates the members of the
125 .I shmid_ds
126 structure (see
127 .BR shmctl (2))
128 associated with the shared memory segment as follows:
129 .IP \(bu 2
130 .I shm_atime
131 is set to the current time.
132 .IP \(bu
133 .I shm_lpid
134 is set to the process-ID of the calling process.
135 .IP \(bu
136 .I shm_nattch
137 is incremented by one.
138 .\"
139 .SS shmdt()
140 .BR shmdt ()
141 detaches the shared memory segment located at the address specified by
142 .I shmaddr
143 from the address space of the calling process.
144 The to-be-detached segment must be currently
145 attached with
146 .I shmaddr
147 equal to the value returned by the attaching
148 .BR shmat ()
149 call.
150 .PP
151 On a successful
152 .BR shmdt ()
153 call, the system updates the members of the
154 .I shmid_ds
155 structure associated with the shared memory segment as follows:
156 .IP \(bu 2
157 .I shm_dtime
158 is set to the current time.
159 .IP \(bu
160 .I shm_lpid
161 is set to the process-ID of the calling process.
162 .IP \(bu
163 .I shm_nattch
164 is decremented by one.
165 If it becomes 0 and the segment is marked for deletion,
166 the segment is deleted.
167 .SH RETURN VALUE
168 On success,
169 .BR shmat ()
170 returns the address of the attached shared memory segment; on error,
171 .I (void\ *)\ \-1
172 is returned, and
173 .I errno
174 is set to indicate the cause of the error.
175 .PP
176 On success,
177 .BR shmdt ()
178 returns 0; on error \-1 is returned, and
179 .I errno
180 is set to indicate the cause of the error.
181 .SH ERRORS
182 When
183 .BR shmat ()
184 fails,
185 .I errno
186 is set to one of the following:
187 .TP
188 .B EACCES
189 The calling process does not have the required permissions for
190 the requested attach type, and does not have the
191 .B CAP_IPC_OWNER
192 capability in the user namespace that governs its IPC namespace.
193 .TP
194 .B EIDRM
195 \fIshmid\fP points to a removed identifier.
196 .TP
197 .B EINVAL
198 Invalid
199 .I shmid
200 value, unaligned (i.e., not page-aligned and \fBSHM_RND\fP was not
201 specified) or invalid
202 .I shmaddr
203 value, or can't attach segment at
204 .IR shmaddr ,
205 or
206 .B SHM_REMAP
207 was specified and
208 .I shmaddr
209 was NULL.
210 .TP
211 .B ENOMEM
212 Could not allocate memory for the descriptor or for the page tables.
213 .PP
214 When
215 .BR shmdt ()
216 fails,
217 .I errno
218 is set as follows:
219 .TP
220 .B EINVAL
221 There is no shared memory segment attached at
222 .IR shmaddr ;
223 or,
224 .\" The following since 2.6.17-rc1:
225 .I shmaddr
226 is not aligned on a page boundary.
227 .SH CONFORMING TO
228 POSIX.1-2001, POSIX.1-2008, SVr4.
229 .\" SVr4 documents an additional error condition EMFILE.
230 .PP
231 In SVID 3 (or perhaps earlier),
232 the type of the \fIshmaddr\fP argument was changed from
233 .I "char\ *"
234 into
235 .IR "const void\ *" ,
236 and the returned type of
237 .BR shmat ()
238 from
239 .I "char\ *"
240 into
241 .IR "void\ *" .
242 .SH NOTES
243 .PP
244 After a
245 .BR fork (2),
246 the child inherits the attached shared memory segments.
247 .PP
248 After an
249 .BR execve (2),
250 all attached shared memory segments are detached from the process.
251 .PP
252 Upon
253 .BR _exit (2),
254 all attached shared memory segments are detached from the process.
255 .PP
256 Using
257 .BR shmat ()
258 with
259 .I shmaddr
260 equal to NULL
261 is the preferred, portable way of attaching a shared memory segment.
262 Be aware that the shared memory segment attached in this way
263 may be attached at different addresses in different processes.
264 Therefore, any pointers maintained within the shared memory must be
265 made relative (typically to the starting address of the segment),
266 rather than absolute.
267 .PP
268 On Linux, it is possible to attach a shared memory segment even if it
269 is already marked to be deleted.
270 However, POSIX.1 does not specify this behavior and
271 many other implementations do not support it.
272 .PP
273 The following system parameter affects
274 .BR shmat ():
275 .TP
276 .B SHMLBA
277 Segment low boundary address multiple.
278 When explicitly specifying an attach address in a call to
279 .BR shmat (),
280 the caller should ensure that the address is a multiple of this value.
281 This is necessary on some architectures,
282 in order either to ensure good CPU cache performance or to ensure that
283 different attaches of the same segment have consistent views
284 within the CPU cache.
285 .B SHMLBA
286 is normally some multiple of the system page size.
287 (On many Linux architectures,
288 .B SHMLBA
289 is the same as the system page size.)
290 .PP
291 The implementation places no intrinsic per-process limit on the
292 number of shared memory segments
293 .RB ( SHMSEG ).
294 .SH EXAMPLE
295 .PP
296 The two programs shown below exchange a string using a shared memory segment.
297 Further details about the programs are given below.
298 First, we show a shell session demonstrating their use.
299 .PP
300 In one terminal window, we run the "reader" program,
301 which creates a System V shared memory segment and a System V semaphore set.
302 The program prints out the IDs of the created objects,
303 and then waits for the semaphore to change value.
304 .PP
305 .in +4n
306 .EX
307 $ \fB./svshm_string_read \fP
308 shmid = 1114194; semid = 15
309 .EE
310 .in
311 .PP
312 In another terminal window, we run the "writer" program.
313 The "writer" program takes three command-line arguments:
314 the IDs of the shared memory segment and semaphore set created
315 by the "reader", and a string.
316 It attaches the existing shared memory segment,
317 copies the string to the shared memory, and modifies the semaphore value.
318 .PP
319 .in +4n
320 .EX
321 $ \fB./svshm_string_write 1114194 15 \(aqHello, world\(aq\fP
322 .EE
323 .in
324 .PP
325 Returning to the terminal where the "reader" is running,
326 we see that the program has ceased waiting on the semaphore
327 and has printed the string that was copied into the
328 shared memory segment by the writer:
329 .PP
330 .in +4n
331 .EX
332 Hello, world
333 .EE
334 .in
335 .PP
336 .\"
337 .SS Program source: svshm_string.h
338 The following header file is included by the "reader" and "writer" programs.
339 .PP
340 .in +4n
341 .EX
342 #include <sys/types.h>
343 #include <sys/ipc.h>
344 #include <sys/shm.h>
345 #include <sys/sem.h>
346 #include <stdio.h>
347 #include <stdlib.h>
348 #include <string.h>
349
350 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
351 } while (0)
352
353 union semun { /* Used in calls to semctl() */
354 int val;
355 struct semid_ds * buf;
356 unsigned short * array;
357 #if defined(__linux__)
358 struct seminfo * __buf;
359 #endif
360 };
361
362 #define MEM_SIZE 4096
363 .EE
364 .in
365 .\"
366 .SS Program source: svshm_string_read.c
367 The "reader" program creates a shared memory segment and a semaphore set
368 containing one semaphore.
369 It then attaches the shared memory object into its address space
370 and initializes the semaphore value to 1.
371 Finally, the program waits for the semaphore value to become 0,
372 and afterwards prints the string that has been copied into the
373 shared memory segment by the "writer".
374 .PP
375 .in +4n
376 .EX
377 /* svshm_string_read.c
378
379 Licensed under GNU General Public License v2 or later.
380 */
381 #include "svshm_string.h"
382
383 int
384 main(int argc, char *argv[])
385 {
386 int semid, shmid;
387 union semun arg, dummy;
388 struct sembuf sop;
389 char *addr;
390
391 /* Create shared memory and semaphore set containing one
392 semaphore */
393
394 shmid = shmget(IPC_PRIVATE, MEM_SIZE, IPC_CREAT | 0600);
395 if (shmid == \-1)
396 errExit("shmget");
397
398 semid = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
399 if (shmid == \-1)
400 errExit("shmget");
401
402 /* Attach shared memory into our address space */
403
404 addr = shmat(shmid, NULL, SHM_RDONLY);
405 if (addr == (void *) \-1)
406 errExit("shmat");
407
408 /* Initialize semaphore 0 in set with value 1 */
409
410 arg.val = 1;
411 if (semctl(semid, 0, SETVAL, arg) == \-1)
412 errExit("semctl");
413
414 printf("shmid = %d; semid = %d\en", shmid, semid);
415
416 /* Wait for semaphore value to become 0 */
417
418 sop.sem_num = 0;
419 sop.sem_op = 0;
420 sop.sem_flg = 0;
421
422 if (semop(semid, &sop, 1) == \-1)
423 errExit("semop");
424
425 /* Print the string from shared memory */
426
427 printf("%s\en", addr);
428
429 /* Remove shared memory and semaphore set */
430
431 if (shmctl(shmid, IPC_RMID, NULL) == \-1)
432 errExit("shmctl");
433 if (semctl(semid, 0, IPC_RMID, dummy) == \-1)
434 errExit("semctl");
435
436 exit(EXIT_SUCCESS);
437 }
438 .EE
439 .in
440 .\"
441 .SS Program source: svshm_string_write.c
442 The writer program takes three command-line arguments:
443 the IDs of the shared memory segment and semaphore set
444 that have already been created by the "reader", and a string.
445 It attaches the shared memory segment into its address space,
446 and then decrements the semaphore value to 0 in order to inform the
447 "reader" that it can now examine the contents of the shared memory.
448 .PP
449 .in +4n
450 .EX
451 /* svshm_string_write.c
452
453 Licensed under GNU General Public License v2 or later.
454 */
455 #include "svshm_string.h"
456
457 int
458 main(int argc, char *argv[])
459 {
460 int semid, shmid;
461 struct sembuf sop;
462 char *addr;
463 size_t len;
464
465 if (argc != 4) {
466 fprintf(stderr, "Usage: %s shmid semid string\en", argv[0]);
467 exit(EXIT_FAILURE);
468 }
469
470 len = strlen(argv[3]) + 1; /* +1 to include trailing \(aq\e0\(aq */
471 if (len > MEM_SIZE) {
472 fprintf(stderr, "String is too big!\en");
473 exit(EXIT_FAILURE);
474 }
475
476 /* Get object IDs from command\-line */
477
478 shmid = atoi(argv[1]);
479 semid = atoi(argv[2]);
480
481 /* Attach shared memory into our address space and copy string
482 (including trailing null byte) into memory. */
483
484 addr = shmat(shmid, NULL, 0);
485 if (addr == (void *) \-1)
486 errExit("shmat");
487
488 memcpy(addr, argv[3], len);
489
490 /* Decrement semaphore t0 0 */
491
492 sop.sem_num = 0;
493 sop.sem_op = \-1;
494 sop.sem_flg = 0;
495
496 if (semop(semid, &sop, 1) == \-1)
497 errExit("semop");
498
499 exit(EXIT_SUCCESS);
500 }
501 .EE
502 .in
503 .SH SEE ALSO
504 .BR brk (2),
505 .BR mmap (2),
506 .BR shmctl (2),
507 .BR shmget (2),
508 .BR capabilities (7),
509 .BR shm_overview (7),
510 .BR sysvipc (7)