3 Types of Memory Real memory Virtual memory Main memory Memory on disk Allows for effective multiprogramming and relieves the user of tight constraints of main memory
4 Virtual Memory Properties A process may be broken up into pieces that do not need to be located contiguously in main memoryNo need to load all pieces of a process in main memoryA process may be swapped in and out of main memory such that it occupies different regionsMemory references are dynamically translated into physical addresses at run timeAdvantages:More processes may be maintained in main memoryWith so many processes in main memory, it is very likely a process will be in the Ready state at any particular timeA process may be larger than all of main memory
5 A System with Virtual Memory (paging) Each process has its own page tableEach page table entry contains the frame number of the corresponding page in main memory or the disk addressMemoryPage Table10:1:PhysicalAddressesVirtualAddressesCPUP-1:N-1DiskAddress Translation: Hardware converts virtual addresses to physical addresses via OS-managed lookup table (page table)
6 VM Address Translation ParametersP = 2p = page size (bytes).N = 2n = Virtual address limitM = 2m = Physical address limitn–1pp–1virtual page numberpage offsetvirtual addressaddress translationm–1pp–1physical page numberpage offsetphysical addressPage offset bits don’t change as a result of translation
7 Execution of a ProgramOperating system brings into main memory a few pieces of the programResident set - portion of process that is in main memoryAn interrupt is generated when an address is needed that is not in main memory - page faultOperating system places the process in a blocking state and a DMA is started to get the page from diskAn interrupt is issued when disk I/O is complete which causes the OS to place the affected process in the Ready QueueThrashing –Swapping out a piece of a process just before that piece is neededThe processor spends most of its time swapping pieces rather than executing user instructions
8 Principle of LocalityProgram and data references within a process tend to clusterOnly a small portion of a process will be needed over a short period of timePossible to make intelligent guesses about which pieces will be needed in the futureTherefore, principle of locality can be used to make virtual memory to work efficiently
9 Page Faults Before fault After fault Page table entry indicates virtual address not in memoryOS exception handler invoked to move data from disk into memorycurrent process suspends, others can resumeOS has full control over placement, etc.Before faultAfter faultMemoryMemoryPage TablePage TableVirtualAddressesPhysicalAddressesVirtualAddressesPhysicalAddressesCPUCPUDiskDisk
10 Servicing a Page Fault Processor Signals Controller Read Occurs (1) Initiate Block ReadProcessor Signals ControllerRead block of length P starting at disk address X and store starting at memory address YRead OccursDirect Memory Access (DMA)Under control of I/O controllerI/O Controller Signals CompletionInterrupt processorOS resumes suspended processProcessorReg(3) Read DoneCacheMemory-I/O bus(2) DMA TransferI/OcontrollerMemorydiskDiskDiskdisk
11 Direct Memory AccessTakes control of the system from the CPU to transfer data to and from memory over the system busOnly one bus master, usually the DMA controller due to tight timing constraints.Cycle stealing is used to transfer data on the system bus. i.e. the instruction cycle is suspended so that data can be transferred by DMA controller in bursts. CPU can only access the bus between these burstsNo interrupts occur (except at the very end)
12 Support Needed for Paging Hardware support for address translation is critically needed for pagingOS must be able to move pages efficiently between secondary memory and main memory (DMA)Software/Hardware support to handle page faults
13 Page Tables Virtual Page Number Memory resident page table (physical pageor disk address)Physical MemoryValid111111Disk Storage(swap file orregular file system file)1
14 Address Translation virtual page number (VPN) page offset virtual addressphysical page number (PPN)physical addressp–1pm–1n–1page table base registerif valid=0then pagenot in memoryvalidmodifyVPN acts astable index
15 Page Table Entries Present bit (P) == valid bit Modify bit (M) == dirty bit is needed to indicate if the page has been altered since it was last loaded into main memoryIf no change has been made, the page does not have to be written back to the disk when it needs to be swapped outIn addition, a Reference (or use) Bit can be used to indicate if the page is referenced (read) since it was last checked.This can be particularly useful for Clock Replacement Policy (to be studied later)
17 Multi-Level Page Tables Given:4KB (212) page size32-bit address space4-byte PTEProblem:Would need a 4 MB page table!220 *4 bytesCommon solutionmulti-level page tablese.g., 2-level table (P6)Level 1 table: 1024 entries, each of which points to a Level 2 page table.Level 2 table: entries, each of which points to a pageLevel 1Table...
18 Two-Level Scheme for 32-bit Address The entire page table may take up too much main memoryPage tables are also stored in virtual memoryWhen a process is running, part of its page table is in memory
19 Translation Lookaside Buffer Each virtual memory reference can cause two physical memory accessesone to fetch the page table entryone to fetch the dataTo overcome this problem a high-speed cache is set up for page table entriescalled the TLB - Translation Lookaside BufferTLB stores most recently used page table entries. Stores VP numbers and the mapping for it. Uses associative mapping (i.e. many virtual page numbers map to the same TLB index).Given a virtual address, processor examines the TLBIf page table entry is present (a hit), the frame number is retrieved and the real address is formedIf page table entry is not found in the TLB (a miss), the page number is used to index the page table, and TLB is updated to include the new page entry
22 Page SizeSmaller page size less amount of internal fragmentation (due to the last page of a process)Smaller page size more pages required per processMore pages per process means larger page tablesLarger page tables large portion of page tables in virtual memory (i.e. double page faults possible)Secondary memory is designed to efficiently transfer large blocks of data which favors a larger page size.
23 Page Size Small page size large number of pages will be found in main memoryAs time goes on during execution, the pages in memory will all contain portions of the process near recent references Page faults low.Increased page size causes pages to contain references which may not be resident in memory (because fewer number of page frames is allowed per process) Page faults may rise.
26 Fetch Policy Replacement Policy Determines when a page should be brought into memoryDemand paging only brings pages into main memory when a reference is made to a location on the pageMany page faults when process first startedPrepaging brings in more pages than neededMore efficient to bring in pages that reside contiguously on the diskReplacement PolicyWhich page should be replaced?Page that is least likely to be referenced in the near futureMost policies predict the future behavior on the basis of past behaviorFrame LockingIf frame is locked, it may not be replacedKernel, Control structures, I/O buffersAssociate a lock bit with each frame
27 Basic Replacement Algorithms Optimal policySelects the page for which the time to the next reference is the longestImpossible to have perfect knowledge of future eventsLeast Recently Used (LRU)Replaces the page that has not been referenced for the longest timeBy the principle of locality, this should be the page least likely to be referenced in the near futureEach page could be tagged with the time of last reference (extra overhead!)First-In, First-Out (FIFO)Simplest to implementPage that has been in memory the longest is replaced (may be needed again soon!)Clock PolicyAdditional bit called use bitWhen a page is first loaded in memory use bit = 1When the page is referenced use bit = 1Going clockwise, the first frame encountered with the use bit = 0 is replaced.During the search for replacement, use bit is changed from 1 to 0
29 Variable Allocation, Global Scope Resident Set SizeFixed-allocationgives a process a fixed number of pages within which to executewhen a page fault occurs, one of the pages of that process is replacedVariable-allocationnumber of pages allocated to a process varies over the lifetime of the processVariable Allocation, Global ScopeEasiest to implement; Adopted by many OSOS keeps list of free framesWhen a page fault occurs, a free frame is added to the resident set of this processIf no free frame, replaces one from any process (replacement algorithm picks!)Variable Allocation, Local ScopeWhen a new process is added, allocate to it a number of page frames based on the application type, or other criteria (resident set)page fault select a page for replacement from the resident set of this processFrom time to time; increase/decrease the resident set size to improve performance
30 Load Control & Process Suspension Load Control determines the optimum number of processes to be resident in main memoryAs multiprogramming level increases, processor utilization rises, butToo many processes small resident set for each process thrashingToo few processes higher probability for all processes to be blocked idle CPU time increasesIf multiprogramming level is to be reduced, some processes must be suspended…Here are some good choices for suspension:Lowest priority processFaulting process (will soon be blocked anyway)high probability that it does not have its working set in main memoryLast process activated (least likely to have its working set resident)Process with smallest resident setthis process requires the least future effort to reloadLargest processgenerates the most free frames making future suspensions unlikely