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

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

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

Preview. Memory Management

12: Memory Management

ECE 598 Advanced Operating Systems Lecture 10

Operating Systems and Computer Networks. Memory Management. Dr.-Ing. Pascal A. Klein

Memory management: outline

Memory management: outline

CS399 New Beginnings. Jonathan Walpole

Memory management. Knut Omang Ifi/Oracle 10 Oct, 2012

CS Operating Systems

CS Operating Systems

OPERATING SYSTEMS. After A.S.Tanenbaum, Modern Operating Systems 3rd edition Uses content with permission from Assoc. Prof. Florin Fortis, PhD

Memory: Overview. CS439: Principles of Computer Systems February 26, 2018

Memory Management. Reading: Silberschatz chapter 9 Reading: Stallings. chapter 7 EEL 358

Memory Management Basics

Operating Systems. Memory Management. Lecture 9 Michael O Boyle

COMPUTER SCIENCE 4500 OPERATING SYSTEMS

ECE 598 Advanced Operating Systems Lecture 12

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

Virtual Memory 1. To do. q Segmentation q Paging q A hybrid system

Memory Management. Chapter 4 Memory Management. Multiprogramming with Fixed Partitions. Ideally programmers want memory that is.

Chapter 4 Memory Management

Virtual Memory. Today. Segmentation Paging A good, if common example

Requirements, Partitioning, paging, and segmentation

Engine Support System. asyrani.com

Operating Systems, Fall

Operating Systems, Fall

CS 550 Operating Systems Spring Memory Management: Paging

Memory Management. COMP755 Advanced Operating Systems

Chapter 4 Memory Management. Memory Management

Operating Systems Memory Management. Mathieu Delalandre University of Tours, Tours city, France

Chapter 9 Memory Management

Memory Management. Dr. Yingwu Zhu

Objectives and Functions Convenience. William Stallings Computer Organization and Architecture 7 th Edition. Efficiency

Motivation. Memory Management. Memory Management. Computer Hardware Review

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 13: Address Translation

UNIT III MEMORY MANAGEMENT

Process. One or more threads of execution Resources required for execution. Memory (RAM) Others

Operating System Support

Chapter 4: Memory Management. Part 1: Mechanisms for Managing Memory

Operating System Labs. Yuanbin Wu

Addresses in the source program are generally symbolic. A compiler will typically bind these symbolic addresses to re-locatable addresses.

In multiprogramming systems, processes share a common store. Processes need space for:

Memory management OS

15 Sharing Main Memory Segmentation and Paging

16 Sharing Main Memory Segmentation and Paging

Memory Management. CSE 2431: Introduction to Operating Systems Reading: , [OSC]

Chapter 3 - Memory Management

Last class: Today: Deadlocks. Memory Management

File Systems. OS Overview I/O. Swap. Management. Operations CPU. Hard Drive. Management. Memory. Hard Drive. CSI3131 Topics. Structure.

Processes and Tasks What comprises the state of a running program (a process or task)?

8: Memory Management

Chapter 8: Virtual Memory. Operating System Concepts

Requirements, Partitioning, paging, and segmentation

Memory Management. Jo, Heeseung

Memory and multiprogramming

VII. Memory Management

Chapter 8: Main Memory

Memory management. Requirements. Relocation: program loading. Terms. Relocation. Protection. Sharing. Logical organization. Physical organization

MEMORY MANAGEMENT. Jo, Heeseung

Chapter 8: Memory-Management Strategies

Memory management. Johan Montelius KTH

Registers Cache Main memory Magnetic disk Magnetic tape

Virtual Memory Outline

Chapter 8: Memory- Management Strategies. Operating System Concepts 9 th Edition

Memory Management Topics. CS 537 Lecture 11 Memory. Virtualizing Resources

Operating Systems Lecture 5: Memory Management I

Memory management. Last modified: Adaptation of Silberschatz, Galvin, Gagne slides for the textbook Applied Operating Systems Concepts

CS6401- Operating System UNIT-III STORAGE MANAGEMENT

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Classifying Information Stored in Memory! Memory Management in a Uniprogrammed System! Segments of a Process! Processing a User Program!

OPERATING SYSTEM. Chapter 9: Virtual Memory

4 MEMORY MANAGEMENT 4.1 BASIC MEMORY MANAGEMENT

CHAPTER 8 - MEMORY MANAGEMENT STRATEGIES

Administrivia. Deadlock Prevention Techniques. Handling Deadlock. Deadlock Avoidance

