Operating System Labs. Yuanbin Wu

Similar documents
Memory management. Johan Montelius KTH

Optimizing Dynamic Memory Management

Princeton University. Computer Science 217: Introduction to Programming Systems. Dynamic Memory Management

Princeton University Computer Science 217: Introduction to Programming Systems. Dynamic Memory Management

Memory Management. q Basic memory management q Swapping q Kernel memory allocation q Next Time: Virtual memory

Memory Management. To do. q Basic memory management q Swapping q Kernel memory allocation q Next Time: Virtual memory

Dynamic Memory Management

Free-Space Management

Motivation for Dynamic Memory. Dynamic Memory Allocation. Stack Organization. Stack Discussion. Questions answered in this lecture:

Project 2 Overview: Part A: User space memory allocation

Review! Lecture 5 C Memory Management !

CS61C : Machine Structures

CSC 1600 Memory Layout for Unix Processes"

CS 322 Operating Systems Programming Assignment 4 Writing a memory manager Due: November 16, 11:30 PM

CSCI-UA /2. Computer Systems Organization Lecture 19: Dynamic Memory Allocation: Basics

Introduction to Computer Systems /18 243, fall th Lecture, Oct. 22 th

Dynamic Memory Allocation: Basic Concepts

Lectures 13 & 14. memory management

Dynamic memory. EECS 211 Winter 2019

In Java we have the keyword null, which is the value of an uninitialized reference type

Dynamic Memory Allocation

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

CS 322 Operating Systems Programming Assignment 4 Writing a memory manager Due: April 5, 11:30 PM

Assignment 5. CS/ECE 354 Spring 2016 DUE: April 22nd (Friday) at 9 am

Dynamic Memory Allocation: Basic Concepts

Dynamic Memory Allocation. Zhaoguo Wang

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

Project 3a: Malloc and Free

Dynamic Memory Allocation I Nov 5, 2002

Interlude: Memory API

Dynamic Memory: Alignment and Fragmentation

PROJECT 2 - MEMORY ALLOCATOR Computer Systems Principles. October 1, 2010

A.Arpaci-Dusseau. Mapping from logical address space to physical address space. CS 537:Operating Systems lecture12.fm.2

Dynamic Memory Allocation. Basic Concepts. Computer Organization 4/3/2012. CSC252 - Spring The malloc Package. Kai Shen

CS Computer Systems. Lecture 8: Free Memory Management

Memory (Stack and Heap)

CS61C : Machine Structures

CIS Operating Systems Non-contiguous Memory Allocation. Professor Qiang Zeng Spring 2018

Heap Arrays. Steven R. Bagley

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

COSC Software Engineering. Lectures 14 and 15: The Heap and Dynamic Memory Allocation

Dynamic Memory Allocation I

Memory Allocation. Copyright : University of Illinois CS 241 Staff 1

Dynamic Memory Management! Goals of this Lecture!

Lecture 8 Dynamic Memory Allocation

10.1. CS356 Unit 10. Memory Allocation & Heap Management

Class Information ANNOUCEMENTS

Foundations of Computer Systems

Engine Support System. asyrani.com

Heap Management portion of the store lives indefinitely until the program explicitly deletes it C++ and Java new Such objects are stored on a heap

CS61C : Machine Structures

Virtual Memory. 1 Notes 4

Processes, Address Spaces, and Memory Management. CS449 Spring 2016

Dynamic Memory Allocation

COSC345 Software Engineering. The Heap And Dynamic Memory Allocation

Memory management. Single process. Multiple processes. How to: All memory assigned to the process Addresses defined at compile time

! What is main memory? ! What is static and dynamic allocation? ! What is segmentation? Maria Hybinette, UGA. High Address (0x7fffffff) !

Requirements, Partitioning, paging, and segmentation

Memory Management william stallings, maurizio pizzonia - sistemi operativi

Process s Address Space. Dynamic Memory. Backing the Heap. Dynamic memory allocation 3/29/2013. When a process starts the heap is empty

CIS Operating Systems Memory Management. Professor Qiang Zeng Fall 2017

Dynamic Memory Allocation

Programs in memory. The layout of memory is roughly:

ECE 15B COMPUTER ORGANIZATION

Memory Allocation. General Questions

Foundations of Network and Computer Security

Programming in C. Lecture 5: Aliasing, Graphs, and Deallocation. Dr Neel Krishnaswami. Michaelmas Term University of Cambridge

Interlude: Memory API

CS 314 Principles of Programming Languages. Lecture 11

ECE 2400 Computer Systems Programming Fall 2018 Topic 6: C Dynamic Allocation

Dynamic Memory Management

CMSC 313 Spring 2010 Exam 3 May 17, 2010

Memory Management. CSC215 Lecture

Requirements, Partitioning, paging, and segmentation

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

CSE351 Spring 2018, Final Exam June 6, 2018

CS 11 C track: lecture 5

Dynamic Data Structures. CSCI 112: Programming in C

o Code, executable, and process o Main memory vs. virtual memory

ECE 2035 A Programming HW/SW Systems Spring problems, 5 pages Exam Three 13 April Your Name (please print clearly)

6. Which of the following operating systems was the predecessor of Microsoft MS-DOS? a. Dynix b. Minix c. CP/M

Memory Management Basics

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Dynamic Memory Allocation. Gerson Robboy Portland State University. class20.ppt

CS61C Midterm Review on C & Memory Management

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Heap Arrays and Linked Lists. Steven R. Bagley

Character Strings. String-copy Example

Topics: Virtual Memory (SGG, Chapter 09) CS 3733 Operating Systems

Low-Level C Programming. Memory map Pointers Arrays Structures

Memory Management: The process by which memory is shared, allocated, and released. Not applicable to cache memory.

CSCE Operating Systems Non-contiguous Memory Allocation. Qiang Zeng, Ph.D. Fall 2018

Events, Memory Management

14. Memory API. Operating System: Three Easy Pieces

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

CSCI 4061: Virtual Memory

CSCI 4500 / 8506 Sample Questions for Quiz 5

Garbage Collection. Vyacheslav Egorov

Recitation #11 Malloc Lab. November 7th, 2017

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

Transcription:

Operating System Labs Yuanbin Wu CS@ECNU

Operating System Labs Project 2 Due 21:00, Oct. 24 Project 3 Group of 3 If you can not find a partner, drop us an email You now have 3 late days, but start early! We will have oral test at week 12 (Nov. 23)

Operating System Labs C Memory API Free Memory Management

C Memory API Type of memory Stack Heap

C Memory API Stack Allocated / Deallocate automatically By the compiler Automatic memory

C Memory API Stack Example (local variable) void func() { int x = 0;... } You only declear the variable Compiler will allocate it when call the function Also deallocate it when func returns

C Memory API Heap Allocated / Deallocate explicitly By you, the programmer

C Memory API Heap Example (malloc) void func() { int *ptr = (int*)malloc(sizeof(int));... } Both stack and heap allocation When func returns, Stack memory will be deallocated Heap memory is still there

C Memory API Stack and Heap Heap From low addr to high addr Stack From high addr to low addr 00000000 Code Heap Let's see Free FFFFFFFF Stack

C Memory API Malloc #include <stdlib.h> void *malloc(size_t size); If failed, return NULL Free #include <stdlib.h> void free(void* ptr);

C Memory API Common errors Forget to allocate memory Not allocating enough memory Forget to initialize allocated memory Forget to free memory Free memory before you are done with it Free memory repeatedly Call free() incorrectly

C Memory API Segment fault char *src = "hello"; char *dst; // oops! unallocated strcpy(dst, src); // segfault and die run this code, it will likely lead to a It is a fancy term for segmentation fault YOU DID SOMETHING WRONG WITH MEMORY YOU FOOLISH PROGRAMMER AND I AM ANGRY.

Dark Forest of Pointers

Fixed-size unit Paging Problem: internal fragmenation Variable-size unit User level memory allocation library Kernel level: VM implemented with segmentation Problem: external fragmentation

Free memory management How to manage variable-size free memory units How to implement malloc(size_t size) free(void *ptr)

Assumptions Focus on external fragmentation No compaction Manage a contiguous region of bytes (by mmap() system call)

Low-level Mechanisms Splitting and Coalescing Tracking allocated regions Implementation of a free list High-level Intelligence Best fit Worst fit First fit Next fit

Splitting and Coalescing Free list: a set of free chunks Two chunks (10 bytes each)

Splitting and Coalescing request less than 10 bytes? (e.g. malloc(1)) Splitting

Splitting and Coalescing Free a chunk? Malloc(20)? Coalescing

Tracking Allocated Regions Observation on free(void *ptr) No size parameter Given a pointer, the malloc library could determine the size of region How? Some extra information header of a memory block

Tracking Allocated Regions header typedef struct header_t { int size; int magic; } header_t; malloc(20)

Tracking Allocated Regions header: example

Tracking Allocated Regions free(ptr) Get the size of the region void free(void *ptr) { header_t *hptr = (void *)ptr - sizeof(header_t); } Check whether ptr is valid assert(hptr->magic == 1234567)

Implementation of the Free List Free list Implementation List node (allocate a node when needed) Can NOT do this here! All you have is a given free space How to build a free list inside the free space?

Implementation of the Free List Node in free list typedef struct node_t { int size; struct node_t *next; } node_t;

Implementation of the Free List Initialization (e.g. 4096) // mmap() returns a pointer to a chunk of free space node_t *head = mmap(null, 4096, PROT_READ PROT_WRITE, MAP_ANON MAP_PRIVATE, -1, 0); head->size = 4096 - sizeof(node_t); head->next = NULL;

Implementation of the Free List malloc(100)

malloc(100)*3

Free(16500) 16384+108+8

Free()*3 Coalesce Merge adjacent chunks

Growing the Heap What if the heap runs out of space? Return NULL Increase the size of heap OS find free phycical pages Map them into address space of the prcess

Summary of low-level Mechanisms Splitting and Coalescing Tracking allocated regions Implementation of a free list Growing the heap

High-level intelligence How to find the proper nodes in the free list? Less fragmentation Fast allocation Some simple strategies The stream of allocation and free requests can be arbitrary Any strategy could be arbitraily bad/good

Best Fit Find the smallest feasible node Worst Fit Find the largest feasible node First Fit Find the first feasible node

Example Best fit Worst fit

Other approaches Segregated List Slab allocator Buddy Allocation Binary search tree