4. Virtual Memory
4์ฅ Virtual Memory ๊ฐ์
Virtual Memory๋ ์ค์ ์ด์ฉ ๊ฐ๋ฅํ ๊ฒ๋ณด๋ค ๋ ํฐ Physical memory๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ํด์ฃผ๋ ๋ฉ๋ชจ๋ฆฌ ๊ณ์ธตํ ๊ธฐ๋ฒ์ด๋ค. ๊ฐ ํ๋ก์ธ์ค์ ๊ณ ์ ํ virtual address space๋ฅผ ์ ๊ณตํด ํ๋ก์ธ์ค ๊ฐ ๊ฒฉ๋ฆฌ์ protection/sharing์ ๋์์ ๋ฌ์ฑํ๋ค.
๊ตฌํ ๋ฐฉ๋ฒ
- โ Virtual Address ๊ณต๊ฐ์ Page ๋จ์๋ก ์ชผ๊ฐ ๋ค. ์: 32-bit ํ๋ก์ธ์๋ฉด $2^{32}$-1์ ๊ฐ์ ์ฃผ์ ๊ณต๊ฐ์ page๋ก ๋ถํ .
- โก ๊ฐ ํ๋ก๊ทธ๋จ์ Page ๋จ์๋ก access (์ ์ฒด๊ฐ ํ์ํ์ง ์์)
- โข Page ์ ์ฅ: Physical memory๊ฐ ํ์ฉํ๋ ๋งํผ physical memory์ ์ฌ๋ฆฌ๊ณ , ๋๋จธ์ง๋ disk (HDD, SSD)์ ์ ์ฅ
- โฃ CPU๋ virtual address๋ฅผ ์ฌ์ฉํ์ฌ page access
์ฅ์
- ํ๋ก๊ทธ๋จ์ด ์ค์ physical memory๋ณด๋ค ๋ ํฐ ๋ฉ๋ชจ๋ฆฌ์์ ๋์๊ฐ ์ ์์
- ์ฌ๋ฌ ํ๋ก๊ทธ๋จ์ด ๊ฐ์ ๋ฉ๋ชจ๋ฆฌ์ ๋์์ ์ํํ ์ ์๊ฒ ํจ
- Virtual Address ๊ณต๊ฐ์ ์ผ์ (ํ๋ก์ธ์ ์ํคํ ์ฒ์ ๋ฐ๋ฆ)ํ๊ณ Physical Memory๋ ์ค์ ๋ด๊ฐ PC์ ์ค์นํ ๋ฉ๋ชจ๋ฆฌ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง
- ํ๋ก๊ทธ๋จ 1๊ฐ ๋น 1๊ฐ์ Virtual Address space๋ฅผ ๊ฐ๊ฐ ๊ฐ์ง๊ฒ ๋จ

Paging
โ Virtual Memory
ํ๋ก์ธ์์ virtual memory ๊ณต๊ฐ์ ๊ฐ์ ์ฌ์ด์ฆ์ page๋ก ๋๋๋ ๊ฒ.
- $p$: page number ($p_{\max}$ page)
- $o$: page offset ($o_{\max}$ B/page)
- Virtual address: $o_{\max} \times p + o$
์: Size of VA 100B, Size of Page 10B. ์ฃผ์ 29 $10 \times 2 + 9$ โ page number 2, page offset = 9.
์: 8-bit processor, Size of Page 16B. VA 0010 0001 โ page number 2, page offset 1 โ $16 \times 2 + 1 = 33$๋ฒ์ง access.
โก Physical Memory
๋์ผํ frame ์ฌ์ด์ฆ๋ก ๋๋.
- $f$: frame number ($f_{\max}$ frame)
- $o$: frame offset ($o_{\max}$ B/frame)
- Physical address: $o_{\max} \times f + o$
Size of page Size of frame โ page offset frame offset, page number โ frame number
Virtual memory์์ physical memory๋ก ๊ฐ ์ ์๋ ๋งํผ ๊ฐ๊ณ ๋๋จธ์ง๋ Disk์ ์์.
Virtual Address โ Physical Address ๋ณํ
| VA (31-0) |
|---|
| Virtual page number (bits 31-12) + Page offset (bits 11-0) |
โ Translation โ
| PA |
|---|
| Physical page number (29-12) + Page offset (11-0) |
page offset = frame offset (๊ฐ์ ํฌ๊ธฐ๋ก ๋๋๊ธฐ ๋๋ฌธ)

