Virtual Memory. 11/8/16 (election day) Vote!

Similar documents
CS 31: Intro to Systems Virtual Memory. Kevin Webb Swarthmore College November 15, 2018

Memory Management. Kevin Webb Swarthmore College February 27, 2018

Memory Management. Before We Begin. Process s Memory Address Space. Process Memory. CSE 120: Principles of Operating Systems.

CS399 New Beginnings. Jonathan Walpole

UNIT III MEMORY MANAGEMENT

Recall from Tuesday. Our solution to fragmentation is to split up a process s address space into smaller chunks. Physical Memory OS.

Virtual Memory. Kevin Webb Swarthmore College March 8, 2018

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

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

Requirements, Partitioning, paging, and segmentation

Memory Management Basics

Lecture 7. Memory Management

The Virtual Memory Abstraction. Memory Management. Address spaces: Physical and Virtual. Address Translation

Operating Systems (2INC0) 2017/18

12: Memory Management

Memory Management. COMP755 Advanced Operating Systems

CS 5523 Operating Systems: Memory Management (SGG-8)

Simple idea 1: load-time linking. Our main questions. Some terminology. Simple idea 2: base + bound register. Protection mechanics.

Chapter 9 Real Memory Organization and Management

Chapter 9 Real Memory Organization and Management

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

Memory Management. Dr. Yingwu Zhu

Requirements, Partitioning, paging, and segmentation

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

Operating Systems. Memory Management. Lecture 9 Michael O Boyle

Operating systems. Part 1. Module 11 Main memory introduction. Tami Sorgente 1

Memory Management. Goals of Memory Management. Mechanism. Policies

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

Segmentation. Multiple Segments. Lecture Notes Week 6

Chapters 9 & 10: Memory Management and Virtual Memory

Main Memory (Part I)

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

CPS 104 Computer Organization and Programming Lecture 20: Virtual Memory

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

The Memory Management Unit. Operating Systems. Autumn CS4023

CPS104 Computer Organization and Programming Lecture 16: Virtual Memory. Robert Wagner

Chapter 8 Memory Management

Preview. Memory Management

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

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

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

CS 61C: Great Ideas in Computer Architecture. Virtual Memory

COSC3330 Computer Architecture Lecture 20. Virtual Memory

15 Sharing Main Memory Segmentation and Paging

@2010 Badri Computer Architecture Assembly II. Virtual Memory. Topics (Chapter 9) Motivations for VM Address translation

Computer Systems II. Memory Management" Subdividing memory to accommodate many processes. A program is loaded in main memory to be executed

Chapter 8: Memory Management

16 Sharing Main Memory Segmentation and Paging

Agenda. CS 61C: Great Ideas in Computer Architecture. Virtual Memory II. Goals of Virtual Memory. Memory Hierarchy Requirements

Frequently asked questions from the previous class survey

Principles of Operating Systems

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

Recall: Address Space Map. 13: Memory Management. Let s be reasonable. Processes Address Space. Send it to disk. Freeing up System Memory

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

Memory Management. Minsoo Ryu. Real-Time Computing and Communications Lab. Hanyang University.

Run-Time Environments/Garbage Collection

Memory Management Minsoo Ryu Real-Time Computing and Communications Lab. Hanyang University

CS 31: Intro to Systems Operating Systems Overview. Kevin Webb Swarthmore College March 31, 2015

Memory Management. CSCI 315 Operating Systems Design Department of Computer Science

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Chapter 7: Main Memory. Operating System Concepts Essentials 8 th Edition

CS370 Operating Systems

CSC 369 Operating Systems

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

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

6 - Main Memory EECE 315 (101) ECE UBC 2013 W2

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

Virtual Memory. CS61, Lecture 15. Prof. Stephen Chong October 20, 2011

COMPUTER SCIENCE 4500 OPERATING SYSTEMS

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Chapter 8: Main Memory

ECE468 Computer Organization and Architecture. Virtual Memory

Virtual memory - Paging

The process. one problem

Process. Memory Management

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

CS 390 Chapter 8 Homework Solutions

ECE4680 Computer Organization and Architecture. Virtual Memory