Process. Memory Management

Process. One or more threads of execution Resources required for execution. Memory (RAM) Others

Chapter 8: Main Memory. Operating System Concepts 9 th Edition

Process. One or more threads of execution Resources required for execution

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

C06: Memory Management

First-In-First-Out (FIFO) Algorithm

Chapter 8: Main Memory

Memory Management. Dr. Yingwu Zhu

CS370 Operating Systems

CS450/550 Operating Systems

Operating Systems (2INC0) 2017/18

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

Memory Management william stallings, maurizio pizzonia - sistemi operativi

CHAPTER 3 RESOURCE MANAGEMENT

Chapter 9 Memory Management Main Memory Operating system concepts. Sixth Edition. Silberschatz, Galvin, and Gagne 8.1

Main Memory (Part I)

The Art and Science of Memory Allocation

Basic Memory Management

Chapter 8. Operating System Support. Yonsei University

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

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

Operating Systems Unit 6. Memory Management

Memory Management Prof. James L. Frankel Harvard University

Transcription:

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

Memory management Ideal memory for a programmer large, fast, nonvolatile and cheap not an option Best alternative memory hierarchy Small amount of fast, expensive memory cache Some medium-speed, medium price main memory Gigabytes of slow, cheap and non-volatile disk storage To handle the memory hierarchy memory manager Allocates scarce resource among competing requests to maximize (memory) utilization and system throughput Offers a convenient abstraction for programming Provides isolation between processes 2

Basic memory management Simplest memory abstraction no abstraction at all Early mainframes (before 60), minicomputers (before 70) and PCs (before 80) MOV REG1, 1000 #REG1 ç (physical memory 1000) Logically, only one program running at a time Why? Still here, some alternatives for organizing memory User program Operating system in ROM Devices in ROM BIOS User program User program Operating system in RAM Operating system in RAM MSDOS Mainframes & minicomputers Some palmtops & embedded systems Early PCs 3

Multiprogramming and swapping Even with no memory abstraction, you can run multiple programs concurrently Switching processes with swapping Corbató s CTSS Simple Bring each process entirely Move another one to disk Process swapped back in restarts from where it was Main memory Operating System Swap out Swap in Backing store Process P 1 Process P 2 User Space Context switching Clearly, moving the whole thing is not too good for performance 4

Multiprogramming with fixed partitions With some additional hardware, avoid swapping IBM OS/360 Split memory in n parts (possible!= sizes) Single or separate input queues for each partition ~MFT: Multiprogramming with Fixed number of Tasks Protection? Operating System 0 64K Relocation? Operating System 0 64K Partition 1 Partition 1 Partition 2 128K Single input queue Partition 2 128K Multiple input queues Partition 3 Partition 4 256K 320K Partition 3 Partition 4 256K 320K 512K 512K 5

Size of the allocation unit Important design issue A pervasive problem fragmentation Internal fragmentation the available portion is larger than what was requested External fragmentation two small partitions left, but one big partition needed!? Internal fragmentation Job D Job C Job B Job A Operating System Partition 1 Job A Partition 2 Job C 0 64K 128K Partition 3 Job B Partition 4 256K 320K 512K 6

Multiprogramming Protection What stops a process from writing outside its allocated memory? IBM 360 Check it for each reference Split memory into 2KB blocks Each block was assigned a 4b protection key kept in CPU The PSW (program status word) also kept a 4b key OS trapped any process trying to access memory with protection key!= the PSW key 7

Multiprogramming Relocation With absolute physical memory the program must always run in the exact same location What a little JMP X can do! IBM 360 stop-gap solution Change it at loading time Static relocation If a program was reloaded starting at 131,072, the constant 131,072 was added to every address at load time Not particularly fast! Operating System Process Partition AB 1 Partition 2 0 64K 128K Process B Process A? Partition 3 Partition 4 256K 320K 512K 8

A new abstraction Address space Address space all the memory a process can reference MOV REG1,???? What does it include Code static, so it won t change while the program runs Stack to keep track of where it is in the function call chain Heap for dynamically allocated memory We placed them opposite so they can both grow (but this is just a convention) Program Code Heap 0KB 1KB (free) Stack 16KB 9

Multiprogramming with variable partitions A simple solution partition physical memory into dynamically allocated blocks CDC 6600 and Intel 8088 Base and limit values Address locations + base value physical address Ideally, base and limit registers can only be modified by OS A disadvantage Comparisons can be done fast but additions can be expensive limit base CPU Logical address < yes + Physical address Transparency Protection Efficiency no Trap to OS; addressing error memory 10

