Search code examples
coperating-systemvirtual-address-space

Why do we need address virtualization in an operating system?


I am currently taking a course in Operating Systems and I came across address virtualization. I will give a brief about what I know and follow that with my question.

Basically, the CPU(modern microprocessors) generates virtual addresses and then an MMU(memory management unit) takes care of translating those virtual address to their corresponding physical addresses in the RAM. The example that was given by the professor is there is a need for virtualization because say for example: You compile a C program. You run it. And then you compile another C program. You try to run it but the resident running program in memory prevents loading a newer program even when space is available.

From my understanding, I think having no virtualization, if the compiler generates two physical addresses that are the same, the second won't run because it thinks there isn't enough space for it. When we virtualize this, as in the CPU generates only virtual addresses, the MMU will deal with this "collision" and find a spot for the other program in RAM.(Our professor gave the example of the MMU being a mapping table, that takes a virtual address and maps it to a physical address). I thought of that idea to be very similar to say resolving collisions in a hash table.

Could I please get some input on my understanding and any further clarification is appreciated.


Solution

  • Could I please get some input on my understanding and any further clarification is appreciated.

    Your understanding is roughly correct.

    Clarifications:

    • The data structures are nothing like a hash table.

    • If anything, the data structures are closer to a BTree, but even there are important differences with that as well. It is really closest to a (Java) N-dimensional array which has been sparsely allocated.

    • It is mapping pages rather than complete virtual / physical addresses. (A complete address is a page address + an offset within the page.).

    • There is no issue with collision. At any point in time, the virtual -> physical mappings for all users / processes give a one-to-one mapping from (process id + virtual page) to a either a physical RAM page or a disk page (or both).


    The reasons we use virtual memory are:

    • process isolation; i.e. one process can't see or interfere with another processes memory

    • simplifying application writing; i.e. each process thinks it has a contiguous set off memory addresses, and the same set each time. (To a first approximation ...)

    • simplifying compilation, linking, loading; i.e. the compilers, etc there is no need to "relocate" code at compile time or run time to take into account other.

    • to allow the system to accommodate more processes than it has physical RAM for ... though this comes with potential risks and performance penalties.