#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define SIZE 4096
#define SHM_NAME "foobar"
int main(void)
{
int fd = shm_open(SHM_NAME, O_RDWR | O_CREAT, 0666);
int r = ftruncate(fd, SIZE);
char *buf1 = mmap(NULL, SIZE, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
strcpy(buf1, "Original buffer");
char *buf2 = mmap(NULL, SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE, fd, 0);
// At this point buf2 is aliased to buf1
// Now modifying buf2 should trigger copy-on-write)...
strcpy(buf2, "Modified buffer");
// buf1 and buf2 are now two separate buffers
strcpy(buf1, "Modified original buffer");
// clean up
r = munmap(buf2, SIZE);
printf("munmap(buf2): %i\n", r);
r = munmap(buf1, SIZE);
printf("munmap(buf1): %i\n", r);
r = shm_unlink(SHM_NAME);
printf("shm_unlink: %i\n", r);
return EXIT_SUCCESS;
}
However under OS X (10.10) the second mmap call returns MAP_FAILED. The OS X man page for mmap seems to suggest that this should work (it even mentions copy-on-write), and I've experimented with various different flags for the calls to mmap, but nothing seems to work. Any ideas ?
Paul
_______________________________________________
Do not post admin requests to the list. They will be ignored.
PerfOptimization-dev mailing list (PerfOptimi...@lists.apple.com)
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/perfoptimization-dev/perfoptimization-dev-garchive-8409%40googlegroups.com
This email sent to perfoptimization-...@googlegroups.com
It seems that the mmap() on OS X is buggy and doesn't work with a file descriptor returned by shm_open(), however using open() with a regular file to get a file descriptor seems to solve the problem.
Paul
> https://lists.apple.com/mailman/options/perfoptimization-dev/prussell%40sonic.net
>
> This email sent to prus...@sonic.net
> It seems that the mmap() on OS X is buggy and doesn't work with a file descriptor returned by shm_open(), however using open() with a regular file to get a file descriptor seems to solve the problem.
Probably worth filing a bug <http://bugreporter.apple.com>.
--
Stephen Checkoway