Operating Systems Lecture 7 Memory management : Virtual memory Overview Virtual memory part Page replacement algorithms Frame allocation Thrashing Other considerations Memory over-allocation Efficient process creation Memory mapped files Page replacement algorithms What page to replace? Page replacement algo should give lowest page fault rate Algorithm evaluation run on a sequence of memory references compute number of page faults for that sequence Reference sequence generated using some distribution traces of actual memory references
FIFO Page replacement algorithms Optimal LRU Counting algorithms Second chance enhanced second chance Page buffering FIFO Replace oldest page in memory imlemented by FIFO queue Not very good in many situations as we soon shall se Example: frames, 5 page replacements Belady s Anomaly Increasing number of frames may increase page faults for some algorithms discovered by Belady et al. in 969 Example: Reference string:,,, 4,,, 5,,,, 4, 5 frames: 4 frames; 4 5 5 4 9 page faults 5 0 page faults 4 4 4
Optimal Algorithm Lowest page fault of all algorithms never suffers from belady s anomaly Replace page that will not be used for longest period of time Example: reference seq.:,,, 4,,, 5,,,, 4, 5 4 6 page faults 4 5 Optimal algorithm Hard to implement why? uses information not present at time of decision Used for measuring quality of other algorithms Least Recently Used (LRU) Replace page least recently used page not use for longest period of time look backwards in time (instead of forward, as optimal) Considered to be good often used does not suffer from Belady s anomaly
LRU - implementation Counters entry in page table has time-of-use field OS (or hardware) maintains clock or counter At access, field is updated page with smallest time value is replaced Stack page numbers kept in stack most recently used on top, LRU at bottom Problems with this? requires update for every memory access too costly LRU approximation Few hardware architectures support true LRU replacement Referenced bit often set by hardware on access separates accessed pages from unaccessed order of access not known Referenced bit can be used to approximate LRU Second chance LRU approximation algorithm Treats set of page table entries as circular buffer advances through pages in order pointer indicates which page should be replaced next Set reference bit to 0 for each pass Does not replace pages that has reference bit= pages get a second chance if they have been accessed since last visit Degenerates to FIFO if all pages accessed since last replacement Also known as the clock algorithm 4
Second chance pointer advances curcular buffer of pages reset bit at pointer replace pages with bit=0 not used since last visit Enhanced second chance I/O time consuming twice as expensive to replace a dirty page Same procedure as second chance, but consider dity pages (0,0) neither used nor dirty (best choice) (0,) not used, but dirty (:nd choice) (,0) used, but clean (:rd choice) (,) used and dirty (worst choice) Queue may have to be scanned several times Counting algorithms Keep counter memory references to each page LFU (Least frequently used) replace page with lowest count MFU (Most frequently used) pages with low count are new should not be swapped out LFU and MFU not used much expensive to implement LFU approximates LRU poorly 5
Page buffering algorithms Page buffering can be used on any algorithm OS keeps pool of free frames process can quickly be given a frame victim frames are selected in the background variant: remember which pages reside in each free frame can quickly be given back if needed OS may keep list of modified pages modified pages written to disk in background fewer dirty pages in system These techniques are often used Page replacement non-dirty pages Possible to not only load from swap space code segment (executable, dynamic library) could be loaded from executable file again just discard frame, even if not in swap faster? sometimes... why not always?» swap space is faster than file system» might be faster to save in swap Allocation of frames How many pages to allocate to a process? more processes? more page faults? Absolute minimum depends on instruction set number of frames used simultaneously by instruction example: mov (r), (r) uses frames Absolute maximum total amount of memory Major schemes fixed allocation priority allocation 6
Frame allocation Fixed allocation same number of frames to each process proportional to size of process #frames varies as processes are started and terminated Priority allocation allocate proportional to priority of process Working set model... Frequency allocation Thrashing If process has to few pages page in new pages very often low cpu utilization much i/o-wait Thrashing more time spent on paging than execution Scheduler might increase multiprogramming because cpu is idle even more severe thrashing Degree of multiprogramming has to be decreased Local allocation of frames thrashing does not spread to other processes thrashing process consumes more resources How many frames does a process really need? Thrashing 7
Working set model Try to estimate number of frames used by process currently working set enough to keep working set in memory How to compute? set of pages referenced by fixed number of page references delta too small we don t get a complete locality delta too large we get several localities Estimating working set Referenced bit combined with regular interrupts Frequency allocation Alternative to working set model Bounds on acceptable page fault frequency lower upper Lower bound reached process has to many pages Upper bound reached process has to few frames Other considerations Prepaging load pages before process starts prevents initial high page fault rate Page size selection internal fragmentation page table size I/O transfer/seek time locality smaller pages gives higher resolution TLB reach reach = page size * # entries working set of process should fit in the TLB maybe several processes in the TLB... 8
Program structure Other considerations arrays stored row-wise each row in one page, or consecutive pages if accessed column-wise one page fault per access otherwise one page fault per row different array order in different languages... Memory over-allocation Possible to increase degreee of multiprogramming over-allocate memory Process only uses a fraction of its pages swap out pages not used use free frames to load another process Increases throughput and resource utilization What if process starts to use its frames? frames has to be paged in again other frames paged out Another type of memory overallocation OS let process allocate a lot of memory OS does not actually allocate anything! process often does not use all allocated memory when process starts using memory, it allocates one page at a time Memory allocated by processes may be larger than RAM may be larger than RAM+swap space! If all processes actually uses all memory we have a problem... optionally, we commit allocated memory to swap make sure there is space in the swap this takes some time, however... 9
Efficient process creation copy on write Do not copy any frames on process creation! flag frames as read-only use the same frames as the old process identical page table, except for the flag copy-on-write copy pages when they are modified if a process writes to a frame trap to kernel kernel allocates new frame copy the old frame restart the write instruction Very efficient if using exec() just after fork() no need to copy the process, and immediately overwrite it Copy-on-Write pages table pages table p new process p write use the same frames trap to kernel on write allocate new frame Memory mapped files Operate on a file as if its content would reside directly in memory portion of file loaded into memory mapped to pages OS responsible to mirror content between disk and memory File can easily be shared between processes Some systems use memory mapping for all file I/O solaris (normal file i/o is a wrapper for memory mapped i/o) 0
Memory mapped files part of file copied to frames frames mapped in page table several processes can share the file as with shared memory End of lecture and end of memory management Virtual memory: demand paging page replacement algos process creation memory mapped files frame allocation thrashing other considerations