Shared Memory Segment in Operating System

吃可爱长大的小学妹 提交于 2019-12-05 20:11:45

The correct way to think about this is like so:

  • The system has a certain amount of physical memory, that's available to the operating system (the physical RAM chips in your computer)
  • Each process has a virtual address space, that does not directly correspond to physical memory.
  • The operating system can map any part of the physical memory it has available into the virtual address space of a process.

Basically, the OS can say: "put this physical memory chunk into the virtual address space at address 0x12345678". Every data that's in a processes virtual address space ultimately resides somewhere in physical memory. Stack, heap, shared memory, ... is all the same in that regard. The only thing that distinguishes shared memory, is that multiple processes have the same piece of physical memory mapped into their address space.

Eye candy:

In reality, things are a bit more complicated, but this description gives the basic idea.

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.

In the shared-memory model, a region of memory that is shared by the cooperating processes is established. Processes can then exchange information by reading and writing data to the shared region.

A shared-memory region resides in the address space of the process creating the shared-memory segment. Other processes that wish to communicate using this shared-memory segment must attach it to their address space. Normally, the operating system tries to prevent one process from accessing another process’s memory (viruses try to break this). Shared memory requires that two or more processes agree to remove this restriction. They can then exchange information by reading and writing data in the shared areas. The form of the data and the location are determined by these processes and are not under the operating system’s control. The processes are also responsible for ensuring that they are not writing to the same location simultaneously.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!