pehache
2024-04-07 19:04:27 UTC
As advised, I'm copying this post here
fd = open("aaa",O_RDWR);
p = mmap ( NULL
, n
, PROT_READ | PROT_WRITE
, MAP_PRIVATE | MAP_NORESERVE
, fd
, 0 );
// writing to p; the changes exist only in memory
void* p2 = mmap( NULL
, n
, PROT_READ | PROT_WRITE
, MAP_SHARED | MAP_NORESERVE
, fd
, 0 );
memcpy(p2,p,n); // copying everything from p to p2
msync(p2,n);
// unmap/remap p so it's ready for new changes
munmap(p,n);
p = mmap ( NULL
, n
, PROT_READ | PROT_WRITE
, MAP_PRIVATE | MAP_NORESERVE
, fd
, 0 );
========================================
- the whole content is copied, not only the changed content
- is this code legal? Is there any potential conflict between the 2 mapping,
with an undefined behavior?
Hello,
When memory mapping a file with the MAP_PRIVATE flag, the modifications (writes)
only exist in memory and are not written back to the file.
According to the man pages, calling msync (3) on a such a mapping does NOT
"When the msync() function is called on MAP_PRIVATE mappings, any modified data
shall not be written to the underlying object and shall not cause such data to be
made visible to other processes"
https://linux.die.net/man/3/msync
So: is there a way to write the changes back to the file?
- mapping the file with MAP_PRIVATE
- make some modifications in memory only (fast) while keeping the original
version on disk (safe)
- at some point (when the user decides, and once the consistency of the changes
have been verified) writing the modifications to the disk
I'm pretty sure it exists some way or another, but I don't know how.
========================================When memory mapping a file with the MAP_PRIVATE flag, the modifications (writes)
only exist in memory and are not written back to the file.
According to the man pages, calling msync (3) on a such a mapping does NOT
"When the msync() function is called on MAP_PRIVATE mappings, any modified data
shall not be written to the underlying object and shall not cause such data to be
made visible to other processes"
https://linux.die.net/man/3/msync
So: is there a way to write the changes back to the file?
- mapping the file with MAP_PRIVATE
- make some modifications in memory only (fast) while keeping the original
version on disk (safe)
- at some point (when the user decides, and once the consistency of the changes
have been verified) writing the modifications to the disk
I'm pretty sure it exists some way or another, but I don't know how.
fd = open("aaa",O_RDWR);
p = mmap ( NULL
, n
, PROT_READ | PROT_WRITE
, MAP_PRIVATE | MAP_NORESERVE
, fd
, 0 );
// writing to p; the changes exist only in memory
void* p2 = mmap( NULL
, n
, PROT_READ | PROT_WRITE
, MAP_SHARED | MAP_NORESERVE
, fd
, 0 );
memcpy(p2,p,n); // copying everything from p to p2
msync(p2,n);
// unmap/remap p so it's ready for new changes
munmap(p,n);
p = mmap ( NULL
, n
, PROT_READ | PROT_WRITE
, MAP_PRIVATE | MAP_NORESERVE
, fd
, 0 );
========================================
- the whole content is copied, not only the changed content
- is this code legal? Is there any potential conflict between the 2 mapping,
with an undefined behavior?