And now a short break 11

Memory management s other client The OS manages physical memory and allocates it to User processes Kernel subsystems Kernel needs small, odd sized chunks of memory Proc structures, file descriptor blocks sizes << a page Pre-allocates part of memory for the KMA Most OS manage memory as set of fixed-size pages, so another approach Introduce a page-level allocator Let the kernel manage the space in a set of pages Kernel Memory Allocator (KMA) 12

Page-level allocator and KMA Page-level allocator has Two main routines: e.g get_page() & freepage() in SVR4 Two main clients: Paging system & KMA Physical memory Provides odd-size buffers to various kernel subsystems Page-level allocator Kernel memory allocator Paging system Network Buffers Proc strutcures inodes, file descriptors User processes Block buffer cache 13

Memory management With dynamically allocated memory OS must keep track of allocated/free memory Two general approaches bit maps and linked lists Bit maps A B C D E 8 16 24 Divide memory into allocation units (size?), track usage with a bitmap 11111000 11111111 11001111 11111000,,, Linked list of allocated or free spaces List ordered by address P 0 5 H 5 3 P 8 6 P 14 4 H 18 2 P 20 6 P 26 3 H 29 3 X Hole Starts at 18 Length 2 Process 14

Picking a place basic algorithms First fit simple and fast Next fit - ~ First fit, starting where it left off Slightly worst performance than First fit Best fit try to waste the least, but many tiny holes and a whole list search Worst fit try to waste the most (making it easier to reuse) Not too good either 15

Tracking the size of allocated units Keep extra info in a header block Size (this for sure) Additional pointers for faster de-allocation Magic number for additional integrity checks hptr ptr Header for allocation Allocated memory typedef struct _header_t { int size; int magic; } header_t; void free(void *ptr) { header_t *hptr = (void *) ptr sizeof(header_t);... assert(header_t->magic == MAGICN);... } 16

KMA Resource map allocator Resource map a set of <base, size> pairs Initially the pool is described by a single pair after a few exchanges a list of entries per contiguous free regions Allocate requests based on First fit, Best fit, Worst fit A simple interface offset_t rmalloc(size); void rmfree(base, size); 256,128 0, 576,448 256, 1024 768 rmalloc(256) rmalloc(320) rmfree(256,128) 17

Resource map allocator Pros Easy to implement Not restricted to memory allocation It avoid waste Client can release any part of the region Allocator coalesces adjacent free regions Cons After a while maps ended up fragmented low utilization Higher fragmentation, longer map Map may need an allocator for its own entries To coalesce regions, keep map sorted expensive Linear search to find a free region large enough 18

KMA Simple power-of-two free list A set of free lists Each list keeps free buffers of a particular size (2 x ) Each buffer has one word header Pointer to next free buffer, if free or to Pointer to free list (or size), if allocated Allocated blocks List headers 32 64 128 256 512 Free blocks 19

KMA Simple power-of-two free list Allocating(size) allocating (size + header) rounded up to next power of two Return pointer to first byte after header Freeing doesn t require size as argument Move pointer back header-size to access header Put buffer in list Initialize allocator by pre-allocating buffers or get pages on demand; if it needs a buffer from an empty list Block request until a buffer is released Satisfy request with a bigger buffer if available Get a new page from page allocator 20

Power-of-two free lists Pros Simple and pretty fast (avoids linear search) Familiar programming interface (malloc, free) Free does not require size; easier to program with Cons Rounding means internal fragmentation As many requests are power of two and we loose header; a lot of waste No way to coalesce free buffers to get a bigger one Rounding up may be a costly operation 21

Other algorithms Buddy Coalescing is critical; buddy allocator Free memory is a 2 N size block When requesting a block, split in half until is big enough When block is freed, check if buddy is free & coalesce Finding a buddy address of each buddy pair differs only by a single bit! Repeat 256KB 128KB 128KB 64KB 64KB 32KB 32KB 22

Kernel memory allocation Evaluation Several approaches to KMA Slab, power-of-two lists, Buddy, Memory utilization Physical memory is limited after all Major cause of wasted memory fragmentation Speed It is used by various kernel subsystems (e.g. interrupt handler) Better be fast both in avg and worst case Simple API A simple free & malloc is nice but you can t release partial memory with too simple of a malloc Allow a two-way exchange with page-level allocator 23

Coming up Virtual memory in all its beauty 24