80 likes | 184 Views
Chapter 21 Swapping: Mechanisms. Chien -Chung Shen CIS, UD cshen@cis.udel.edu. Go Beyond Physical Memory. How to support many concurrently-running large address spaces ? OS needs a place to stash away portions of address spaces that currently aren’t in great demand (where ?)
E N D
Chapter 21Swapping: Mechanisms Chien-Chung Shen CIS, UD cshen@cis.udel.edu
Go Beyond Physical Memory • How to support many concurrently-running large address spaces ? • OS needs a place to stash away portions of address spaces that currently aren’t in great demand (where ?) • “swap space” on hard disk drive • how can OS make use of a larger, slower device (disk) to transparentlyprovide the illusion of a large virtual address space • virtual memory(with swap space on disk) • why support a single large address space for process ? • convenience and ease of use
Swap Space • Reserved space on disk for moving pages back and forth • remember disk address of a given page • Code page(s) of a.outare initially on disk
TLB Algorithm (Review) VPN = (VirtualAddress & VPN_MASK) >> SHIFT (Success, TlbEntry) = TLB_Lookup(VPN)if (Success == True) // TLB Hit if (CanAccess(TlbEntry.ProtectBits) == True) Offset = VirtualAddress & OFFSET_MASK PhysAddr= (TlbEntry.PFN << SHIFT) | Offset AccessMemory(PhysAddr) else RaiseException(PROTECTION_FAULT) else // TLB Miss PTEAddr= PTBR + (VPN * sizeof(PTE)) PTE = AccessMemory(PTEAddr) if (PTE.Valid == False) RaiseException(SEGMENTATION_FAULT) else if (CanAccess(PTE.ProtectBits) == False) RaiseException(PROTECTION_FAULT) else TLB_Insert(VPN, PTE.PFN, PTE.ProtectBits) RetryInstruction()
Present Bit and Page Fault • When hardware looks in PTE, it may find page is not present in physical memory • present bit == 0 • page fault • OS page-fault handler (for both hardware-managed and software-managed TLBs) • why not hardware handle page fault ? • disk is too slow and too much details to handle • where to find the desired page ? • page table (PFN or disk address)
When Memory Is Full • OS pages out one or more pages to make room for new page(s) OS is about to page in • page-replacement policy • disk-like speed vs. memory-like speed (10,000 or 100,000 times slower)
Page Fault Control Flow VPN = (VirtualAddress & VPN_MASK) >> SHIFT (Success, TlbEntry) = TLB_Lookup(VPN)if (Success == True) // TLB Hit if (CanAccess(TlbEntry.ProtectBits) == True) Offset = VirtualAddress & OFFSET_MASK PhysAddr= (TlbEntry.PFN << SHIFT) | Offset Register = AccessMemory(PhysAddr) else RaiseException(PROTECTION_FAULT) Else // TLB Miss PTEAddr= PTBR + (VPN * sizeof(PTE)) PTE = AccessMemory(PTEAddr) if (PTE.Valid == False) RaiseException(SEGMENTATION_FAULT) else if (CanAccess(PTE.ProtectBits) == False) RaiseException(PROTECTION_FAULT) else if (PTE.Present == True) // assuming hardware-managed TLB TLB_Insert(VPN, PTE.PFN, PTE.ProtectBits) RetryInstruction() else RaiseException(PAGE_FAULT)
When Replacement Occurs • OS keeps a small amount of memory free by having high watermark (HW) and low watermark (LW) to help decide when to start evicting pages from memory • When OS notices that there are fewer than LW pages available, a background thread (swap daemon or page daemon)that is responsible for freeing memory runs. The thread evicts pages until there are HW pages available • Cluster or group a number of pages and write them out at once to the swap space, thus increasing the efficiency of the disk