Is there a difference between mmap MAP_SHARED and MAP_PRIVATE when PROT_READ is also used?
c, linux, linux-kernel, mmap
Solution
Under `MAP_PRIVATE`, the Linux manpage says that it is unspecified whether changes made to the file after the mmap() call are visible in the mapped region. That is not the case with `MAP_SHARED`. So if you need the contents of the mapping to be updated together with the contents of the file, you had better use `MAP_SHARED`. If the underlying file itself is read-only and cannot change then of course none of this is applicable.
If `PROT_READ` is used, I can see nothing else that should be different between `MAP_PRIVATE` and `MAP_SHARED`. In fact, despite the above warning about unspecified behaviour, my guess (which I have not tested) would be that in practice there is no difference at all between the two under `PROT_READ`.
Problem
If I create an `mmap(2)` of a file with a `prot` parameter of `PROT_READ` only and the file backing it is also read-only and does not change, is there any performance difference (or any difference at all) between `MAP_SHARED` and `MAP_PRIVATE` ? Will the kernel do something differently between the two? (The documentation only refers to difference of behaviour in terms of "updates", but as it is `PROT_READ` there can be no updates. I wonder if there is some other difference?)