1 CMSC 15400 Memory Management (Cont’d) Segmentation, Virtual Memory OSTEP Book (See Schedule Page) Chapter 12-16.

Slides:



Advertisements
Similar presentations
Memory Management Questions answered in this lecture: How do processes share memory? What is static relocation? What is dynamic relocation? What is segmentation?
Advertisements

OS Memory Addressing.
CS 333 Introduction to Operating Systems Class 11 – Virtual Memory (1)
Memory Management and Paging CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CS 333 Introduction to Operating Systems Class 11 – Virtual Memory (1)
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.
Chapter 3.2 : Virtual Memory
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 Management Chapter 5.
CS 333 Introduction to Operating Systems Class 9 - Memory Management
Chap 8 Memory Management. Background Program must be brought into memory and placed within a process for it to be run Input queue – collection of processes.
Operating System Chapter 7. Memory Management Lynn Choi School of Electrical Engineering.
Operating Systems Chapter 8
CS333 Intro to Operating Systems Jonathan Walpole.
1 Chapter 3.2 : Virtual Memory What is virtual memory? What is virtual memory? Virtual memory management schemes Virtual memory management schemes Paging.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 9: Memory Management Background Swapping Contiguous Allocation Paging Segmentation.
COSC 3407: Operating Systems Lecture 13: Address Translation.
Background Program must be brought into memory and placed within a process for it to be run. Input queue – collection of processes on the disk that are.
Chapter 8 – Main Memory (Pgs ). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.
CE Operating Systems Lecture 14 Memory management.
Chapter 4 Memory Management Virtual Memory.
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming.  To allocate scarce memory.
1 Memory Management. 2 Fixed Partitions Legend Free Space 0k 4k 16k 64k 128k Internal fragmentation (cannot be reallocated) Divide memory into n (possible.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Virtual Memory Hardware.
Memory Management Continued Questions answered in this lecture: What is paging? How can segmentation and paging be combined? How can one speed up address.
OS Memory Addressing. Architecture CPU – Processing units – Caches – Interrupt controllers – MMU Memory Interconnect North bridge South bridge PCI, etc.
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.
Memory Management Chapter 5 Advanced Operating System.
Chapter 8: Memory Management. 8.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 8: Memory Management Background Swapping Contiguous.
Chapter 7: Main Memory CS 170, Fall Program Execution & Memory Management Program execution Swapping Contiguous Memory Allocation Paging Structure.
W4118 Operating Systems Instructor: Junfeng Yang.
CS703 - Advanced Operating Systems By Mr. Farhan Zaidi.
Memory Management Virtual Memory.
Non Contiguous Memory Allocation
Virtualization Virtualize hardware resources through abstraction CPU
Segmentation COMP 755.
Chapter 8: Main Memory.
CS703 - Advanced Operating Systems
Outline Paging Swapping and demand paging Virtual memory.
Memory Management and Virtual Memory
Chien-Chung Shen CIS/UD
Instructors: Haryadi Gunawi
CS510 Operating System Foundations
Chapter 8: Main Memory.
CSCI206 - Computer Organization & Programming
Operating System Concepts
Lecture 14 Virtual Memory and the Alpha Memory Hierarchy
Memory Management 11/17/2018 A. Berrached:CS4315:UHD.
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 8 11/24/2018.
Background Program must be brought into memory and placed within a process for it to be run. Input queue – collection of processes on the disk that are.
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 9 12/1/2018.
Memory Management-I 1.
Main Memory Background Swapping Contiguous Allocation Paging
CS399 New Beginnings Jonathan Walpole.
Virtual Memory Hardware
Lecture 3: Main Memory.
CSE451 Memory Management Introduction Autumn 2002
CSE 451: Operating Systems Autumn 2005 Memory Management
Operating System Chapter 7. Memory Management
Chapter 8: Memory Management strategies
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 9 4/5/2019.
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
Lecture 7: Flexible Address Translation
Memory Management CSE451 Andrew Whitaker.
CS703 - Advanced Operating Systems
CSE 542: Operating Systems
Page Main Memory.
Presentation transcript:

1 CMSC Memory Management (Cont’d) Segmentation, Virtual Memory OSTEP Book (See Schedule Page) Chapter Address Spaces, Memory API, Address Translation, Segmentation Instructors: HSG, HH, MW

2...

3 OS Process 2 Process 1 Process 2 Process 3 P2 base = 2000 P1 bound 0 0 P1 base = 5000 P3 base = 7000 Physical Addresses: Virtual Addresses Process 3

4 Code (e.g., 1 MB) P1’s Heap (e.g., 2 MB) P1’s Stack (e.g, 3 MB) P1’s Heap Code (e.g. Excel) P1’s Stack 0 0 Base + Bound Physical Addresses: Process address space (virtual addresses) Bound Logical View Physical View (unused !! Internal fragmentation) (unused) Base

5 Code (1 MB) P1’s Heap (2 MB) P1’s Stack (3 MB) P1’s Heap Code (e.g. Excel) P1’s Stack 0x x MB 0 0 0x x MB 0x x MB Physical Addresses: Process address space (virtual addresses) 2n2n Logical View Physical View

 Divide address space into logical segments  Ex: Code, stack, heap  Goals  Remove internal fragmentation  More sharing  (whole-process base and bound doesn’t allow sharing) 6 Stack P1P1 Heap Code Stack Heap Code P2P2 Stack Heap Excel Stack Heap Memory 2n2n 0

7 P1’s Heap P1’s Code (e.g. Excel) P1’s Stack 0x x MB 0 0x x MB 0x x MB Physical View  Each segment has its own base and bound  Each segment can independently …  be placed separately in physical memory  grow and shrink  be protected (R/W bits)  int *p = main; *p = 7;  segfault  Code segment (R=1, W=0)

8 P1’s Heap P1’s Code (e.g. Excel) P1’s Stack 0x x MB 0 0x x MB 0x x MB Physical View SegBaseBoundRW Bits Stack MB1 Heap MB1 Code MB1 0  Segment table  Each process has a segment table  Stored in PCB  Also installed to the MMU  Note: MMU is more complex now, not just has two (base&bound) registers, but now a table

9 P1’s Heap P1’s Code (e.g. Excel) P1’s Stack 0x x x Physical View load ebx mem[0x ] CPU MMU (base=..; bound=..) Now: Segment table Logical address Physical address SegBaseBoundRW Bits Stack MB1 Heap MB1 Code MB1 0 Input: 0x Output: base + input “hmmm.. stack? heap? or code? Which base to use?”

10  How to convert a logical address to a physical address with segmentation?  Divide the logical address  Top bits of logical address select segment  Low bits of logical address select offset within segment - (hence, the address space that a process can access is reduced) Logical address Ex: 32-bit address

11 SegBaseBound Stack (10) MB Heap (01) MB Code (00) MB Seg # (2 bits) Physical address (e.g. 0x ) 30-bit Virtual Address (Segment offset) Logical address (e.g. load ebx mem[0x ])32 bit P1’s segment table + MMU 1 MB = Stack P1P1 Heap Code Stack Heap Excel Memory

12 Hex Bits 0x x x x x x x x Hex Bits 0x8 ? 0x9 ? 0xA ? 0xB ? 0xC ? 0xD ? 0xE ? 0xF ? 0x07 + 0x0C = ??

13  Example: 14 bit logical address, 3 segments  (i.e. use top-2 bits for segment indexing, and low-12 bits for offset)  Translate logical addresses to physical addresses:  Read mem[0x265c]: …  Read mem[0x1108]: …  Write mem[0x0240]: …  Read mem[0x3002]: … SegmentBaseBoundR W 0 (code)0x20000x06ff1 0 1 (heap)0x00000x04ff1 2 (stack)0x30000x0fff1 30x00000xffff0

14 Code (1 MB) P1’s Heap (2 MB) Stack 0 func(){ int *p = rand(); *p = 7; // what happens? } Int *p 2n2n

15 Code (1 MB) P1’s Heap (2 MB) Stack 0 Int *q func(){ int *q = rand(); *q = 7; // what happens? } 2n2n

16  How many bits for segment indexing?  Say, we need 3, for code, stack, and heap ……  How many bits for the virtual address (the segment offset part)? ……  What’s the largest size of a segment? ……  Implication to malloc()?  Implication to program/code size?  What’s the maximum memory space that a process can occupy? ……

17  So far:  00 – code  01 – heap  02 – stack  03 – unused  Who decides?  Contract between OS, architecture, and compilers - E.g. compilers create jump addresses that start with 00 - Try: compile the same program on different OSes - Then print function pointers, heap and stack addresses (might get different results)  It’s also possible in an advanced computer:  00 – code  01 – heap  02 – mmap segments (shared with other processes)  03 – unused  04 – stack  (other purposes)  Always read the specification  Each OS/architecture doesn’t work the same  (That’s why you must always recompile your program if you want to run it on another machine)

18  A) OS’ job when P1 is created?  (Assume in this OS, stack initially gets 3 KB, heap 2 KB, and code 1 KB; don’t use the “MB” numbers in the picture)  …  When P1 is run?  …

19  B) If P1’s heap grows?  …  C) Context switch from P1 to P2?  …

