by eridius · 11y ago · 0 comments
Unlike the OP's article, Mike Ash's version uses vm_remap() to remap memory around instead of hitting the filesystem and relying on tempfs to keep the data in-memory. vm_remap() is an OS X API, and I don't know offhand if there is any equivalent on Linux (though I would be surprised if there isn't some way to do the same thing).
int fd = shm_open("test", O_RDWR|O_CREAT, 0600);
ftruncate(fd, 4096*2);
char *part1 = mmap(NULL, 4096, PROT_WRITE|PROT_WRITE, MAP_SHARED, fd, 0);
char *part2 = mmap(part1 + 4096, 4096, PROT_WRITE|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, 0);
part1[0] = 'X';
assert(part2 - part1 == 4096);
assert(part1[0] == part2[0]);
shm_unlink("test");A very important caveat is that the POSIX shared memory namespace is shared among all processes, so you need to wrap shm_open with a mkstemp()-style algorithm that generates a random name, opens with O_EXCL, and tries again if it fails. Unfortunately it's very easy to mess that up and introduce a security vulnerability.
Wouldn't mkstemp("/dev/shm/tmp-XXXXXX") be enough. As strace tells /shm_open/ is just a wrapper for /open/ with /dev/shm/ prefix.
https://groups.google.com/forum/#!topic/comp.os.linux.develo...