![]() |
![]() |
![]() |
open a shared memory object
#include <fcntl.h> #include <sys/mman.h> int shm_open( const char *name, int oflag, mode_t mode );
The shm_open() function returns a file descriptor that is associated with the shared "memory object" specified by name. This file descriptor is used by other functions (such as mmap() and mprotect()) to refer to the shared memory object. The FD_CLOEXEC file descriptor flag is set for this file descriptor.
The state of the shared memory object, including all data associated with it, persists until the shared memory object is unlinked and all other references are gone.
The value of oflag is constructed by the bitwise ORing of the following flags. At least the following flag bits are defined in <fcntl.h>:
The check for the existence of the shared memory object, and the creation of the object if it doesn't exist, are atomic with respect to other processes executing shm_open(), naming the same shared memory object with O_EXCL and O_CREAT set.
The permission bits for the memory object will be set to the value of mode, except those bits set in the process's file creation mask (see the umask() function and <sys/stat.h>).
On success, the shm_open() function returns a nonnegative integer, which is the lowest numbered unused file descriptor. Otherwise, -1 is returned, and errno is set.
The shared memory object exists and the permissions specified by oflag are denied, or O_TRUNC is specified and write permission is denied.
/* * EXAMPLE 1: */ #include <stdio.h> #include <string.h> #include <fcntl.h> #include <errno.h> #include <stdlib.h> #include <sys/mman.h> main() { int fd, i; char *addr, c; /* Open physical memory */ fd = shm_open("Physical", O_RDWR, 0777); if (fd == -1) { fprintf(stderr, "Open failed:%s\n", strerror(errno)); exit(1); } /* Map BIOS ROM */ addr = mmap(0, PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0xf0000); if (addr == (void *) -1) { fprintf(stderr, "mmap failed : %s\n", strerror(errno)); exit(1); } printf("Map addr is %6.6X\n", addr); for (i = 0; i < 3 * 80; ++i) { c = *addr++; if (c >= ' ' && c <= 0x7f) putchar(c); else putchar('.'); } exit(0); } /* * EXAMPLE 2: */ #include <stdio.h> #include <string.h> #include <fcntl.h> #include <errno.h> #include <stdlib.h> #include <unistd.h> #include <sys/mman.h> main(int argc, char *argv[]) { int fd; char *addr; /* * In case the unlink code is * not executed at the end */ if (argc != 1) { shm_unlink("bolts"); exit(0); } /* Create a new memory object */ fd = shm_open("bolts", O_RDWR | O_CREAT, 0777); if (fd == -1) { fprintf(stderr, "Open failed:%s\n", strerror(errno)); exit(1); } /* Set memory objects size */ if (ltrunc(fd, PAGESIZE, SEEK_SET) == -1) { fprintf(stderr, "ltrunc : %s\n", strerror(errno)); exit(1); } /* Map memory object */ addr = mmap(0, PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (addr == (void *) -1) { fprintf(stderr, "mmap failed : %s\n", strerror(errno)); exit(1); } printf("Map addr is %6.6X\n", addr); /* Write to shared memory */ *addr = 1; /* * The memory object remains in * the system after the close */ close(fd); /* * To remove a memory object * you must unlink it like a file. */ /* * This may be done by another process. */ shm_unlink("bolts"); exit(0); } /* * EXAMPLE 3: */ #include <stdio.h> #include <string.h> #include <fcntl.h> #include <errno.h> #include <stdlib.h> #include <unistd.h> #include <sys/mman.h> #include <sys/kernel.h> main(int argc, char *argv[]) { int fd; char *addr; /* * In case the unlink code isn't executed at the end */ if (argc != 1) { shm_unlink("bolts"); exit(0); } /* Create a new memory object */ fd = shm_open("bolts", O_RDWR | O_CREAT, 0777); if (fd == -1) { fprintf(stderr, "Open failed : %s\n", strerror(errno)); exit(1); } /* Set memory objects size */ if (ltrunc(fd, PAGESIZE, SEEK_SET) == -1) { fprintf(stderr, "ltrunc : %s\n", strerror(errno)); exit(1); } /* Map memory object */ addr = mmap(0, PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (addr == (void *) -1) { fprintf(stderr, "mmap failed:%s\n",strerror(errno)); exit(1); } printf("Map addr is %6.6X\n", addr); printf("Hit break to stop.\n"); sleep(3); /* So you can read above message */ /* * We unlink so object goes away on last close. */ shm_unlink("bolts"); *addr = '0'; if (fork()) for (;;) if (*addr == '0') putc(*addr = '1', stderr); else Yield(); else for (;;) if (*addr == '1') putc(*addr = '0', stderr); else Yield(); }
POSIX 1003.4
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | Yes, but modifies errno |
Thread | Yes |
errno, ltrunc(), mmap(), munmap(), mprotect(), open(), shm_unlink(), /etc/readme/technotes/shmem.txt (or shmem.ps)
![]() |
![]() |
![]() |