Virtual Address Translation
A page table maps virtual pages to physical frames.
CPU๋ Virtual Address ($p, o$)๋ฅผ ์์ฑ โ Page Table๋ก $p \to f$ ๋ณํ โ Physical Address ($f, o$)๋ก ๋ฉ๋ชจ๋ฆฌ ์ ๊ทผ.
Page Table ๊ตฌ์ฑ
๊ฐ ํ๋ก์ธ์๋ง๋ค ํ๋์ ํ ์ด๋ธ์ ๊ฐ์ง.
Flags (1bit๋ก ์ด๋ค์ฌ ๋ค์์ ์๋ ค์ค): - Valid/invalid (= resident) bit: Page๊ฐ ๋ฉ์ธ๋ฉ๋ชจ๋ฆฌ์ ์๋์ง ์๋์ง (์์ผ๋ฉด ๋์คํฌ์ ์กด์ฌ) - Dirty bit: ์ ๋ฐ์ดํธ ์ฌ๋ถ - Reference (= clock or used) bit
Page frame number: ๋ณํ ๊ฒฐ๊ณผ
PTBR (Page Table Base Register)
- Page Table์ด ๋ฉ๋ชจ๋ฆฌ์ ์ด๋์ ์๋์ง๋ฅผ ๋ํ๋
- Page Table์ด ์์ํ๋ ๋ถ๋ถ์ ๊ฐ๋ฆฌํด
- PTBR + p โ ํด๋น PTE(Page Table Entry) ์์น
Page์ ๊ฐ์: 32-bit processor, size of page = 4KB โ $2^{32} / 4\text{KB} = 2^{20}$๊ฐ
์ค์ ํฌ์ธํธ
- ๊ฐ ํ๋ก๊ทธ๋จ(ํ๋ก์ธ์ค)๋ง๋ค ํ๋์ Page Table์ ๊ฐ๋๋ค
- Page Table์ DRAM์ ์ ์ฅ๋๋ค

Demand Paging
CPU๊ฐ ํด๋น page๋ฅผ ์ฝ์ ๋ ๋น๋ก์ main memory์ ์ฌ๋ฆผ (ํ์ํ ๋).
์ฅ์
- ๋ชจ๋ page๋ฅผ ๋ฏธ๋ฆฌ ๋ฌผ๋ฆฌ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ฆฌ์ง ์์๋ ๋จ โ ํฐ ํ๋ก๊ทธ๋จ ์คํ ๊ฐ๋ฅ
- ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ฑ
๋จ์ : External Fragmentation
Page์ ์ฐ์๋ ์์ญ์ด ํ์ํ ๋ ์ธ๋ถ ํํธํ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์์ (๋จ paging ์์ฒด๋ fixed size๋ก ๋๋๊ธฐ ๋๋ฌธ์ external fragmentation์ ๊ฑฐ์ ์์ง๋ง internal fragmentation์ ๋ฐ์).
Translation Look-aside Buffer (TLB)
Address translation์ ์ํด ๋งค๋ฒ Page Table (in DRAM) access โ Cache access โ DRAM access๊ฐ ํ์ โ ๋๋ฌด ๋๋ฆผ.
Solution: TLB (Translation Look-aside Buffer) = page table์ cache.
- Fully-associative cache
- Recent page ๋ณํ์ ์บ์ฑ โ ๋๋ถ๋ถ์ ๊ฒฝ์ฐ TLB hit์ผ๋ก DRAM page table access๋ฅผ ๊ฑด๋๋
- TLB hit: Page table access๊ฐ ํ์ ์์ด ๋ฐ๋ก physical address ๊ณ์ฐ

A Big Picture (TLB + Cache + Memory)
์ ์ฒด ๊ฐ์ โ ๋ฌผ๋ฆฌ ์ฃผ์ ๋ณํ + ๋ฐ์ดํฐ ์ ๊ทผ ํ๋ฆ:
- CPU๊ฐ VA ์์ฑ
- TLB access โ TLB Hit/Miss
- TLB Miss โ Page Table access โ PT Hit/Miss
- PT Miss โ Page fault โ Disk I/O๋ก page ๊ฐ์ ธ์ด
- ๋ฌผ๋ฆฌ ์ฃผ์๋ก Cache access
- Cache miss โ Main memory access
On TLB Miss
- TLB๊ฐ page table entry(PTE)๋ฅผ ์ฐพ๊ธฐ ์ํด DRAM ์ ๊ทผ
- Page table entry๊ฐ valid bit=1 โ TLB ๊ฐฑ์ , ์ฃผ์ ๋ณํ ์ฑ๊ณต
- Page table entry valid bit=0 โ Page fault โ OS์์ ์ฒ๋ฆฌ (page fault handler)
TLB Miss Handler
Page table์ ๋ฉ๋ชจ๋ฆฌ์์ ๊ฐ์ ธ์์ TLB ๊ฐฑ์ .

