cs356 unit 10
play

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

10.1 10.2 CS356 Unit 10 Memory Allocation & Heap Management BASIC OS CONCEPTS & TERMINOLOGY 10.3 10.4 User vs. Kernel Mode Processes Program/Process Kernel mode is a special mode of the processor for executing __________


  1. 10.1 10.2 CS356 Unit 10 Memory Allocation & Heap Management BASIC OS CONCEPTS & TERMINOLOGY 10.3 10.4 User vs. Kernel Mode Processes Program/Process • Kernel mode is a special mode of the processor for executing __________ • Process 1,2,3,… code – (def 1.) Address Space + Threads 0xffff ffff - – Certain ___________________ (such as I/O access) are only allowed to code • 1 or more threads Mapped running in kernel mode – (def 2.) : Running instance of a program that has I/O limited rights – OS and other system software should run in kernel mode - 0xc0000000 • Memory is protected: Address translation (VM) ensures no Stack • User mode is where user applications are designed to run to limit what access to any other processes' memory - they can do on their own • I/O is protected: Processes execute in user-mode (not Heap kernel mode) which generally means direct I/O access is – Provides _________________ by forcing them to use the OS for many services disallowed instead requiring system calls into the kernel - • User vs. kernel mode determined by some bit(s) in some processor control • Kernel is not considered a "process" Data 0x10000000 register - – Has access to all resources and much of its code is = Thread – x86 Architecture uses lower 2-bits in the CS segment register (referred to as invoked under the execution of a user process thread (i.e. Code 0x00000000 the Current Privilege Level bits [CPL]) during a system call) – 0=Most privileged (___________ mode) and 3=Least privileged (_____ mode) • User process invokes the OS (kernel code) via Address Spaces system calls (see next slide) • Levels 1 and 2 may also be used but are not by Linux

  2. 10.5 10.6 System Calls and Mode Switches User User • What causes user to kernel Process Process mode switch? OS OS Library Library – An exception: interrupt, error, or syscall syscall system call Kernel Code • System Calls: Provide a OS Virtual Scheduler Memory Kernel controlled method for user File Device System Drivers mode applications to _______ enum HEAP MANAGEMENT { /* 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 10.7 10.8 Overview C Dynamic Memory Allocation CS:APP 9.9.1 Memory / RAM void* malloc( int num_bytes ) function in • 0xfffffffc • Heap management is an - Memory / RAM 0x80000000 stdlib.h 0xfffffffc User stack - important component that 0x80000000 – Allocates the number of bytes requested and User stack returns a pointer to the block of memory affects program performance - free(void * ptr) function • Memory-mapped - Regions for shared – Given the pointer to the (starting location of the) • Need to balance: libraries Memory-mapped block of memory, free returns it to the system for re- ... Regions for shared malloc libraries Heap use by subsequent malloc calls allocates: – __________ & performance of scores[3] 0x0006de4c • C++ uses the familiar new/delete scores[2] - 0x0006de48 allocation/deallocation scores[1] 0x0006de44 int main() 0x0006de40 scores[0] { Uninitialized Data Heap int num; – Memory utilization (reduce (.bss) printf("How many students?\n"); Initialized Data Uninitialized Data scanf("%d", &num); (.data) (.bss) 0x10000000 ________________ areas) Initialized Data - int *scores = (int*) malloc(num * sizeof(int)); (.data) 0x10000000 Code (.text) // can now access scores[0] .. scores[num-1]; – Ease of usage by the - 0x00400000 Code (.text) free(scores); // deallocate - programmer return 0; 0x00400000 } -

  3. 10.9 10.10 OS & the Heap A First Look at Malloc/New (1) • The OS kernel maintains the brk • The C-library implementation will provide Memory / RAM an implementation to manage the heap Memory / RAM pointer 0xfffffffc 0xfffffffc - - • At startup, the C-Library will allocate an 0x80000000 0x80000000 – Virtual address of the _____________ User stack User stack initialize size of the heap via sbrk – ______ process – void* heap_init; - - • brk pointer is updated via a _______ – heap_init = sbrk(________); // 1 MB Memory-mapped Memory-mapped Regions for shared Regions for shared ______ (see Linux example below) libraries libraries – #include <unistd.h> - - new brk – __________ sbrk(intptr_t increment); brk ptr. Heap • Increments the brk pointer (up or down) orig brk Uninitialized Data Uninitialized Data and returns the _______ pointer on success (.bss) (.bss) Initialized Data Initialized Data – Newly allocated memory is ______________ (.data) (.data) 0x10000000 0x10000000 - - • Malloc/new provide the common interface Code (.text) Code (.text) to use this 0x00400000 0x00400000 - - intptr_t is a signed integer type that will match the size of pointers (32- or 64-bits) 10.11 10.12 A First Look at Malloc/New (2) A First Look at Malloc/New (3) • The C-library implementation will provide • The C-library implementation will provide an implementation to manage the heap an implementation to manage the heap Memory / RAM Memory / RAM 0xfffffffc 0xfffffffc - - • At startup, the C-Library will allocate an • At startup, the C-Library will allocate an 0x80000000 0x80000000 User stack User stack initialize size of the heap via sbrk initialize size of the heap via sbrk • Subsequent requests by malloc or new will • Subsequent requests by malloc or new will - - give out portions of the heap Memory-mapped give out portions of the heap Memory-mapped Regions for shared Regions for shared libraries libraries • Calls to free or delete will reclaim those • Calls to free or delete will reclaim those - new brk - memory areas memory areas Heap brk old brk allocated free allocated free Heap Heap free allocated free allocated • If there is not enough free heap memory to • If there is not enough free heap memory to allocated alloc allocated alloc Heap Heap free allocated free allocated satisfy a call to malloc/new then the library satisfy a call to malloc/new then the library Uninitialized Data Uninitialized Data (.bss) (.bss) will use sbrk to increase the size of the Initialized Data will use sbrk to increase the size of the Initialized Data (.data) (.data) 0x10000000 0x10000000 heap - heap - Code (.text) Code (.text) – When no memory exists, an _______________________ – When no memory exists, an exception or NULL pointer will 0x00400000 0x00400000 will be returned and the program may fail be returned and the program may fail - -

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