20  10 users open 5 MB Excel software, how much memory used? ……  Enables sparse allocation of address space (i.e. 0 – 2 n )  2^32 = 4 GB !!!  Stack and heap can grow independently  Heap: If full, dynamic memory allocator requests more from OS - (e.g., UNIX: malloc() library calls sbrk() system call)  Stack: OS recognizes reference outside legal segment, extends stack implicitly  Is there Internal fragmentation? …  Different protection for different segments (sharing!)  Read-only status for code segment (can share code segments!)  Enables sharing of selected segments  Supports dynamic relocation of each segment

21  Files  Pipe  Shared memory  … (many more) 

22  Shared memory  (e.g. in segmentation, use free segment slots) - 4 segment bits  16 segments - 3 for code/heap/stack - Have 13 free slots  Today: “unlimited” shared slots  System calls (see manual)  shmat, shmctl, shmget, … Stack P1P1 Heap Code Stack Heap Code P2P2 mmap2 Memory mmap1 mmap Stack Heap Code P3P3 mmap

23  External fragmentation (luggage overhead bin analogy)?  Variable sized segments  Lots of small holes that can’t be used for processes with large segments  In such case, must relocate segments  Each segment must be allocated contiguously  Some processes might have big segments (big heap/stack)  No memory slot for a big segment  reshuffle