Page Fault Handler
- โ ๋์คํฌ์์ ํด๋น๋๋ page๋ฅผ ์ฐพ์
- โก Main memory์์ ๋์ฒดํด์ผ ํ page ์ ํ (LRU ์ฌ์ฉ)
- ๋ง์ฝ dirty bit์ด 1์ด๋ผ๋ฉด Main memory์ ๋ฐ์ดํฐ๋ฅผ ๋จผ์ Disk์ ์ ๋ฐ์ดํธ
- โข Main memory๋ก page๋ฅผ ์ฎ๊ฒจ์ฃผ๊ณ page table์ ์ ๋ฐ์ดํธ (PTE์ valid bit์ 1๋ก ๋ง๋ฆ)
- โฃ Process ๋ค์ ์์
Page Size ๊ฒฐ์
โ Page size๋ฅผ ์๊ฒ ํ์ ๋
- ์ฅ์ : Internal fragmentation problem์ด ์ ๊ฒ ๋ฐ์ โ ๋ฉ๋ชจ๋ฆฌ ํจ์จ โ
- ๋จ์ : Page table์ด ์ปค์ง, page fault handling overhead๊ฐ ๋ ์์ฃผ ๋ฐ์
โก Page size๋ฅผ ํฌ๊ฒ ํ์ ๋
- ์ฅ์ : Page table์ด ์์์ง, page fault handling overhead๊ฐ ๋ ๋ฐ์
- ๋จ์ : Internal fragmentation problem์ด ์์ฃผ ๋ฐ์ โ ๋ฉ๋ชจ๋ฆฌ ํจ์จ โ
Instruction์ ์ฃฝ ์ฝ์ด๋๊ฐ๋ค๊ฐ ๊ฒฝ๊ณ์ ๋ง์ฃผํ๋ฉด page fault ๋ฐ์ โ ๋ฐ์๋น๋ โ
Paging โ Protection and Sharing
โ Protection
- Page ๋จ์๋ก ๊ฐ๋ฅ (ํจ์จ์ ์ธ Protection)
- ๊ฐ processor๋ ์์ ๋ง์ address space๋ฅผ ๊ฐ๊ธฐ์ ๋ค๋ฅธ processor๋ก๋ถํฐ ๋ณดํธ
โก Sharing
- ๋ค๋ฅธ ํ๋ก์ธ์๊ฐ ๊ฐ์ physical memory๋ฅผ accessํ ์ ์์ (๊ฐ์ frame์ ๊ณต์ )
โข Basic Mechanism
- User mode: user address space๋ฅผ accessํ๋ ๊ถํ์ ๊ฐ์ง
- Supervisor (kernel) mode: OS๊ฐ accessํ ์ ์๋ ์์ญ (๋ชจ๋ ๊ณณ access ๊ฐ๋ฅ)
- Page๋ง๋ค ๊ถํ์ ์ ํ ์ ์์ (Read/Write)
โฃ System Call
- Processor๊ฐ ์ํ ์ค ๋ค๋ฅธ ๊ถํ์ด ํ์ํ ๋ OS์ ์์ฒญ (exception์ ํ ์ข ๋ฅ)
- User โ Supervisor (I/O ์ฌ์ฉ ์ ์์ฒญ)
- ๊ถํ์ด ํ์ํ์ง ์์์ง๋ฉด Supervisor โ User (๊ณผ์ ๋ฐ๋ณต)

Virtual Memory Performance
Example
- Memory access time: 100 ns
- Disk access time: 25 ms (= 25,000,000 ns, page fault cost)
- Effective access time:
- If $p$ = probability of a page fault
- $1 - p$: normal access (100 ns)
- $p$: page fault (100 + 25,000,000 ns)
- If we want only 10% degradation โ $p$๊ฐ ๋งค์ฐ ์์์ผ ํจ (๋ฏธ์ธํ fault ratio)
LRU Approximation โ Second Chance Algorithm
Clock (page replacement) + Reference bit.
- ๊ฐ ํ์ด์ง๋ง๋ค reference bit์ด ์์
- Page ์ฐธ์กฐ ์ reference bit์ 1๋ก ์ค์
- ๊ต์ฒด๊ฐ ํ์ํ ๋: clock pointer๊ฐ ๋๋ฉฐ, reference bit 0์ธ page๋ฅผ ์ ๊ฑฐ, reference bit 1์ธ page๋ 0์ผ๋ก ๋ฆฌ์ ํ ๋ค์์ผ๋ก ์ด๋
- ๊ฑฐ์ LRU์ ๋น์ทํ ํจ๊ณผ, ๊ตฌํ์ด ๊ฐ๋จ

