Case Study: Virtual Memory in UNIX Reference – Maurice J.Bach, The Design of the UNIX Operating System Terminology - Physical Memory (Main Memory) Secondary Memory (H.Disk) Virtual Memory Swap File
Region Virtual space of a process Stack, Data, Text – are regions of every process
Per Process Region Table - pregion For every region – Starting virtual address Number of pages in this region Access mode Pointer to the Page Table (page table for every region)
Page Table
Page Table Entry Demand paging fields Valid Reference Modify Copy on Write (COW) Age Modify, Reference -handle by the hardware
More tables Page frame data table (pfdata) One table Mapping all physical frames to disk Tells how many process are using this frame List of free frames Hash from disk address to frame Swap-use For every swap device Tells how many frames points to the same disk address
Using the Copy on Write in fork()
Kernel process for Virtual Memory Pager Every time the OS needs to swap a virtual page to the physical memory Get the first frame from the list of free pages, if the Modify bit is set write it’s data to the disk. Load the page to this frame Set the valid bit Resume the waiting process Page Stealer (page daemon) Every period of time Enter old frames to the list of free pages Update the age of every valid frame if the reference bit is 0 Put 0 in the the reference bit => LRU with second chance
Page Fault Some of the possibilities : 1. The page is in the swap file 2. The page is in the list of free pages FrameDisk Block Count Virt AddrPhysState Block 0K 1K1648invFile3 4KNoneinvFile5 64K1917invdisk K 66K1036invdisk847 67K Virt AddrPhysState Block 66K1776valdisk847 PageDisk Block Count After Swapping Page into Memory Page Table Entries Disk Block Page Frames
Event - Page Daemon done Event – Proc A reads from Pg2 Event – Proc A call to fork()
Event – Proc A reads from Pg1 Event – Proc B write to Pg6 Event – Page Daemon done
Event – Proc B reads from Pg2 & Pg5 Event – Page Daemon done Event – Proc A ends