Understanding program execution

Terminology

  • Program: A compiled binary file generated from source code by a compiler and linker.
  • Process: A running instance of a program, created by the OS to execute the program.

Procedure

  • Starting a Program
    When a program is started, the OS creates a virtual address space for it and sets up the necessary page tables to enable virtual-to-physical address translation. This virtual address space includes key sections of the program, such as Text (instructions) and Data (global variables), as well as regions allocated for the stack and the heap.
  • Loading Pages on Demand
    Instead of loading the entire program into memory, the OS often uses demand paging to load only the initial pages required for startup from the storage (HDD or SSD) into the memory (RAM). As soon as a page fault occurs, the OS loads the missing page from the storage into the memory.
  • Handling Memory Constraints
    If the system’s memory becomes full, the OS may need to free up space by moving infrequently used pages back to persistent storage. This process, sometimes referred to as paging out, ensures active pages remain in memory while keeping the total memory footprint within system limits.
  • Terminating the Process
    When a process completes, the OS releases its virtual address space. This includes removing its page tables from the MMU and clearing any remaining page table entries. Resources used by the process, such as memory and open file handles, are also released back to the system.

Disclaimer

This post provides a high-level overview of how programs are executed in memory, without delving into the specific details of operating systems or binary formats.