clinuxoperating-systemsystems-programming

Confusion of virtual memory


Consider a sample below.

char* p = (char*)malloc(4096);
p[0] = 'a';
p[1] = 'b';

The 4KB memory is allocated by calling malloc(). OS handles the memory request by the user program in user-space. First, OS requests memory allocation to RAM, then RAM gives physical memory address to OS. Once OS receives physical address, OS maps the physical address to virtual address then OS returns the virtual address which is the address of p to user program.

I wrote some value(a and b) in virtual address and they are really written into main memory(RAM). I'm confusing that I wrote some value in virtual address, not physical address, but it is really written to main memory(RAM) even though I didn't care about them.

What happens in behind? What OS does for me? I couldn't found relevant materials in some books(OS, system programming). Could you give some explanation? (Please omit the contents about cache for easier understanding)


Solution

  • A detailed answer to your question will be very long - and too long to fit here at StackOverflow.

    Here is a very simplified answer to a little part of your question.

    You write:

    I'm confusing that I wrote some value in virtual address, not physical address, but it is really written to main memory

    Seems you have a very fundamental misunderstanding here.

    There is no memory directly "behind" a virtual address. Whenever you access a virtual address in your program, it is automatically translated to a physical address and the physical address is then used for access in main memory.

    The translation happens in HW, i.e. inside the processor in a block called "MMU - Memory management unit" (see https://en.wikipedia.org/wiki/Memory_management_unit).

    The MMU holds a small but very fast look-up table that tells how a virtual address is to be translated into a physical address. The OS configures this table but after that, the translation happens without any SW being involved and - just to repeat - it happens whenever you access a virtual memory address.

    enter image description here

    The MMU also takes some kind of process ID as input in order to do the translation. This is needed because two different processes may use the same virtual address but they will need translation to two different physical addresses.

    As mentioned above the MMU look-up table (TLB) is small so the MMU can't hold a all translations for a complete system. When the MMU can't do a translation, it can make an exception of some kind so that some OS software can be triggered. The OS will then re-program the MMU so that the missing translation gets into the MMU and the process execution can continue. Note: Some processors can do this in HW, i.e. without involving the OS.