Use mmap to map a single file multiple times. Will the changes be applied to all locations?

Viewed 426

I am reading the source code of a paper. Mmap is used here. There is a 4kB page called 4kb.file. First, I use mmap to map this file multiple times.

ret = mmap((void *) target, PAGE, PROT_READ|PROT_WRITE, MAP_SHARED | MAP_FILE, fd, 0);

target is the starting address of the mapping I set. Use different target variable and call the above instruction multiple times. Is this file mapped to multiple locations in memory? Suppose that the return values of the above function executed multiple times are ret1, ret2, ret3, and ret4. Now I modify the content pointed to by ret1, will the positions of ret2, ret3... change (the same file is mapped to a different location)?

Is the first parameter in mmap, which is the target above, a virtual address? The source code always starts with 0x300000000000ULL. Does this number have any special meaning? Thanks

1 Answers

You should consider using msync() and especially the MS_INVALIDATE flag :

msync() flushes changes made to the in-core copy of a file that was mapped into memory using mmap(2) back to the filesystem.
MS_INVALIDATE
Asks to invalidate other mappings of the same file (so that they can be updated with the fresh values just written).

Related