Swapping
- 모든 Page는 Physical Memory에 매핑된다.
- 한 번에 많은 양의 Page가 사용되지 않기 때문에 낭비이다.
- Physical Memory의 크기는 제한적이다.
- 당장 사용할 Page만 매핑한다면 더 효율적일 것이다.
→ 사용하지 않는 Page를 Disk로 내보내기
What is Swap Space
- Swap Space
- Physical Memory에 매핑되지 않은 Page를 위한 Disk의 임시 공간
- Swap Out: Physical Memory → Swap Space
- Swap In: Swap Space → Physical Memory

- Consideration
- Page가 언제 Swap Out 되는가?
- 메모리가 가득 찬 경우
- 여유 공간이 임의의 기준치 이하가 된 경우
- 어떤 Page가 Swap Out 되는가?
- 가장 먼저 들어온 Page
- 가장 늦게 들어온 Page
- 참조해야 할 Page가 Physical Memory에 존재하지 않는 경우에는 어떻게 할 것인가?
When Pages are Swapped Out?
- Free Memory Size < Low Watermark (Pre-Defined)
- 하나 또는 그 이상의 Page들이 Free Memory = High Watermark를 만족할 때까지 Swap된다.
⠀⠀
⠀⠀
Example
Low Watermark = 2, High Watermark = 3

What if the Page to Reference is Not in Physical Memory?
- Page Fault Occurs → Page Swapped In → Page Reference
Example
Page 6 is Referenced.

Implementation: Present Bit
- 각 Page Table Entry는 Present Bit, P를 갖는다.
- Present Bit = 1 → Page가 Physical Memory 내에 존재
- Present Bit = 0 → Page가 Swap Space 내에 존재
Implementation: Swap In
- Page Fault가 발생하면
- OS의 Page Fault Handler가 Page를 교체한다.
- I/O 작업이 진행되는 동안, Process는 Blocked State가 된다.

- Process A
- Page Fault Occurs
⠀⠀

- Process A
- Page Fault Handler Runs
⠀⠀

- Process B
- Process A → Blocked State
⠀⠀

⠀⠀

- Process A
- Page Table Entry Update
⠀⠀

- Process A
- Process References Page 4 → Not Page Fault
Implementation: Swap Out
- Free Memory < Low Watermark
- Free Memory > High Watermark를 만족할 때까지 하나 또는 그 이상의 Daemon Process가 Swap Out 된다.

Beyond Physical Memory: Mechanisms
- Memory Hierarchy에 추가 레벨이 필요하다.
- OS에는 수요가 많지 않은 Address Space의 일부를 숨겨둘 장소가 필요하다.
- 현대 시스템에서, 이 역할은 대부분 Hard Disk Drive가 수행한다.
