Chapter 9:  หน่วยความจำเสมือน Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames  Thrashing Demand Segmentation Operating System Examples พื้นฐาน การจัดเพจตามต้องความต้องการ การสร้างโปรเซส  การแทนที่เพจ การจัดสรรพื้นที่สำหรับเฟรม การทำแธรช การทำเซ็กเม้นท์ตามความต้องการ ตัวอย่างระบบปฏิบัติการ
Background พื้นฐาน  หน่วยความจำเสมือน เป็นการแบ่งหน่วยความจำเชิงตรรกของผู้ใช้ออกจาก หน่วยความจำทางกายภาพ  เป็นส่วนเดียวของโปรแกรมที่ต้องการเข้าไปอยู่ในหน่วยความจำ เพื่อการเอ็กซ์ซีคิวท์หรือการประมวลผล  พื้นที่แอดเดรสเชิงตรรก สามารถที่จะมีขนาดที่ใหญ่กว่าพื้นที่แอดเดรสทางกายภาพ  ยอมให้พื้นที่แอดเดรสถูกแบ่งปันการใช้งานจากหลายๆ โปรเซส ยอมให้เกิดหรือสร้างโปรเซสหลายโปรเซสขึ้นได้อย่างมีประสิทธิภาพ  หน่วยความจำเสมือน สามารถถูกใช้ผ่าน การจัดเพจตามความต้องการ การทำเซ็กเม้นท์ตามความต้องการ
Virtual Memory That is Larger Than Physical Memory หน่วยความจำเสมือนที่มีขนาดใหญ่กว่าหน่วยความจำทางกายภาพ
Demand Paging การจัดเพจแบบตามความต้องการ นำเพจไปไว้ในหน่วยความจำ เมื่อมีความต้องการ ต่างๆ ดังนี้  มีความต้องใช้อินพุทเอ้าท์พุทน้อย มีความต้องการใช้หน่วยความจำน้อย มีการตอบสนองที่รวดเร็ว มีผู้ใช้งานจำนวนมาก  มีความต้องการเพจ     โดยการอ้างอิงถึง  การอ้างอิงไม่ถูกต้อง     ปฏิเสธ ไม่อยู่ในหน่วยความจำ     นำไปไว้ในหน่วยความจำ
Transfer of a Paged Memory to Contiguous Disk Space การโอนย้ายหน่วยความจำที่ถูกจัดเพจ ไปไว้ยังพื้นที่ต่อเนื่องบนดิสก์
Valid-Invalid Bit บิทบ่งชี้ความถูกต้องและไม่ถูกต้อง ด้วยการเข้าถึงแต่ละตารางเพจ สัมพันธ์อยู่กับบิทบ่งชี้ความถูกต้องและไม่ถูกต้อง ( 1     อยู่ในหน่วยความจำ ,  0     ไม่อยู่ในหน่วยความจำ ) ในเวลาเริ่มต้นบิทบ่งชี้ความถูกต้องและไม่ถูกต้องถูกกำหนดให้เป็น  0  สำหรับทุกๆ การเข้าถึง ตัวอย่างของตารางเพจ ในขณะทำการแปลงค่าแอดเดรส หากบิทบ่งชี้ฯ ในการเข้าถึงตารางเพจ มีค่าเป็น  0     ไม่สามารถเข้าถึงเพจได้ หรือเกิดการผิดพลาด 1 1 1 1 0 0 0  Frame # valid-invalid bit page table
Page Table When Some Pages Are Not in Main Memory ตารางเพจ ในขณะที่บางเพจไม่อยู่ในหน่วยความจำหลัก
เพจผิดพลาด  Page Fault หากเกิดการอ้างอิงไปยังเพจใดๆ การอ้างอิงครั้งแรก จะแทรปไปยังระบบปฏิบัติการ     เพจผิดพลาด ระบบปฏิบัติการจะพิจารณาไปยังตารางอื่นๆ เพื่อการตัดสินใจ  การอ้างอิงไม่ถูกต้อง     ปฏิเสธ ยังไม่อยู่ในหน่วยความจำ รับเฟรมว่าง สลับเพจไปไว้ในเฟรม  กำหนดค่าตารางใหม่ บิทบ่งชี้ฯ  = 1 ทำคำสั่งอีกครั้ง  auto increment/decrement location
ขั้นตอนในการจัดการเพจผิด  Steps in Handling a Page Fault
ขั้นตอนในการจัดการเพจผิด  Steps in Handling a Page Fault 1.  ตรวจดูตารางข้อมูลการควบคุมโปรเซส  ( PCB)  ที่อ้างอิงถึงเพจที่ต้องการถูกต้องหรือไม่  2.  หากการอ้างอิงผิดพลาดให้ยกเลิก โปรเซส หากการอ้างอิงถูกต้อง แต่เพจที่ต้องการไม่มีอยู่ในหน่วยความจำหลัก ให้ไปดำเนินการขั้นต่อไป  3.  หาพื้นที่ว่างในเฟรม  1  เพจ โดยอาจเลือกจากรายการแสดงแฟรมว่าง  4.  ส่งรีเควสต์ไปยังฮาร์ดดิสก์ เพื่อให้ฮาร์ดดิสก์ทำการอ่านเพจที่ต้องการส่งมาไว้ยังพื้นที่ว่างในเฟรมที่เลือกไว้ก่อนหน้า 5.  เมื่อดิสก์โหลดเพจเสร็จสิ้นแล้ว ระบบจะทำการแก้ไขบิตสถานะในตารางเพจ เป็น “สามารถใช้งานได้”  6.  ให้โปรเซสทำงานต่อจากจุดที่เกิด “การอ้างอิงถึงเพจผิดพลาด หรือ  Page Fault ”  โปรเซสก็จะสามารถทำงานต่อไปเสมือนหนึ่งว่า เพจที่ต้องการใช้งานมีอยู่ในหน่วยความจำตลอดเวลา
กรณีไม่มีเฟรมว่างจะเกิดอะไรขึ้น   What happens if there is no free frame? การจัดสรรพื้นที่ให้เพจใหม่ โดยหาเพจใดๆ ในหน่วยความจำ แต่ไม่ได้ถูกใช้งานจริง และทำการสลับมันออกไป  อัลกอริธึม  ประสิทธิภาพ ต้องการอัลกอริธึมเพื่อจัดการให้ได้ผลลัพท์ ที่มีจำนวนเพจผิดพลาดน้อยที่สุด โดยเพจเดียวกันอาจถูกนำไปไว้ยังหน่วยความจำเวลาต่างๆ กัน
ประสิทธิภาพของการจัดเพจตามความต้องการที่เกิดขึ้น Performance of Demand Paging อัตราการเกิดเพจผิดพลาด  ให้  P  เป็นโอกาสการเกิดความผิดพลาดการจัดเพจผิดพลาด เพื่อประสิทธิภาพที่ดีจึงต้องการให้  P  มีค่าเข้าใกล้  0  มากที่สุด ซึ่งหมายถึงเกิดความผิดพลาดน้อยที่สุด  ระยะเวลาการเข้าถึงหน่วยความจำ  Effective Access Time (EAT) EAT = (1 –  p ) x memory access +  p  (page fault overhead + [swap page out ] + swap page in + restart overhead)
ตัวอย่างการจัดเพจตามความต้องการ Demand Paging Example Swap Page Time = 10 msec = 10,000 msec EAT = (1 – p) x 1 + p (15000) 1 + 15000P  (in msec) ระยะเวลาในการเข้าถึงหน่วยความจำ  1  ไมโครวินาที 50 %  ของระยะเวลาของเพจที่ถูกใช้ไปในการสลับเข้าออกจากพื้นที่หน่วยความจำ ระยะเวลาในการสลับการทำงาน
การสร้างโปรเซส Process Creation Virtual memory allows other benefits during process creation: - Copy-on-Write - Memory-Mapped Files หน่วยความจำเสมือนยอมให้เกิดการทำงานอื่นๆ ขึ้นได้ระหว่างการสร้างโปรเซส  ก๊อปปี้ ขณะ เขียน ไฟล์ที่แมพค่าเข้ากับหน่วยความจำ
COW  ยอมให้ทั้งโปรเซสต้นกำเนิดและโปรเซสลูก สามารถใช้เพจร่วมกันในหน่วยความจำ  หาก ทั้งสองโปรเซสเปลี่ยนแปลงเพจที่ใช้งานร่วมกัน ก็เพียงทำการสำเนาเพจนั้นไว้ก่อน COW  ยอมให้การสร้างโปรเซสหลายโปรเซสได้อย่างมีประสิทธิภาพ โดยเพียงแต่แก้ไขเพจที่ถูกสำเนา เพจว่างถูกจัดสรรให้จาก พูลของเพจที่ไม่ได้ถูกใช้งาน Copy-on-Write
Memory-Mapped Files Memory-mapped file I/O allows file I/O to be treated as routine memory access by  mapping  a disk block to a page in memory. A file is initially read using demand paging. A page-sized portion of the file is read from the file system into a physical page. Subsequent reads/writes to/from the file are treated as ordinary memory accesses. Simplifies file access by treating file I/O through memory rather than  read()   write()  system calls. Also allows several processes to map the same file allowing the pages in memory to be shared.
Memory Mapped Files
Page Replacement การสลับแทนที่เพจ  การเกิดการอ้างอิงถึงเพจที่ไม่มีอยู่ในหน่วยความจำหลัก ทำให้เกิดปัญหาการผิดพลาดการจัดเพจ โดยฮาร์ดแวร์จะทำการแจ้งไปยังระบบปฏิบัติการ โดยตรวจสอบความผิดพลาดที่เกิดขึ้นจากตารางการขัดจังหวะ จากนั้นระบบจะดำเนินการโหลดเพจที่ต้องการกลับมาไว้ยังพื้นที่ในเฟรมว่างที่จัดสรรไว้ก่อนแล้ว เพื่อการทำงานของโปรเซสที่ต้องการใช้หรือเกิดการอ้างอิงถึงเพจดังกล่าวสามารถทำงานต่อไปได้
Page Replacement แต่หากการโหลดเพจที่ต้องการนั้นกลับเข้ามา ระบบไม่สามารถจัดหาเฟรมว่างเพื่อรองรับเพจนั้นได้ ระบบสามารถดำเนินการด้วยวิธีต่อไปนี้  ให้โปรเซสที่อ้างอิงถึงเพจนั้นหยุดการทำงาน ชั่วคราวจนกว่าจะเกิดเฟรมว่างจากการคืนทรัพยากรของโปรเซสอื่นๆ แต่ด้วยวิธีนี้จะส่งผลต่อผู้ใช้งานที่สั่งให้เกิดการทำงานของโปรเซสนั้นขึ้น และทำให้ประสิทธิภาพการทำงานของระบบโดยรวมลดลง
Page Replacement 2.  การให้โปรเซสใดๆ หยุดการทำงานชั่วคราว เพื่อสามารถนำเพจที่เกิดจากการอ้างอิงของโปรเซสใดๆ นั้นไปเก็จไว้ยังฮาร์ดดิสก์ก่อน ซึ่งจะทำให้เกิดพื้นที่ว่างหรือเฟรมว่างในหน่วยความจำหลัก และสามารถนำเฟรมว่างที่เกิดขึ้นนี้ไปให้เพจที่ต้องการเข้าใช้งานก่อน
Page Replacement 3.  โดยวิธีที่สองนี้เรียกว่า เทคนิคการสับเปลี่ยนเพจ  (Page Replacement)  กล่าวคือหากในหน่วยความจำหลักไม่มีเฟรมว่างอยู่เลย อันเนื่องมาจากการใช้งานของเพจใดๆ อยู่เต็มทุกพื้นที่ เมื่อเกิดความต้องการการเข้าใช้งานเฟรม หรือต้องการเฟรมว่าง จากเพจที่เกิดขึ้นใหม่และจำเป็นต้องจัดสรรเฟรมว่างให้ใช้งานก่อน ระบบจะทำการจัดสรรเฟรมว่างด้วยการนำเพจที่มีอยู่ในหน่วยความจำหลักก่อนหน้านี้ และเกิดการใช้งานน้อยที่สุดหรือเกิดขึ้นก่อนสุด ให้ออกไปจากหน่วยความจำหลักไปไว้ยังฮาร์ดดิสก์ก่อนชั่วคราวเพื่อให้เกิดเฟรมว่างขึ้นในหน่วยความจำหลัก จากนั้นจึงนำเฟรมที่ว่างขึ้นไปให้กับเพจที่เกิดขึ้นภายหลังและจำเป็นต้องเข้าทำงานก่อนเข้าใช้ ทั้งนี้ระบบจะต้องทำการแก้ไขตารางการจัดเพจให้สถานะของเพจที่ถูกนำออกไปจากหน่วยความจำหลักนั้นมีสถานะเป็นไม่สามารถใช้งานได้เพื่อป้องกันความผิดพลาดที่อาจเกิดขึ้นจากโปรเซสที่อ้างถึงเพจนั้นด้วย
Need For Page Replacement
Basic Page Replacement Find the location of the desired page on disk. Find a free frame: - If there is a free frame, use it. - If there is no free frame, use a page replacement  algorithm to select a  victim  frame. Read the desired page into the (newly) free frame. Update the page and frame tables. Restart the process.
Page Replacement
Page Replacement Algorithms In all our examples, the reference string is  1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5. อัลกอริธึมการสลับเพจ ด้วยเทคนิคการสลับเพจมีหลากหลายวิธี โดยปกติจะเลือกใช้วิธีที่ทำให้เกิดความผิดพลาดในการอ้างอิงถึงเพจ ต่ำสุด  สามารถค้นหาวิธีที่ดีที่สุดได้ด้วยการทดสอบด้วยการสร้างข้อมูล เพื่อให้เกิดการเข้าใช้ และดูว่าวิธีใดเกิดการผิดพลาดน้อยครั้งที่สุด
Graph of Page Faults Versus The Number of Frames
First-In-First-Out (FIFO) Algorithm   อัลกอริธึมแบบ มาก่อนออกก่อน  Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 3 frames (3 pages can be in memory at a time per process) 4 frames FIFO Replacement – Belady’s Anomaly more frames    less 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 4 4 3
FIFO Page Replacement (First-In-First-Out Algorithm: FIFO) เป็นวิธีการจัดการที่ง่ายที่สุด  เลือกเพจที่มีอายุอยู่ในหน่วยความจำหลักมากที่สุด เป็นเพจที่จะต้องออกไปก่อน แต่ด้วยวิธีนี้เพจที่เก็บค่าสำคัญเพื่อการทำงานหลักของระบบอาจเป็นเพจที่ถูกเลือกให้เป็นเพจที่ต้องออกจากหน่วยความจำ และเมื่อเกิดการอ้างอิงถึงเพจนั้นก็จะต้องทำการโหลดเข้ามาใหม่ หากเพจนั้นถูกอ้างอิงถึงบ่อย จะทำให้เกิดความผิดพลาดในการจัดเพจ ขึ้นบ่อยไปด้วยแม้ว่าจะสามารถโหลดเพจนั้นกลับเข้ามาใหม่ก็ตามแต่ก็จะเกิดความสูญเสียเวลาและงานขึ้น ทำให้ประสิทธิภาพของระบบลดลง  ปรากฏการณ์เช่นนี้เรียกว่า ความผิดปกติ แบบเบลาดี้
FIFO Illustrating Belady’s Anomaly
Optimal Algorithm Replace page that will not be used for longest period of time. 4 frames example   1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 How do you know this? Used for measuring how well your algorithm performs. 1 2 3 4 6 page faults 4 5
Optimal Page Replacement อัลกอริธึมแบบประสิทธิภาพดี เป็นวิธีที่สามารถลดปัญหาการเกิดความผิดพลาดการจัดเพจ และหลีกเลี่ยงปัญหาความผิดพลาดแลลเบลาดี้  เลือกสับเปลี่ยนเพจที่จะเกิดการอ้างอิงถึงในอนาคตอันไกลที่สุด ออกจากหน่วยความจำก่อน  ทำให้เกิดความผิดพลาดจากการจัดเพจขึ้นน้อยที่สุด
Least Recently Used (LRU) Algorithm Reference string:  1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 Counter implementation Every page entry has a counter; every time page is referenced through this entry, copy the clock into the counter. When a page needs to be changed, look at the counters to determine which are to change. 1 2 3 5 4 4 3 5
LRU Page Replacement อัลกอริธึมแบบเพจที่เคยถูกใช้เป็นครั้งสุดท้ายนานที่สุด  ด้วยแบบประสิทธิภาพดีที่สุดยากต่อการเขียนโปรแกรม เนื่องจากยากที่จะทราบได้ว่าเพจใดจะถูกเรียกใช้ในอนาคตอันไกลสุด เพราะต้องเกิดจากการคาดการณ์ซึ่งยากที่จะคาดการณ์ได้
LRU Algorithm (Cont.) LRU  เป็นวิธีที่ให้ผลทางประสิทธิภาพใกล้เคียงกับแบบประสิทธิภาพดี มากที่สุดโดยมีแนวคิดในการจัดการคือ เมื่อเกิดความต้องการที่จะต้องสลับเพจหนึ่งเพจใดออกจากหน่วยความจำหลัก ระบบจะเลือกเพจที่เคยถูกใช้เป็นครั้งสุดท้ายที่มีระยะเวลานานที่สุด หรืออีกนัยหนึ่งคือเพจนั้นเคยถูกใช้งานล่าสุดมานานกว่าเพจใดๆ ในระบบ จึงคาดว่าระบบนั้นอาจจะไม่ถูกใช้งานหรืออีกนานกว่าจะถูกใช้งาน หรือมีความถี่ที่ถูกอ้างอิงถึงน้อยที่สุดนั่นเอง ระบบก็จะเลือกนำเพจนี้ออกไปจากหน่วยความจำหลักก่อนเพื่อเปิดโอกาสให้เกิดเฟรมว่างขึ้นในหน่วยความจำหลักและสามารถให้เพจที่เกิดขึ้นใหม่และจำเป็นต้องเข้าใช้หน่วยความจำหลักสามารถทำงานได้  ระบบจะทำการบันทึกเวลาที่แต่ละเพจถูกอ้างอิงถึง ครั้งล่าสุดไว้ เมื่อต้องการเลือกเพจใดให้ออกจากหน่วยความจำ ก็จะเลือกจากเพจที่ไม่ถูกอ้างอิงหรือใช้งานเป็นระยะเวลานานที่สุด
Use Of A Stack to Record The Most Recent Page References การใช้แสต็กในการจัดการกับ อ้างอิงถึงเพจเพื่อให้เพจที่ถูกอ้างอิงถึงล่าสุดอยู่บนสุด
LRU Approximation Algorithms  อัลกอริธึมเลียนแบบ  LRU  โดยอาศัยบิทอ้างอิง  ขณะเริ่มต้นเพจใดๆ จะถูกกำหนดให้มีบิทด้างอิงมีค่าเป็น  0 เมื่อเพจนั้นๆ ถูกอ้างอิงถึงบิทอ้างอิงจะถูกเปลี่ยนค่าไปเป็น  1  อัลกอริธึมแบบ เปิดโอกาสให้เป็นครั้งที่สอง มีการอ้างอิงถึงบิทอ้างอิง มีการสลับเวลา เมื่อเพจถูกโหลดกลับเข้ามาใว้ในหน่วยความจำอีกครั้ง จากการกำหนดของเวลา และบิทอ้างอิงมีค่าเป็น  1  แล้วให้ดำเนินการ กำหนดค่าบิทอ้างอิงให้เป็น  0 นำเพจออกจากหน่วยความจำ  นำเพจถัดไปมาไว้ในหน่วยความจำ ด้วยเงื่อนไขเช่นเดียวกัน
Second-Chance (clock) Page-Replacement Algorithm
Counting Algorithms   อัลกอริธึมการนับ  ใช้ตัวนับจำนวนในการอ้างอิงถึงแต่ละเพจ  อัลกอริธึมแบบ  LFU  จะเลือกเพจที่ต้องถูกนำออกจาหน่วยความจำจากการนับที่มีค่าน้อยที่สุด  อัลกอริธึมแบบ  MFU  จะเลือกเพจที่มีการอ้างอิงถึงมากออกก่อน จากแนวคิดที่ว่าเพจที่มีการอ้างอิงถึงน้อยเป็นเพจที่เพิ่งเกิดใหม่ ส่วนเพจที่ถูกอ้างอิงถึงบ่อยเป็นเพจที่เกิดขึ้นมานานแล้ว
Allocation of Frames Each process needs  minimum  number of pages. Example:  IBM 370 – 6 pages to handle SS MOVE instruction: instruction is 6 bytes, might span 2 pages. 2 pages to handle  from . 2 pages to handle  to . Two major allocation schemes. fixed allocation priority allocation
Fixed Allocation Equal allocation – e.g., if 100 frames and 5 processes, give each 20 pages. Proportional allocation – Allocate according to the size of process.
Priority Allocation Use a proportional allocation scheme using priorities rather than size. If process  P i  generates a page fault, select for replacement one of its frames. select for replacement a frame from a process with lower priority number.
Global vs. Local Allocation Global  replacement – process selects a replacement frame from the set of all frames; one process can take a frame from another. Local  replacement – each process selects from only its own set of allocated frames.
Thrashing If a process does not have “enough” pages, the page-fault rate is very high.  This leads to: low CPU utilization. operating system thinks that it needs to increase the degree of multiprogramming. another process added to the system. Thrashing     a process is busy swapping pages in and out.
Thrashing  Why does paging work? Locality model Process migrates from one locality to another. Localities may overlap. Why does thrashing occur?   size of locality > total memory size
Locality In A Memory-Reference Pattern
Working-Set Model       working-set window    a fixed number of page references  Example:  10,000 instruction WSS i  (working set of Process  P i ) = total number of pages referenced in the most recent    (varies in time) if    too small will not encompass entire locality. if    too large will encompass several localities. if    =       will encompass entire program. D  =     WSS i     total demand frames  if  D  >  m     Thrashing Policy if  D  > m, then suspend one of the processes.
Working-set model
Keeping Track of the Working Set Approximate with interval timer + a reference bit Example:    = 10,000 Timer interrupts after every 5000 time units. Keep in memory 2 bits for each page. Whenever a timer interrupts copy and sets the values of all reference bits to 0. If one of the bits in memory = 1    page in working set. Why is this not completely accurate? Improvement = 10 bits and interrupt every 1000 time units.
Page-Fault Frequency Scheme Establish “acceptable” page-fault rate. If actual rate too low, process loses frame. If actual rate too high, process gains frame.
Other Considerations Prepaging  Page size selection fragmentation table size  I/O overhead locality
Other Considerations (Cont.) TLB Reach  - The amount of memory accessible from the TLB. TLB Reach = (TLB Size) X (Page Size) Ideally, the working set of each process is stored in the TLB. Otherwise there is a high degree of page faults.
Increasing the Size of the TLB Increase the Page Size . This may lead to an increase in fragmentation as not all applications require a large page size. Provide Multiple Page Sizes . This allows applications that require larger page sizes the opportunity to use them without an increase in fragmentation.
Other Considerations (Cont.) Program structure int A[][] = new int[1024][1024]; Each row is stored in one page  Program 1  for (j = 0; j < A.length; j++) for (i = 0; i < A.length; i++) A[i,j] = 0; 1024 x 1024 page faults  Program 2  for (i = 0; i < A.length; i++) for (j = 0; j < A.length; j++) A[i,j] = 0; 1024 page faults
Other Considerations (Cont.) I/O Interlock  – Pages must sometimes be locked into memory. Consider I/O. Pages that are used for copying a file from a device must be locked from being selected for eviction by a page replacement algorithm.
Reason Why Frames Used For I/O Must Be In Memory
Demand Segmentation Used when insufficient hardware to implement demand paging. OS/2 allocates memory in segments, which it keeps track of through segment descriptors Segment descriptor contains a valid bit to indicate whether the segment is currently in memory. If segment is in main memory, access continues, If not in memory, segment fault.
Operating System Examples Windows NT Solaris 2
Windows NT Uses demand paging with  clustering . Clustering brings in pages surrounding the faulting page. Processes are assigned  working set minimum  and  working set maximum . Working set minimum is the minimum number of pages the process is guaranteed to have in memory. A process may be assigned as many pages up to its working set maximum. When the amount of free memory in the system falls below a threshold,  automatic working set trimming  is performed to restore the amount of free memory. Working set trimming removes pages from processes that have pages in excess of their working set minimum.
Solaris 2 Maintains a list of free pages to assign faulting processes. Lotsfree  – threshold parameter to begin paging. Paging is performed by  pageout  process. Pageout scans pages using modified clock algorithm. Scanrate  is the rate at which pages are scanned. This ranged from  slowscan  to  fastscan . Pageout is called more frequently depending upon the amount of free memory available.
Solaris 2 Page Scanner
Fig 10.03
Fig. 10.2

