6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)
DESCRIPTION
TRANSCRIPT
![Page 1: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/1.jpg)
โครงสร�างหน่วยความจำ�าโครงสร�างหน่วยความจำ�าเสม�อน่เสม�อน่
Virtual Memory Structure
ว�เชษฐ์� พลายมาศ | สถาปั�ตยกรรมคอมพิ�วเตอร� | (CA: Computer Architecture)
![Page 2: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/2.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 2
Virtual Memory
Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples
![Page 3: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/3.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 3
Learning Objectives
เพิ��อศึ�กษาความส�าค�ญของหน่�วยความจำ�าเสม�อน่ เพิ��อเข!าใจำการสลั�บหน่!าตามค�าขอทั�น่ทั& (demand paging) ของ
หน่�วยความจำ�าเสม�อน่ เพิ��อเข!าใจำถ�งความซั�บซั!อน่แลัะค�าใช้!จำ�ายใน่การใช้!งาน่หน่�วยความจำ�า
เสม�อน่
![Page 4: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/4.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 4
ความเป#น่มา Background
ช้+ดค�าส��งทั&�จำะถ-กกระทั�าการได!น่�/น่จำะต!องอย-�ใน่หน่�วยความจำ�ากายภาพิเสมอ
เราจำ�าเปั1น่ต!องใส�พิ�/น่ทั&�เลัขทั&�อย-�ของหน่�วยความจำ�าแบบตรรกะทั�/งหมดลังใน่หน่�วยความจำ�าแบบกายภาพิ (แต�ถ-กจำ�าก�ดด!วยขน่าดของพิ�/น่ทั&�หน่�วยความจำ�าจำร�ง)
กรณี& single program ใช้!ว�ธี&การ overlay ค�อการทั�าให!โปัรแกรมทั&�ม&ขน่าดใหญ�กว�าพิ�/น่ทั&�ใน่
หน่�วยความจำ�า ให!สามารถด�าเน่�น่ได! จำ�ดการโดยโปัรแกรมเมอร� โดยการแบ�งโปัรแกรมออกเปั1น่โปัรแกรมย�อย (sub program)
หลัายๆ ส�วน่ แลัะให!แต�ลัะส�วน่ม&ขน่าดเลั6กกว�าหน่�วยความจำ�า กรณี& Multiprogram
ใช้!หน่�วยความจำ�าเสม�อน่ (virtual memory) จำ�ดการโดย OS
![Page 5: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/5.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 5
ความเป#น่มา Background
โปัรแกรมจำะถ-กแบ�งออกเปั1น่ 2 ส�วน่ main program เปั1น่ส�วน่ของโปัรแกรมทั&�อย-�ใน่หน่�วยความจำ�าไปัตลัอดจำน่กระทั��งการทั�างาน่ส�/น่ส+ดลัง แลัะ Sub-Program ใน่ส�วน่ทั&�เหลั�อ จำะถ-กแบ�งออกเปั1น่ส�วน่ย�อยๆ แลัะจำะถ-กโหลัดเข!าส-�หน่�วยความจำ�าก6ต�อเม��อต!องการร�น่เทั�าน่�/น่
![Page 6: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/6.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 6
ความเป#น่มา Background
หน่วยความจำ�าเสม�อน่ (Virtual memory) – การแยกส�วน่ของหน่�วยความเช้�งตรรกะ (logical memory ) ของผู้-!ใช้!ออกจำากหน่�วยความจำ�าเช้�งกายภาพิ (physical memory) ม&เพิ&ยงส�วน่ของโปัรแกรมทั&�ต!องการอย-�ใน่หน่�วยความจำ�าเพิ��อกระทั�าการ
(execution ) เทั�าน่�/น่ พิ�/น่ทั&� (Logical address) จำ�งสามารถใหญ�กว�าขน่าดของพิ�/น่ทั&�หน่�วย
ความจำ�าเช้�งกายภาพิ (physical address space ) ได! ย�น่ยอมให!ม&การใช้!พิ�/น่ทั&�หน่�วยความจำ�าร�วมก�น่ได! จำากหลัายๆ
กระบวน่การ ทั�าให!ม&การสร!างกระบวน่การข�/น่มาได!โดยสะดวก
Virtual memory สามารถใช้!งาน่ผู้�าน่ Demand paging Demand segmentation
![Page 7: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/7.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 7
Virtual Memory That is Larger Than Physical Memory
![Page 8: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/8.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 8
Demand Paging
น่�า page ไปัไว!ใน่หน่�วยความจำ�าเฉพิาะเม��อเวลัาทั&�ต!องการเทั�าน่�/น่ ลัดการใช้! (Less I/O needed) ลัดการใช้!หน่�วยความจำ�า (Less memory needed) โต!ตอบได!รวดเร6วกว�า (Faster response) รองร�บผู้-!ใช้!ได!มากกว�า (More users)
Page ทั&�ต!องการ ต!องการม&การอ!างอ�งถ�ง invalid reference ยกเลั�ก (abort) not-in-memory น่�าเข!าหน่�วยความจำ�า (bring to memory)
![Page 9: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/9.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 9
Transfer of a Paged Memory to Contiguous Disk Space
![Page 10: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/10.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 10
Valid-Invalid Bit จำะม& valid–invalid bit เช้��อมโยงอย-�ใน่แต�ลัะรายการ page table
(1 in-memory, 0 not-in-memory) ต!องก�าหน่ดค�าเร��มต!น่ valid–invalid เปั1น่ 0 ทั+กรายการ (entries) Example of a page table snapshot.
ระหว�างการแปัลังเลัขทั&�อย-� ถ!า valid–invalid bit ใน่ page table entry เปั1น่ 0 page fault
111
1
0
00
Frame # valid-invalid bit
page table
![Page 11: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/11.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 11
Page Table When Some Pages Are Not in Main Memory
![Page 12: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/12.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 12
การผิ�ดหน่�าPage Fault
ถ!าเคยม&การอ!างอ�งบน่ page คร�/งแรกของการอ!างอ�งจำะ trap to OS page fault
OS จำะมองหาตารางอ��น่เพิ��อต�ดส�น่ใจำว�า Invalid reference abort. ย�งไม�ม&อย-�ใน่หน่�วยความจำ�า
จำะหา empty frame Swap page ไปัย�ง frame น่�/น่ Reset tables, validation bit = 1. เร��มค�าส��ง: Least Recently Used
block move
เพิ��มหร�อลัดต�าแหน่�งอย�างอ�ตโน่ม�ต�
![Page 13: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/13.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 13
Steps in Handling a Page Fault
![Page 14: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/14.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 14
จำะเป#น่อยางไรถ้�าไมม(เฟรมวาง (free frame)?
การแทัน่ทั&� Page (Page replacement) – จำะค!น่หาบาง page ใน่หน่�วยความจำ�าทั&�ย�งไม�ได!ม&การใช้!งาน่จำร�ง แลั!วสลั�บออกไปั (swap out) algorithm performance – ต!องใช้! algorithm ทั&�ทั�าให!ผู้ลัการค!น่หาจำ�าน่วน่
การผู้�ดหน่!าเหลั�อน่!อยทั&�ส+ด
บางหน่!า (page) อาจำม&การน่�าเข!าออกหน่�วยความจำ�าหลัายคร�/ง
![Page 15: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/15.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 15
Performance of Demand Paging
Page Fault Rate 0 p 1.0 if p = 0 no page faults if p = 1, every reference is a fault
Effective Access Time (EAT)EAT = (1 – p) x memory access
+ p (page fault overhead+ [swap page out ]+ swap page in+ restart overhead)
![Page 16: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/16.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 16
Demand Paging Example
Memory access time = 1 microsecond
50% ของเวลัาทั&�ม&การแก!ไข page ทั&�ถ-กแทัน่ทั&� แลัะต!องถ-กสลั�บออกไปั
Swap Page Time = 10 msec = 10,000 msecEAT = (1 – p) x 1 + p (15000)
1 + 15000P (in msec)
![Page 17: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/17.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 17
การสร�างกระบวน่การProcess Creation
Virtual memory ย�น่ยอมให!ใช้!ปัระโยช้น่�อย�างอ��น่ได!ใน่ขณีะทั&�สร!างกระบวน่การ
- Copy-on-Write
- Memory-Mapped Files
![Page 18: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/18.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 18
Copy-on-Write
Copy-on-Write (COW) ย�น่ยอมให!ทั�/ง parent and child processes ใน่การก�าหน่ดค�า share ใน่ page เด&ยวก�น่ใน่หน่�วยความจำ�า
ถ!าแม!ม&การแก!ไข page ทั&�ใช้!ร �วมก�น่ ก6จำะม&เพิ&ยง page น่�/น่ทั&�ถ-กส�าเน่าไปั
COW จำะม&การสร!างกระบวน่การทั&�สะดวกกว�า เช้�น่เด&ยวก�บการส�า page ทั&�ม&การแก!ไข
Free pages จำะถ-กจำ�ดสรรจำาก pool ของ zeroed-out pages.
![Page 19: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/19.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 19
Memory-Mapped Files
Memory-mapped file I/O ย�น่ยอมให! file I/O ถ-กใช้!งาน่เหม�อน่ร-ทั&น่การเข!าถ�งหน่�วยความจำ�าได!โดยการ mapping a disk block ไปัเปั1น่ page ใน่หน่�วยความจำ�า
ไฟลั�จำะถ-กก�าหน่ดค�าเร��มต!น่ของการอ�าน่โดยใช้! demand paging ส�ดส�วน่ของ page-sized ของไฟลั�ทั&�อ�าน่จำากระบบไฟลั�ไปัย�ง physical page. ลั�าด�บย�อยของ reads/writes ไฟลั�จำะด�าเน่�น่การเช้�น่เด&ยวก�บการเข!าถ�งหน่�วยความจำ�า
การเข!าถ�งไฟลั�อย�างง�ายโดย treating file I/O ผู้�าน่หน่�วยความจำ�ามากกว�าการใช้! read() write() system calls.
อน่+ญาตให!หลัายกระบวน่การทั�าการ map ไฟลั�เด&ยวก�น่ให!ใช้! pages ร�วมก�น่ใน่หน่�วยความจำ�าได!อ&กด!วย
![Page 20: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/20.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 20
Memory Mapped Files
![Page 21: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/21.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 21
การแทน่ท(,หน่�าPage Replacement
การปั:องก�น่การจำ�ดสรรเก�น่หน่�วยความจำ�าได!โดยการแก!ไข page-fault service routine เพิ��มเข!าไปัใน่ page replacement
ใช้! modify (dirty) bit ใน่การลัด overhead ของการถ�ายหน่!า โดยการแก!ไขเฉพิาะหน่!าทั&�ถ-กเข&ยน่ลังบน่ด�สก�เทั�าน่�/น่
Page replacement จำะแยกจำากก�น่อย�างอ�สระระหว�าง logical memory แลัะ physical memory – virtual memory ขน่าดใหญ�สามารถสร!างบน่ physical memory ทั&�ม&ขน่าดเลั6กกว�าได!
![Page 22: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/22.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 22
Need For Page Replacement
![Page 23: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/23.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 23
Basic Page Replacement
1 .หาต�าแหน่�งของ page ทั&�ต!องการบน่ด�สก�
2. หา free frame:- ถ!าม& free frame ก6ใช้!- ถ!าไม�ม& free frame ให!ใช้! page replacement
algorithm ใน่การเลั�อก victim frame
3. อ�าน่ page ทั&�ต!องการไปัไว!บน่ free frame ใหม� แลั!ว Update ตาราง page แลัะ frame
4. เร��มด�าเน่�น่การกระบวน่การ
![Page 24: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/24.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 24
Page Replacement
![Page 25: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/25.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 25
Page Replacement Algorithms
ต!องการให!ม&อ�ตราการผู้�ดหน่!าต��าทั&�ส+ด (lowest page-fault rate)
ปัระเม�น่ algorithm โดยร�น่ reference string บน่หน่�วยความจำ�าแลัะค�าน่วณีจำ�าน่วน่ page faults ของ string น่�/น่
In all our examples, the reference string is 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5.
![Page 26: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/26.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 26
Graph of Page Faults Versus The Number of Frames
![Page 27: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/27.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 27
First-In-First-Out (FIFO) Algorithm
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 3 frames (3 pages สามารถอย-�ใน่หน่�วยความจำ�า ณี เวลัาใดเวลัาหน่��ง ต�อหน่��ง
กระบวน่การ)
4 frames
FIFO Replacement – Belady’s Anomaly frames ย��งมาก page faults ย��งลัดลัง
1
2
3
1
2
3
4
1
2
5
3
4
9 page faults
1
2
3
1
2
3
5
1
2
4
5 10 page faults
44 3
![Page 28: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/28.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 28
FIFO Page Replacement
![Page 29: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/29.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 29
FIFO Illustrating Belady’s Anamoly
![Page 30: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/30.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 30
Optimal Algorithm
แทัน่ทั&� page ทั&�จำะไม�ถ-กเร&ยกใช้!อ&กใน่เวลัาอ�น่ใกลั!น่&/ 4 frames example
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
จำะแทัน่ทั&�โดยว�ธี&ใด จำะว�ดปัระส�ทัธี�ภาพิของอ�ลักอร�ธี�มได!อย�างไร
1
2
3
4
6 page faults
4 5
![Page 31: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/31.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 31
Optimal Page Replacement
![Page 32: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/32.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 32
Least Recently Used (LRU) Algorithm
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
ใช้!ต�วน่�บ (Counter implementation) น่�บรายการ (entry) ทั+ก page ทั&�เข!ามา , ทั+กคร�/งทั&� page ถ-กอ!างอ�ง
ผู้�าน่รายการ , แลั!วส�าเน่าค�าน่าฬิ�กาไปัย�ง counter เม��อต!องการเปัลั&�ยน่ page ก6ให!มองหา counter เพิ��อจำะใช้!เปัลั&�ยน่
หน่!า
1
2
3
5
4
4 3
5
![Page 33: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/33.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 33
LRU Page Replacement
![Page 34: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/34.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 34
LRU Algorithm (Cont.)
ใช้!แบบสแต6ก (Stack implementation) – เก6บสแต6กของหมายเลัขหน่!า (page numbers ) ใน่ร-ปัการเช้��อมโยงแบบค-� (double link form): Page referenced:
ย!ายเพิ6จำน่�/น่ไปัย�งด!าน่บน่ ใช้! 6 pointers ใน่การเปัลั&�ยน่
ไม�จำ�าเปั1น่ต!องค!น่หาเพิ��อแทัน่ทั&�
การใช้! stack algorithm ช้�วยแก!ปั�ญหา Belady’s Anomaly แต�ต!องใช้!อ+ปักรณี�ช้�วยแทัน่การใช้!เข&ยน่โปัรแกรมควบค+มการเร&ยก
interrupt เอง
![Page 35: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/35.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 35
Use Of A Stack to Record The Most Recent Page References
![Page 36: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/36.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 36
LRU Approximation Algorithms
การเพิ��มบ�ตอ!างอ�ง (Additional-Reference-bits algorithm) ใช้!บน่ระบบทั&�ไม�ม&อ+ปักรณี�ช้�วย จำ�บค-�แต�ลัะหน่!าด!วย bit, ค�าเร��มต!น่เปั1น่ = 0 (ย�งไม�ใช้!) เม��อหน่!าถ-กอ!างอ�ง บ�ตอ!างอ�งกะจำะถ-กเปัลั&�ยน่เปั1น่ 1 (ใช้!แลั!ว) การแทัน่ทั&�ใน่ระบบน่&/สามารถตรวจำสอบได!ว�าม&หน่!าใดถ-กใช้!แลั!วหร�อย�ง
การให!โอกาสคร�/งทั&�สอง (Second chance) ใช้!บ�ตอ!างอ�ง (reference bit ) ใช้!การแทัน่ทั&�แบบน่าฬิ�กา (Clock
replacement ) เพิ��อปั:องก�น่การส�บเปัลั&�ยน่หน่!าทั&�ถ-กเร&ยกใช้!บ�อยออกไปั
ถ!าหน่!าถ-กแทัน่ทั&� (ตามลั�าด�บน่าฬิ�กา ) บ�ตอ!างอ�งจำะ = 1 จำากน่�/น่: เปัลั&�ยน่บ�ตกลั�บไปัเปั1น่ 0 เปัลั&�ยน่เวลัาทั&�เข!ามาใน่หน่�วยความจำ�าให!เหม�อน่เพิ��งเข!ามา ทั�าการตรวจำสอบหน่!าอ��น่ๆ ตามหลั�กการเช้�น่เด&ยวก�น่
![Page 37: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/37.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 37
Second-Chance (clock) Page-Replacement Algorithm
![Page 38: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/38.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 38
Counting Algorithms
การใช้!ต�วน่�บการอ!างอ�งของแต�ลัะหน่!าไว!
LFU (least frequently used) Algorithm: การเก6บจำ�าน่วน่คร�/งทั&�หน่!าถ-กอ!างอ�ง แลัะเลั�อกทั&�ถ-กอ!างอ�งน่!อยทั&�ส+ดออกก�อน่
MFU (most frequently used) Algorithm: อย-�บน่สมม+ต�ฐาน่ทั&�ว�า หน่!าทั&�ถ-กอ!างอ�งน่!อยน่�/น่ม&โอกาสทั&�จำะถ-กมากใน่เวลัาต�อไปัเพิราะอาจำเปั1น่หน่!าทั&�เพิ��งถ-กย!ายเข!ามาใน่หน่�วยความจำ�า
อย�างไรก6ตาม ว�ธี&การแทัน่ทั&�หน่!าทั�/งแบบใช้!น่!อยออกก�อน่แลัะแบบใช้!มากออกก�อน่น่�/น่เปั1น่แน่วค�ดทั&�ไม�แตกต�างก�น่เทั�าใดน่�ก ปัระส�ทัธี�ภาพิจำ�งข�/น่อย-�ก�บการคาดการณี�ลั�วงหน่!าได!ถ-กต!องมากน่!อยแค�ไหน่ การใช้!งาน่ม&ค�าใช้!จำ�ายส-ง แลัะส-!ว�ธี&การแบบเหมาะทั&�ส+ด (OPT) ไม�ได!
![Page 39: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/39.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 39
Page-Buffering Algorithm
ระบบทั&�ม�กเก6บหน่!าว�างๆ ไว!เปั1น่กลั+�มๆ เม��อเก�ด page fault หน่!าว�างๆ หน่!าหน่��งก6จำะถ-กเลั�อกมาใช้! แต�ย�ง
ไม�ย!ายออกไปัทั�น่ทั& ระบบจำะอ�าน่หน่!าทั&�ต!องการเข!ามาใน่เน่�/อทั&�ว�างได!เลัยโดยไม�ต!องรอให!
ม&การย!ายหน่!าเด�มออกไปัก�อน่ ทั�าให!ลัดเวลัาใน่การย!ายหน่!าลังมาก เม��อม&เวลัาว�าง ระบบค�อยทั�าการ
ย!ายหน่!าทั&�เลั�อกออกไปัใน่ภายหลั�ง จำากน่�/น่ รวมพิ�/น่ทั&�ว�างเข!าไว!ใน่กลั+�ม (clustering)
![Page 40: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/40.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 40
Operating System Examples
Windows NT
Solaris 2
![Page 41: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/41.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 41
Windows NT
ใช้! demand paging ร�วมก�บ clustering โดยใส�ใน่ page รอบๆบร�เวณีทั&�เก�ดการผู้�ดหน่!า
กระบวน่การทั�/งหมดจำะถ-กก�าหน่ด working set minimum แลัะ working set maximum
Working set minimum ค�อจำ�าน่วน่ต��าส+ดของหน่!าอย-�ใน่หน่�วยความจำ�า
กระบวน่การหน่��งอาจำถ-กก�าหน่ด working set maximum ม&อย-�หลัายๆ หน่!า
เม��อพิ�/น่ทั&�ว�างใน่หน่�วยความจำ�าเหลั�อกว�าค�าเปั:าหมายทั&�ก�าหน่ดไว! (threshold ) การต�ดแต�ง working set อย�างอ�ตโน่ม�ต� (automatic working set trimming) ก6จำะเร��มทั�างาน่ เพิ��อค�น่พิ�/น่ทั&�ว�างของหน่�วยความจำ�า
Working set trimming จำะย!ายหน่!าออกจำากกระบวน่การทั&�ม&การใช้!งาน่หน่!า working set minimum ของกระบวน่การเหลั�าน่�/น่
![Page 42: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/42.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 42
Paging System ของ Windows NT
![Page 43: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/43.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 43
Address translation ของ Windows
![Page 44: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/44.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 44
Solaris 2
บ�าร+งร�กษารายการของหน่!าว�าง (free pages) เพิ��อก�าหน่ดให!ก�บกระบวน่การทั&�เก�ดการผู้�ดหน่!า
Lotsfree – threshold parameter จำ+ดเร��มต!น่ของการค!น่หาหน่!า
การค!น่หาหน่!าจำะทั�าโดยกระบวน่การสลั�บหน่!าออก (pageout process)
Pageout scans pages using modified clock algorithm.
Scanrate ค�อ อ�ตราทั&�หน่!าจำะถ-กสแกน่ ม&ขอบเขตต�/งแต� slowscan ถ�ง fastscan
Pageout ถ-กเร&ยกใช้!บ�อยหร�อไม�ข�/น่ก�บจำ�าน่วน่หน่�วยความจำ�าทั&�ว�างเหลั�อมากน่!อยเพิ&ยงใด
![Page 45: 6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)](https://reader036.vdocuments.pub/reader036/viewer/2022062614/546d0e6daf795912528b563f/html5/thumbnails/45.jpg)
ว�เชษฐ์� พลายมาศ | ระบบปฏิ�บ ติ�การ (OS: Operating Systems) | หน่�วยความจำ�าเสม�อน่ (Virtual Memory) | 45
Solar Page Scanner