5

An operating system/computer architecture question here. I was reading about caches, about how virtually indexing the cache is an option to reduce address translation time. I came across the following:

"Virtual cache difficulties include:
    Aliasing
        Two different virtual addresses may have the same physical address."

I can't think of a scenario when this can occur. It's been a while since my O/S days and I'm drawing a blank.

Could someone provide an example? Thanks

jdphenix
  • 15,022
  • 3
  • 41
  • 74
JDS
  • 16,388
  • 47
  • 161
  • 224

4 Answers4

7

Two processes might have a shared mapping. E.g., in Unix, executable code is typically mapped into a region shared between all processes that execute the same program. (In fact, a single process might have several mappings of the same underlying memory, e.g. when it mmap's the same file twice.)

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • It wouldn't be that unusual for a single process to do this. For example, think of something like .bss. It could all be mapped to a single page that's zero initialized with COW semantics. – FatalError Oct 13 '12 at 22:49
  • This is always done (almost) for executables, and it is also very easy to do even from userlevel using functions like `mmap()` and `shmat()` – perh Oct 13 '12 at 22:49
3

I believe that the executable sections of programs can possibly be shared between processes--thus being mapped twice.

For example: if you load two instances of vim, there will be two processes. Both process will likely map to the same executable code in physical memory.

Geoff Montee
  • 2,587
  • 13
  • 14
  • ok that makes sense from an instruction point of view, but the real danger here (aside from redundancy in the cache) is the same `data` being loaded, and one version being modified. When can 2 virtual addresses refer to the same physical data of a program? Maybe if 2 processes are both trying to write memory-mapped I/O output? But I don't know if that gets cached. – JDS Oct 13 '12 at 22:56
  • Thats a possibility. Also, I'm not sure how MPI and OpenMP implement shared memory, but maybe it happens using those. – Geoff Montee Oct 13 '12 at 23:00
  • Obviously it's been a while since I've used MPI. That uses message passing, rather than shared memory. – Geoff Montee Oct 13 '12 at 23:06
3

shmat() is a typical example of same physical address being mapped as two different virtual address in two different processes. If you do pmap -x pid_A . you will you see the virtual mem map for process A similarly for Process B. Actual Phy mem is not exposed to the user-space program.

Now SayProcess A and B share a shared memory segment and shared memory pointer be sh_mem_ptr_A and Sh_mem_ptr_B. If you print these pointers their address(virtual) will be different. Because Sh_mem_ptr_A is a part of memory map of Process A, Similarly sh_mem_ptr_B for Process B.

Kernel maintains the maaping of Virtual-to- phy addr. By page table and offset. Higher bits map to the page table and offset maps to offset in the page table. So If you notice the Lower order bits of sh_mem_ptr_A and sh_mem_ptr_B they will be same(but may not be true always).

user660783
  • 31
  • 1
1

Also each process is allocated 4GB of virtual space (in 32 bit system), out of which 1 GB (depends upon Os to Os) is mapped for OS. Since OS is common for all processes, so the lower 1GB of virtual addresses are common for all the process, which are mapped to same OS physical pages.

peeyush
  • 2,841
  • 3
  • 24
  • 43