Implementing copy-on-write buffer with mmap

2 views
Skip to first unread message

Paul Russell

unread,
Mar 13, 2015, 12:49:13 PM3/13/15
to perfoptimization-dev@lists.apple.com List
I've been playing around with copy-on-write buffers on Linux and the following example seems to work as intended:

#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

Paul Russell

unread,
Mar 14, 2015, 7:25:54 AM3/14/15
to perfoptimization-dev@lists.apple.com List
Just in case someone stumbles across this question ion the future, I found a workaround with the help of StackOverflow: http://stackoverflow.com/questions/29037110/implementing-copy-on-write-buffer-with-mmap-on-mac-os-x

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

Stephen Checkoway

unread,
Mar 14, 2015, 3:54:25 PM3/14/15
to Paul Russell, perfoptimization-dev@lists.apple.com List

On Mar 14, 2015, at 7:25 AM, Paul Russell <prus...@sonic.net> wrote:

> 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

Reply all
Reply to author
Forward
0 new messages