Download
virtual dynamic memory management n.
Skip this Video
Loading SlideShow in 5 Seconds..
Virtual & Dynamic Memory Management PowerPoint Presentation
Download Presentation
Virtual & Dynamic Memory Management

Virtual & Dynamic Memory Management

140 Views Download Presentation
Download Presentation

Virtual & Dynamic Memory Management

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Virtual & Dynamic Memory Management Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University

  2. Your vision? Seek with all your heart? Course Objectives • The better knowledge of computer systems, the better programing. DMM

  3. Your vision? Seek with all your heart? Course Contents • Introduction to computer systems: B&O 1 • Introduction to C programming: K&R 1 – 4 • Data representations: B&O 2.1 – 2.4 • C: advanced topics: K&R 5.1 – 5.10, 6 – 7 • Introduction to IA32 (Intel Architecture 32): B&O 3.1 – 3.8, 3.13 • Compiling, linking, loading, and executing: B&O 7 (except 7.12) • Dynamic memory management – Heap: B&O 9.1–2, 9.4–5, 9.9.1–2, 9.9.4–5, 9.11 • Code optimization: B&O 5.1 – 5.6, 5.13 • Memory hierarchy, locality, caching: B&O 5.12, 6.1 – 6.3, 6.4.1 – 6.4.2, 6.5, 6.6.2 – 6.6.3, 6.7 • Virtual memory (if time permits): B&O 9.4 – 9.5 DMM

  4. Your vision? Seek with all your heart? Unit Learning Objectives • Which type of addresses are used by machine instructions? Virtual addresses, or physical addresses? • List the three advantages of using virtual memory. • What hardware component translates virtual addresses to physical addresses? • Explain how virtual memory support memory protection? • Use of malloc() and free() in C programs. • Identify memory-related problems in given C code. DMM

  5. Your vision? Seek with all your heart? Unit Contents • Physical and Virtual Addressing • Address Spaces • VM as a Tool for Memory Management • VM as a Tool for Memory Protection • Dynamic Memory Management • Common Memory-Related Bugs in C Programs DMM

  6. Your vision? Seek with all your heart? 9.1 Physical and Virtual Addressing DMM

  7. Carnegie Mellon A System Using Physical Addressing • Used in “simple” systems like embedded microcontrollers in devices like cars, elevators, and digital picture frames • Instructions use physical addresses. Programmers need to think of main memory. Main memory 0: 1: 2: Physical address (PA) 3: CPU 4: 4 5: 6: 7: 8: ... M-1: Data word [Q] Do instructions use PAs or VAs?

  8. Carnegie Mellon A System Using Virtual Addressing • Used in all modern servers, desktops, and laptops. • One of the great ideas in computer science. • Instructions use virtual addresses. Programmer do not have to think of main memory. Main memory 0: CPU Chip 1: 2: Virtual address (VA) Physical address (PA) 3: MMU CPU 4: 4 4100 5: 6: (Memory Management Unit: VAs are translated to PAs.) 7: 8: ... M-1: Data word [Q] Do instructions use PAs or VAs?

  9. Your vision? Seek with all your heart? 9.2 Address Spaces DMM

  10. Carnegie Mellon Virtual memory Physical memory Address Spaces 0 VP 0 Unallocated 0 VP 1 PP 0 Cached Empty PP 1 Uncached Unallocated Empty • Linear address space: Ordered set of contiguous non-negative integer addresses: {0, 1, 2, 3 … } • Virtual address space: Set of N = 2n virtual addresses {0, 1, 2, 3, …, N-1} [Q] For each process ??? Yes. Uniform view for every process. • Physical address space: Set of M = 2m physical addresses {0, 1, 2, 3, …, M-1} [Q] For each process ??? • Clean distinction between data (bytes) and their attributes (addresses) • Every byte in main memory: one physical address, one (or more) virtual addresses Cached Uncached Empty PP 2m-p-1 Cached M-1 VP 2n-p-1 Uncached N-1 Through MMU Virtual Pages (VP's) stored on disk Physical Pages (PP's) cached in DRAM [Q] M >= N ???

  11. Carnegie Mellon Why Virtual Memory (VM)? 0 VP 0 Unallocated 0 VP 1 PP 0 Cached Empty PP 1 Uncached Unallocated Empty • Uses main memory efficiently • Use DRAM as a cache for the parts of a virtual address space • [Q] Should all the parts of an executable file be loaded into DRAM? • Simplifies memory management • Each process gets the same uniform linear address space -> easy jobs for compilers, linkers and loaders • Isolates address spaces -> memory protection • One process can’t interfere with another’s memory • User program cannot access privileged kernel information • [Q] How? By software, or hardware? Cached Uncached Empty PP 2m-p-1 Cached M-1 VP 2n-p-1 Uncached N-1 Through MMU

  12. Your vision? Seek with all your heart? 9.4 VM for Memory Management DMM

  13. Carnegie Mellon VM as a Tool for Memory Management • Key idea: Each process has its own virtual address space. • It can view memory as a simple linear array. • Mapping function scatters addresses through physical memory • Well chosen mappings simplify memory allocation and management. • [Q] Is a VP-PP mapping table used for each process, or one common table for all processes? • Each process Address translation [Q] by what? 0 0 Physical Address Space (DRAM) Virtual Address Space for Process 1: VP 1 VP 2 PP 2 ... N-1 (e.g., read-only library code) PP 6 0 Virtual Address Space for Process 2: PP 8 VP 1 VP 2 ... ... M-1 N-1

  14. Carnegie Mellon • Memory allocation • Each virtual page can be mapped to any physical page. • A virtual page can be stored in different physical pages at different times. • Sharing code and data among processes • Map virtual pages to the same physical page (here: PP 6). Address translation [Q] by what? 0 0 Physical Address Space (DRAM) Virtual Address Space for Process 1: VP 1 VP 2 PP 2 ... N-1 (e.g., read-only library code) PP 6 0 Virtual Address Space for Process 2: PP 8 VP 1 VP 2 ... ... M-1 N-1

  15. Carnegie Mellon Simplifying Linking and Loading Memory invisible to user code Kernel virtual memory • Linking • Each program has similar virtual address space • Code, stack, and shared libraries always start at the same address • Loading • [Q] Does each process use its own VP-PP mapping table? • execve() allocates virtual pages for .text and .data sections = creates PTEs (Page Table Entries) marked as invalid • The .text and .data sections are copied, page by page, on demand by the virtual memory system 0xc0000000 User stack (created at runtime) %esp (stack pointer) Memory-mapped region for shared libraries 0x40000000 brk Run-time heap (created by malloc) Loaded from the executable file Read/write segment (.data, .bss) Read-only segment (.init, .text, .rodata) 0x08048000 Unused 0

  16. Your vision? Seek with all your heart? 9.5 VM for Memory Protection DMM

  17. Carnegie Mellon VM as a Tool for Memory Protection • Extend PTEs (Page Table Entries) with permission bits • Page fault handler checks these before remapping • If violated, send process SIGSEGV (segmentation fault). [Q] To what? • [Q] Is segmentation fault detected by software, or hardware? Physical Address Space SUP READ WRITE Address Process i: VP 0: No Yes No PP 6 VP 1: No Yes Yes PP 4 PP 2 VP 2: Yes Yes Yes PP 2 • • • PP 4 SUP indicates whether the process must be running in kernel (supervisor) mode to access the page. PP 6 SUP READ WRITE Address Process j: PP 8 No Yes No PP 9 VP 0: PP 9 Yes Yes Yes PP 6 VP 1: PP 11 No Yes Yes PP 11 VP 2: MMU

  18. Your vision? Seek with all your heart? 9.9 Dynamic Memory Allocation DMM

  19. Top of heap (brk ptr) Carnegie Mellon Dynamic Memory Allocation • Programmers use dynamic memory allocators (such as malloc) to acquire VM at run time. • For data structures whose size is only known at runtime. • Dynamic memory allocators manage an area of process virtual memory known as the heap. Application Dynamic Memory Allocator Heap User stack Heap (via malloc) Uninitialized data (.bss) Initialized data (.data) Program text (.text) 0

  20. Carnegie Mellon • Allocator maintains heap as collection of variable sized blocks, which are either allocated or free • Types of allocators • Explicit allocator: application allocates and frees space • E.g., malloc()and free()in C • Implicit allocator: application allocates, but does not free space • E.g. garbage collection in Java, ML, and Lisp • Will discuss simple explicit memory allocation

  21. Carnegie Mellon The malloc Package #include <stdlib.h> void *malloc(size_t size) • Successful: • Returns a pointer to a memory block of at least size bytes(typically) aligned to 8-byte boundary • If size == 0, returns NULL • Unsuccessful: returns NULL (0) and sets errno void free(void *p) • Returns the block pointed at by p to pool of available memory • p must come from a previous call to malloc or realloc Other functions • calloc: Version of malloc that initializes allocated block to zero. • realloc: Changes the size of a previously allocated block. • brk, sbrk: Used internally by allocators to grow or shrink the heap

  22. Carnegie Mellon malloc Example void foo(int n, int m) { int i, *p; /* Allocate a block of n ints */ p = (int *) malloc(n * sizeof(int)); if (p == NULL) { perror("malloc"); // [Q] what is it??? exit(0); } /* Initialize allocated block */ for (i=0; i<n; i++) p[i] = i; /* Return p to the heap */ free(p); }