shm_open(3) Library Functions Manual shm_open(3) shm_open, shm_unlink - / POSIX (librt -lrt) #include #include /* For mode constants */ #include /* For O_* constants */ int shm_open(const char *name, int oflag, mode_t mode); int shm_unlink(const char *name); shm_open() POSIX . POSIX mmap(2) . shm_unlink() shm_open(). shm_open() open(2). name . /somename NAME_MAX ( 255) . oflag ORing O_RDONLY O_RDWR : O_RDONLY . mmap(2) (PROT_READ). O_RDWR . O_CREAT . mode ( umask(2)) . mode open(2). ( .) -- ftruncate(2). 0. O_EXCL O_CREAT name . . O_TRUNC . . shm_open() . . FD_CLOEXEC ( fcntl(2)) . ftruncate(2) ( ) mmap(2). mmap(2) . shm_unlink() unlink(2): . shm_unlink() shm_open() name ( O_CREAT ). shm_open() ( ). shm_unlink() 0. -1 errno . EACCES shm_unlink() . EACCES shm_open() name mode O_TRUNC . EEXIST O_CREAT O_EXCL shm_open() name . EINVAL name shm_open() . EMFILE . ENAMETOOLONG name PATH_MAX. ENFILE . ENOENT shm_open() name O_CREAT. ENOENT shm_unlink() name . attributes(7). +----------------------+------------------------------------+---------------------------------------------------------------------------------------------+ | | | | +----------------------+------------------------------------+---------------------------------------------------------------------------------------------+ |shm_open(), | | (locale) (MT-Safe) | |shm_unlink() | | | +----------------------+------------------------------------+---------------------------------------------------------------------------------------------+ POSIX O_RDONLY O_TRUNC . -- UNIX . POSIX tmpfs(5) /dev/shm. POSIX.1-2008. glibc 2.2. POSIX.1-2001. POSIX.1-2001 " ". POSIX.1-2008 . POSIX POSIX . "bounce" ( ) "send". "send" . : $ ./pshm_ucase_bounce /myshm & [1] 270171 $ ./pshm_ucase_send /myshm hello; HELLO . : pshm_ucase.h . . #ifndef PSHM_UCASE_H #define PSHM_UCASE_H #include #include #include #include #include #define BUF_SIZE 1024 /* Maximum size for exchanged string */ /* Define a structure that will be imposed on the shared memory object */ struct shmbuf { sem_t sem1; /* POSIX unnamed semaphore */ sem_t sem2; /* POSIX unnamed semaphore */ size_t cnt; /* Number of bytes used in 'buf' */ char buf[BUF_SIZE]; /* Data being transferred */ }; #endif // include guard : pshm_ucase_bounce.c "bounce" shmbuf . POSIX 0. "send" "bounce" "send" "send" . /* pshm_ucase_bounce.c Licensed under GNU General Public License v2 or later. */ #include #include #include #include #include #include #include "pshm_ucase.h" int main(int argc, char *argv[]) { int fd; char *shmpath; struct shmbuf *shmp; if (argc != 2) { fprintf(stderr, "Usage: %s /shm-path\n", argv[0]); exit(EXIT_FAILURE); } shmpath = argv[1]; /* Create shared memory object and set its size to the size of our structure. */ fd = shm_open(shmpath, O_CREAT | O_EXCL | O_RDWR, 0600); if (fd == -1) err(EXIT_FAILURE, "shm_open"); if (ftruncate(fd, sizeof(struct shmbuf)) == -1) err(EXIT_FAILURE, "ftruncate"); /* Map the object into the caller's address space. */ shmp = mmap(NULL, sizeof(*shmp), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (shmp == MAP_FAILED) err(EXIT_FAILURE, "mmap"); /* Initialize semaphores as process-shared, with value 0. */ if (sem_init(&shmp->sem1, 1, 0) == -1) err(EXIT_FAILURE, "sem_init-sem1"); if (sem_init(&shmp->sem2, 1, 0) == -1) err(EXIT_FAILURE, "sem_init-sem2"); /* Wait for 'sem1' to be posted by peer before touching shared memory. */ if (sem_wait(&shmp->sem1) == -1) err(EXIT_FAILURE, "sem_wait"); /* Convert data in shared memory into upper case. */ for (size_t j = 0; j < shmp->cnt; j++) shmp->buf[j] = toupper((unsigned char) shmp->buf[j]); /* Post 'sem2' to tell the peer that it can now access the modified data in shared memory. */ if (sem_post(&shmp->sem2) == -1) err(EXIT_FAILURE, "sem_post"); /* Unlink the shared memory object. Even if the peer process is still using the object, this is okay. The object will be removed only after all open references are closed. */ shm_unlink(shmpath); exit(EXIT_SUCCESS); } : pshm_ucase_send.c "send" : "bounce" . . "" . "" "" . /* pshm_ucase_send.c Licensed under GNU General Public License v2 or later. */ #include #include #include #include #include #include #include #include "pshm_ucase.h" int main(int argc, char *argv[]) { int fd; char *shmpath, *string; size_t len; struct shmbuf *shmp; if (argc != 3) { fprintf(stderr, "Usage: %s /shm-path string\n", argv[0]); exit(EXIT_FAILURE); } shmpath = argv[1]; string = argv[2]; len = strlen(string); if (len > BUF_SIZE) { fprintf(stderr, "String is too long\n"); exit(EXIT_FAILURE); } /* Open the existing shared memory object and map it into the caller's address space. */ fd = shm_open(shmpath, O_RDWR, 0); if (fd == -1) err(EXIT_FAILURE, "shm_open"); shmp = mmap(NULL, sizeof(*shmp), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (shmp == MAP_FAILED) err(EXIT_FAILURE, "mmap"); /* Copy data into the shared memory object. */ shmp->cnt = len; memcpy(&shmp->buf, string, len); /* Tell peer that it can now access shared memory. */ if (sem_post(&shmp->sem1) == -1) err(EXIT_FAILURE, "sem_post"); /* Wait until peer says that it has finished accessing the shared memory. */ if (sem_wait(&shmp->sem2) == -1) err(EXIT_FAILURE, "sem_wait"); /* Write modified data in shared memory to standard output. */ if (write(STDOUT_FILENO, &shmp->buf, len) == -1) err(EXIT_FAILURE, "write"); if (write(STDOUT_FILENO, "\n", 1) == -1) err(EXIT_FAILURE, "write"); exit(EXIT_SUCCESS); } close(2), fchmod(2), fchown(2), fcntl(2), fstat(2), ftruncate(2), memfd_create(2), mmap(2), open(2), umask(2), shm_overview(7) 3 . . : . 6.18 8 2026 shm_open(3)