Learning Question

How does the operating system turn a process address space into protected usable memory?

Operating systems usually manage virtual memory in fixed-size chunks called pages.

A page is a unit of virtual memory mapping and protection.

The first mental model is:

A process address space is built from page-sized mappings, and each mapping says what the process may do with that memory.

Pages as Mapping Units

The process sees a continuous-looking virtual address space, but the system manages it in page-sized units.

Common page sizes include 4 KiB, though systems may support larger pages too.

Each virtual page may be mapped to:

  • physical memory
  • a region of a file
  • shared memory
  • zero-filled memory created on demand
  • no valid backing

The page table records the translation and permissions needed by the hardware.

The exact page-table structure is architecture-specific, but the programmer-facing result is stable:

Some address ranges are valid, some are not, and valid ranges have permissions.

Protection Bits

Memory mappings carry permissions.

Common permissions include:

  • readable
  • writable
  • executable

Code pages are often mapped executable and read-only.

Constant data may be readable but not writable.

Heap and stack pages are usually readable and writable but not executable.

These permissions help catch mistakes and enforce security boundaries.

If a process writes to a read-only page or executes from a non-executable page, the CPU raises a fault and the operating system handles it.

For an invalid user process access, the result may be a signal such as SIGSEGV on Unix/Linux.

Mapped Files

A mapping does not always mean anonymous memory.

The OS can map file contents into a process address space.

Executable code, shared libraries, and memory-mapped application files may all appear this way.

With a file mapping, reading memory can cause the OS to bring file-backed page contents into physical memory.

This is one reason loading a program does not always mean copying the entire executable into memory immediately.

The process gets mappings.

Pages can be brought in as needed.

Demand Paging

Demand paging means the OS can delay real work until a page is actually accessed.

A process may have a valid mapping for a page that is not currently resident in physical memory.

When the process touches that page, a page fault occurs.

A page fault is not always a crash.

It can be a normal mechanism that lets the kernel load or allocate the needed page and then resume the process.

The important distinction is:

A page fault means the current memory access needs kernel attention. It only becomes a program failure when the access is invalid or cannot be satisfied.

Copy-on-Write

Copy-on-write is a mapping strategy where pages are shared until someone tries to modify them.

Unix/Linux process creation commonly uses this idea after fork.

The parent and child can initially share the same physical pages for private memory.

If either process writes to a shared copy-on-write page, the kernel creates a private copy for the writer.

This avoids copying an entire address space immediately when many pages may never be changed.

The programmer still sees private process memory.

The OS uses sharing internally until mutation requires separation.

Stack, Heap, and Shared Libraries

The familiar regions of a process are built from mappings.

The stack is a mapped region used for call frames and per-thread execution.

The heap grows through allocator behavior that eventually relies on OS memory mappings or related system calls.

Shared libraries are mapped into the process so their code and data can be used at runtime.

These are not magical memory categories.

They are address ranges with intended roles, permissions, and backing.

Core Mental Model

Memory protection works because the process does not directly control all memory.

The OS configures mappings.

The hardware checks translations and permissions during memory access.

When reasoning about memory behavior, ask:

Is this virtual page mapped, what backs it, and what access permissions does it allow?

Final Summary

Pages are the practical units that connect a process address space to physical memory, files, shared mappings, protection, and faults.

Memory mapping lets the operating system provide private memory, shared code, lazy loading, copy-on-write behavior, and access protection while the program uses ordinary-looking addresses.