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