1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
2 .\" and Copyright 2020 Michael Kerrisk <mtk.manpages@gmail.com>
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.
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.
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
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
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
40 .TH SHMOP 2 2021-03-22 "Linux" "Linux Programmer's Manual"
42 shmat, shmdt \- System V shared memory operations
45 .B #include <sys/shm.h>
47 .BI "void *shmat(int " shmid ", const void *" shmaddr ", int " shmflg );
48 .BI "int shmdt(const void *" shmaddr );
53 attaches the System\ V shared memory segment identified by
55 to the address space of the calling process.
56 The attaching address is specified by
58 with one of the following criteria:
63 the system chooses a suitable (unused) page-aligned address to attach
73 the attach occurs at the address equal to
75 rounded down to the nearest multiple of
80 must be a page-aligned address at which the attach occurs.
84 the following flags may be specified in the
88 .BR SHM_EXEC " (Linux-specific; since Linux 2.6.9)"
89 Allow the contents of the segment to be executed.
90 The caller must have execute permission on the segment.
93 Attach the segment for read-only access.
94 The process must have read permission for the segment.
95 If this flag is not specified,
96 the segment is attached for read and write access,
97 and the process must have read and write permission for the segment.
98 There is no notion of a write-only shared memory segment.
100 .BR SHM_REMAP " (Linux-specific)"
102 that the mapping of the segment should replace
103 any existing mapping in the range starting at
105 and continuing for the size of the segment.
108 error would result if a mapping already exists in this address range.)
115 value of the calling process is not altered by the attach.
116 The segment will automatically be detached at process exit.
117 The same segment may be attached as a read and as a read-write
118 one, and more than once, in the process's address space.
122 call updates the members of the
126 associated with the shared memory segment as follows:
129 is set to the current time.
132 is set to the process-ID of the calling process.
135 is incremented by one.
139 detaches the shared memory segment located at the address specified by
141 from the address space of the calling process.
142 The to-be-detached segment must be currently
145 equal to the value returned by the attaching
151 call, the system updates the members of the
153 structure associated with the shared memory segment as follows:
156 is set to the current time.
159 is set to the process-ID of the calling process.
162 is decremented by one.
163 If it becomes 0 and the segment is marked for deletion,
164 the segment is deleted.
168 returns the address of the attached shared memory segment; on error,
172 is set to indicate the error.
176 returns 0; on error \-1 is returned, and
178 is set to indicate the error.
181 can fail with one of the following errors:
184 The calling process does not have the required permissions for
185 the requested attach type, and does not have the
187 capability in the user namespace that governs its IPC namespace.
190 \fIshmid\fP points to a removed identifier.
195 value, unaligned (i.e., not page-aligned and \fBSHM_RND\fP was not
196 specified) or invalid
198 value, or can't attach segment at
207 Could not allocate memory for the descriptor or for the page tables.
210 can fail with one of the following errors:
213 There is no shared memory segment attached at
216 .\" The following since 2.6.17-rc1:
218 is not aligned on a page boundary.
220 POSIX.1-2001, POSIX.1-2008, SVr4.
221 .\" SVr4 documents an additional error condition EMFILE.
223 In SVID 3 (or perhaps earlier),
224 the type of the \fIshmaddr\fP argument was changed from
227 .IR "const void\ *" ,
228 and the returned type of
237 the child inherits the attached shared memory segments.
241 all attached shared memory segments are detached from the process.
245 all attached shared memory segments are detached from the process.
252 is the preferred, portable way of attaching a shared memory segment.
253 Be aware that the shared memory segment attached in this way
254 may be attached at different addresses in different processes.
255 Therefore, any pointers maintained within the shared memory must be
256 made relative (typically to the starting address of the segment),
257 rather than absolute.
259 On Linux, it is possible to attach a shared memory segment even if it
260 is already marked to be deleted.
261 However, POSIX.1 does not specify this behavior and
262 many other implementations do not support it.
264 The following system parameter affects
268 Segment low boundary address multiple.
269 When explicitly specifying an attach address in a call to
271 the caller should ensure that the address is a multiple of this value.
272 This is necessary on some architectures,
273 in order either to ensure good CPU cache performance or to ensure that
274 different attaches of the same segment have consistent views
275 within the CPU cache.
277 is normally some multiple of the system page size.
278 (On many Linux architectures,
280 is the same as the system page size.)
282 The implementation places no intrinsic per-process limit on the
283 number of shared memory segments
286 The two programs shown below exchange a string using a shared memory segment.
287 Further details about the programs are given below.
288 First, we show a shell session demonstrating their use.
290 In one terminal window, we run the "reader" program,
291 which creates a System V shared memory segment and a System V semaphore set.
292 The program prints out the IDs of the created objects,
293 and then waits for the semaphore to change value.
297 $ \fB./svshm_string_read\fP
298 shmid = 1114194; semid = 15
302 In another terminal window, we run the "writer" program.
303 The "writer" program takes three command-line arguments:
304 the IDs of the shared memory segment and semaphore set created
305 by the "reader", and a string.
306 It attaches the existing shared memory segment,
307 copies the string to the shared memory, and modifies the semaphore value.
311 $ \fB./svshm_string_write 1114194 15 \(aqHello, world\(aq\fP
315 Returning to the terminal where the "reader" is running,
316 we see that the program has ceased waiting on the semaphore
317 and has printed the string that was copied into the
318 shared memory segment by the writer:
326 .SS Program source: svshm_string.h
327 The following header file is included by the "reader" and "writer" programs.
331 #include <sys/types.h>
339 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
342 union semun { /* Used in calls to semctl() */
344 struct semid_ds * buf;
345 unsigned short * array;
346 #if defined(__linux__)
347 struct seminfo * __buf;
351 #define MEM_SIZE 4096
355 .SS Program source: svshm_string_read.c
356 The "reader" program creates a shared memory segment and a semaphore set
357 containing one semaphore.
358 It then attaches the shared memory object into its address space
359 and initializes the semaphore value to 1.
360 Finally, the program waits for the semaphore value to become 0,
361 and afterwards prints the string that has been copied into the
362 shared memory segment by the "writer".
366 /* svshm_string_read.c
368 Licensed under GNU General Public License v2 or later.
370 #include "svshm_string.h"
373 main(int argc, char *argv[])
376 union semun arg, dummy;
380 /* Create shared memory and semaphore set containing one
383 shmid = shmget(IPC_PRIVATE, MEM_SIZE, IPC_CREAT | 0600);
387 semid = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
391 /* Attach shared memory into our address space. */
393 addr = shmat(shmid, NULL, SHM_RDONLY);
394 if (addr == (void *) \-1)
397 /* Initialize semaphore 0 in set with value 1. */
400 if (semctl(semid, 0, SETVAL, arg) == \-1)
403 printf("shmid = %d; semid = %d\en", shmid, semid);
405 /* Wait for semaphore value to become 0. */
411 if (semop(semid, &sop, 1) == \-1)
414 /* Print the string from shared memory. */
416 printf("%s\en", addr);
418 /* Remove shared memory and semaphore set. */
420 if (shmctl(shmid, IPC_RMID, NULL) == \-1)
422 if (semctl(semid, 0, IPC_RMID, dummy) == \-1)
430 .SS Program source: svshm_string_write.c
431 The writer program takes three command-line arguments:
432 the IDs of the shared memory segment and semaphore set
433 that have already been created by the "reader", and a string.
434 It attaches the shared memory segment into its address space,
435 and then decrements the semaphore value to 0 in order to inform the
436 "reader" that it can now examine the contents of the shared memory.
440 /* svshm_string_write.c
442 Licensed under GNU General Public License v2 or later.
444 #include "svshm_string.h"
447 main(int argc, char *argv[])
455 fprintf(stderr, "Usage: %s shmid semid string\en", argv[0]);
459 len = strlen(argv[3]) + 1; /* +1 to include trailing \(aq\e0\(aq */
460 if (len > MEM_SIZE) {
461 fprintf(stderr, "String is too big!\en");
465 /* Get object IDs from command\-line. */
467 shmid = atoi(argv[1]);
468 semid = atoi(argv[2]);
470 /* Attach shared memory into our address space and copy string
471 (including trailing null byte) into memory. */
473 addr = shmat(shmid, NULL, 0);
474 if (addr == (void *) \-1)
477 memcpy(addr, argv[3], len);
479 /* Decrement semaphore to 0. */
485 if (semop(semid, &sop, 1) == \-1)
497 .BR capabilities (7),
498 .BR shm_overview (7),