User-Level Memory Management in Linux Programming

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Part IV: Memory Management
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Pointers A pointer is a reference to another variable (memory location) in a program –Used to change variables inside a function (reference parameters)
1 Memory Allocation Professor Jennifer Rexford COS 217.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Kernighan/Ritchie: Kelley/Pohl:
Week 7 - Friday.  What did we talk about last time?  Allocating 2D arrays.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Stacks and HeapsCS-3013 A-term A Short Digression on Stacks and Heaps CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
CS 536 Spring Run-time organization Lecture 19.
Run-Time Storage Organization
Run time vs. Compile time
Unix Process Environment. main Function A C program starts execution with a function called main. The prototype for the main function is: int main (int.
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
Pointers Applications
System Calls 1.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Compiler Construction
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Week 9 Part 1 Kyle Dewey. Overview Dynamic allocation continued Heap versus stack Memory-related bugs Exam #2.
Operating Systems CMPSC 473 Virtual Memory Management (4) November – Lecture 22 Instructor: Bhuvan Urgaonkar.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
Dynamic memory allocation and Pointers Lecture 4.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
ECE Application Programming
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Processes and Virtual Memory
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Lecture 3 Translation.
Stack and Heap Memory Stack resident variables include:
Processes and threads.
Advanced Programming with C
Lesson One – Creating a thread
Dynamic Memory Allocation
Memory Allocation CS 217.
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
EECE.2160 ECE Application Programming
Chapter 10-1: Dynamic Memory Allocation
Dynamic Memory – A Review
Week 7 - Friday CS222.
Presentation transcript:

User-Level Memory Management in Linux Programming

User-Level Memory Management in Linux Programming Linux/Unix Address Space Memory Allocation Library and System Calls Programming Example

Linux/Unix Address Space USER-LEVEL MEMORY MANAGEMENT Linux/Unix Address Space Without memory for storing data, it's impossible for a program to get any work done. (Or rather, it's impossible to get any useful work done.) Real-world programs can't afford to rely on fixed-size buffers or arrays of data structures. They have to be able to handle inputs of varying sizes, from small to large. This in turn leads to the use of dynamically allocated memory—memory allocated at runtime instead of at compile time.

Linux/Unix Address Space USER-LEVEL MEMORY MANAGEMENT Linux/Unix Address Space A process is a running program. This means that the operating system has loaded the executable file for the program into memory, has arranged for it to have access to its command-line arguments and environment variables, and has started it running.

Linux/Unix Address Space USER-LEVEL MEMORY MANAGEMENT Linux/Unix Address Space A process has five conceptually different areas of memory allocated to it: Code Initialized data Zero-initialized data Heap Stack

Linux/Unix Address Space USER-LEVEL MEMORY MANAGEMENT Linux/Unix Address Space Code Often referred to as the text segment. this is the area in which the executable instructions reside. Linux and Unix arrange things so that multiple running instances of the same program share their code if possible. Only one copy of the instructions for the same program resides in memory at any time. (This is transparent to the running programs.) The portion of the executable file containing the text segment is the text section.

Linux/Unix Address Space USER-LEVEL MEMORY MANAGEMENT Linux/Unix Address Space Initialized data Statically allocated and global data that are initialized with nonzero values live in the data segment. Each process running the same program has its own data segment. The portion of the executable file containing the data segment is the data section.

Linux/Unix Address Space USER-LEVEL MEMORY MANAGEMENT Linux/Unix Address Space Zero-initialized data Global and statically allocated data that are initialized to zero by default are kept in what is colloquially called the BSS area of the process. Each process running the same program has its own BSS area. When running, the BSS data are placed in the data segment. In the executable file, they are stored in the BSS section.

Linux/Unix Address Space USER-LEVEL MEMORY MANAGEMENT Linux/Unix Address Space The format of a Linux/Unix executable is such that only variables that are initialized to a nonzero value occupy space in the executable's disk file. Thus, a large array declared 'static char somebuf[2048];', which is automatically zero-filled, does not take up 2 KB worth of disk space. Some compilers have options that let you place zero-initialized data into the data segment.

Linux/Unix Address Space USER-LEVEL MEMORY MANAGEMENT Linux/Unix Address Space Heap The heap is where dynamic memory (obtained by malloc() and friends) comes from. As memory is allocated on the heap, the process's address space grows. Although it is possible to give memory back to the system and shrink a process's address space, this is almost never done. We distinguish between releasing nolonger-needed dynamic memory and shrinking the address space.

Linux/Unix Address Space USER-LEVEL MEMORY MANAGEMENT Linux/Unix Address Space It is typical for the heap to "grow upward." This means that successive items that are added to the heap are added at addresses that are numerically greater than previous items. It is also typical for the heap to start immediately after the BSS area of the data segment.

Linux/Unix Address Space USER-LEVEL MEMORY MANAGEMENT Linux/Unix Address Space Stack The stack segment is where local variables are allocated. Local variables are all variables declared inside the opening left brace of a function body (or other left brace) that aren't defined as static. On most architectures, function parameters are also placed on the stack, as well as "invisible" bookkeeping information generated by the compiler, such as room for a function return value and storage for the return address representing the return from a function to its caller. (Some architectures do all this with registers.)

