1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
2 .\" and Copyright 2020 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" Modified Sun Nov 28 17:06:19 1993, Rik Faith (faith@cs.unc.edu)
7 .\" with material from Luigi P. Bai (lpb@softint.com)
8 .\" Portions Copyright 1993 Luigi P. Bai
9 .\" Modified Tue Oct 22 22:04:23 1996 by Eric S. Raymond <esr@thyrsus.com>
10 .\" Modified, 5 Jan 2002, Michael Kerrisk <mtk.manpages@gmail.com>
11 .\" Modified, 19 Sep 2002, Michael Kerrisk <mtk.manpages@gmail.com>
12 .\" Added SHM_REMAP flag description
13 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
14 .\" Added notes on capability requirements
15 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
16 .\" Language and formatting clean-ups
17 .\" Changed wording and placement of sentence regarding attachment
18 .\" of segments marked for destruction
20 .TH SHMOP 2 (date) "Linux man-pages (unreleased)"
22 shmat, shmdt \- System V shared memory operations
25 .RI ( libc ", " \-lc )
28 .B #include <sys/shm.h>
30 .BI "void *shmat(int " shmid ", const void *" shmaddr ", int " shmflg );
31 .BI "int shmdt(const void *" shmaddr );
36 attaches the System\ V shared memory segment identified by
38 to the address space of the calling process.
39 The attaching address is specified by
41 with one of the following criteria:
46 the system chooses a suitable (unused) page-aligned address to attach
56 the attach occurs at the address equal to
58 rounded down to the nearest multiple of
63 must be a page-aligned address at which the attach occurs.
67 the following flags may be specified in the
71 .BR SHM_EXEC " (Linux-specific; since Linux 2.6.9)"
72 Allow the contents of the segment to be executed.
73 The caller must have execute permission on the segment.
76 Attach the segment for read-only access.
77 The process must have read permission for the segment.
78 If this flag is not specified,
79 the segment is attached for read and write access,
80 and the process must have read and write permission for the segment.
81 There is no notion of a write-only shared memory segment.
83 .BR SHM_REMAP " (Linux-specific)"
85 that the mapping of the segment should replace
86 any existing mapping in the range starting at
88 and continuing for the size of the segment.
91 error would result if a mapping already exists in this address range.)
98 value of the calling process is not altered by the attach.
99 The segment will automatically be detached at process exit.
100 The same segment may be attached as a read and as a read-write
101 one, and more than once, in the process's address space.
105 call updates the members of the
109 associated with the shared memory segment as follows:
112 is set to the current time.
115 is set to the process-ID of the calling process.
118 is incremented by one.
122 detaches the shared memory segment located at the address specified by
124 from the address space of the calling process.
125 The to-be-detached segment must be currently
128 equal to the value returned by the attaching
134 call, the system updates the members of the
136 structure associated with the shared memory segment as follows:
139 is set to the current time.
142 is set to the process-ID of the calling process.
145 is decremented by one.
146 If it becomes 0 and the segment is marked for deletion,
147 the segment is deleted.
151 returns the address of the attached shared memory segment; on error,
155 is set to indicate the error.
159 returns 0; on error \-1 is returned, and
161 is set to indicate the error.
164 can fail with one of the following errors:
167 The calling process does not have the required permissions for
168 the requested attach type, and does not have the
170 capability in the user namespace that governs its IPC namespace.
173 \fIshmid\fP points to a removed identifier.
178 value, unaligned (i.e., not page-aligned and \fBSHM_RND\fP was not
179 specified) or invalid
181 value, or can't attach segment at
190 Could not allocate memory for the descriptor or for the page tables.
193 can fail with one of the following errors:
196 There is no shared memory segment attached at
199 .\" The following since 2.6.17-rc1:
201 is not aligned on a page boundary.
203 POSIX.1-2001, POSIX.1-2008, SVr4.
204 .\" SVr4 documents an additional error condition EMFILE.
206 In SVID 3 (or perhaps earlier),
207 the type of the \fIshmaddr\fP argument was changed from
210 .IR "const void\ *" ,
211 and the returned type of
220 the child inherits the attached shared memory segments.
224 all attached shared memory segments are detached from the process.
228 all attached shared memory segments are detached from the process.
235 is the preferred, portable way of attaching a shared memory segment.
236 Be aware that the shared memory segment attached in this way
237 may be attached at different addresses in different processes.
238 Therefore, any pointers maintained within the shared memory must be
239 made relative (typically to the starting address of the segment),
240 rather than absolute.
242 On Linux, it is possible to attach a shared memory segment even if it
243 is already marked to be deleted.
244 However, POSIX.1 does not specify this behavior and
245 many other implementations do not support it.
247 The following system parameter affects
251 Segment low boundary address multiple.
252 When explicitly specifying an attach address in a call to
254 the caller should ensure that the address is a multiple of this value.
255 This is necessary on some architectures,
256 in order either to ensure good CPU cache performance or to ensure that
257 different attaches of the same segment have consistent views
258 within the CPU cache.
260 is normally some multiple of the system page size.
261 (On many Linux architectures,
263 is the same as the system page size.)
265 The implementation places no intrinsic per-process limit on the
266 number of shared memory segments
269 The two programs shown below exchange a string using a shared memory segment.
270 Further details about the programs are given below.
271 First, we show a shell session demonstrating their use.
273 In one terminal window, we run the "reader" program,
274 which creates a System V shared memory segment and a System V semaphore set.
275 The program prints out the IDs of the created objects,
276 and then waits for the semaphore to change value.
280 $ \fB./svshm_string_read\fP
281 shmid = 1114194; semid = 15
285 In another terminal window, we run the "writer" program.
286 The "writer" program takes three command-line arguments:
287 the IDs of the shared memory segment and semaphore set created
288 by the "reader", and a string.
289 It attaches the existing shared memory segment,
290 copies the string to the shared memory, and modifies the semaphore value.
294 $ \fB./svshm_string_write 1114194 15 \(aqHello, world\(aq\fP
298 Returning to the terminal where the "reader" is running,
299 we see that the program has ceased waiting on the semaphore
300 and has printed the string that was copied into the
301 shared memory segment by the writer:
309 .SS Program source: svshm_string.h
310 The following header file is included by the "reader" and "writer" programs:
313 .\" SRC BEGIN (svshm_string.h)
317 Licensed under GNU General Public License v2 or later.
319 #include <sys/types.h>
327 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
330 union semun { /* Used in calls to semctl() */
332 struct semid_ds * buf;
333 unsigned short * array;
334 #if defined(__linux__)
335 struct seminfo * __buf;
339 #define MEM_SIZE 4096
344 .SS Program source: svshm_string_read.c
345 The "reader" program creates a shared memory segment and a semaphore set
346 containing one semaphore.
347 It then attaches the shared memory object into its address space
348 and initializes the semaphore value to 1.
349 Finally, the program waits for the semaphore value to become 0,
350 and afterwards prints the string that has been copied into the
351 shared memory segment by the "writer".
354 .\" SRC BEGIN (svshm_string_read.c)
356 /* svshm_string_read.c
358 Licensed under GNU General Public License v2 or later.
366 #include "svshm_string.h"
373 union semun arg, dummy;
376 /* Create shared memory and semaphore set containing one
379 shmid = shmget(IPC_PRIVATE, MEM_SIZE, IPC_CREAT | 0600);
383 semid = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
387 /* Attach shared memory into our address space. */
389 addr = shmat(shmid, NULL, SHM_RDONLY);
390 if (addr == (void *) \-1)
393 /* Initialize semaphore 0 in set with value 1. */
396 if (semctl(semid, 0, SETVAL, arg) == \-1)
399 printf("shmid = %d; semid = %d\en", shmid, semid);
401 /* Wait for semaphore value to become 0. */
407 if (semop(semid, &sop, 1) == \-1)
410 /* Print the string from shared memory. */
412 printf("%s\en", addr);
414 /* Remove shared memory and semaphore set. */
416 if (shmctl(shmid, IPC_RMID, NULL) == \-1)
418 if (semctl(semid, 0, IPC_RMID, dummy) == \-1)
427 .SS Program source: svshm_string_write.c
428 The writer program takes three command-line arguments:
429 the IDs of the shared memory segment and semaphore set
430 that have already been created by the "reader", and a string.
431 It attaches the shared memory segment into its address space,
432 and then decrements the semaphore value to 0 in order to inform the
433 "reader" that it can now examine the contents of the shared memory.
436 .\" SRC BEGIN (svshm_string_write.c)
438 /* svshm_string_write.c
440 Licensed under GNU General Public License v2 or later.
448 #include "svshm_string.h"
451 main(int argc, char *argv[])
459 fprintf(stderr, "Usage: %s shmid semid string\en", argv[0]);
463 len = strlen(argv[3]) + 1; /* +1 to include trailing \(aq\e0\(aq */
464 if (len > MEM_SIZE) {
465 fprintf(stderr, "String is too big!\en");
469 /* Get object IDs from command\-line. */
471 shmid = atoi(argv[1]);
472 semid = atoi(argv[2]);
474 /* Attach shared memory into our address space and copy string
475 (including trailing null byte) into memory. */
477 addr = shmat(shmid, NULL, 0);
478 if (addr == (void *) \-1)
481 memcpy(addr, argv[3], len);
483 /* Decrement semaphore to 0. */
489 if (semop(semid, &sop, 1) == \-1)
502 .BR capabilities (7),
503 .BR shm_overview (7),