More Related Content

PPT
Lecture9
PPT
10 ca-mass-storage system structure (wichet p.'s conflicted copy 2012-12-17)
PPT
PPT
6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)
PPTX
Windows memory management
PPT
PDF
ระบบติดตามรถ
PPT
Ch07ath
Lecture9
10 ca-mass-storage system structure (wichet p.'s conflicted copy 2012-12-17)
6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)
Windows memory management
ระบบติดตามรถ
Ch07ath

What's hot (20)

DOC
แผนการสอน โปรแกรมคอมพิวเตอร์กราฟิก ม.4
PPT
PPT
Operating System Chapter 4
PPT
PDF
cache memory
PPT
8 ca-file system implementation (wichet p.'s conflicted copy 2012-12-17)
PPTX
Paging and Segmentation
PDF
ระบบจองห้องประชุมออนไลน์
DOC
แผนการสอน ระบบปฏิบัติการ Windows ม.1
ODP
Unix Memory Management - Operating Systems
PPTX
Virtual memory
PPTX
Swap space management and protection in os
PPT
Operating System Chapter 5
PPTX
ทฤษฎีการสร้างความรู้ด้วยตัวเอง constructivism
PDF
zOSMF Software Update Lab.pdf
PPT
08 Operating System Support
PDF
ODP
Google's Dremel
PPTX
Virtual Memory in Operating System
PPTX
Distributed file systems chapter 9
แผนการสอน โปรแกรมคอมพิวเตอร์กราฟิก ม.4
Operating System Chapter 4
cache memory
8 ca-file system implementation (wichet p.'s conflicted copy 2012-12-17)
Paging and Segmentation
ระบบจองห้องประชุมออนไลน์
แผนการสอน ระบบปฏิบัติการ Windows ม.1
Unix Memory Management - Operating Systems
Virtual memory
Swap space management and protection in os
Operating System Chapter 5
ทฤษฎีการสร้างความรู้ด้วยตัวเอง constructivism
zOSMF Software Update Lab.pdf
08 Operating System Support
Google's Dremel
Virtual Memory in Operating System
Distributed file systems chapter 9
Ad

Viewers also liked (20)

PPT
Chapter 9 - Virtual Memory
PPS
Fallas2008 Desfiles
PPT
Drupal nagw
PPS
Infantil
PPT
Rancangan Perniagaan
PPT
PPT
Ec2009 ch05 online advertising
PPS
Sevilla Mi Destino
PPT
Wave accounting-by-wang-ming
PPT
Urbanization
PPS
Cine Gratis
PPT
my e-learning
PPS
An Amazing Man Wb
PPT
Ptolemy, Copernicus, and the Church
PPS
Malaysia Boleh
PPT
Lesson Plan Two Us Govt. U23
PPT
PPT
holiday chirstmas
PPS
Library mashups
Chapter 9 - Virtual Memory
Fallas2008 Desfiles
Drupal nagw
Infantil
Rancangan Perniagaan
Ec2009 ch05 online advertising
Sevilla Mi Destino
Wave accounting-by-wang-ming
Urbanization
Cine Gratis
my e-learning
An Amazing Man Wb
Ptolemy, Copernicus, and the Church
Malaysia Boleh
Lesson Plan Two Us Govt. U23
holiday chirstmas
Library mashups
Ad

Similar to Ch10th (20)

PPT
Ch9 2003
PPT
6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)
PPT
Data processing
PPTX
PPT
7 ca-file-system management
PDF
หน่วยที่1 เรื่อง เทคโนโลยีการสื่อสาร นางสาว สิรินยา ปาโจด ม.5
PDF
หน่วยที่1 เรื่อง เทคโนโลยีการสื่อสาร นางสาว สิรินยา ปาโจด ม.5
PPT
C:\Documents And Settings\Administrator\Desktop\Ch00 คอมพิวเตอร์เบื้องต้น
 
PDF
นางสาว หัทยา เชื้อสมเกียรติ ม.5 pp
PDF
นางสาว หัทยา เชื้อสมเกียรติ ม.5 pp
PDF
หน่วยที่ 1เรื่อง การจัดการข้อมูล ธนพงษ์ น่านกร ม.5
PDF
หน่วยที่ 1
PDF
บทที่ 1 ความรู้พื้นฐานระบบฐานข้อมูล
PPT
4 ca-process structure
PDF
3 instruction design
PPT
หน่วยที่ 1
PPT
หน่วยที่ 1
ODP
Introduction to Database
ODP
Introduction to Database
Ch9 2003
6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)
Data processing
7 ca-file-system management
หน่วยที่1 เรื่อง เทคโนโลยีการสื่อสาร นางสาว สิรินยา ปาโจด ม.5
หน่วยที่1 เรื่อง เทคโนโลยีการสื่อสาร นางสาว สิรินยา ปาโจด ม.5
C:\Documents And Settings\Administrator\Desktop\Ch00 คอมพิวเตอร์เบื้องต้น
 
