(Original post at: www.duartes.org)
Anatomy of a Program in Memory
Memory management is the heart of operating systems; it is crucial
for both programming and system administration. In the next few posts
I’ll cover memory with an eye towards practical aspects, but without
shying away from internals. While the concepts are generic, examples are
mostly from Linux and Windows on 32-bit x86. This first post describes
how programs are laid out in memory.
Each process in a multi-tasking OS runs in its own memory sandbox. This sandbox is the
virtual address space, which in 32-bit mode is
always a 4GB block of memory addresses. These virtual addresses are mapped to physical memory by
page tables,
which are maintained by the operating system kernel and consulted by
the processor. Each process has its own set of page tables, but there is
a catch. Once virtual addresses are enabled, they apply to
all software running in the machine,
including the kernel itself. Thus a portion of the virtual address space must be reserved to the kernel: