Operating Systems (2INC0) 2019/20

Operating Systems (2INC0) 2019/20

Operating Systems (2INC0) 2019/20 Virtual Memory (10) Dr. Tanir Ozcelebi Courtesy of Dr. I. Radovanovic, Dr. R. Mak System Architecture and Networking Group Agenda • Recap memory management in early systems • Principles of virtual memory • Paging • Segmentation • Paging and Segmentation Tanir Ozcelebi 10-Dec-19 2 Agenda • Recap memory management in early systems • Principles of virtual memory • Paging • Segmentation • Paging and Segmentation Tanir Ozcelebi 10-Dec-19 3 Memory management: requirements IDEALLY, memory needs to be • Simple to use • Private (isolation) • Non-volatile / permanent (data remains in memory) • Fast (zero-time) access • Huge (unlimited) capacity • Cheap (cost-effective) Conflicting requirements! We make use of memory hierarchy and virtualization to find a good tradeoff. Tanir Ozcelebi 10-Dec-19 4 Memory Hierarchy CPU Registers L1 Cache Memory L2 Cache \Memory Primary “Main” Memory (Executable) Solid State Memory Rotating Magnetic Memory Larger storage Larger Faster access Faster Secondary Optical Memory Sequentially Accessed Memory Tanir Ozcelebi 10-Dec-19 5 Early systems (properties) • Every active process resides in its entirety in MM • Large programs can’t execute (except with an overlay structure) − Overlay: Structure a program in independent parts (e.g. function calls) which can be overlaid in memory A B C versus D E • Every active process is allocated a contiguous part of main memory. • Three partitioning schemes • fixed, dynamic, relocatable dynamic partitions Tanir Ozcelebi 10-Dec-19 6 Early systems (partitioning schemes) • Fixed partitions • limits the number and maximal size of the active processes • internal fragmentation • Dynamic partitions • external fragmentation • Relocatable dynamic partitions • requires dynamic binding and relocatable load modules • no fragmentation, but expensive compaction (or swapping) Tanir Ozcelebi 10-Dec-19 7 Recap: Do we have the ideal memory properties so far? With the exception Simple Yes of overlays Isolation can be provided with dynamic Private Yes/No binding. No sharing Unless the programmer enforces Permanent No this during execution. Process-based compaction and swapping Fast No are expensive Process size cannot exceed main Huge No memory size Cost-effective Yes Hardware hierarchy Tanir Ozcelebi 10-Dec-19 8 Agenda • Recap memory management in early systems • Principles of virtual memory • Paging • Segmentation • Paging and Segmentation Tanir Ozcelebi 10-Dec-19 9 VM: abstraction What needs to be done such that • programs do not have to be stored contiguously, and • not the entire program, but only parts are stored in main memory? Approach: • Split the memory into segments and try to fit parts of the program into those segments. • Terminology: • If segments are of different sizes: segmentation • If segments are of the same size: paging − Physical memory blocks: (page) frames − Logical memory blocks: pages • Paging and segmentation can be combined Tanir Ozcelebi 10-Dec-19 10 Paging memory allocation • Processes divided into pages (memory blocks) of equal size • provides non-contiguous memory allocation (size: 350 lines) • works well if page size is equal to page frame size − and the disk’s section size (sectors) • Advantages: • An empty page frame is always usable by any process • The compaction scheme is not required − No external and almost no internal fragmentation • Disadvantage: • Mechanism needed to keep track of page locations Tanir Ozcelebi 10-Dec-19 11 Non-contiguous allocation -an example- Page Process 1 Main Memory frame # P1 size=350 lines 1st 100 lines Operating system 0 1 page size = 100 lines Page 0 2 3 OS size = 300 lines 2nd 100 lines 4 Page 1 Process 1 – Page 2 5 6 7 3rd 100 lines Process 1 – Page 0 8 Page 2 9 Process 1 – Page 1 10 Remaining 50 lines Page 3 Process 1 – Page 3 11 Internal fragmentation Wasted space 12 • The number of free page frames left (13-3-4) = 6 • Any process of more than 600 lines has to wait until Proc1 ends. • Any process of more than 1000 lines cannot fit into memory at all! • Problem remains (needs solving): • Entire process must be stored in memory during its execution Tanir Ozcelebi 10-Dec-19 12 VM: Demand paging • Bring a page into memory only when it is needed • Takes advantage of the fact that programs are written sequentially (not all pages are necessary at once). For example: • User-written error handling modules • Mutually exclusive modules • Some parts are not always accessible • Only a fraction of table is actually used • No restriction of having the entire process stored in memory • Gives appearance of an infinite physical memory Tanir Ozcelebi 10-Dec-19 13 VM: implementation issues • Address binding / translation • logical addresses translated to physical ones at run time • HW support to reduce access time & to enforce isolation • Placement strategies • simple for pages: any free page frame will do. • for segments, strategies are similar to those for dynamic partitions • Replacement strategies • that decide which page(s) or segment(s) must be swapped out in case there is not enough free space in MM • Load control policies that determine • how many pages of a process are resident in MM? • when to load pages into MM (demand paging, pre-paging)? • Sharing • possible with both paging and segmentation Tanir Ozcelebi 10-Dec-19 14 Agenda • Recap memory management in early systems • Principles of virtual memory • Paging • address translation − frame table, page table, TLBs • replacement strategies • load policies • Segmentation • Segmentation with paging Tanir Ozcelebi 10-Dec-19 15 Frame table vs Page table Process Process Page ID page Frame number Number 0 3 9 0 2 1 2 11 1 7 2 1 0 2 5 3 1 3 3 3 Process Page Index 4 3 8 Say the CPU wants to load an 5 1 2 Page Frame Index instruction of a certain process (at 6 3 12 a certain page p and an offset w). 7 1 1 How to find physical address (pa)? Tanir Ozcelebi 10-Dec-19 16 Virtual vs physical address |p| bits |w| bits Virtual Address (for a process id) va Page number p w 2|p| pages |w| w: offset in the corresponding page (# words) 2 words/page VM size: 2|p|+|w| words Goal: address a word Physical Address |f | bits |w| bits 2|f| page frames pa Frame number f w 2|w| words/page (or /frame) PM size: 2|f|+|w| words Tanir Ozcelebi 10-Dec-19 17 How to find physical address using the frame table? address_map (id, p, w) { Virtual address: id p w · . pa = UNDEFINED; . for (f = 0; f<F; f++) { if (FT[f].pid == id && frame f 0 FT[f].page == p ) 1 pa = f || w w page p } · · Concatenate . return pa; bits f and w . } Recall example id p pid p f · · 0 3 9 . 1 2 11 . · . 2 1 0 F-1 3 1 3 . pid page Page Frame Index Frame Page 4 3 8 Tanir Ozcelebi 10-Dec-19 18 How to find physical address using page tables? Virtual address: id p w address_map (p, w) { pa = *(PTR+p) || w; return pa; . } PTR frame f page p p w A Page Table Register (PTR) is Recall example used for fast access (hardware f support). Its content is stored in Page the process table. Frame Number Reading/writing from/to memory 0 2 requires two memory accesses: 1 7 · · One for accessing the page table, . and one for accessing actual data. 2 5 . 3 3 Proc. Page Proc. Index Page Tanir Ozcelebi 10-Dec-19 19 Process table • …maintained by OS for context switching and scheduling, etc. • Entries (rows) are process control info: name, size, state, priority, registers, a semaphore waited on, page table location etc. Process Table (a) Process Table (b) Process Table (c) Process Size Page Table Process Size Page Table Process Size Page Table Location Location Location P1 400 3096 400 3096 400 3096 P2 200 3100 700 3100 500 3150 500 3150 500 3150 P3 (a) PT has 3 entries initially; one for each process (b) second process ends, entry in table is released and replaced by (c) information about next process that is processed Tanir Ozcelebi 10-Dec-19 20 Translation Look-aside Buffer Problems with page tables • They can be huge. • It requires 2 memory accesses per reference. Approach • use a TLB (a cache memory) • keeps track of locations of the most recently used pages in MM • does not contain actual data or instructions • most common replacement scheme: least recently used (LRU) • if the page location cannot be found in the TLB, the page table is used • if the page location still cannot be found a page fault is generated • meaning the page is currently not found in the physical memory. Tanir Ozcelebi 10-Dec-19 21 Page faults • Page faults happen when a part of program needs to be brought into MM • Upon page fault: Page fault handler determines whether there are empty frames in MM • If not, it must decide which page to swap out • This depends on predefined policy for page removal • A single memory reference may create several page faults • e.g. when the page table itself is not in MM • Tables to be updated after swapping: • Page tables of two tasks (1 in 1 out) and table to locate free frames • Problem with swapping: Thrashing Tanir Ozcelebi 10-Dec-19 22 Thrashing • A process may spend more time paging than executing • When does it happen? • Pages in active use are replaced by other pages in active use • Happens with increased degree of multiprogramming • Solution: • Provide the process with as many frames in MM as it needs − needs a replacement strategy Tanir Ozcelebi 10-Dec-19 23 Thrashing -an example- for (j=1; j<100; ++j) { Page 0 k:=j*j; Swapping between these two pages m:=a*j; printf(“\n%d %d %d”, j, k, m); } Page 1 printf(“\n”); Tanir Ozcelebi 10-Dec-19 24 Agenda • Recap memory management in early systems • Principles of virtual memory

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    51 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us