May 1st Lecture ================= logical address - memory address seen by a program physical address - index into main memory Logical address: +--------+--------+ | page # | offset | +--------+--------+ Logical addresses are contiguous; physical addresses may not be. Address translation: * extract page # from logical address * find corresponding frame # * find base physical address of frame * add offset of logical address Given an address: - page num = addr / page size - offset = addr % page size (assume integer division) Ex. page size = 1024 bytes (10 bit offset) process A: 5120 bytes (5 pages: 000 to 100) memory 10 frames (0000 to 1001) page table: 000: 0111 001: 1001 010: 0101 011: 0001 100: 0000 Prog Addr: 0101101101111 page # = 010, offset = 1101101111 Physical Addr: 01011101101111 Virtual Memory * extension of simple paging * idea: no need to put all of a proc's pages in memory at once (store others on secondary storage) * advantages: - programs can be larger than main memory - efficient sharing of memory among processes; keep active parts of each proc in memory, other parts on disk virtual memory is an illusion created for the programmer: giant main memory Pages are on disk, kernel loads pages into memory as necessary Page Replacement * kernel needs to evict pages sometimes * page replacement algorithm is used to select the page to replace * if modified ("dirty"), page is written back to disk before being replaced in memory Virtual address space -- the huge "imaginary" contiguous address space seen by the program (virtual address = logical address) Physical address space -- location in main memory Important values: * size of virt addr space (# of bits) * size of phys addr space (# of bits) * page size (bits in offset) * max # of frames (bits per frame #) * max # of pages (bits per page #) Ex. V=14 bit, P=13 bit, Page=11-bit Size of VM: 16K Size of PM: 8K # of pages: (14-11=3) bits = 8 pages # of frames: (13-11=2) bits = 4 frames Ex. Page size is 2K (2048) bytes # of page table entries is 32 # of frames is 16 2k => 11 bit offset 32 entries = 5 bit page # so 16 bit virtual address 16 frames = 4 bit frame # so 15 bit physical address page fault - event when a program references an address in a page that is not loaded into a memory frame Page Replacement Algorithms: * Not Recently Used (NRU) * First In, First Out (FIFO) * Second Chance -- FIFO + extra bit for "recent" use; if R=0, evict, if R=1, set R=0, move to end of list * Least Recently Used (LRU) * Matrix LRU Algorithm: * N x N matrix (row/col for each page) * upon ref to page k: - set all bits in row k to 1 - set all bits in column k to 0 * magically guarantees that row with lowest binary num is LRU frame * Optimal -- evict page that won't be needed for the longest time - impossible in general to predict future; used for theoretical comparison - if program *always* takes same path through program and accesses memory in the same order, we can run it once and record the page accesses and use those to setup an optimal PRA for future runs