Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation.

Similar presentations


Presentation on theme: "1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation."— Presentation transcript:

1 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

2 2 Overview 1. Virtual Memory 2. Paging 3. Segmentation 4. Combined Segmentation and Paging 5. Bibliography

3 3 1. Virtual Memory 1.1 Why Virtual Memory (VM)? 1.2 What is VM ? 1.3 The Mapping Process 1.4 Terms & Definitions 1.5 The Principle of Locality 1.6 VM: Features 1.7 VM: Advantages 1.8 VM: Disadvantages 1.9 VM: Implementation

4 4 1.1 Why Virtual Memory (VM)? Shortage of memory Efficient memory management needed OS Process 3 Process 1 Process 2 Process 4 Memory Process may be too big for physical memory More active processes than physical memory can hold Requirements of multiprogramming Efficient protection scheme Simple way of sharing

5 5 1.2 What is VM? Program:.... Mov AX, 0xA0F xA0F4 0xC0F4 Mapping Unit (MMU) Virtual Memory Physical Memory Virtual Address Physical Address Table (one per Process) „Piece“ of Virtual Memory „Piece“ of Physical Memory Note: It does not matter at which physical address a „piece“ of VM is placed, since the corresponding addresses are mapped by the mapping unit.

6 6 MMU 1.3 The Mapping Process Usually every process has its own mapping table  own virtual address space (assumed from now on) virtual address piece in physical memory? memory access fault OS brings „piece“ in from HDD physical address OS adjusts mapping table translate address yes check using mapping table Not every „piece“ of VM has to be present in PM „Pieces“ may be loaded from HDD as they are referenced Rarely used „pieces“ may be discarded or written out to disk (  swapping)

7 7 1.4 Terms & Notions Virtual memory (VM) is Not a physical device but an abstract concept Comprised of the virtual address spaces (of all processes) Virtual address space (VAS) (of one process) Set of visible virtual addresses (Some systems may use a single VAS for all processes) Resident set Pieces of a process currently in physical memory Working set Set of pieces a process is currently working on

8 8 1.5 The Principle of Locality Principle of Locality: Memory references within a process tend to cluster Working set should be part of the resident set to operate efficiently (else: frequent memory access faults)  honor the principle of locality to achieve this repeated references: single jumps: working set: initialization data initialization code early phase of process lifetime code 1code 2data main phase of process lifetime finalization code final phase of process lifetime

9 9 1.6 VM: Features Swapping Danger: Thrashing: „Piece“ just swapped out is immediately requested again System swaps in/out all the time, no real work is done Thus: „piece“ for swap out has to be chosen carefully Keep track of „piece“ usage („age of piece“) Hopefully „piece“ used frequently lately will be used again in near future (principle of locality!) lack of memory find rarely used „piece" adjust mapping table „piece“ modified? save HDD location of „piece“ discard „piece“ no write „piece“ out to disk yes no need to swap out complete process!!!

10 VM: Features Protection Each process has its own virtual address space Processes invisible to each other Process cannot access another processes memory MMU checks protection bits on memory access (during address mapping) „Pieces“ can be protected from being written to or being executed or even being read System can distinguish different protection levels (user / kernel mode) Write protection can be used to implement copy on write (  Sharing)

11 VM: Features Sharing „Pieces“ of different processes mapped to one single „piece“ of physical memory Allows sharing of code (saves memory), e.g. libraries Copy on write: „piece“ may be used by several processes until one writes to it (then that process gets its own copy) Simplifies interprocess-communication (IPC) Piece 2 Piece 1 Virtual memory Process 1 Piece 0 Piece 1 Piece 2 Piece 0 Physical memory shared memory Virtual memory Process 2 Piece 1 Piece 0 Piece 2

12 VM: Advantages (1) VM supports Swapping  Rarely used „pieces“ can be discarded or swapped out  „Piece“ can be swapped back in to any free piece of physical memory large enough, mapping unit translates addresses Protection Sharing  Common data or code may be shared to save memory Process need not be in memory as a whole No need for complicated overlay techniques (OS does job) Process may even be larger than all of physical memory Data / code can be read from disk as needed

13 13 Code can be placed anywhere in physical memory without relocation (adresses are mapped!) Increased cpu utilization more processes can be held in memory (in part)  more processes in ready state (consider: 80% HDD I/O wait time not uncommon) 1.7 VM: Advantages (2)

14 VM: Disadvantages Memory requirements (mapping tables) Longer memory access times (mapping table lookup) Can be improved using TLB

15 VM: Implementation VM may be implemented using Paging Segmentation Combination of both Note: Everything said in the first chapter still holds for the following chapters!

16 16 2. Paging 2.1 What is Paging? 2.2 Paging: Implementation 2.3 Paging: Features 2.4 Paging: Advantages 2.5 Paging: Disadvantages 2.6 Summary: Conversion of a Virtual Address

17 What is Paging? Page 7 Page 5 Page 4 Page 3 Page 2 Page 1 Page 6 Virtual memory (divided into equal size pages) Page 0 0x00 Page Table (one per process, one entry per page maintained by OS) Page 7 Page 5 Page 4 Page 3 Page 2 Page 1 Page 6 Page 0 v v v Frame 0 Frame 1 Frame 3 Frame 0 Frame 1 Frame 2 Frame 3 Physical memory (divided into equal size page frames) 0x00 v v v

18 Paging: Implementation Typical Page Table Entry Page Frame # executex writew readr rwx validv rwx v referencedre v modifiedm re m shareds m s caching disabledc s c super-pagesu c process idpid su pid guard data (extended) guard gd g pid ggd g other

19 Paging: Implementation Singlelevel Page Tables 0x140x2 Virtual address Page #Offset Physical address 0x14 Offset 0x8 Frame # Problem: Page tables can get very large, e.g. 32 bit address space, 4KB pages  2^20 entries per process  4MB at 4B per entry 64 bit  GB page table!!!! Page Table Base Register (PTBR) Page Table 0x8... 0x0 * L 0x1 * L 0x2 * L L : size of entry one entry per page one table per process

20 Paging: Implementation Multilevel Page Tables OffsetPage #1Page #2Page #3 Page Directory Page Middle Directory Page Table Page Frame # Offset Frame # Oversized Super-Page v=0 not all need be present saves memory table size can be restricted to one page

21 Paging: Implementation Inverted Page Tables 0x140xA Virtual address Page #Offset 0x14 Physical address Offset 0x2 Frame # Problem: Additional information about pages not presently in memory must still be kept somehow (e.g. normal page tables on HDD) hash table one entry per frame one table for all processes Inverted Page Table 0xAPID... hash fkt. 0x0 0x1

22 22 Page Directory (guarded) 2.2 Paging: Implementation Guarded Page Tables (1) 0xA0x50x140x2 Virtual address Page #1OffsetPage #2Page #3 Page Middle Directory Page Table Frame # 0x3B2 only one valid entry per table 0xA50x3B20x8 page fault no yes = Frame # or page table base guard length 0x14 Physical address Offset table not needed, if guard in place

23 Paging: Implementation Guarded Page Tables (2) Guarded page tables especially interesting if hardware offers only TLB (zero-level paging, MIPS) OS has total flexibility, may use  Different sizes of pages and page tables (all powers of 2 ok) and as many levels as desired  Guarded page tables  Inverted page tables Optimization: guarded table entry will usually not contain guard and guard length but equivalent information Note that handling of protection is to be modified

24 24 VM 2.3 Paging: Features Prepaging Process requests consecutive pages (or just one)  OS loads following pages into memory as well (expecting they will also be needed) Saves time when large contiguous structures are used (e.g. huge arrays) Wastes memory and time case pages not needed referenced by process prepaged by OS

25 Paging: Features Demand Paging On process startup only first page is loaded into physical memory Pages are then loaded as referenced Saves memory But: may cause frequent page faults until process has its working set in physical memory. OS may adjust its policy (demand / prepaging) dependent on Available free physical memory Process types and history