นางสาว หัทยา เชื้อสมเกียรติ ม.5 pp
นางสาว หัทยา เชื้อสมเกียรติ ม.5 pp
หน่วยที่ 1เรื่อง การจัดการข้อมูล ธนพงษ์ น่านกร ม.5
หน่วยที่ 1
บทที่ 1 ความรู้พื้นฐานระบบฐานข้อมูล
4 ca-process structure
3 instruction design
หน่วยที่ 1
หน่วยที่ 1
Introduction to Database
Introduction to Database

More from Nuth Otanasap (20)

PPT
กองทุนท่าจีน
PPT
มาตรฐานผลการเรียนรู้
PPT
SA Chapter 14
PPT
SA Chapter 13
PPT
SA Chapter 12
PPT
SA Chapter 11
PPT
SA Chapter 10
PPT
SA Chapter 9
PPT
SA Chapter 8
PPT
SA Chapter 7
PPT
SA Chapter 6
PPT
SA Chapter 5
PPT
SA Chapter 4
PPT
SA Chapter 3
PPT
SA Chapter 2
PPT
Ec2009 ch01 overview of electronic commerce
PPT
Ec2009 ch14 creating and launching
PPT
Ec2009 ch15 e commerce strategy
PPT
Ec2009 ch12 legal ethical and social
PPT
Ec2009 ch11 electronic payment systems
กองทุนท่าจีน
มาตรฐานผลการเรียนรู้
SA Chapter 14
SA Chapter 13
SA Chapter 12
SA Chapter 11
SA Chapter 10
SA Chapter 9
SA Chapter 8
SA Chapter 7
SA Chapter 6
SA Chapter 5
SA Chapter 4
SA Chapter 3
SA Chapter 2
Ec2009 ch01 overview of electronic commerce
Ec2009 ch14 creating and launching
Ec2009 ch15 e commerce strategy
Ec2009 ch12 legal ethical and social
Ec2009 ch11 electronic payment systems

