Operating Systems – Understanding Virtual Memory for All Processes

cmemoryoperating systems

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.

Best Answer

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.