2 .\" Copyright (C) 2002, 2020 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH shm_open 3 (date) "Linux man-pages (unreleased)"
8 shm_open, shm_unlink \- create/open or unlink POSIX shared memory objects
11 .RI ( librt ", " \-lrt )
14 .B #include <sys/mman.h>
15 .BR "#include <sys/stat.h>" " /* For mode constants */"
16 .BR "#include <fcntl.h>" " /* For O_* constants */"
18 .BI "int shm_open(const char *" name ", int " oflag ", mode_t " mode );
19 .BI "int shm_unlink(const char *" name );
23 creates and opens a new, or opens an existing, POSIX shared memory object.
24 A POSIX shared memory object is in effect a handle which can
25 be used by unrelated processes to
27 the same region of shared memory.
30 function performs the converse operation,
31 removing an object previously created by
36 is analogous to that of
39 specifies the shared memory object to be created or opened.
41 a shared memory object should be identified by a name of the form
43 that is, a null-terminated string of up to
45 (i.e., 255) characters consisting of an initial slash,
46 .\" glibc allows the initial slash to be omitted, and makes
47 .\" multiple initial slashes equivalent to a single slash.
48 .\" This differs from the implementation of POSIX message queues.
49 followed by one or more characters, none of which are slashes.
50 .\" glibc allows subdirectory components in the name, in which
51 .\" case the subdirectory must exist under /dev/shm, and allow the
52 .\" required permissions if a user wants to create a shared memory
53 .\" object in that subdirectory.
56 is a bit mask created by ORing together exactly one of
60 and any of the other flags listed here:
63 Open the object for read access.
64 A shared memory object opened in this way can be
71 Open the object for read-write access.
74 Create the shared memory object if it does not exist.
75 The user and group ownership of the object are taken
76 from the corresponding effective IDs of the calling process,
77 .\" In truth it is actually the filesystem IDs on Linux, but these
78 .\" are nearly always the same as the effective IDs. (MTK, Jul 05)
80 permission bits are set according to the low-order 9 bits of
82 except that those bits set in the process file mode
85 are cleared for the new object.
86 A set of macro constants which can be used to define
90 (Symbolic definitions of these constants can be obtained by including
93 A new shared memory object initially has zero length\[em]the size of the
94 object can be set using
96 The newly allocated bytes of a shared memory
97 object are automatically initialized to 0.
102 was also specified, and a shared memory object with the given
104 already exists, return an error.
105 The check for the existence of the object, and its creation if it
106 does not exist, are performed atomically.
109 If the shared memory object already exists, truncate it to zero bytes.
111 Definitions of these flag values can be obtained by including
114 On successful completion
116 returns a new file descriptor referring to the shared memory object.
117 This file descriptor is guaranteed to be the lowest-numbered file descriptor
118 not previously opened within the process.
123 is set for the file descriptor.
125 The file descriptor is normally used in subsequent calls
128 (for a newly created object) and
132 the file descriptor may be closed without affecting the memory mapping.
139 it removes a shared memory object name, and, once all processes
140 have unmapped the object, deallocates and
141 destroys the contents of the associated memory region.
146 an object with the same
150 was specified, in which case a new, distinct object is created).
154 returns a file descriptor (a nonnegative integer).
158 On failure, both functions return \-1 and set
160 to indicate the error.
166 the shared memory object was denied.
169 Permission was denied to
176 was specified and the caller does not have write permission on the object.
185 and the shared memory object specified by
197 The per-process limit on the number of open file descriptors has been reached.
206 The system-wide limit on the total number of open files has been reached.
209 An attempt was made to
213 that did not exist, and
218 An attempt was to made to
224 These functions are provided in glibc 2.2 and later.
226 For an explanation of the terms used in this section, see
234 Interface Attribute Value
238 T} Thread safety MT-Safe locale
244 POSIX.1-2001, POSIX.1-2008.
246 POSIX.1-2001 says that the group ownership of a newly created shared
247 memory object is set to either the calling process's effective group ID
248 or "a system default group ID".
249 POSIX.1-2008 says that the group ownership
250 may be set to either the calling process's effective group ID
251 or, if the object is visible in the filesystem,
252 the group ID of the parent directory.
254 POSIX leaves the behavior of the combination of
259 On Linux, this will successfully truncate an existing
260 shared memory object\[em]this may not be so on other UNIX systems.
262 The POSIX shared memory object implementation on Linux makes use
265 filesystem that is normally mounted under
268 The programs below employ POSIX shared memory and POSIX unnamed semaphores
269 to exchange a piece of data.
270 The "bounce" program (which must be run first) raises the case
271 of a string that is placed into the shared memory by the "send" program.
272 Once the data has been modified, the "send" program then prints
273 the contents of the modified shared memory.
274 An example execution of the two programs is the following:
278 $ \fB./pshm_ucase_bounce /myshm &\fP
280 $ \fB./pshm_ucase_send /myshm hello\fP
285 Further detail about these programs is provided below.
287 .SS Program source: pshm_ucase.h
288 The following header file is included by both programs below.
289 Its primary purpose is to define a structure that will be imposed
290 on the memory object that is shared between the two programs.
293 .\" SRC BEGIN (pshm_ucase.h)
296 #include <semaphore.h>
299 #include <sys/mman.h>
300 #include <sys/stat.h>
303 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
306 #define BUF_SIZE 1024 /* Maximum size for exchanged string */
308 /* Define a structure that will be imposed on the shared
312 sem_t sem1; /* POSIX unnamed semaphore */
313 sem_t sem2; /* POSIX unnamed semaphore */
314 size_t cnt; /* Number of bytes used in \[aq]buf\[aq] */
315 char buf[BUF_SIZE]; /* Data being transferred */
321 .SS Program source: pshm_ucase_bounce.c
322 The "bounce" program creates a new shared memory object with the name
323 given in its command-line argument and sizes the object to
324 match the size of the
326 structure defined in the header file.
327 It then maps the object into the process's address space,
328 and initializes two POSIX semaphores inside the object to 0.
330 After the "send" program has posted the first of the semaphores,
331 the "bounce" program upper cases the data that has been placed
332 in the memory by the "send" program and then posts the second semaphore
333 to tell the "send" program that it may now access the shared memory.
336 .\" SRC BEGIN (pshm_ucase_bounce.c)
338 /* pshm_ucase_bounce.c
340 Licensed under GNU General Public License v2 or later.
344 #include "pshm_ucase.h"
347 main(int argc, char *argv[])
354 fprintf(stderr, "Usage: %s /shm\-path\en", argv[0]);
360 /* Create shared memory object and set its size to the size
363 fd = shm_open(shmpath, O_CREAT | O_EXCL | O_RDWR, 0600);
367 if (ftruncate(fd, sizeof(struct shmbuf)) == \-1)
368 errExit("ftruncate");
370 /* Map the object into the caller\[aq]s address space. */
372 shmp = mmap(NULL, sizeof(*shmp), PROT_READ | PROT_WRITE,
374 if (shmp == MAP_FAILED)
377 /* Initialize semaphores as process\-shared, with value 0. */
379 if (sem_init(&shmp\->sem1, 1, 0) == \-1)
380 errExit("sem_init\-sem1");
381 if (sem_init(&shmp\->sem2, 1, 0) == \-1)
382 errExit("sem_init\-sem2");
384 /* Wait for \[aq]sem1\[aq] to be posted by peer before touching
387 if (sem_wait(&shmp\->sem1) == \-1)
390 /* Convert data in shared memory into upper case. */
392 for (size_t j = 0; j < shmp\->cnt; j++)
393 shmp\->buf[j] = toupper((unsigned char) shmp\->buf[j]);
395 /* Post \[aq]sem2\[aq] to tell the peer that it can now
396 access the modified data in shared memory. */
398 if (sem_post(&shmp\->sem2) == \-1)
401 /* Unlink the shared memory object. Even if the peer process
402 is still using the object, this is okay. The object will
403 be removed only after all open references are closed. */
413 .SS Program source: pshm_ucase_send.c
414 The "send" program takes two command-line arguments:
415 the pathname of a shared memory object previously created by the "bounce"
416 program and a string that is to be copied into that object.
418 The program opens the shared memory object
419 and maps the object into its address space.
420 It then copies the data specified in its second argument
421 into the shared memory,
422 and posts the first semaphore,
423 which tells the "bounce" program that it can now access that data.
424 After the "bounce" program posts the second semaphore,
425 the "send" program prints the contents of the shared memory
429 .\" SRC BEGIN (pshm_ucase_send.c)
433 Licensed under GNU General Public License v2 or later.
437 #include "pshm_ucase.h"
440 main(int argc, char *argv[])
443 char *shmpath, *string;
448 fprintf(stderr, "Usage: %s /shm\-path string\en", argv[0]);
454 len = strlen(string);
456 if (len > BUF_SIZE) {
457 fprintf(stderr, "String is too long\en");
461 /* Open the existing shared memory object and map it
462 into the caller\[aq]s address space. */
464 fd = shm_open(shmpath, O_RDWR, 0);
468 shmp = mmap(NULL, sizeof(*shmp), PROT_READ | PROT_WRITE,
470 if (shmp == MAP_FAILED)
473 /* Copy data into the shared memory object. */
476 memcpy(&shmp\->buf, string, len);
478 /* Tell peer that it can now access shared memory. */
480 if (sem_post(&shmp\->sem1) == \-1)
483 /* Wait until peer says that it has finished accessing
484 the shared memory. */
486 if (sem_wait(&shmp\->sem2) == \-1)
489 /* Write modified data in shared memory to standard output. */
491 write(STDOUT_FILENO, &shmp\->buf, len);
492 write(STDOUT_FILENO, "\en", 1);
506 .BR memfd_create (2),