Reputation: 24876
Situation:
If process a & b each use mmap()
to create a shared memory mapping, with the same shared memory object /shm-a
as backed file.
My guess:
I originally thought there is only 1 copy of memory, which processes write/read on.
But later I think there are actually 3 copy of them, right? Each process has 1 copy which is created by mmap()
, and the 3rd copy is the shared memory object, which is used to sync between process, but I am not sure.
The questions are:
fd
directly?Upvotes: 1
Views: 801
Reputation: 8913
Then how many copy of memory there are? 1 or n+1 (where n is process count)
There is only one copy of the shared memory.
The same physical memory is mapped into different processes. But it may be mapped to different addresses.
And is it proper for process to read/write to the shared memory object via its fd directly?
Yes it is. That is, in fact, the purpose of shared memory. What one process writes into shared memory can be read by the other process. This a very fast form of IPC. But you do have to be careful in how you use it. In particular, you need to worry about concurrent access, and sharing pointers in shared memory.
Upvotes: 4