0
$\begingroup$

I am just trying to visualize how computers work with Virtual Memory/Address.

Assume there is a program on the disk that looks like this:

a b c d e f g h i j k l m n o p where the a .. p are instructions and their address is from 0 to 15.

What happens inside the CPU while executing this program? Assume that the operating system uses pages of 8 bytes.

To begin with, my understanding is that the operating system loads the program into physical memory in page-sized chunks and creates a mapping to the pages of the program numbered from 0..n which looks like:

# assume it is in reverse in the real memory (16-31)!

[i j k l m n o p] [a b c d e f g h]

# cpu's view of the program

  page1 (24-31)     page2 (16-31)
[a b c d e f g h] [i j k l m n o p]
 |
 PC
  1. Depending on the program size, program-size/page-size page's will be created. So here it would be 16B/8B = 2 pages numbered as page1 and page2.
  2. Now, the Program Counter (PC) would be set to the Virtual Address of a which is 0 and the CPU will somehow resolve it and load the corresponding instruction from the real physical address of a which is 24 here.
  3. But wait, how did CPU know that it has to look up into a certain page map, page1 in this case? The operating system knows that but how does the CPU know that? Is it something like the Virtual Address of a program corresponds to one and only one page map and that can be determined directly from the Virtual Address itself? For example, 0x00 automatically means to lookup in page1, 0x08 means to lookup in page2. Divide by 8 and look into the page{quotient} with the remainder indicating the offset to look into the real memory?
  4. Suppose there is an instruction d that refers to the memory corresponding to o which is in page2, how will that be resolved? Is it using the same principle? If you know the Virtual Address, then you know the place to look up for the real address?
  5. Now, when there are multiple programs how do we uniquely determine what is page1? Could another program have a page1 as well? Is it like page maps are unique (which I think is complex because I thought it has to be sequential and starting from 1 so that given a Virtual Address you can calculate the page map + offset) or is there a per process Page Table managed by the Operating System?
  6. Suppose we finished executing the instruction h, the PC would automatically load the Virtual Address of i, which gets automatically resolved to page2 using the same above principle?
$\endgroup$
1
  • 2
    $\begingroup$ Re, "...and the CPU will somehow resolve it..." Usually that is the responsibility of a Memory Management Unit (a.k.a., "MMU") which is considered to be a separate functional unit from the CPU. The MMU either translates the upper bits of the virtual address to a physical address to which the memory can respond, or else it signals a page fault to the CPU. It's the responsibility of the operating system to decide why a page fault happens and, how to proceed. $\endgroup$ Apr 4, 2018 at 19:44

2 Answers 2

1
$\begingroup$

Virtual memory is handled just in the part of the CPU that handles reading and writing memory.

At some point, the CPU has calculated a memory address and has decided to read or write memory at that location. If there is no virtual memory used, then the memory address is the actual address in the physical memory connected to the CPU. With virtual memory, the memory access part of the CPU will first translate the virtual memory address to a real memory address.

$\endgroup$
0
$\begingroup$

Your first step might be with two of the following books: Design Unix Operating System or Design Implementation FreeBSD Operating System.

Also this https://en.wikipedia.org/wiki/Virtual_memory and this http://www.cs.jhu.edu/~phi/csf/slides/lecture-virtual-memory.pdf will help.

$\endgroup$
4
  • 2
    $\begingroup$ Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. $\endgroup$
    – xskxzr
    Apr 4, 2018 at 18:08
  • 1
    $\begingroup$ @xskxzr Really, in this case, I think the probelm is that the question is just too broad. The asker wants to know, essentially, "How does virtual memory work? You know, all of it." and that's just too much for a SE question. $\endgroup$ Apr 4, 2018 at 18:41
  • $\begingroup$ @DavidRicherby, I understand it is a broad topic. Hence I made up a simple example and split it into six separate logical questions that a beginner can have so as to not be too vague about what I am asking. I am hoping all these ideas do a have a simple logical explanation at its core from where it is optimized and such. $\endgroup$
    – Nishant
    Apr 4, 2018 at 19:34
  • 1
    $\begingroup$ @Nishant Numbering the separate parts of the question doesn't make it any less broad. $\endgroup$ Apr 4, 2018 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.