8: Memory Management

Chapter 8: Memory-Management Strategies

Background. Contiguous Memory Allocation

Last Class: Deadlocks. Where we are in the course

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

Random-Access Memory (RAM) Systemprogrammering 2007 Föreläsning 4 Virtual Memory. Locality. The CPU-Memory Gap. Topics

Performance of Various Levels of Storage. Movement between levels of storage hierarchy can be explicit or implicit

Topics: Memory Management (SGG, Chapter 08) 8.1, 8.2, 8.3, 8.5, 8.6 CS 3733 Operating Systems

MEMORY MANAGEMENT. Jo, Heeseung

Chapter 9 Memory Management

CS252 S05. Main memory management. Memory hardware. The scale of things. Memory hardware (cont.) Bottleneck

Random-Access Memory (RAM) Systemprogrammering 2009 Föreläsning 4 Virtual Memory. Locality. The CPU-Memory Gap. Topics! The memory hierarchy

Memory Management. Jo, Heeseung

CS420: Operating Systems

Memory Management william stallings, maurizio pizzonia - sistemi operativi

Memory Management. Dr. Yingwu Zhu

Memory Management (1) Memory Management

Memory Management! Goals of this Lecture!

Memory Management (1) Memory Management. CPU vs. memory. No.8. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

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

CHAPTER 8 - MEMORY MANAGEMENT STRATEGIES

Goals of Memory Management

Transcription:

Virtual Memory 11/8/16 (election day) Vote!

Recall: the job of the OS The OS is an interface layer between a user s programs and hardware. Program Operating System Computer Hardware It provides an abstract view of the hardware that makes it easier to use.

What we Know about Physical Memory (RAM) RAM is array of addressable bytes, from 0x0 to max (ex) address 0 to 2 30-1 for 1 GB of RAM space Regs Cache Main memory (RAM) Secondary storage The OS needs to be in RAM Usually loaded at address 0x0 0x0 RAM OS Physical Storage for running processes: Process Address Spaces are stored in RAM P1 P2 x: x: P1 and P2 each get their own copy of x P3 max 3

How much memory is allocated? int main(){ int i; printf("a stack address: %p\n", &i); printf("a text address: %p\n", main); printf("the difference: %d\n", (unsigned int)(&i) (unsigned int)(main)); } a stack address: 0x7fff7f1e9774 a text address: 0x400596 the difference: 2128515550 > 2 gigabytes

The virtual memory abstraction Solves two problems: Not enough physical memory to go around. Don t want multiple programs to accidentally modify the same physical memory location. Key ideas: OS allocates memory to processes as needed. Program s addresses get translated to physical addresses.

Memory Abstraction goal: make every process think it has the same memory layout. MUCH simpler for compiler if the stack always starts at 0xFFFFFFFF, etc. Reality: there s only so much memory to go around, and no two processes should use the same (physical) memory addresses. OS Text Data Heap Stack OS Process 1 Process 2 Process 1 OS (with help from hardware) will keep track of who s using each memory region.

Memory Terminology Virtual (logical) Memory: The abstract view of memory given to processes. Each process gets an independent view of the memory. Physical Memory: The contents of the hardware (RAM) memory. Managed by OS. Only ONE of these for the entire machine! OS OS OS Text Text Text Data Data Heap Data Heap Heap Stack Stack Stack 0x0 Address Space: Range of addresses for a region of memory. The set of available storage locations. 0x0 OS Process 1 Process 2 Process 1 Virtual address space (VAS): fixed size. 0xFFFFFFFF 0x (Determined by amount of installed RAM.)

Memory Terminology Note: It is common for VAS to appear larger than physical memory. 32-bit (IA32): Can address up to 4 GB, might have less installed 64-bit (X86-64): Our lab machines have 48-bit VAS (256 TB), 36-bit PAS (64 GB) OS OS OS Text Text Text Data Data Heap Data Heap Heap Stack Stack Stack 0x0 Address Space: Range of addresses for a region of memory. The set of available storage locations. 0x0 OS Process 1 Process 2 Process 1 Virtual address space (VAS): fixed size. 0xFFFFFFFF 0x (Determined by amount of installed RAM.)