I/O Interlock
DMA (Direct Memory Access) Problem: Page๊ฐ ์ฐธ์กฐ๋ ๋์ง ์์ง๋ง I/O๊ฐ ์ฌ์ฉ ์ค์ผ ๋ I/O๊ฐ ์์ ์ค์ธ ๋ฐ์ดํฐ๊ฐ page fault๊ฐ ๋ ๊ฒฝ์ฐ ๋ฌธ์ ๋ฐ์.
Solution: lock bit ์ฌ์ฉ. Second chance algorithm์ด ๋๋ค๊ฐ lock bit = 1์ด๋ฉด ํต๊ณผ (I/O๊ฐ ์ฌ์ฉ ์ค).
Segmentation with Paging
Paging์ ํ๊ธฐ ์ ์ ๋น์ทํ ์ญํ ์ ํ๋ ๊ฒ๋ผ๋ฆฌ ๋๋๋ค โ Segmentation.
Heap โ Heap
Run-Time Stack โ Run-Time Stack
Program Data โ Program Data, Libraries
Program Text โ Program Text, User Code
- Sharing๊ณผ Protection์ ์กฐ๊ธ ๋ ํจ์จ์ ์ผ๋ก ํ ์ ์์
- Virtual address = $(s \times p_{\max} + p) \times o_{\max} + o$
- $s$: segment number, $p$: page number, $o$: page offset
Address Translation with Segmentation
- STBR (Segment Table Base Register) โ Segment table์ด DRAM์ ์ด๋์๋ถํฐ ์์๋๋์ง ์๋ ค์ค
- Segmentation Table: Page table์ด ์๋ ์์น๋ฅผ ์๋ ค์ค (์ด๋์๋ถํฐ ์์ํ๋์ง)
- TLB๋ ๊ทธ๋๋ก ์ฌ์ฉ, s+p๋ฅผ Tag๋ก ์ฌ์ฉ

์ฅ์
Protection, sharing์ด segment ๋จ์๋ก ํ๋ ๊ฒ page ๋จ์๋ณด๋ค ๋ ์ด๋. Program text, shared segment ๊ฐ์ ๋จ์๋ก ๊ณต์ ๊ด๋ฆฌ.
Typical Memory Hierarchy (The Big Picture)
VA = [Virtual page number (20) | Page offset (12)]
โ
TLB (Valid, Dirty, Tag, Physical page number)
โ Hit
PA = [Physical page number | Page offset]
โ
Cache (Valid, Tag, Data)
โ Hit
Data

์ ์ฒด ํ๋ฆ โ Flowchart
Virtual address โ TLB access
โโ TLB hit?
โ โโ No โ TLB miss exception
โ โโ Yes โ Physical address
โ โ Write?
โ โโ No โ Try to read from cache
โ โ โโ Cache hit โ Deliver data to CPU
โ โ โโ Cache miss stall, read block
โ โโ Yes โ Write access bit?
โ โโ Off โ Write protection exception
โ โโ On โ Try to write to cache
โ โโ Cache hit โ Write + update dirty bit
โ โโ Cache miss stall, read block
TLB / Page Table / Cache ์กฐํฉ ๊ฐ๋ฅ์ฑ
| TLB | Page table | Cache | Possible? |
|---|---|---|---|
| Hit | Hit | Miss | Possible (TLB ์์ผ๋ฉด PT ์ ํ์ธ) |
| Miss | Hit | Hit | TLB miss, PT ์์, ์บ์์ ์์ |
| Miss | Hit | Miss | TLB miss, PT ์์, ์บ์ miss |
| Miss | Miss | Miss | Page fault โ ์ฌ์๋ ํ์ ์บ์ miss |
| Hit | Miss | Miss | Impossible: TLB์ ์๋๋ฐ ๋ฉ๋ชจ๋ฆฌ์ ์์์ ๋ถ๊ฐ๋ฅ |
| Hit | Miss | Hit | Impossible |
| Miss | Miss | Hit | Impossible: ๋ฉ๋ชจ๋ฆฌ์ ์๋๋ฐ ์บ์์ ์๋ ๊ฑด ๋ถ๊ฐ๋ฅ |
| Hit | Hit | Hit | ๊ฐ์ฅ ์์ฃผ ๋ฐ์ (๋น์ฐํ ๊ฒฐ๊ณผ) |