24  Page table, single-level paging  Solve external fragmentation problem (sacrifice small internal fragmentation)  E.g. small fixed-size luggage analogy  small fixed-size 4KB pages  … but page table can be big …  Then, Multi-level page tables  … but access to page tables is slow  … up to 400% slowdown when accessing memory  Then, TLB was invented …  Cache address translation  … but only good for OS running on bare hardware, but not efficient for Virtualization (VMware, Xen, VirtualBox, …)  Then, TLB with para-virtualization was invented  … life goes on, new hardware, new feature, new problem  (SW research never dies! yeah!!)

25...

26 fr 0 pg 2 pg 0 pg 1 Process 3 fr 3 fr 2 fr 1 Physical View (4KB-frames) code (e.g. 6 KB) heap (e.g. 12 KB) stack (e.g. 16 KB) 0 2n2n pg Logical View Physical View

27 ……

28

29

30...

31  al_memory al_memory  Software within the operating system may extend these capabilities to provide a virtual address space that can exceed the capacity of real memory and thus reference more memory than is physically present in the computer  malloc(4GB) possible!  Swap-out/in  Swap-out least recently used pages  Swap-in: “Page faults”: page brought in to memory only when you need it  Key: temporal and spatial locality Stack P1P1 Heap Code Stack Heap Code P2P2 Stack Excel Heap Memory swap space

32...

33  Efficiency  Do not waste memory resources  Keep fragmentation low  Sharing  Several processes coexist in main memory  Cooperating processes can share portions of address space - Two processes (e.g. banking system) share the same data segment - Multiple processes open Office or Excel (share code segment)  Transparency  Processes are not aware that memory is shared  Works regardless of number and/or location of processes  Protection  Robustness: Cannot corrupt OS or other processes  Privacy: Cannot read data of other processes

34  Memory management problems  Appear in many layers of software stack  malloc(), etc.  malloc is a library, not a system call  malloc manages the dynamic data (i.e. pointers) in the heap  How a process grows the heap (call sbrk system call)  (interested: end of Chapter 9)  malloc(2000)  get how many bytes?  So, deal with internal or external problem?  p=malloc(100); *(p+101) = 0;  crash or not? Why?  Java Garbage Collector (GC)  Same as malloc (but users don’t’ need to call malloc or free)  Automatic free space management

35...