cs356 unit 10
play

CS356 Unit 10 Memory Allocation & Heap Management 10.2 BASIC - PowerPoint PPT Presentation

10.1 CS356 Unit 10 Memory Allocation & Heap Management 10.2 BASIC OS CONCEPTS & TERMINOLOGY 10.3 User vs. Kernel Mode Kernel mode is a special processor mode for executing trusted (OS) code Certain features/privileges (such


  1. 10.1 CS356 Unit 10 Memory Allocation & Heap Management

  2. 10.2 BASIC OS CONCEPTS & TERMINOLOGY

  3. 10.3 User vs. Kernel Mode • Kernel mode is a special processor mode for executing trusted (OS) code – Certain features/privileges (such as I/O access) are only allowed to code running in kernel mode – OS and other system software should run in kernel mode • User mode is where user applications are designed to run to limit what they can do on their own – Provides protection by forcing them to use the OS for many services • User/kernel mode determined by bits in some processor control register – x86 Architecture uses lower 2-bits in the CS segment register (referred to as the Current Privilege Level bits [CPL]) – 0=Most privileged (kernel mode) and 3=Least privileged (user mode) • Levels 1 and 2 may also be used but are not used by Linux

  4. 10.4 Processes Program/Process • Process 1,2,3,… – (def 1.) Address Space + Threads 0xffff ffff - • 1 or more threads Mapped – (def 2.) : Running instance of a program that has I/O limited rights - 0xc0000000 • Memory is protected: Address translation (VM) ensures no Stack access to any other processes' memory - • I/O is protected: Processes execute in user-mode (not Heap kernel mode) which generally means direct I/O access is disallowed instead requiring system calls into the kernel - • Kernel is not considered a "process" Data 0x10000000 - – Has access to all resources and much of its code is = Thread invoked under the execution of a user process thread (i.e. Code 0x00000000 during a system call) • User process invokes the OS (kernel code) via Address Spaces system calls (see next slide)

  5. 10.5 System Calls and Mode Switches • What causes user to kernel User User Process Process mode switch? OS Library OS Library – An exception: interrupt, error, syscall syscall or system call Kernel Code • System Calls: Provide a OS Scheduler Virtual Kernel Memory controlled method for user Device File System Drivers mode applications to call enum { /* Projects 2 and later. */ kernel mode (OS) code SYS_HALT, /* 0 = Halt the operating system. */ SYS_EXIT, /* 1 = Terminate this process. */ SYS_EXEC, /* 2 = Start another process. */ – OS will define all possible system SYS_WAIT, /* 3 = Wait for a child process */ SYS_CREATE, /* 4 = Create a file. */ SYS_REMOVE, /* 5 = Delete a file. */ calls available to user apps. SYS_OPEN, /* 6 = Open a file. */ SYS_FILESIZE, /* 7 = Obtain a file's size. */ SYS_READ, /* 8 = Read from a file. */ SYS_WRITE, /* 9 = Write to a file. */ ... }; Syscalls from Pintos OS

  6. 10.6 HEAP MANAGEMENT

  7. 10.7 Overview CS:APP 9.9.1 • Heap management is an Memory / RAM 0xfffffffc - important component that 0x80000000 User stack affects program performance - • Need to balance: Memory-mapped Regions for shared libraries – Speed & performance of - allocation/deallocation – Memory utilization (reduce Heap Uninitialized Data wasted areas) (.bss) Initialized Data (.data) 0x10000000 – Ease of usage by the - Code (.text) programmer 0x00400000 -

  8. 10.8 C Dynamic Memory Allocation Memory / RAM Functions from stdlib.h 0xfffffffc - • void * malloc (size_t size) 0x80000000 – Allocates size bytes and returns a pointer to the block User stack • void * calloc (size_t nmemb, size_t size) - – Allocates nmemb*size bytes, sets the memory to 0 , Memory-mapped returns a pointer to the block Regions for shared libraries • void free (void *ptr) function ... malloc Heap allocates: – Frees the block at address ptr (returned by malloc/calloc), scores[3] 0x0006de4c returns it to the system for re-use by subsequent malloc calls scores[2] 0x0006de48 scores[1] 0x0006de44 int main() { 0x0006de40 scores[0] int num; Uninitialized Data (.bss) printf("How many students?\n"); scanf("%d", &num); Initialized Data (.data) 0x10000000 // cast “(int*)” from void* not necessary in C - int *scores = malloc(num * sizeof(int)); Code (.text) // can now access scores[0] .. scores[num-1]; 0x00400000 free(scores); // deallocate - return 0; }

  9. 10.9 OS & the Heap • The OS kernel maintains the brk Memory / RAM pointer 0xfffffffc - 0x80000000 – Virtual address of the top of the heap User stack – Per process (threads share the heap) - • brk pointer is updated via a system Memory-mapped Regions for shared call (see Linux example below) libraries – #include <unistd.h> - – void *sbrk(intptr_t increment); brk ptr. • Increments the brk pointer (up or down) Heap Uninitialized Data and returns the old brk pointer on success (.bss) – Newly allocated memory is zero-initialized Initialized Data (.data) 0x10000000 • malloc/free allow the reuse of blocks - Code (.text) allocated on the heap with sbrk 0x00400000 - intptr_t is a signed integer type that will match the size of pointers (32- or 64-bits)

  10. 10.10 A First Look at malloc (1) • The C-library implementation will provide Memory / RAM an implementation to manage the heap 0xfffffffc • At startup, the C-Library will allocate an - 0x80000000 User stack initialize size of the heap via sbrk – void *heap_init; - – heap_init = sbrk(1 << 20); // 1 MB Memory-mapped Regions for shared libraries - new brk orig brk Uninitialized Data (.bss) Initialized Data (.data) 0x10000000 - Code (.text) 0x00400000 -

  11. 10.11 A First Look at malloc (2) • The C-library implementation will provide Memory / RAM an implementation to manage the heap 0xfffffffc • At startup, the C-Library will allocate an - 0x80000000 User stack initialize size of the heap via sbrk • Subsequent requests by malloc (or new) - will give out portions of the heap Memory-mapped Regions for shared libraries • Calls to free or delete will reclaim those - memory areas brk Heap allocated free free allocated allocated alloc Heap free allocated Uninitialized Data (.bss) Initialized Data (.data) 0x10000000 - Code (.text) 0x00400000 -

  12. 10.12 A First Look at malloc (3) • The C-library implementation will provide Memory / RAM an implementation to manage the heap 0xfffffffc • At startup, the C-Library will allocate an - 0x80000000 User stack initialize size of the heap via sbrk • Subsequent requests by malloc (or new) - will give out portions of the heap Memory-mapped Regions for shared libraries • Calls to free or delete will reclaim those - new brk memory areas Heap old brk Heap allocated free • If there is not enough contiguous free heap free allocated allocated alloc Heap free allocated memory to satisfy a call to malloc/new Uninitialized Data (.bss) then the library will use sbrk to increase Initialized Data (.data) 0x10000000 the size of the heap - Code (.text) – When no memory exists, an exception or NULL pointer will be returned and the program may fail 0x00400000 -

  13. 10.13 Allocators and Garbage Collection • An allocator will manage the free Memory / RAM space of the heap 0xfffffffc - 0x80000000 • Types: User stack – Explicit Allocator: Requires the - programmer to explicitly free memory Memory-mapped Regions for shared when it is no longer used libraries - • Exemplified by malloc/new in C/C++ brk Heap – Implicit Allocator: Requires the allocator Heap allocated free free allocated to determine when memory can be allocated alloc Heap free allocated Uninitialized Data reclaimed and freed (i.e., known as (.bss) Initialized Data garbage collection) (.data) 0x10000000 - • Used by Java, Python, etc. Code (.text) 0x00400000 -

  14. 10.14 Allocator Requirements CS:APP 9.9.3 Memory / RAM • Arbitrary request sequences 0xfffffffc – No correlation to when allocation and free - 0x80000000 requests will be made User stack • Immediate response - – Cannot delay a request to optimize allocation Memory-mapped strategy Regions for shared libraries • Use only the heap - brk – Any heap management data must exist on the Heap heap or be scalar (single & not arrays) variables Heap allocated free free allocated • Align blocks allocated alloc Heap free allocated – Allocated blocks must be aligned to any type of Uninitialized Data (.bss) data Initialized Data (.data) 0x10000000 • Previously allocated blocks may not be moved - – Once allocated the block cannot be altered by Code (.text) the allocator until it is freed 0x00400000 -

  15. 10.15 Allocator Goals • Maximize throughput (i.e., fast allocation / deallocation) • Maximize memory utilization – Take as little memory as possible from the OS with sbrk – We need a formal definition of peak memory utilization U(k) = max { P(i) for i = 1, .., k } / H(k) • P(i) = memory allocated after i malloc/free requests • H(k) = total heap size (allocated/free) after k requests (Monotonically non-decreasing.) H k These goals are at odds with each other! P k • E.g., no need to keep track of free blocks for reuse k (~time) if we always allocate new blocks with sbrk.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend