Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I hope this is the right place. This is a homework assignment for my Operating Systems course and I have to implement a working virtual memory system in C++ so programming is directly involved.

I've read some sources about Paging and Virtual Memory now (Tanenbaum, What Every Programmer Should Know About Memory, etc) and noticed that everybody starts off with the phrase

Every process gets its own virtual memory.

Then off they go into a detailed analysis about page size, page replacement algorithms, the MMU and TLB, etc. Those things are actually quite fine in my mind, what doesn't make sense is the concept of virtual memory.

If I have four processes running simultaneously and each process uses it's own virtual address space, how is the physical memory protected? The MMU translated the virtual address 0 the same whether it comes from process one, two, three or four.

Let me give an example:

Every process has a 32-bit virtual address space that it can use. Every process starts off the same way

MOVI 10, 0  // r10 = 0 
LOAD 11, 10 // r11 = MEM[r10]; value at address 0x00000000

I just don't understand this concept of distinct virtual address space if every process tries to call the same virtual address which will be translated the same way by the MMU. So all four processes will receive the same data?

We have a page table which keeps an index of every mapped and unmapped page to a page frame. If each page entry is unique, then how can it account for the duplicated virtual address space that each process has?

I'm at the point where I have written design drafts for the MMU, TLB and the Page Table, but I can't continue until I properly understand how the heck virtual space is defined and used for each process.

share|improve this question
add comment

1 Answer

up vote 8 down vote accepted

The MMU translated the virtual address 0 the same whether it comes from process one, two, three or four.

No it doesn't. The whole point of virtual addresses is that they go through another layer of indirection. Each process has an individual page table which tells the CPU (or OS, I forget) how to translate each virtual address. If there is no entry in the page table for the virtual address for that process, or the page doesn't have the requisite rights (RWX), then that is what an access violation/SIGSEGV is. Two processes can have two separate virtual addresses map to the same physical address for interprocess communication or code sharing, or even to something completely different, for example swap pages, guard pages, etc. However, as the OS has complete and total control over the page table, it is impossible for any application to access physical memory beyond what the OS has allowed, or the virtual memory of any other process.

The physical memory is protected because every access must go through the page table and the OS can control the entries just fine. You can't just make a random pointer and de-reference it because there will be no page table entry.

share|improve this answer
    
Are we talking about multilevel page tables now? The OS Page Table checks in the Page Table of process A for the translation of its virtual address into a physical address and then checks if that address is mapped in the OS Page Table? –  IAE Jan 22 '12 at 19:16
1  
@SoulBeaver: No, there is just multiple page tables. I don't think there even is such a thing as an OS page table. A page table is a per-process structure. –  DeadMG Jan 22 '12 at 19:22
    
Oooooh. That's probably what tripped me up. I thought there was a central Page Table and that all processes get their address translated through that central structure. All others suddenly stopped making sense. But if every process has an individual page table, how do they know they aren't infringing on another process' memory physical memory space? They could just map their virtual address to the same page frame without knowing about it? I suppose this is the job of the OS or MMU? –  IAE Jan 22 '12 at 19:36
1  
@SoulBeaver in fact the 'infringing' can be extremely useful. The same physical memory can be mapped into two or more processes and used for interprocess communication. –  Charles E. Grant Jan 22 '12 at 19:58
1  
@SoulBeaver: You only need to unload the data in main memory which is going to be replaced with data from the hard drive. There's no need to replace the entire main memory. –  DeadMG Jan 22 '12 at 20:57
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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