Where is shared memory belongs to ? Which means it is owned by each individual process like stack and heap. So, other program cannot able to access the stack of some other program. Or it is a common segment of memory which is used by any number of process. The below figure shows my question diagramatically.
Figure 1:
----------------- ----------------- -----------------
| stack | | stack | | stack |
| | | | | |
| Shared m/y | --->| Shared m/y |<--- | Shared m/y |
| | | | | | | |
| heap | | | Heap | | | Heap |
| | | | | | | |
| Data segment | | | Data segment | | | Data segment |
| | | | | | | |
| text |___| | text | |___| text |
----------------- ----------------- -----------------
Process 1 Process 2 Process 3
(OR)
Figure 2:
-----------------------------------------
| |
| |
| Shared Memory |<--
------->| | |
| | | |
| ----------------------------------------- |
| ^ |
| | |
----------------- ----------------- -----------------
| stack | | stack | | stack |
| | | | | |
| heap | | Heap | | Heap |
| | | | | |
| Data segment | | Data segment | | Data segment |
| | | | | |
| text | | text | | text |
----------------- ----------------- -----------------
Process 1 Process 2 Process 3
In Figure 1, Each process have a segment of shared memory within the address space of a process. On that shared memory of process 2 is accessed by process 1 and process 3. In Figure 2, the shared memory is a segment of memory which is accessed by all the processes. So, in above two scenario, which is used by process for shared memory segment.
I suspect part of your confusion is from terminology. In pre-64-bit Intel, data was organized in segments. The term segment is also used in linkers to describe how data is assembled in a program (that in 32-bit Intel may be mapped to hardware segments). However, for you question, you should eliminate the term segment.
The general way things work in Intel 64-bit and most non-Intel systems is that the physical memory is divided into PAGE FRAMES of some fixed size (e.g. 4K, 1K). The memory management unit of the CPU operates on PAGES of the same size. The operating system sets up a linear logical address space for each process consisting of pages. The operating system maps the pages of the logical process address space to physical page frames using a PAGE TABLE.
When each process runs it sees its own logical address space with addresses in the range 0 to whatever. The pages within each process's address space are mapped to physical page frames and the Memory Management Unit does the translation from logical addresses using pages to physical memory addresses using page frames automatically using the page table(s).
This system keeps each process from messing with other processes. Generally, if Process X accesses logical address Q and Process Y accesses logical address Q, they will be accessing different physical memory locations because their page tables will have different mappings.
Every system I am aware of that uses logical memory translation has the ability to for multiple processes to map a logical page(s) to the same physical page frame(s): shared memory. Processes can use this mechanism to quickly exchange data (at the cost of having manage the synchronization to that data).
In this type of sharing, the physical page frame does not have to be mapped to the same logical address (and usually is not).
Process X can map page frame P to page A while Process Y can map page frame P to page B
There is another form of shared memory that usually is implemented quite differently. The processor or operating system (on some processors) defines a range of logical addresses for a System Address Space. This range of addresses is the same for all processes and all processes have the same mapping of logical addresses to physical page frames in this range.
The System address is protected so that it can only be accessed in Kernel mode so processes cannot muck with each other.
Think in terms of pages rather than segments here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With