Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between MAP_SHARED and MAP_PRIVATE in the mmap function?

Tags:

c

linux

mmap

Playing around with mmap for the fun of it, I have the following code:

(.. snip ..)
fd = open("/home/me/straight_a.txt", O_RDONLY);
if (fd == -1) {
    perror("open");
    exit(1);
}

m = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_FILE|MAP_PRIVATE, fd, 0);

if (m == MAP_FAILED) {
    perror("mmap");
    exit(1);
}

printf("m is %p\n", m);

printf("*m = %c\n", *m);
printf("*(m+1) = %c\n", *(m+1));
(.. snip ..)

This works as expected. But before I got to this, I tried...

m = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, fd, 0);

... and mmap errored out with:

mmap: Permission denied

In general, what's the difference between the two flags (man page isn't to generous on this subject)? What sort of permission (and where) am I missing?

EDIT

Like it usually happens.. partially figured it out.

Turns out open needed an O_RDWR flag.

So, am I correct to assume that:

  • MAP_PRIVATE - changes are made in memory only, not saved to disk?
  • MAP_SHARED - changes would be saved to disk...

... but I'm not saving anything to disk anywhere, I thought? Just operating on memory.

like image 478
ntl0ve Avatar asked Mar 01 '12 16:03

ntl0ve


People also ask

What is Map_shared?

In this context MAP_SHARED implies that if you write to the mmap'd region your changes will be committed back to the mapped file itself. You can't do this because you opened the file in read-only mode. MAP_PRIVATE works because writes to the mmap'd region are not committed back to the original file.

What is Map_private?

MAP_PRIVATE Create a private copy-on-write mapping. Updates to the mapping are not visible to other processes mapping the same file, and are not carried through to the underlying file. It is unspecified whether changes made to the file after the mmap() call are visible in the mapped region.

What is mmap function?

The mmap() function establishes a mapping between a process' address space and a stream file. The address space of the process from the address returned to the caller, for a length of len, is mapped onto a stream file starting at offset off.

What is the difference between shared memory and mmap?

Both methods are viable. mmap method is a little bit more restrictive then shmget , but easier to use. shmget is the old System V shared memory model and has the widest support. mmap / shm_open is the new POSIX way to do shared memory and is easier to use.


1 Answers

You opened the file in read-only mode. Then you attempted to mmap part of it in read/write mode with MAP_SHARED set. In this context MAP_SHARED implies that if you write to the mmap'd region your changes will be committed back to the mapped file itself. You can't do this because you opened the file in read-only mode.

MAP_PRIVATE works because writes to the mmap'd region are not committed back to the original file. When you write to the region the pages that were written to are copied to a different region of memory, possibly backed by swap space.

like image 68
karunski Avatar answered Sep 22 '22 12:09

karunski