Cohabitating Physical Memory If process is given CPU, must also be in memory. Problem Context-switching time (CST): 10 µsec Loading from disk: 10 MB/s To load 1 MB process: 100 msec = 10,000 x CST Too much overhead! Breaks illusion of simultaneity Solution: keep multiple processes in memory Context switch only between processes in memory

Memory Issues and Topics Where should process memories be placed? Topic: Classic memory management How does the compiler model memory? Topic: Logical memory model How to deal with limited physical memory? Topics: Virtual memory, paging Plan: Start with the basics (out of date) to motivate why we need the complex machinery of virtual memory and paging.

Problem: Placement Where should process memories be placed? Topic: Classic memory management How does the compiler model memory? Topic: Logical memory model How to deal with limited physical memory? Topics: Virtual memory, paging

Memory Management Physical memory starts as one big empty space.

Memory Management Physical memory starts as one big empty space. Processes need to be in memory to execute.

Memory Management Physical memory starts as one big empty space. When creating process, allocate memory Find space that can contain process Allocate region within that gap Typically, leaves a (smaller) free space

Memory Management Physical memory starts as one big empty space. When creating process, allocate memory Find space that can contain process Allocate region within that gap Typically, leaves a (smaller) free space When process exits, free its memory Creates a gap in the physical address space. If next to another gap, coalesce.

Fragmentation Eventually, memory becomes fragmented After repeated allocations/de-allocations Internal fragmentation Unused space within process Cannot be allocated to others Can come in handy for growth External fragmentation Unused space outside any process (gaps) Can be allocated (too small/not useful?)

Which form of fragmentation is easiest for the OS to reduce/eliminate? A. Internal fragmentation B. External fragmentation C. Neither

Placing Memory When searching for space, what if there are multiple options? Algorithms First (or next) fit Best fit Worst fit Complication Is region fixed or variable size?

Placing Memory When searching for space, what if there are multiple options? Algorithms First (or next) fit Best fit Worst fit Complication Is region fixed or variable size?

Placing Memory When searching for space, what if there are multiple options? Algorithms First (or next) fit Best fit Worst fit Complication Is region fixed or variable size?

Which memory allocation algorithm leaves the smallest fragments (external)? A. first-fit B. worst-fit Is leaving small fragments a good thing or a bad thing? C. best-fit

Placing Memory When searching for space, what if there are multiple options? Algorithms First (or next) fit: fast Best fit Worst fit Complication Is region fixed or variable size?

Placing Memory When searching for space, what if there are multiple options? Algorithms First (or next) fit Best fit: leaves small fragments Worst fit Complication Is region fixed or variable size?

Placing Memory When searching for space, what if there are multiple options? Algorithms First (or next) fit Best fit Worst fit: leaves large fragments Complication Is region fixed or variable size?

What if it doesn t fit? There may still be significant unused space External fragments Internal fragments Approaches

What if it doesn t fit? There may still be significant unused space External fragments Internal fragments Approaches Compaction

What if it doesn t fit? There may still be significant unused space External fragments Internal fragments Approaches Compaction Break process memory into pieces Easier to fit. More state to keep track of.

Problem Summary: Placement When placing process, it may be hard to find a large enough free region in physical memory. Fragmentation makes this harder over time (free pieces get smaller, spread out). General solution: don t require all of a process s memory to be in one piece!

Problem Summary: Placement General solution: don t require all of a process s memory to be in one piece! Physical Memory OS Process 1 Process 2 Process 1 Process 2

Problem Summary: Placement General solution: don t require all of a process s memory to be in one piece! Physical Memory OS OS: Place Process 1 Process 2 Process 1 Process 2

Problem Summary: Placement General solution: don t require all of a process s memory to be in one piece! Physical Memory OS OS: Place Process 1 Process 2 Process 1 Process 2

Problem Summary: Placement General solution: don t require all of a process s memory to be in one piece! Physical Memory OS OS: Place OS may choose not to place parts in memory at all. Process 1 Process 2 Process 1 Process 2

