Shared memory
Physical memory can be shared between two processes merely by manipulating their page tables. This happens automatically in modern Unixes in various circumstances, e.g.,- When implementing shared libraries, the dynamic loader will mmap the library, and the kernel will share the maps amongst processes.
- When forking, the child gets a copy of the parent's page table, i.e., their pages originally all coexist in physical memory; but the first time a page is written (by either), the kernel traps the write and makes a copy. Thus a child can share a large read-only data structure constructed by the parent prior to forking; although as caveat, in same languages, a read-only data structure is still written to by the runtime (e.g., garbage collection metadata).
- Threads share their entire page map. The OS will simply reset the stack pointer when switching contexts, as opposed to flushing the TLB.