26 Paging: Features Cheap Memory Allocation No search for large enough a piece of PM necessary Any requested amount of memory is divided into pages  can be distributed over the available frames OS keeps a list of free frames If memory is requested the first frame is taken 164 Linked list of free frames Page 1 Page 2 Process started, requiring 6KB (4KB pages) Frame 0 Frame 1 Frame 2 Frame 3 Frame 4 Frame 5 Frame 6 Frame 7 PM Frame 0 Frame 1 Frame 2 Frame 3 Frame 4 Frame 5 Frame 6 Frame 7 PM 4 Page 1 Page 2

27 27 Process requires memory Paging VM systemNon-Paging VM system 2.3 Paging: Features Simplified Swapping Process requires 3 frames  swap out 3 most seldomly used pages Swapping out the 3 most seldomly used „pieces“ will not work  Swap algo must try to create free pieces as big as possible (costly!) 1 „piece“ 3 pages PM rarely used

28 Paging: Advantages Allocating memory is easy and cheap Any free page is ok, OS can take first one out of list it keeps Eliminates external fragmentation Data (page frames) can be scattered all over PM  pages are mapped appropriately anyway Allows demand paging and prepaging More efficient swapping No need for considerations about fragmentation Just swap out page least likely to be used

29 Paging: Disadvantages Longer memory access times (page table lookup) Can be improved using  TLB  Guarded page tables  Inverted page tables Memory requirements (one entry per VM page) Improve using  Multilevel page tables and variable page sizes (super-pages)  Guarded page tables  Page Table Length Register (PTLR) to limit virtual memory size Internal fragmentation Yet only an average of about ½ page per contiguous address range

30 30 OS Hard ware 2.6 Summary: Conversion of a Virtual Address Virtual address Physical address no protection fault reference legal? copy on write? HDD I/O complete: interrupt memory full? update page table process into ready state copy page process into blocking state yes page fault no exception to process no TLB page table miss page in mem? hit access rights? yes update TLB yes exception to process no swap out a page yes HDD I/O read req. no yes bring in page from HDD! process into blocking state

31 31 3. Segmentation 3.1 What is Segmentation? 3.2 Segmentation: Advantages 3.3 Segmentation: Disadvantages

32 32 MMU STBR STLR BaseLimitOther Segment table 3.1 What is Segmentation? Seg 1 (code) Seg 2 (data) Seg 3 (stack) Virtual memory Segment #Offset virtual address memory access fault no Physical memory Seg 2 (data) Seg 1 (code) Seg 3 (stack) 0x00 as in paging: valid, modified, protection, etc. offset < limit ? External fragmentation Segment Base + Offset physical address yes

33 Segmentation: Advantages As opposed to paging: No internal fragmentation (but: external fragmentation) May save memory if segments are very small and should not be combined into one page (e.g. for reasons of protection) Segment tables: only one entry per actual segment as opposed to one per page in VM Average segment size >> average page size  less overhead (smaller tables)

34 Segmentation: Disadvantages External fragmentation Costly memory management algorithms Segmentation: find free memory area big enough (search!) Paging: keep list of free pages, any page is ok (take first!) Segments of unequal size not suited as well for swapping

35 35 4. Combined Segmentation and Paging (CoSP) 4.1 What is CoSP? 4.2 CoSP: Advantages 4.3 CoSP: Disadvantages

36 36 limitbase Segment Table 4.1 What is CoSP? Page Directory size limited by segment number size limited by segment limit OffsetSeg #Page #1Page #2 Virtual Address Page Table Page Frame #Offset Physical Address not all need be present

37 CoSP: Advantages Reduces memory usage as opposed to pure paging Page table size limited by segment size Segment table has only one entry per actual segment Simplifies handling protection and sharing of larger modules (define them as segments) Most advantages of paging still hold Simplifies memory allocation Eliminates external fragmentation Supports swapping, demand paging, prepaging etc.

38 CoSP: Disadvantages Internal fragmentation Yet only an average of about ½ page per contiguous address range Page 1 Page 2 Process requests a 6KB address range (4KB pages) internal fragmentation

39 39 The End


Download ppt "1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation."

Similar presentations


Ads by Google