Problem: Addressing Where should process memories be placed? Topic: Classic memory management How does the compiler model memory? Topic: Logical memory model How to deal with limited physical memory? Topics: Virtual memory, paging

(More) Problems with Memory Cohabitation Addressing: Compiler generates memory references Unknown where process will be located Protection: Modifying another process s memory Space: The more processes there are, the less memory each individually can have P 2 P 1 P 3

Compiler s View of Memory Compiler generates memory addresses Needs empty region for text, data, heap, stack Ideally, very large to allow heap and stack to grow Alternative: four independent empty regions What compiler needs to know, but doesn t Physical memory size Where to place data (e.g., stack at high end) Must avoid allocated regions in memory

Address Spaces Address space Set of addresses for memory Usually linear: 0 to N-1 (size N) Physical Address Space 0 to N-1, N = size Kernel occupies lowest addresses PAS 0 PM kernel N-1

Virtual vs. Physical Addressing Virtual addresses Assumes separate memory starting at 0 Compiler generated Independent of location in physical memory VAS s 0 N 1-1 0 VM s P 1 P 2 PAS 0 PM P 2 OS: Map virtual to physical N 2-1 P 1 0 P 3 P 3 N 3-1 N-1

When should we perform the mapping from virtual to physical address? Why? A. When the process is initially loaded: convert all the addresses to physical addresses B. When the process is running: map the addresses as they re used. C. Perform the mapping at some other time. When?

Hardware for Virtual Addressing Base register filled with start address To translate address, add base Achieves relocation To move process: change base 0 Base + 0 P 2 P 1 P 2 N 2-1 P 3 N-1

Hardware for Virtual Addressing Base register filled with start address To translate address, add base Achieves relocation To move process: change base 0 Base + 0 P 2 P 1 P 2 N 2-1 P 3 N-1

Hardware for Virtual Addressing Base register filled with start address To translate address, add base Achieves relocation To move process: change base 0 Base + 0 P 2 P 1 Protection? P 2 N 2-1 P 3 N-1

Protection Bound register works with base register Is address < bound Yes: add to base No: invalid address, TRAP Achieves protection 0 Base + Bound < P 2 P 1 0 y/n? When would we need to update these base & bound registers? N 2-1 P 2 N-1 P 3

Given what we currently know about memory, what must we do during a context switch? A. Allocate memory to the switching process B. Load the base and bound registers C. Convert logical to physical memory addresses

Memory Registers Part of Context On Every Context Switch Load base/bound registers for selected process Only kernel does loading of these registers Kernel must be protected from all processes Benefit Allows each process to be separately located Protects each process from all others

Problem Summary: Addressing Compiler has no idea where, in physical memory, the process s data will be. Compiler generates instructions to access VAS. General solution: OS must translate process s VAS accesses to the corresponding physical memory location.

Problem Summary: Addressing General solution: OS must translate process s VAS accesses to the corresponding physical memory location. movl (address 0x74), %eax When the process tries to access a virtual address, the OS translates it to the corresponding physical address. OS: Translate Physical Memory OS Process 1 Process 2 Process 1 Process 2

Problem Summary: Addressing General solution: OS must translate process s VAS accesses to the corresponding physical memory location. When the process tries to access a virtual address, the OS translates it to the corresponding physical address. Physical Memory OS Process 1 movl (address 0x74), %eax OS: Translate 0x42F80 Process 2 Process 1 Process 2

Let s combine these ideas: 1. Allow process memory to be divided up into multiple pieces. 2. Keep state in OS (+ hardware/registers) to map from virtual addresses to physical addresses. Result: Keep a table to store the mapping of each region.

Problem Summary: Addressing General solution: OS must translate process s VAS accesses to the corresponding physical memory location. When the process tries to access a virtual address, the OS translates it to the corresponding physical address. Physical Memory OS Process 1 movl (address 0x74), %eax OS: Translate OS must keep Process a table, 3 for each process, to map VAS to PAS. One entry per divided region. 0x42F80 Process 2 Process 1 Process 2

Two (Real) Approaches Segmented address space/memory Partition address space and memory into segments Segments are generally different sizes Paged address space/memory Partition address space and memory into pages All pages are the same size