1

I have a query on MAP_PRIVATE. Once I map a region with MAP_PRIVATE I know that the changes I made are invisible to other process. But I just want to make sure that if some other process which is also running at the same instance, if it makes some modifications to the mmap file, will those changes be visible inside my process (which mapped it using MAP_PRIVATE).

Basically my requirement is that I want to mmap a file at one moment in my application and the condition is that the updates made to this file by other processes should not be visible inside my process until my task is done. So can I mmap it using MAP_PRIVATE safely?

Thanks, Ram

Bose
  • 381
  • 3
  • 16

1 Answers1

1

Read carefully and several times mmap(2) man page.

  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.

So you are in the "unspecified" case

You need to have some synchronization between the other processes (writing to the file) and your process. See perhaps sem_overview(7) and mlock(2) and msync(2)

If the other processes are arbitrary (and you cannot change their behavior), you are stuck. This is the Unix file philosophy: arbitrary processes can write simultaneously to the same file, but what happens then is not always specified (and might depend upon the filesystem). If you want several processes to write to the same file, they should synchronize and cooperate somehow (e.g. lock with flock(2) or lockf(3), etc...).

Maybe you should use some database system instead of plain files! E.g. sqlite would do some synchronization for you (assuming of course all the processes are writing to the common file using sqlite ...)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547