์ ์ฒด ์์ฝ โ Virtual โ Physical Access ๋จ๊ณ
- CPU โ Virtual Address $(s, p, o)$
- TLB์ valid bit = 1์ธ ๊ฒ์ Tag ๋น๊ต
- TLB Hit: Physical page number + $o$ โ Cache access (Tag + Index + Offset)
- TLB Miss: Page table ์ ๊ทผ ((STBR + $s$ โ Segment table) + $p$ โ page table)
- PT valid bit = 1 โ PT Hit, TLB๋ก ๊ฐ์ ์ฎ๊ฒจ์ฃผ๊ณ TLB ์ฌ์ ๊ทผ
- PT valid bit = 0 โ PT Miss (Page fault), LRU ์๊ณ ๋ฆฌ์ฆ์ ํตํด PT์ ๋์ฒด๋ ํ์ด์ง ์ ํ (I/O ์ํ)
- Dirty bit = 0 โ Disk๋ก๋ถํฐ PT์ ๋ฐ์ดํฐ๋ฅผ ์ฎ๊ฒจ์ฃผ๊ณ TLB๋ก ๊ฐ์ ๊ฐ์ ธ์ค๊ณ TLB ์ฌ์ ๊ทผ
- Dirty bit = 1 โ PT โ Disk ํ, Disk๋ก๋ถํฐ PT์ ๋ฐ์ดํฐ๋ฅผ ์ฎ๊ฒจ์ฃผ๊ณ TLB๋ก ๊ฐ ๊ฐ์ ธ์ค๊ณ TLB ์ฌ์ ๊ทผ
Cache access: Index๋ก ์ฐพ์๊ฐ valid bit = 1์ธ ๊ฒ์ Tag ๋น๊ต
- Hit: Data ์ฌ์ฉ
- Miss: Main memory๋ก๋ถํฐ Data๋ฅผ ์ฎ๊ฒจ์ค โ Cache ๋ค์ Access (dirty bit / LRU ์๊ณ ๋ฆฌ์ฆ ์ด์ฉ)

์๋๋ฆฌ์ค ์์ โ Context Switching
- P0๊ฐ CPU ์ฌ์ฉ ์ Timer๋ฅผ ๋ง์ถค (์: 10 ms)
- TLB Miss์ exception ๋ฐ์
- Fault๋๋ฉด I/O Controller๊ฐ ๊ฐ์ ธ์ฌ ๋๊น์ง ๋ค๋ฅธ ํ๋ก๊ทธ๋จ์๊ฒ ์ ์ด๊ถ์ ๋๊น (P1)
- Context Switching (valid = 0์ผ๋ก ๋ค ์ค์ ): TLB Miss๊ฐ ๋์ง๋ง DRAM์ ์๊ธฐ์ ๊ณ์ ์ฌ์ฉ
- I/O Controller๊ฐ ์ํจ ์ผ์ ๋ค ํด์ (exception ๋ฐ์)
- P1์ ์ฌ์ฉ์๊ฐ์ด ๋ฎ์์ง๊ฒ ๊ณ์ ์ฌ์ฉ, ํ P1์ ์ฌ์ฉ์๊ฐ์ ๋ค ์ฌ์ฉ (Timer๊ฐ ์ธ๋ฆผ) โ P0๊ฐ ์ฌ์ฉ Context Switching์ด ์ผ์ด๋จ
- Disk์์ ์ฎ๊ฒจ๊ฐ๊ธฐ์ DRAM์์ ์ฎ๊ฒจ์ ์ฌ์ฉ

์ ๋ฆฌ
- Virtual memory extends the physical memory illusion, enables multi-programming, provides protection/sharing
- Paging splits memory into fixed-size pages / frames
- Page table maps virtual pages to physical frames (stored in DRAM, indexed by PTBR)
- TLB caches page table entries to avoid DRAM access on every memory reference
- Page fault handler swaps pages between disk and memory (LRU with clock reference bit)
- Segmentation with paging gives coarse-grained sharing/protection on top of paging
- Overall memory access: TLB โ Cache โ Main Memory โ Disk
Comments (0)
No comments yet. Be the first to comment!
Please to write a comment.