问题
when a program started the OS will create a virtual memory
, which divided into stack
, heap
, data
, text
to run a process on it.I know that each segment is used for specification purpose such as text
saves the binary code of program, data
saves static
and global
variable. My question is why the OS need to create the virtual memory
and divide it into the segments ? How about if OS just use the physical memory and the process run directly on the physical memory. I think maybe the answer is related with running many process at the same time, sharing memory between process but i am not sure. It is kind if you give me an example about the benefit of creating virtual memory
and dividing it into the segments.
回答1:
In an environment with memory protection via a memory mapping unit, all memory is virtual (mapped via the MMU). It's possible to simply map each virtual address linearly to physical addresses, while still using the protection capabilities of the MMU, but doing that makes no sense. There are many reasons to prefer that memory not be directly mappped, such as being able to share program instructions and shared library code between instances of the same program or different programs, being able to fork
, etc.
来源:https://stackoverflow.com/questions/62566970/why-os-create-the-virtual-memory-for-running-process