1

As far I know CPU generates logical address for each instruction on run time.

Now this logical address will point to linear or virtual address of the instruction.

Now my questions are ,

1) Can OS generate same logical address for two different processes ?

With reference to "In virtual memory, can two different processes have the same address?" , If two different processes can have same virtual address in that case it is also quit possible that logical addresses can also be the same.

2) Just to clarify my understanding whenever we write a complex C code or simple "hello world" code,Virtual address will be generated at build time (compile->Assemble->link) where logical address will generated by CPU at run time ?

Please clarify my doubts above and also do correct me if I am on wrong way.

Community
  • 1
  • 1
Nishith Goswami
  • 353
  • 5
  • 13

1 Answers1

2

The logical address and the virtual address are the same thing. The CPU translates from logical/virtual addresses to physical addresses during execution.

As such, yes, it's not just possible but quite common for two processes to use the same virtual addresses. Under a 32-bit OS this happens quite routinely, simply because the address space is fairly constrained, and there's often more physical memory than address space. But to give one well-known example, the traditional load address for Windows executables is 0x400000 (I might have the wrong number of zeros on the end, but you get the idea). That means essentially every process running on Windows would typically be loaded at that same logical/virtual address.

More recently, Windows (like most other OSes) has started to randomize the layout of executable modules in memory. Since most of a 32-bit address space is often in use, this changes the relative placement of the modules (their order in memory) but means many of the same locations are used in different processes (just for different modules in each).

A 64-bit OS has a much larger address space available, so when it's placing modules at random addresses it has many more choices available. That larger number of choices means there's a much smaller chance of the same address happening to be used in more than one process. It's probably still possible, but certainly a lot less likely.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • If "logical address and the virtual address are the same thing".But if I am not mistaken logical address contain segment selector( 16bit ) which is mentioning the segment (CS,DS etc) and 32 bit offset (Offset within a segment means virtual address part) so how CPU generates logical address ? Does it take reference of segment headers while generating logical address? – Nishith Goswami Apr 13 '14 at 04:19
  • @NishithGoswami: A 32-bit virtual address does include a segment and an offset, *but* all current (major) OSes basically just ignore them--i.e., they point the segment registers to selectors that all have a base address of 0 and a limit of 4GB, so they have no effect on the address that's generated. – Jerry Coffin Apr 13 '14 at 05:34
  • Thanks you for clarifying , one more doubt, OS generates logical address (Containing 16 bit segment selector and 32 bit offset) base on virtual address generated at build time ? – Nishith Goswami Apr 13 '14 at 05:55
  • @NishithGoswami: Not really, no. It loads the segment registers with selectors, then (mostly) leaves them alone. All the addresses it generates are just 32-bit offsets from those. The linker generates a base address for each executable module, but the loader can (and frequently will) adjust those as a process is being built, both to keep modules from colliding and for address space randomization. – Jerry Coffin Apr 13 '14 at 06:05
  • So Whenever new segment is required to be loaded in Main Memory OS generates 48 bit address (logical address) and then on wards within the same segment it will generate 32bit address correct ? – Nishith Goswami Apr 13 '14 at 06:12
  • @NishithGoswami: For any practical purpose, nothing really generates a 48-bit address ever. While it's booting, the OS loads CS, DS, ES and SS with selectors that all have bases of 0 and limits of 4 GB. Everything just uses offsets from that base or 0. After boot, the segment registers are mostly ignored. In 64-bit mode, even that goes away: the segment registers basically just disappear. – Jerry Coffin Apr 13 '14 at 06:26
  • If you want to get really technical: 32-bit Windows (and I think Linux too) does actually use FS with a non-zero base. As kind of a convenience shortcut, it contains a pointer to a Thread Environment Block (aka Thread Information Block). – Jerry Coffin Apr 13 '14 at 06:29
  • Jerry:Many Thanks to you I got my answers. – Nishith Goswami Apr 13 '14 at 08:09