Linux/Unix Address Space USER-LEVEL MEMORY MANAGEMENT Linux/Unix Address Space It is the use of a stack for function parameters and return values that makes it convenient to write recursive functions (functions that call themselves). Variables stored on the stack "disappear" when the function containing them returns. The space on the stack is reused for subsequent function calls. On most modern architectures, the stack "grows downward," meaning that items deeper in the call chain are at numerically lower addresses.

Linux/Unix Address Space USER-LEVEL MEMORY MANAGEMENT Linux/Unix Address Space When a program is running, the initialized data, BSS, and heap areas are usually placed into a single contiguous area: the data segment. The stack segment and code segment are separate from the data segment and from each other.

Linux/Unix Address Space USER-LEVEL MEMORY MANAGEMENT Linux/Unix Address Space Although it's theoretically possible for the stack and heap to grow into each other, the operating system prevents that event. Any program that tries to make it happen is asking for trouble. This is particularly true on modern systems, on which process address spaces are large and the gap between the top of the stack and the end of the heap is a big one. The different memory areas can have different hardware memory protection assigned to them.

Linux/Unix Address Space USER-LEVEL MEMORY MANAGEMENT Linux/Unix Address Space For example, the text segment might be marked "execute only," whereas the data and stack segments would have execute permission disabled. This practice can prevent certain kinds of security attacks.

Linux/Unix Address Space USER-LEVEL MEMORY MANAGEMENT Linux/Unix Address Space The relationship among the different segments is summarized in below: Program Address space Executablefile memory segment section Code Text Text Initialized data Data Data BSS Data BSS Heap Data Stack Stack Table 3.1 Executable program segments and their locations

Linux/Unix Address Space USER-LEVEL MEMORY MANAGEMENT Linux/Unix Address Space Finally, we'll mention that threads represent multiple threads of execution within a single address space. Typically, each thread has its own stack, and a way to get thread local data, that is, dynamically allocated data for private use by the thread.

USER-LEVEL MEMORY MANAGEMENT Memory Allocation Library Calls System Calls

USER-LEVEL MEMORY MANAGEMENT Library Calls malloc() calloc() realloc() free() Dynamic memory is allocated by either the malloc() or calloc() functions. These functions return pointers to the allocated memory.

USER-LEVEL MEMORY MANAGEMENT Library Calls Once you have a block of memory of a certain initial size, you can change its size with the realloc() function. Dynamic memory is released with the free() function.

USER-LEVEL MEMORY MANAGEMENT Library Calls void *calloc(size_t nmemb, size_t size) Allocate and zero fill void *malloc(size_t size) Allocate raw memory void free(void *ptr) Release memory void *realloc(void *ptr, size_t size) Change size of existing allocation

USER-LEVEL MEMORY MANAGEMENT Library Calls The allocation functions all return type void *. This is a typeless or generic pointer. The type size_t is an unsigned integral type that represents amounts of memory. It is used for dynamic memory allocation.

Initially Allocating Memory USER-LEVEL MEMORY MANAGEMENT Initially Allocating Memory void *malloc(size_t size) Memory is allocated initially with malloc(). The value passed in is the total number of bytes requested. The return value is a pointer to the newly allocated memory or NULL if memory could not be allocated. The memory returned by malloc() is not initialized. It can contain any random garbage. You should immediately initialize the memory with valid data or at least with zeros.

USER-LEVEL MEMORY MANAGEMENT Releasing Memory void free(void *ptr) When you're done using the memory, you "give it back" by using the free() function. The single argument is a pointer previously obtained from one of the other allocation routines. It is safe (although useless) to pass a null pointer to free().

USER-LEVEL MEMORY MANAGEMENT Changing Size void *realloc(void *ptr, size_t size) It is possible to change the size of a dynamically allocated memory area. Although it's possible to shrink a block of memory, more typically, the block is grown. Changing the size is handled with realloc().

Allocating and Zero-filling USER-LEVEL MEMORY MANAGEMENT Allocating and Zero-filling void *calloc(size_t nmemb, size_t size) The calloc() function is a straightforward wrapper around malloc(). Its primary advantage is that it zeros the dynamically allocated memory. It also performs the size calculation for you by taking as parameters the number of items and the size of each.

USER-LEVEL MEMORY MANAGEMENT System Calls brk() sbrk()

USER-LEVEL MEMORY MANAGEMENT System Calls int brk(void *end_data_segment) The brk() system call actually changes the process's address space. The address is a pointer representing the end of the data segment. Its argument is an absolute logical address representing the new end of the address space. It returns 0 on success or -1 on failure.

USER-LEVEL MEMORY MANAGEMENT System Calls void *sbrk(ptrdiff_t increment) The sbrk() function is easier to use. Its argument is the increment in bytes by which to change the address space. By calling it with an increment of 0, you can determine where the address space currently ends.

USER-LEVEL MEMORY MANAGEMENT System Calls Practically speaking, you would not use brk() directly. Instead, you would use sbrk() exclusively to grow (or even shrink) the address space.

USER-LEVEL MEMORY MANAGEMENT Program example The following program summarizes everything about address space. Note that you should not use alloca() or brk() or sbrk() in practice. Example 8.1: Memory Address