Understanding virtual address and virtual address space

11,529

Solution 1

Programs and data are stored as numbers in memory cells. Each memory cell has a unique number, called its address. The range of numbers representing valid addresses is called address space.

When programs run, the CPU reads data from memory and writes results back to memory. CPU communicates the desired location to the memory by specifying the address of the memory cell targeted by a read or a write operation.

There are multiple ways in which the CPU can come up with an address (remember, address is only a number). The number representing the address could be in a register, it could be stored at another memory location, it could be calculated by adding or subtracting an offset to a register, and so on. In all cases your compiled program instructs CPU on how to come up with (or generate) the address it needs to read or write.

Modern architectures let multiple programs execute as if they own the entire logical address space. In other words, several programs could write to memory location at the same address without stepping over each others' results. This is done by virtualizing the address space: let's say programs A and B generate a write to memory location at 0x1000. The CPU, aided by the operating system, could performs additional adjustments to the address, and map it to physical address 0x60001000 for program A, and to 0x5F001000 for program B. Both programs think that they wrote to the location at 0x1000, because they operate in a virtual address space. Their model of the memory is a contiguous block starting at 0 and continuing to 0x000100000000 (assuming that your system has 4GiB of memory available to processes). But this model works only because the CPU additionally translates their logical addresses to physical addresses, which are allocated and taken away as needed in the process of running the program.

Because the same number representing an address means different things to a program and to a CPU, the address space of the program is called virtual, and the address space of the CPU is called physical.

Solution 2

When a program accesses memory, it does not know or care where the physical memory backing the address is stored. It knows it is up to the operating system and hardware to work together to map locate the right physical address and thus provide access to the data it wants. Thus we term the address a program is using to access memory a virtual address. A virtual address consists of two parts; the page and an offset into that page.

Share:
11,529

Related videos on Youtube

program-o-steve
Author by

program-o-steve

I love to write code. When i am not writing code , i am anxious to do so ! Anyway if am anxious, typing code is anxiolytic. Learning ruby these days.

Updated on July 26, 2020

Comments

  • program-o-steve
    program-o-steve almost 4 years

    I read that , "When a program executes an instruction like : MOV REG,1000 , it does so to copy the contents of the memory address 1000 to REG. Address can be generated using indexing,base registers,segment registers and other ways.

    These program generated address are called virtual address and form the virtual address space."

    Can anyone please explain me,what does it (These program generated address are called virtual address) mean ?

  • program-o-steve
    program-o-steve over 12 years
    In your example 0x1000 is a program generated address ?
  • Sergey Kalinichenko
    Sergey Kalinichenko over 12 years
    @program-o-steve Yes, it is a program generated address. Note that it does not matter how the program generated that address: it could be an address of a static variable, an element 0 of an array at the address 0x1000, an element 32 of an array at the address 0x0FE0, and so on.
  • program-o-steve
    program-o-steve over 12 years
    but the statement in my question says the program generated address to be a virtual address. 0x1000 doesn't seem to be a virtual address
  • Sergey Kalinichenko
    Sergey Kalinichenko over 12 years
    @program-o-steve Of course it is virtual: it is 0x1000 only to the program. The physical (hardware) address of the virtual 0x1000 cell in my example is 0x60001000 for program A, and 0x5F001000 for program B. Your program thinks that it is writing to 0x1000, but the hardware writes to an entirely different address.
  • JohnMerlino
    JohnMerlino about 11 years
    @dasblinkenlight "There are multiple ways in which the CPU can come up with an address". Isn't it the responsibility of the address bus to designate an address so that the data bus can read and write to that address?
  • Sergey Kalinichenko
    Sergey Kalinichenko about 11 years
    @JohnMerlino Memory bus is a passive unit controlled by the CPU (in some architectures, the address bus can be controlled by other active devices that need to gain access to memory, such as peripheral I/O devices with direct memory access, but the CPU designates the times when these other devices can take control of the address bus). CPU decides which address it wants, puts it out onto the address bus, and waits for the memory to give it back the data at that address.
  • HelloWorld123456789
    HelloWorld123456789 almost 10 years
    @dasblinkenlight Sorry it's a bit late. I understood what you said but then again what is the utility of using the virtual address space? Why can't program A directly say "write to 0x60001000" and program B directly say "write to 0x5F001000"?
  • Sergey Kalinichenko
    Sergey Kalinichenko almost 10 years
    @RikayanBandyopadhyay Because the 0x60001000 and 0x5F001000 addresses are determined by the OS when it loads the executable, not something that can be possibly compiled into the program.
  • HelloWorld123456789
    HelloWorld123456789 almost 10 years
    But aren't the addresses relocatable and are adjusted by the loader before executing?
  • Sergey Kalinichenko
    Sergey Kalinichenko almost 10 years
    @RikayanBandyopadhyay On systems with no virtual memory support, yes. Besides, not all addresses can be corrected by the loader - for example, in situations when an address is computed dynamically, the loader may have no visibility into data manipulation to recognize that the result would be used as an address.