Ch10th

  • 1. Chapter 9: หน่วยความจำเสมือน Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Demand Segmentation Operating System Examples พื้นฐาน การจัดเพจตามต้องความต้องการ การสร้างโปรเซส การแทนที่เพจ การจัดสรรพื้นที่สำหรับเฟรม การทำแธรช การทำเซ็กเม้นท์ตามความต้องการ ตัวอย่างระบบปฏิบัติการ
  • 2. Background พื้นฐาน หน่วยความจำเสมือน เป็นการแบ่งหน่วยความจำเชิงตรรกของผู้ใช้ออกจาก หน่วยความจำทางกายภาพ เป็นส่วนเดียวของโปรแกรมที่ต้องการเข้าไปอยู่ในหน่วยความจำ เพื่อการเอ็กซ์ซีคิวท์หรือการประมวลผล พื้นที่แอดเดรสเชิงตรรก สามารถที่จะมีขนาดที่ใหญ่กว่าพื้นที่แอดเดรสทางกายภาพ ยอมให้พื้นที่แอดเดรสถูกแบ่งปันการใช้งานจากหลายๆ โปรเซส ยอมให้เกิดหรือสร้างโปรเซสหลายโปรเซสขึ้นได้อย่างมีประสิทธิภาพ หน่วยความจำเสมือน สามารถถูกใช้ผ่าน การจัดเพจตามความต้องการ การทำเซ็กเม้นท์ตามความต้องการ
  • 3. Virtual Memory That is Larger Than Physical Memory หน่วยความจำเสมือนที่มีขนาดใหญ่กว่าหน่วยความจำทางกายภาพ
  • 4. Demand Paging การจัดเพจแบบตามความต้องการ นำเพจไปไว้ในหน่วยความจำ เมื่อมีความต้องการ ต่างๆ ดังนี้ มีความต้องใช้อินพุทเอ้าท์พุทน้อย มีความต้องการใช้หน่วยความจำน้อย มีการตอบสนองที่รวดเร็ว มีผู้ใช้งานจำนวนมาก มีความต้องการเพจ  โดยการอ้างอิงถึง การอ้างอิงไม่ถูกต้อง  ปฏิเสธ ไม่อยู่ในหน่วยความจำ  นำไปไว้ในหน่วยความจำ
  • 5. Transfer of a Paged Memory to Contiguous Disk Space การโอนย้ายหน่วยความจำที่ถูกจัดเพจ ไปไว้ยังพื้นที่ต่อเนื่องบนดิสก์
  • 6. Valid-Invalid Bit บิทบ่งชี้ความถูกต้องและไม่ถูกต้อง ด้วยการเข้าถึงแต่ละตารางเพจ สัมพันธ์อยู่กับบิทบ่งชี้ความถูกต้องและไม่ถูกต้อง ( 1  อยู่ในหน่วยความจำ , 0  ไม่อยู่ในหน่วยความจำ ) ในเวลาเริ่มต้นบิทบ่งชี้ความถูกต้องและไม่ถูกต้องถูกกำหนดให้เป็น 0 สำหรับทุกๆ การเข้าถึง ตัวอย่างของตารางเพจ ในขณะทำการแปลงค่าแอดเดรส หากบิทบ่งชี้ฯ ในการเข้าถึงตารางเพจ มีค่าเป็น 0  ไม่สามารถเข้าถึงเพจได้ หรือเกิดการผิดพลาด 1 1 1 1 0 0 0  Frame # valid-invalid bit page table
  • 7. Page Table When Some Pages Are Not in Main Memory ตารางเพจ ในขณะที่บางเพจไม่อยู่ในหน่วยความจำหลัก
  • 8. เพจผิดพลาด Page Fault หากเกิดการอ้างอิงไปยังเพจใดๆ การอ้างอิงครั้งแรก จะแทรปไปยังระบบปฏิบัติการ  เพจผิดพลาด ระบบปฏิบัติการจะพิจารณาไปยังตารางอื่นๆ เพื่อการตัดสินใจ การอ้างอิงไม่ถูกต้อง  ปฏิเสธ ยังไม่อยู่ในหน่วยความจำ รับเฟรมว่าง สลับเพจไปไว้ในเฟรม กำหนดค่าตารางใหม่ บิทบ่งชี้ฯ = 1 ทำคำสั่งอีกครั้ง auto increment/decrement location
  • 10. ขั้นตอนในการจัดการเพจผิด Steps in Handling a Page Fault 1. ตรวจดูตารางข้อมูลการควบคุมโปรเซส ( PCB) ที่อ้างอิงถึงเพจที่ต้องการถูกต้องหรือไม่ 2. หากการอ้างอิงผิดพลาดให้ยกเลิก โปรเซส หากการอ้างอิงถูกต้อง แต่เพจที่ต้องการไม่มีอยู่ในหน่วยความจำหลัก ให้ไปดำเนินการขั้นต่อไป 3. หาพื้นที่ว่างในเฟรม 1 เพจ โดยอาจเลือกจากรายการแสดงแฟรมว่าง 4. ส่งรีเควสต์ไปยังฮาร์ดดิสก์ เพื่อให้ฮาร์ดดิสก์ทำการอ่านเพจที่ต้องการส่งมาไว้ยังพื้นที่ว่างในเฟรมที่เลือกไว้ก่อนหน้า 5. เมื่อดิสก์โหลดเพจเสร็จสิ้นแล้ว ระบบจะทำการแก้ไขบิตสถานะในตารางเพจ เป็น “สามารถใช้งานได้” 6. ให้โปรเซสทำงานต่อจากจุดที่เกิด “การอ้างอิงถึงเพจผิดพลาด หรือ Page Fault ” โปรเซสก็จะสามารถทำงานต่อไปเสมือนหนึ่งว่า เพจที่ต้องการใช้งานมีอยู่ในหน่วยความจำตลอดเวลา
  • 11. กรณีไม่มีเฟรมว่างจะเกิดอะไรขึ้น What happens if there is no free frame? การจัดสรรพื้นที่ให้เพจใหม่ โดยหาเพจใดๆ ในหน่วยความจำ แต่ไม่ได้ถูกใช้งานจริง และทำการสลับมันออกไป อัลกอริธึม ประสิทธิภาพ ต้องการอัลกอริธึมเพื่อจัดการให้ได้ผลลัพท์ ที่มีจำนวนเพจผิดพลาดน้อยที่สุด โดยเพจเดียวกันอาจถูกนำไปไว้ยังหน่วยความจำเวลาต่างๆ กัน
  • 12. ประสิทธิภาพของการจัดเพจตามความต้องการที่เกิดขึ้น Performance of Demand Paging อัตราการเกิดเพจผิดพลาด ให้ P เป็นโอกาสการเกิดความผิดพลาดการจัดเพจผิดพลาด เพื่อประสิทธิภาพที่ดีจึงต้องการให้ P มีค่าเข้าใกล้ 0 มากที่สุด ซึ่งหมายถึงเกิดความผิดพลาดน้อยที่สุด ระยะเวลาการเข้าถึงหน่วยความจำ Effective Access Time (EAT) EAT = (1 – p ) x memory access + p (page fault overhead + [swap page out ] + swap page in + restart overhead)
  • 13. ตัวอย่างการจัดเพจตามความต้องการ Demand Paging Example Swap Page Time = 10 msec = 10,000 msec EAT = (1 – p) x 1 + p (15000) 1 + 15000P (in msec) ระยะเวลาในการเข้าถึงหน่วยความจำ 1 ไมโครวินาที 50 % ของระยะเวลาของเพจที่ถูกใช้ไปในการสลับเข้าออกจากพื้นที่หน่วยความจำ ระยะเวลาในการสลับการทำงาน
  • 14. การสร้างโปรเซส Process Creation Virtual memory allows other benefits during process creation: - Copy-on-Write - Memory-Mapped Files หน่วยความจำเสมือนยอมให้เกิดการทำงานอื่นๆ ขึ้นได้ระหว่างการสร้างโปรเซส ก๊อปปี้ ขณะ เขียน ไฟล์ที่แมพค่าเข้ากับหน่วยความจำ
  • 15. COW ยอมให้ทั้งโปรเซสต้นกำเนิดและโปรเซสลูก สามารถใช้เพจร่วมกันในหน่วยความจำ หาก ทั้งสองโปรเซสเปลี่ยนแปลงเพจที่ใช้งานร่วมกัน ก็เพียงทำการสำเนาเพจนั้นไว้ก่อน COW ยอมให้การสร้างโปรเซสหลายโปรเซสได้อย่างมีประสิทธิภาพ โดยเพียงแต่แก้ไขเพจที่ถูกสำเนา เพจว่างถูกจัดสรรให้จาก พูลของเพจที่ไม่ได้ถูกใช้งาน Copy-on-Write
  • 16. Memory-Mapped Files Memory-mapped file I/O allows file I/O to be treated as routine memory access by mapping a disk block to a page in memory. A file is initially read using demand paging. A page-sized portion of the file is read from the file system into a physical page. Subsequent reads/writes to/from the file are treated as ordinary memory accesses. Simplifies file access by treating file I/O through memory rather than read() write() system calls. Also allows several processes to map the same file allowing the pages in memory to be shared.
  • 18. Page Replacement การสลับแทนที่เพจ การเกิดการอ้างอิงถึงเพจที่ไม่มีอยู่ในหน่วยความจำหลัก ทำให้เกิดปัญหาการผิดพลาดการจัดเพจ โดยฮาร์ดแวร์จะทำการแจ้งไปยังระบบปฏิบัติการ โดยตรวจสอบความผิดพลาดที่เกิดขึ้นจากตารางการขัดจังหวะ จากนั้นระบบจะดำเนินการโหลดเพจที่ต้องการกลับมาไว้ยังพื้นที่ในเฟรมว่างที่จัดสรรไว้ก่อนแล้ว เพื่อการทำงานของโปรเซสที่ต้องการใช้หรือเกิดการอ้างอิงถึงเพจดังกล่าวสามารถทำงานต่อไปได้
  • 19. Page Replacement แต่หากการโหลดเพจที่ต้องการนั้นกลับเข้ามา ระบบไม่สามารถจัดหาเฟรมว่างเพื่อรองรับเพจนั้นได้ ระบบสามารถดำเนินการด้วยวิธีต่อไปนี้ ให้โปรเซสที่อ้างอิงถึงเพจนั้นหยุดการทำงาน ชั่วคราวจนกว่าจะเกิดเฟรมว่างจากการคืนทรัพยากรของโปรเซสอื่นๆ แต่ด้วยวิธีนี้จะส่งผลต่อผู้ใช้งานที่สั่งให้เกิดการทำงานของโปรเซสนั้นขึ้น และทำให้ประสิทธิภาพการทำงานของระบบโดยรวมลดลง
  • 20. Page Replacement 2. การให้โปรเซสใดๆ หยุดการทำงานชั่วคราว เพื่อสามารถนำเพจที่เกิดจากการอ้างอิงของโปรเซสใดๆ นั้นไปเก็จไว้ยังฮาร์ดดิสก์ก่อน ซึ่งจะทำให้เกิดพื้นที่ว่างหรือเฟรมว่างในหน่วยความจำหลัก และสามารถนำเฟรมว่างที่เกิดขึ้นนี้ไปให้เพจที่ต้องการเข้าใช้งานก่อน
  • 21. Page Replacement 3. โดยวิธีที่สองนี้เรียกว่า เทคนิคการสับเปลี่ยนเพจ (Page Replacement) กล่าวคือหากในหน่วยความจำหลักไม่มีเฟรมว่างอยู่เลย อันเนื่องมาจากการใช้งานของเพจใดๆ อยู่เต็มทุกพื้นที่ เมื่อเกิดความต้องการการเข้าใช้งานเฟรม หรือต้องการเฟรมว่าง จากเพจที่เกิดขึ้นใหม่และจำเป็นต้องจัดสรรเฟรมว่างให้ใช้งานก่อน ระบบจะทำการจัดสรรเฟรมว่างด้วยการนำเพจที่มีอยู่ในหน่วยความจำหลักก่อนหน้านี้ และเกิดการใช้งานน้อยที่สุดหรือเกิดขึ้นก่อนสุด ให้ออกไปจากหน่วยความจำหลักไปไว้ยังฮาร์ดดิสก์ก่อนชั่วคราวเพื่อให้เกิดเฟรมว่างขึ้นในหน่วยความจำหลัก จากนั้นจึงนำเฟรมที่ว่างขึ้นไปให้กับเพจที่เกิดขึ้นภายหลังและจำเป็นต้องเข้าทำงานก่อนเข้าใช้ ทั้งนี้ระบบจะต้องทำการแก้ไขตารางการจัดเพจให้สถานะของเพจที่ถูกนำออกไปจากหน่วยความจำหลักนั้นมีสถานะเป็นไม่สามารถใช้งานได้เพื่อป้องกันความผิดพลาดที่อาจเกิดขึ้นจากโปรเซสที่อ้างถึงเพจนั้นด้วย
  • 22. Need For Page Replacement
  • 23. Basic Page Replacement Find the location of the desired page on disk. Find a free frame: - If there is a free frame, use it. - If there is no free frame, use a page replacement algorithm to select a victim frame. Read the desired page into the (newly) free frame. Update the page and frame tables. Restart the process.
  • 25. Page Replacement Algorithms In all our examples, the reference string is 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5. อัลกอริธึมการสลับเพจ ด้วยเทคนิคการสลับเพจมีหลากหลายวิธี โดยปกติจะเลือกใช้วิธีที่ทำให้เกิดความผิดพลาดในการอ้างอิงถึงเพจ ต่ำสุด สามารถค้นหาวิธีที่ดีที่สุดได้ด้วยการทดสอบด้วยการสร้างข้อมูล เพื่อให้เกิดการเข้าใช้ และดูว่าวิธีใดเกิดการผิดพลาดน้อยครั้งที่สุด
  • 26. Graph of Page Faults Versus The Number of Frames
  • 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 can be in memory at a time per process) 4 frames FIFO Replacement – Belady’s Anomaly more frames  less 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 4 4 3
  • 28. FIFO Page Replacement (First-In-First-Out Algorithm: FIFO) เป็นวิธีการจัดการที่ง่ายที่สุด เลือกเพจที่มีอายุอยู่ในหน่วยความจำหลักมากที่สุด เป็นเพจที่จะต้องออกไปก่อน แต่ด้วยวิธีนี้เพจที่เก็บค่าสำคัญเพื่อการทำงานหลักของระบบอาจเป็นเพจที่ถูกเลือกให้เป็นเพจที่ต้องออกจากหน่วยความจำ และเมื่อเกิดการอ้างอิงถึงเพจนั้นก็จะต้องทำการโหลดเข้ามาใหม่ หากเพจนั้นถูกอ้างอิงถึงบ่อย จะทำให้เกิดความผิดพลาดในการจัดเพจ ขึ้นบ่อยไปด้วยแม้ว่าจะสามารถโหลดเพจนั้นกลับเข้ามาใหม่ก็ตามแต่ก็จะเกิดความสูญเสียเวลาและงานขึ้น ทำให้ประสิทธิภาพของระบบลดลง ปรากฏการณ์เช่นนี้เรียกว่า ความผิดปกติ แบบเบลาดี้
  • 30. Optimal Algorithm Replace page that will not be used for longest period of time. 4 frames example 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 How do you know this? Used for measuring how well your algorithm performs. 1 2 3 4 6 page faults 4 5
  • 31. Optimal Page Replacement อัลกอริธึมแบบประสิทธิภาพดี เป็นวิธีที่สามารถลดปัญหาการเกิดความผิดพลาดการจัดเพจ และหลีกเลี่ยงปัญหาความผิดพลาดแลลเบลาดี้ เลือกสับเปลี่ยนเพจที่จะเกิดการอ้างอิงถึงในอนาคตอันไกลที่สุด ออกจากหน่วยความจำก่อน ทำให้เกิดความผิดพลาดจากการจัดเพจขึ้นน้อยที่สุด
  • 32. Least Recently Used (LRU) Algorithm Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 Counter implementation Every page entry has a counter; every time page is referenced through this entry, copy the clock into the counter. When a page needs to be changed, look at the counters to determine which are to change. 1 2 3 5 4 4 3 5
  • 33. LRU Page Replacement อัลกอริธึมแบบเพจที่เคยถูกใช้เป็นครั้งสุดท้ายนานที่สุด ด้วยแบบประสิทธิภาพดีที่สุดยากต่อการเขียนโปรแกรม เนื่องจากยากที่จะทราบได้ว่าเพจใดจะถูกเรียกใช้ในอนาคตอันไกลสุด เพราะต้องเกิดจากการคาดการณ์ซึ่งยากที่จะคาดการณ์ได้
  • 34. LRU Algorithm (Cont.) LRU เป็นวิธีที่ให้ผลทางประสิทธิภาพใกล้เคียงกับแบบประสิทธิภาพดี มากที่สุดโดยมีแนวคิดในการจัดการคือ เมื่อเกิดความต้องการที่จะต้องสลับเพจหนึ่งเพจใดออกจากหน่วยความจำหลัก ระบบจะเลือกเพจที่เคยถูกใช้เป็นครั้งสุดท้ายที่มีระยะเวลานานที่สุด หรืออีกนัยหนึ่งคือเพจนั้นเคยถูกใช้งานล่าสุดมานานกว่าเพจใดๆ ในระบบ จึงคาดว่าระบบนั้นอาจจะไม่ถูกใช้งานหรืออีกนานกว่าจะถูกใช้งาน หรือมีความถี่ที่ถูกอ้างอิงถึงน้อยที่สุดนั่นเอง ระบบก็จะเลือกนำเพจนี้ออกไปจากหน่วยความจำหลักก่อนเพื่อเปิดโอกาสให้เกิดเฟรมว่างขึ้นในหน่วยความจำหลักและสามารถให้เพจที่เกิดขึ้นใหม่และจำเป็นต้องเข้าใช้หน่วยความจำหลักสามารถทำงานได้ ระบบจะทำการบันทึกเวลาที่แต่ละเพจถูกอ้างอิงถึง ครั้งล่าสุดไว้ เมื่อต้องการเลือกเพจใดให้ออกจากหน่วยความจำ ก็จะเลือกจากเพจที่ไม่ถูกอ้างอิงหรือใช้งานเป็นระยะเวลานานที่สุด
  • 35. Use Of A Stack to Record The Most Recent Page References การใช้แสต็กในการจัดการกับ อ้างอิงถึงเพจเพื่อให้เพจที่ถูกอ้างอิงถึงล่าสุดอยู่บนสุด
  • 36. LRU Approximation Algorithms อัลกอริธึมเลียนแบบ LRU โดยอาศัยบิทอ้างอิง ขณะเริ่มต้นเพจใดๆ จะถูกกำหนดให้มีบิทด้างอิงมีค่าเป็น 0 เมื่อเพจนั้นๆ ถูกอ้างอิงถึงบิทอ้างอิงจะถูกเปลี่ยนค่าไปเป็น 1 อัลกอริธึมแบบ เปิดโอกาสให้เป็นครั้งที่สอง มีการอ้างอิงถึงบิทอ้างอิง มีการสลับเวลา เมื่อเพจถูกโหลดกลับเข้ามาใว้ในหน่วยความจำอีกครั้ง จากการกำหนดของเวลา และบิทอ้างอิงมีค่าเป็น 1 แล้วให้ดำเนินการ กำหนดค่าบิทอ้างอิงให้เป็น 0 นำเพจออกจากหน่วยความจำ นำเพจถัดไปมาไว้ในหน่วยความจำ ด้วยเงื่อนไขเช่นเดียวกัน
  • 38. Counting Algorithms อัลกอริธึมการนับ ใช้ตัวนับจำนวนในการอ้างอิงถึงแต่ละเพจ อัลกอริธึมแบบ LFU จะเลือกเพจที่ต้องถูกนำออกจาหน่วยความจำจากการนับที่มีค่าน้อยที่สุด อัลกอริธึมแบบ MFU จะเลือกเพจที่มีการอ้างอิงถึงมากออกก่อน จากแนวคิดที่ว่าเพจที่มีการอ้างอิงถึงน้อยเป็นเพจที่เพิ่งเกิดใหม่ ส่วนเพจที่ถูกอ้างอิงถึงบ่อยเป็นเพจที่เกิดขึ้นมานานแล้ว
  • 39. Allocation of Frames Each process needs minimum number of pages. Example: IBM 370 – 6 pages to handle SS MOVE instruction: instruction is 6 bytes, might span 2 pages. 2 pages to handle from . 2 pages to handle to . Two major allocation schemes. fixed allocation priority allocation
  • 40. Fixed Allocation Equal allocation – e.g., if 100 frames and 5 processes, give each 20 pages. Proportional allocation – Allocate according to the size of process.
  • 41. Priority Allocation Use a proportional allocation scheme using priorities rather than size. If process P i generates a page fault, select for replacement one of its frames. select for replacement a frame from a process with lower priority number.
  • 42. Global vs. Local Allocation Global replacement – process selects a replacement frame from the set of all frames; one process can take a frame from another. Local replacement – each process selects from only its own set of allocated frames.
  • 43. Thrashing If a process does not have “enough” pages, the page-fault rate is very high. This leads to: low CPU utilization. operating system thinks that it needs to increase the degree of multiprogramming. another process added to the system. Thrashing  a process is busy swapping pages in and out.
  • 44. Thrashing Why does paging work? Locality model Process migrates from one locality to another. Localities may overlap. Why does thrashing occur?  size of locality > total memory size
  • 45. Locality In A Memory-Reference Pattern
  • 46. Working-Set Model   working-set window  a fixed number of page references Example: 10,000 instruction WSS i (working set of Process P i ) = total number of pages referenced in the most recent  (varies in time) if  too small will not encompass entire locality. if  too large will encompass several localities. if  =   will encompass entire program. D =  WSS i  total demand frames if D > m  Thrashing Policy if D > m, then suspend one of the processes.
  • 48. Keeping Track of the Working Set Approximate with interval timer + a reference bit Example:  = 10,000 Timer interrupts after every 5000 time units. Keep in memory 2 bits for each page. Whenever a timer interrupts copy and sets the values of all reference bits to 0. If one of the bits in memory = 1  page in working set. Why is this not completely accurate? Improvement = 10 bits and interrupt every 1000 time units.
  • 49. Page-Fault Frequency Scheme Establish “acceptable” page-fault rate. If actual rate too low, process loses frame. If actual rate too high, process gains frame.
  • 50. Other Considerations Prepaging Page size selection fragmentation table size I/O overhead locality
  • 51. Other Considerations (Cont.) TLB Reach - The amount of memory accessible from the TLB. TLB Reach = (TLB Size) X (Page Size) Ideally, the working set of each process is stored in the TLB. Otherwise there is a high degree of page faults.
  • 52. Increasing the Size of the TLB Increase the Page Size . This may lead to an increase in fragmentation as not all applications require a large page size. Provide Multiple Page Sizes . This allows applications that require larger page sizes the opportunity to use them without an increase in fragmentation.
  • 53. Other Considerations (Cont.) Program structure int A[][] = new int[1024][1024]; Each row is stored in one page Program 1 for (j = 0; j < A.length; j++) for (i = 0; i < A.length; i++) A[i,j] = 0; 1024 x 1024 page faults Program 2 for (i = 0; i < A.length; i++) for (j = 0; j < A.length; j++) A[i,j] = 0; 1024 page faults
  • 54. Other Considerations (Cont.) I/O Interlock – Pages must sometimes be locked into memory. Consider I/O. Pages that are used for copying a file from a device must be locked from being selected for eviction by a page replacement algorithm.
  • 55. Reason Why Frames Used For I/O Must Be In Memory
  • 56. Demand Segmentation Used when insufficient hardware to implement demand paging. OS/2 allocates memory in segments, which it keeps track of through segment descriptors Segment descriptor contains a valid bit to indicate whether the segment is currently in memory. If segment is in main memory, access continues, If not in memory, segment fault.
  • 57. Operating System Examples Windows NT Solaris 2
  • 58. Windows NT Uses demand paging with clustering . Clustering brings in pages surrounding the faulting page. Processes are assigned working set minimum and working set maximum . Working set minimum is the minimum number of pages the process is guaranteed to have in memory. A process may be assigned as many pages up to its working set maximum. When the amount of free memory in the system falls below a threshold, automatic working set trimming is performed to restore the amount of free memory. Working set trimming removes pages from processes that have pages in excess of their working set minimum.
  • 59. Solaris 2 Maintains a list of free pages to assign faulting processes. Lotsfree – threshold parameter to begin paging. Paging is performed by pageout process. Pageout scans pages using modified clock algorithm. Scanrate is the rate at which pages are scanned. This ranged from slowscan to fastscan . Pageout is called more frequently depending upon the amount of free memory available.
  • 60. Solaris 2 Page Scanner