0

I have doubt with respect to the address space. I have thought that the RAM if 4 GB is split up into 2 halves for kernel space(1GB) and user space(3GB).

1] Does RAM also maintains stack,heap,code and data section as hard disk.

2] Won't the process running is not given a boundary where the stack, data, code and heap have to grow in RAM.

3] My thought was that the stack,heap,code and data segment all be in the consecutive address space given to the process at the time of process creation.

4] How does the CPU takes the correct address of the process to execute, as the processes are not contiguous in physical memory.

enter image description here

Angus
  • 12,133
  • 29
  • 96
  • 151

1 Answers1

2

No, only the virtual memory address space is split in two. Physical memory, the RAM in the machine, contains an entirely random collection of blocks that map to virtual memory addresses. From both operating system pages and user program pages. Much like the image shows although it is a bit misleading about showing the OS pages at the bottom.

That mapping constantly changes, a page fault is the essential mechanism to get a virtual memory page mapped to RAM. Which is triggered when a program accesses a virtual memory page that isn't present in RAM yet. As needed, RAM pages may be unmapped to make room, their content is either discarded or written to the pagefile. Code is usually discardable, it can be read back from the executable file, data usually isn't.

Some pages in RAM are special, they contain code and data that's used by drivers. They are page-locked. Required when the driver handles device interrupts and the code/data used by the interrupt handler must be present in RAM to allow the interrupt to be handled, can't afford a page fault at such a critical time. The probable reason the image was drawn like that.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks Hans. But I have a confusion that, how the CPU takes the correct physical address or the instruction pointer in the CPU knows the address of the process as the segments of the process are scattered in the physical memory. – Angus Nov 02 '13 at 18:11
  • I have studied that a seg fault happens when a process tries to access the address beyond the process address limit provided. I'm confused with this. – Angus Nov 02 '13 at 18:13
  • 1
    The operating system sets up the page mapping tables that the CPU uses to translate a virtual address to a physical address. A seg fault occurs when the CPU hits a virtual address that doesn't have an entry in the page mapping table. – Hans Passant Nov 02 '13 at 18:30