Process, Pointers, and Heap Manager COMPSCI210 Recitation 31 Aug 2012 Vamsi Thummala.

Slides:



Advertisements
Similar presentations
Malloc Recitation By sseshadr. Agenda Macros in C Pointer declarations Casting and Pointer Arithmetic Malloc.
Advertisements

Part IV: Memory Management
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
Kernighan/Ritchie: Kelley/Pohl:
Malloc Recitation Section K (Kevin Su) November 5 th, 2012.
Memory Management Questions answered in this lecture: How do processes share memory? What is static relocation? What is dynamic relocation? What is segmentation?
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Lab 3: Malloc Lab. “What do we need to do?”  Due 11/26  One more assignment after this one  Partnering  Non-Honors students may work with one other.
1 CSE 380 Computer Operating Systems Instructor: Insup Lee University of Pennsylvania, Fall 2002 Lecture Note: Memory Management.
Dr Mohamed Menacer College of Computer Science and Engineering Taibah University CS-334: Computer.
Processes CSCI 444/544 Operating Systems Fall 2008.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Operating System Support Focus on Architecture
Chapter 8 Operating System Support
Memory Management 1 CS502 Spring 2006 Memory Management CS-502 Spring 2006.
CS-3013 & CS-502, Summer 2006 Memory Management1 CS-3013 & CS-502 Summer 2006.
Memory ManagementCS-502 Fall Memory Management CS-502 Operating Systems Fall 2006 (Slides include materials from Operating System Concepts, 7 th.
Computer Organization and Architecture
Pointers Applications
CS364 CH08 Operating System Support TECH Computer Science Operating System Overview Scheduling Memory Management Pentium II and PowerPC Memory Management.
Layers and Views of a Computer System Operating System Services Program creation Program execution Access to I/O devices Controlled access to files System.
Computer Organization and Architecture Operating System Support Chapter 8.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Operating System Chapter 7. Memory Management Lynn Choi School of Electrical Engineering.
Protection and the Kernel: Mode, Space, and Context.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Implementing Processes. Review: Threads vs. Processes 1. The process is a kernel abstraction for an independent executing program. includes at least one.
Programs and Processes Jeff Chase Duke University.
Chapter 5 Operating System Support. Outline Operating system - Objective and function - types of OS Scheduling - Long term scheduling - Medium term scheduling.
Compiler Construction
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
The Structure of Processes (Chap 6 in the book “The Design of the UNIX Operating System”)
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Topic 2d High-Level languages and Systems Software
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
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.
More on Programs and Processes Jeff Chase Duke University.
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming.  To allocate scarce memory.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Programs and Processes Jeff Chase Duke University.
Programs and Processes. The Virtual Address Space A typical process VAS space includes: user regions in the lower half V->P mappings specific to each.
Malloc Lab : Introduction to Computer Systems Recitation 11: Nov. 4, 2013 Marjorie Carlson Recitation A.
Virtual Memory and Address Translation. Review: the Program and the Process VAS text dataidata wdata header symbol table relocation records program text.
Carnegie Mellon 1 Malloc Recitation Ben Spinelli Recitation 11: November 9, 2015.
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.
Hello world !!! ASCII representation of hello.c.
Operating Systems A Biswas, Dept. of Information Technology.
RealTimeSystems Lab Jong-Koo, Lim
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Lecture 3 Translation.
Processes and threads.
ENERGY 211 / CME 211 Lecture 25 November 17, 2008.
Dynamic Memory Allocation
William Stallings Computer Organization and Architecture
PA1 is out Best by Feb , 10:00 pm Enjoy early
Introduction to C Programming Language
The Hardware/Software Interface CSE351 Winter 2013
Memory Allocation CS 217.
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
Lecture 3: Main Memory.
Lecture Topics: 11/1 General Operating System Concepts Processes
Operating System Chapter 7. Memory Management
Malloc Lab CSCI 380: Operating Systems
CSE 153 Design of Operating Systems Winter 2019
COMP755 Advanced Operating Systems
Presentation transcript:

Process, Pointers, and Heap Manager COMPSCI210 Recitation 31 Aug 2012 Vamsi Thummala

Agenda Process Macros/Pointers in C Manipulating and casting pointers Heap Manager: Dynamic memory allocation

Operating Systems: The Classical View data Programs run as independent processes. Protected system calls...and upcalls (e.g., signals) Protected OS kernel mediates access to shared resources. Threads enter the kernel for OS services. Each process has a private virtual address space and one or more threads. The kernel code and data are protected from untrusted processes.

Key Concepts for Classical OS kernel The software component that controls the hardware directly, and implements the core privileged OS functions. Modern hardware has features that allow the OS kernel to protect itself from untrusted user code. thread An executing instruction path and its CPU register state. virtual address space An execution context for thread(s) defining a name space for executing instructions to address data and code. process An execution of a program, consisting of a virtual address space, one or more threads, and some OS kernel state.

Parts of a process  Thread  Sequence of executing instructions  Active: does things  Address space  Data the process uses as it runs  Passive: acted upon by threads

Play analogy  Process is like a play performance  Program is like the play’s script Threads Address space What are the threads? What is the address space?

What is in the address space?  Program code  Instructions, also called “text”  Data segment  Global variables, static variables  Heap (where “new” memory comes from)  Stack  Where local variables are stored

cps-104 Lecture-11.9 ©GK Fall 2010 Review: Address Space Layout 0x x x7fffffff Static data Dynamic data Text segment Data segment Stack segment Reserved

Program Running a program When a program launches, the OS platform allocates memory to store its code and data. It may establish a new context and/or thread. data code constants initialized data imports/exports symbols types/interfaces

The Birth of a Program (C/Ux) int j; char* s = “hello\n”; int p() { j = write(1, s, 6); return(j); } myprogram.c compiler ….. p: store this store that push jsr _write ret etc. myprogram.s assembler data myprogram.o linker object file data program (executable file) myprogram data libraries and other objects

What’s in an Object File or Executable? int j = 327; char* s = “hello\n”; char sbuf[512]; int p() { int k = 0; j = write(1, s, 6); return(j); } text data idata wdata header symbol table relocation records Used by linker; may be removed after final link step and strip. Header “magic number” indicates type of image. Section table an array of (offset, len, startVA) program sections program instructions p immutable data (constants) “hello\n” writable global/static data j, s j, s,p,sbuf

A Peek Inside a Running Program 0 high code library your data heap registers CPU R0 Rn PC “memory” x x your program common runtime stack address space (virtual or physical) SP y y

CPS104 Lec26.14 ©GK Fall 2010 An Execution Context  The state of the CPU associated with a thread of control (process)  general purpose registers (integer and floating point)  status registers (e.g., condition codes)  program counter, stack pointer  Need to be able to switch between contexts  timesharing: sharing the machine among many processes  better utilization of machine (overlap I/O of one process with computation of another)  different modes (Kernel v.s. user)  Maintained by operating system

Process Context: A Closer Look

“Classic Linux Address Space”

How to allocate memory from heap? Parking with differently sized cars along a street with no marked parking space dividers. Wasted space from external fragmentation

How to allocate memory from heap? Wasted space from internal fragmentation Parking with differently sized cars along a street with fixed parking space dividers.

The Programming C Guru's (Ken Thompson& Dennis Ritchie)

Unix: A Lasting Achievement? “Perhaps the most important achievement of Unix is to demonstrate that a powerful operating system for interactive use need not be expensive…it can run on hardware costing as little as $40,000.” The UNIX Time-Sharing System* D. M. Ritchie and K. Thompson 1974 DEC PDP-11/24

Macros in C

Macros Runtime, compile-time, or pre-compile time? Constant: #define WORD_SIZE 4 OK Macro #define DWORD(x) 2*x Not OK DWORD(x+1) becomes 2*x+1 #define DWORD(x) (2*(x)) OK Use lots of parenthesis, it’s a naïve search-and-replace!

Macros Why macros? “Faster” than function calls Why? For malloc Quick access to header information (payload size, valid) What’s the keyword inline do? At compile-time replaces “function calls” with code

Pointers in C

C operators (K&R p. 53) OperatorsAssociativity () [] ->.left to right ! ~ * & (type) sizeofright to left * / %left to right + -left to right >left to right >=left to right == !=left to right &left to right ^left to right |left to right &&left to right ||left to right ?:right to left = += -= *= /= %= &= ^= != >=right to left,left to right Note: Unary +, -, and * have higher precedence than binary forms

Review of C Pointer Declarations (K&R section 5.12) int *p int *p[13] int *(p[13]) int **p int (*p)[13] int *f() int (*f)() int (*(*f())[13])() int (*(*x[3])())[5] p is a pointer to int p is an array[13] of pointer to int p is a pointer to a pointer to an int p is a pointer to an array[13] of int f is a function returning a pointer to int f is a pointer to a function returning int f is a function returning ptr to an array[13] of pointers to functions returning int x is an array[3] of pointers to functions returning pointers to array[5] of ints

Pointer casting, arithmetic, and dereferencing

Pointer casting Separate from non-pointer casting float to int, int to float ok struct_a to struct_b gcc throws an error Cast from * to * * to integer/ unsigned int integer/ unsigned int to *

Pointer casting What actually happens in a pointer cast? Nothing! It’s just an assignment. Remember all pointers are the same size. The magic happens in dereferencing and arithmetic

Pointer arithmetic The expression ptr + a doesn’t always evaluate into the arithmetic sum of the two Consider: * pointer = …; (void *) pointer2 = (void *) (pointer + a);

Pointer arithmetic int * ptr = (int *)0x ; int * ptr2 = ptr + 1; char * ptr = (char *)0x ; char * ptr2 = ptr + 1; int * ptr = (int *)0x ; int * ptr2 = ((int *) (((char *) ptr) + 1)); void * ptr = (char *)0x ; void * ptr2 = ptr + 1; void * ptr = (int *)0x ; void * ptr2 = ptr + 1;

Pointer arithmetic int * ptr = (int *)0x ; int * ptr2 = ptr + 1; //ptr2 is 0x char * ptr = (char *)0x ; char * ptr2 = ptr + 1; //ptr2 is 0x int * ptr = (int *)0x ; int * ptr2 = ((int *) (((char *) ptr) + 1)); //ptr2 is 0x void * ptr = (char *)0x ; void * ptr2 = ptr + 1; //ptr2 is 0x void * ptr = (int *)0x ; void * ptr2 = ptr + 1; //ptr2 is still 0x

More pointer arithmetic int ** ptr = (int **)0x ; int * ptr2 = (int *) (ptr + 1); char ** ptr = (char **)0x ; short * ptr2 = (short *) (ptr + 1); int * ptr = (int *)0x ; void * ptr2 = &ptr + 1; int * ptr = (int *)0x ; void * ptr2 = ((void *) (*ptr + 1)); This is on a 64-bit machine!

More pointer arithmetic int ** ptr = (int **)0x ; int * ptr2 = (int *) (ptr + 1); //ptr2 = 0x c char ** ptr = (char **)0x ; short * ptr2 = (short *) (ptr + 1); //ptr2 = 0x c int * ptr = (int *)0x ; void * ptr2 = &ptr + 1; //ptr2 = ?? //ptr2 is actually 8 bytes higher than the address of the variable ptr int * ptr = (int *)0x ; void * ptr2 = ((void *) (*ptr + 1)); //ptr2 = ?? //ptr2 is just one higher than the value at 0x (so probably segfault)

Pointer dereferencing Basics It must be a POINTER type (or cast to one) at the time of dereference Cannot dereference (void *) The result must get assigned into the right datatype (or cast into it)

Pointer dereferencing What gets “returned?” int * ptr1 = malloc(100); *ptr1 = 0xdeadbeef; int val1 = *ptr1; int val2 = (int) *((char *) ptr1); What are val1 and val2?

Pointer dereferencing What gets “returned?” int * ptr1 = malloc(sizeof(int)); *ptr1 = 0xdeadbeef; int val1 = *ptr1; int val2 = (int) *((char *) ptr1); //val1 = 0xdeadbeef; //val2 = 0xffffffef; What happened??

Heap Manager

Dynamic memory allocation Terms you will need to know malloc / calloc / realloc free sbrk payload fragmentation (internal vs. external) coalescing Bi-directional Immediate vs. Deferred

Design considerations I found a chunk that fits the necessary payload… should I look for a better fit or not? Splitting a free block: void* ptr = malloc(200); free(ptr); ptr = malloc(50); //use same space, then “mark” remaining bytes as free void* ptr = malloc(200); free(ptr); ptr = malloc(192);//use same space, then “mark” remaining bytes as free??

Fragmentation Internal fragmentation Result of payload being smaller than block size. void * m1 = malloc(3); m1,m2 both have to be aligned to 8 bytes… External fragmentation

Implementation Hurdles How do we know where the chunks are? How do we know how big the chunks are? How do we know which chunks are free? Remember: can’t buffer calls to malloc and free… must deal with them real-time. Remember: calls to free only takes a pointer, not a pointer and a size. Solution: Need a data structure to store information on the “chunks” Where do I keep this data structure?

The data structure Requirements: The data structure needs to tell us where the chunks are, how big they are, and whether they’re free We need to be able to CHANGE the data structure during calls to malloc and free We need to be able to find the next free chunk that is “a good fit for” a given payload We need to be able to quickly mark a chunk as free/allocated We need to be able to detect when we’re out of chunks. What do we do when we’re out of chunks?

The data structure It would be convenient if it worked like: malloc_struct malloc_data_structure; … ptr = malloc(100, &malloc_data_structure); … free(ptr, &malloc_data_structure); … Instead all we have is the memory we’re giving out. All of it doesn’t have to be payload! We can use some of that for our data structure.

The data structure The data structure IS your memory! A start: What goes in the header? That’s your job! Lets say somebody calls free(p2), how can I coalesce? Maybe you need a footer? Maybe not?

The data structure Common types Implicit List Root -> chunk1 -> chunk2 -> chunk3 -> … Explicit List Root -> free chunk 1 -> free chunk 2 -> free chunk 3 -> … Segregated List Small-malloc root -> free small chunk 1 -> free small chunk 2 -> … Medium-malloc root -> free medium chunk 1 -> … Large-malloc root -> free large chunk1 -> …

Design considerations Free blocks: address-ordered or LIFO or FIFO What’s the difference? Pros and cons? What are the efficiency tradeoffs?