[시스템 프로그래밍] Virtual Memory

여경민·2023년 8월 8일

Review: Process

A process is an instance of a running program.

  • Not the same as "program" or "processor"
  • A process has a context (state) for the program execution

Process provides each program with two key abstractions

  • Logical control flow
    - Each process seems to have exclusive use of the CPU
    • Provided by kernel mechanism called context switching
  • Private address space
    - Each process seems to have exclusive use of memory
    • Provided by kernel mechanism called virtual memory

Private Address Space (illusion)

A System Using Virtual Addressing


Used in all modern servers, laptops, and smart phones

Address Spaces

  • Linear address space: Ordered set of contiguous non-negative integer addresses
  • Virtual address space: Set of N = 2^n virtual addresses
  • Physical address space: Set of M = 2^m physical addresses

VM as a Tool for Memory Management

  • Key idea: each process has its own virtual address space
    -Each process can view memory as a simple linear array
    -Address translation maps these addresses in physical memory
  • Page is basic unit of address management (commonly 4KB)
    -Each virtual page will be mapped to a physical page
  • Allows sharing of code and data among process
    -By mapping virtual pages to the same physical page

Enabling Data Structure: Page Table

  • A page table is an array of page table entries that maps virtual pages to physical pages.
    -Per-process kernel data structure in physical memory (DRAM)
    -Special register(PTBR) tells CPU the address of page table

Virtual Memory and Hard Disk

Not all virtual pages can be mapped to physical pages.
Therefore, we actually store all the virtual pages in hard disk (which is relatively large size)
And some of the virtual pages are loaded in physical memory
-Similar to the "caching" concept

Page Hit

access to a virtual address that is in physical memory

Page Fault

  • access to a virtual address that is not in physical memory
  • Page fault causes an exception in CPU
  • Page fault handler selects a victim to be evicted (VP 4)
  • Faulting instruction is restarted: page hit!

Memory Protection with Page Table

Extended PTEs with permission bits

  • "SUP" bit means supervisor (kernel) mode
    Your hardware checks these bits on each access

1개의 댓글

comment-user-thumbnail
2023년 8월 8일

개발자로서 배울 점이 많은 글이었습니다. 감사합니다.

답글 달기