I just wanted to know where shared memory resides in a Linux system? Is it in physical memory or virtual memory?
I am aware about the process\'s virtual memory send
All memory that is committed is physical.
However processes cannot address physical memory directly. They have virtual addresses which the kernel will resolve to physical addresses. When a shared memory region is setup, the same physical memory location is addressed by the multiple processes. However the virtual addresses can be different though. Each process uses the virtual address it received only in its own context. Both the virtual addresses refer to the same physical memory.
To elaborate, in case of a shared memory region, the same physical memory address is addressable by multiple processes simultaneously as both the processes have virtual addresses that point to the same physical address.
For example consider the following :
are both pointing to the shared memory region.
This is actually a common physical address P.
Now,
Process T1 referencing virtual address V1
OR
Process T2 referencing virtual address V2
will result in an access to physical address P as the kernel translates both the virtual address locations to the same physical location in memory.
For example, in the following diagram, the physical memory PFN4 is shared by processes X and Y using VPFN3 and VPFN1 in their respective contexts (PFN stands for Page Frame Number).