Ken Birman. How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines all.

Slides:



Advertisements
Similar presentations
Memory.
Advertisements

Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 13: Main Memory (Chapter 8)
Virtual Memory. 2 What is virtual memory? Each process has illusion of large address space –2 32 for 32-bit addressing However, physical memory is much.
Virtual Memory.
CS 153 Design of Operating Systems Spring 2015
Chapter 101 Virtual Memory Chapter 10 Sections and plus (Skip:10.3.2, 10.7, rest of 10.8)
Virtual Memory Chapter 8. Hardware and Control Structures Memory references are dynamically translated into physical addresses at run time –A process.
1 Virtual Memory vs. Physical Memory So far, all of a job’s virtual address space must be in physical memory However, many parts of programs are never.
CS 104 Introduction to Computer Science and Graphics Problems
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 2010.
Virtual Memory Chapter 8.
1 Lecture 9: Virtual Memory Operating System I Spring 2007.
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.
Main Memory. Background Program must be brought (from disk) into memory and placed within a process for it to be run Main memory and registers are only.
Demand Paging Virtual memory = indirect addressing + demand paging –Without demand paging, indirect addressing by itself is valuable because it reduces.
03/17/2008CSCI 315 Operating Systems Design1 Virtual Memory Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Memory Management ◦ Operating Systems ◦ CS550. Paging and Segmentation  Non-contiguous memory allocation  Fragmentation is a serious problem with contiguous.
Virtual Memory Prof. Van Renesse & Sirer. Segments Note: overloaded term… Chunks of virtual address space Access Protection – User/Supervisor – Read/Write/Execute.
Virtual Memory.
Rensselaer Polytechnic Institute CSC 432 – Operating Systems David Goldschmidt, Ph.D.
Review of Memory Management, Virtual Memory CS448.
Operating Systems Chapter 8
Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes.
CIS250 OPERATING SYSTEMS Memory Management Since we share memory, we need to manage it Memory manager only sees the address A program counter value indicates.
1 Memory Management 4.1 Basic memory management 4.2 Swapping 4.3 Virtual memory 4.4 Page replacement algorithms 4.5 Modeling page replacement algorithms.
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.
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming  To allocate scarce memory resources.
© 2004, D. J. Foreman 1 Virtual Memory. © 2004, D. J. Foreman 2 Objectives  Avoid copy/restore entire address space  Avoid unusable holes in memory.
1 Address Translation Memory Allocation –Linked lists –Bit maps Options for managing memory –Base and Bound –Segmentation –Paging Paged page tables Inverted.
CE Operating Systems Lecture 14 Memory management.
CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming.  To allocate scarce memory.
Main Memory. Chapter 8: Memory Management Background Swapping Contiguous Memory Allocation Paging Structure of the Page Table Segmentation Example: The.
Memory Management Continued Questions answered in this lecture: What is paging? How can segmentation and paging be combined? How can one speed up address.
1 Lecture 8: Virtual Memory Operating System Fall 2006.
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.
1 Contents Memory types & memory hierarchy Virtual memory (VM) Page replacement algorithms in case of VM.
Virtual Memory. 2 Last Week Memory Management Increase degree of multiprogramming –Entire process needs to fit into memory Dynamic Linking and Loading.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 8: Main Memory.
Chapter 7: Main Memory CS 170, Fall Program Execution & Memory Management Program execution Swapping Contiguous Memory Allocation Paging Structure.
Virtual Memory What if program is bigger than available memory?
Logistics Homework 5 will be out this evening, due 3:09pm 4/14
CS703 - Advanced Operating Systems
Virtual Memory.
Chapter 8: Main Memory.
Lecture 10: Virtual Memory
Chapter 8: Main Memory.
Memory Management 11/17/2018 A. Berrached:CS4315:UHD.
Chapter 9: Virtual-Memory Management
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.
Main Memory Background Swapping Contiguous Allocation Paging
Lecture 3: Main Memory.
CSE 451: Operating Systems Autumn 2005 Memory Management
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
Contents Memory types & memory hierarchy Virtual memory (VM)
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 10 Paging & TLBs
Computer Architecture
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
COMP755 Advanced Operating Systems
Virtual Memory.
Chapter 8 & 9 Main Memory and Virtual Memory
CSE 542: Operating Systems
Presentation transcript:

Ken Birman

How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines all object files into one executable Loader: loads executable in memory 2 ld loader Cache Compile timeruntime

What does process look like? 3 heap stack code initialized data address 2^n-1 address >= 0 Process divided into segments, each has: - Code, data, heap (dynamic data) and stack (procedure calls)

Processes in Memory We want many of them to coexist Issues: PowerPoint needs more memory than there is on machine? What is visual studio tries to access 0x0005? If PowerPoint is not using enough memory? If OS needs to expand? 4 OS PowerPoint Visual Studio 0x0000 0x1100 0x1210 0x2300

Issues Protection: Errors in process should not affect others Transparency: Should run despite memory size/location 5 gcc virtual address CPU Load Store legal addr? Illegal? Physical address Physical memory fault data Translation box (MMU) How to do this mapping?

Scheme 1: Load-time Linking Link as usual, but keep list of references At load time: determine the new base address Accordingly adjust all references (addition) Issues: handling multiple segments, moving in memory 6 0x1000 static a.out 0x3000 0x4000 OS jump 0x2000jump 0x5000 0x6000 jump 0x2000

Scheme 2: Execution-time Linking Use hardware (base + limit reg) to solve the problem Done for every memory access Relocation: physical address = logical (virtual) address + base Protection: is virtual address < limit? When process runs, base register = 0x3000, bounds register = 0x2000. Jump addr = 0x x3000 = 0x x1000 a.out 0x3000 0x4000 OS a.out jump 0x2000 0x6000 Base: 0x3000 Limit: 0x2000 MMU

Dynamic Translation Memory Management Unit in hardware Every process has its own address space 8 CPU MMU logical addrs memory Physical addrs

Logical and Physical Addresses 9 Logical Address View Physical Address View MMU OS

Scheme 2: Discussion Pro: cheap in terms of hardware: only two registers cheap in terms of cycles: do add and compare in parallel Con: only one segment prob 1: growing processes. How to expand gcc? prob 2: how to share code and data?? how can Word copies share code? prob 3: how to separate code and data? A solution: multiple segments “segmentation” 10 gcc p2 Free space p3 gcc Word2 Word1

Segmentation Processes have multiple base + limit registers Processes address space has multiple segments Each segment has its own base + limit registers Add protection bits to every segment 11 gcc Text seg r/o Stack seg r/w 0x1000 0x3000 0x5000 0x6000 Real memory 0x2000 0x8000 0x6000 Base&Limit? How to do the mapping?

Types of Segments Each “file” of a program (or library) typically results in An associated code segment (instructions) The “initialized” data segment for that code The zeroed part of the data segment (“bss”) Programs also have One heap segment – grows “upward” A stack segment for each active thread (grows down; all but first have a size limit) 12

Lots of Segments Bottom line: a single process might have a whole lot of active segments! And it can add them while it runs, for example by linking to a DLL or forking a thread Address space is a big range with chunks of data separated by gaps It isn’t uncommon for a process on a modern platform to have hundreds of segments in memory… 13

Mapping Segments Segment Table An entry for each segment Is a tuple Each memory reference indicates segment and offset 14 Virtual addr Seg# offset Seg table Prot base len r 0x mem seg x1000 ? yes no fault

Segmentation Example If first two bits are for segments, next 12 for offset where is 0x0240? 0x1108? 0x265c? 0x3002? 0x1700? 15 Seg base bounds rw 0 0x4000 0x6ff x0000 0x4ff x3000 0xfff x4000 0x3000 0x2000 0x1500 0x1000 0x0700 0x0000 logical physical 0x4700 0x4000 0x3000 0x500 0x0

Segmentation: Discussion Advantages: Allows multiple segments per process Easy to allow sharing of code Do not need to load entire process in memory Disadvantages: Extra translation overhead: Memory & speed An entire segment needs to reside contiguously in memory!  Fragmentation 16

Fragmentation “Free memory is scattered into tiny, useless pieces” External Fragmentation: Variable sized pieces  many small holes over time Internal Fragmentation: Fixed sized pieces  internal waste if entire piece is not used 17 gcc emacs doom stack allocated Unused (“internal fragmentation”) External fragmentation Word ??

Paging Divide memory into fixed size pieces Called “frames” or “pages” Pros: easy, no external fragmentation 18 gcc emacs internal frag Pages typical: 4k-8k

Mapping Pages If 2 m virtual address space, 2 n page size  (m - n) bits to denote page number, n for offset within page Translation done using a Page Table 19 Virtual addr VPN page offset (12bits) page table Prot VPN PPN r 3 1 mem seg 128 0x1000 ((1<<12)|128) “invalid” ?PPN

Paging: Hardware Support Entire page table (PT) in registers PT can be huge ~ 1 million entries Store PT in main memory Have PTBR point to start of PT Con: 2 memory accesses to get to any physical address Solution: a special cache (next slide) Each time we context switch, must switch the current PT too, so that the process we execute will be able to see its memory region 20

MMU includes a cache We call it the translation lookaside buffer (TLB) Just a cache, with a special name This cache contains page table entries The OS needs to “clear” the TLB on context switch So first few operations by a freshly started process are very slow! But then the cache will warm up Goal is that MMU can find the PTE entries it needs without actually fetching them from the page table

Paging: Discussion Advantages: No external fragmentation Easy to allocate Easy to swap, since page size usually same as disk block size Disadvantages: Space and speed One PT entry for every page, vs. one entry for contiguous memory for segmentation 22 0xFFFF0x0000 Page table B=0x0,len=0xffff

Size of the page Small page size: High overhead: What is size of PT if page size is 512 bytes, and 32-bit addr space? Large page size: High internal fragmentation 23 page size Internal frag & writeback cost Overhead & locality More

Paging + Segmentation Paged segmentation Handles very long segments The segments are paged Segmented Paging When the page table is very big Segment the page table Let’s consider System 370 (24-bit address space) 24 Seg #page # (8 bits)page offset (12 bits) (4 bits)

Segmented Paging Example Base bound prot 0x2000 0x14 R 0x0000 0x00 0x1000 0x0d RW Segment table 0x001f 0x0011 0x2020 … 0x0003 0x002a 0x0013 0x2000 … 0x000c 0x0007 0x1020 … 0x004 0x00b 0x006 0x1000 Table entry addresses Page table (2-byte entries) 0x read? 0x104c84 read? 0x read? 0x write? Seg 2: page 2, 0x1004, addr = 0x4016 Seg 1: page 4, protection error Seg 0, page 0x11, addr = 0x1f424 Seg 2, page 0x10, bounds violation Seg 2: page 2, 0x1004, addr = 0x4016 Seg 1: page 4, protection error Seg 0, page 0x11, addr = 0x1f424 Seg 2, page 0x10, bounds violation

Handling PT size Segmented Paging removes need for contiguous alloc OS maintains one copy of PT for every process And a global copy of all frames Other approaches: Hierarchical Paging: Page the page table Hashed Page Table: Each entry maps to linked list of pages Inverted Page Table: Map from Frame to (VA, process) instead of VA to frame per process 26

What is virtual memory? Each process has illusion of large address space 2 32 for 32-bit addressing However, physical memory is much smaller How do we give this illusion to multiple processes? Virtual Memory: some addresses reside in disk 28 page table Physical memory disk

Virtual Memory Load entire process in memory (swapping), run it, exit Is slow (for big processes) Wasteful (might not require everything) Solutions: partial residency Paging: only bring in pages, not all pages of process Demand paging: bring only pages that are required Where to fetch page from? Have a contiguous space in disk: swap file (pagefile.sys) 29

How does VM work? Modify Page Tables with another bit (“is present”) If page in memory, is_present = 1, else is_present = 0 If page is in memory, translation works as before If page is not in memory, translation causes a page fault 30 Disk Mem 32 :P=1 4183:P=0 177 :P=1 5721:P=0

Page Faults On a page fault: OS finds a free frame, or evicts one from memory (which one?) Want knowledge of the future? Issues disk request to fetch data for page (what to fetch?) Just the requested page, or more? Block current process, context switch to new process (how?) Process might be executing an instruction When disk completes, set present bit to 1, and current process in ready queue 31

Resuming after a page fault Should be able to restart the instruction For RISC processors this is simple: Instructions are idempotent until references are done More complicated for CISC: E.g. move 256 bytes from one location to another Possible Solutions: Ensure pages are in memory before the instruction executes 32

When to fetch? Just before the page is used! Need to know the future Demand paging: Fetch a page when it faults Prepaging: Get the page on fault + some of its neighbors, or Get all pages in use last time process was swapped 33

What to replace? Page Replacement When process has used up all frames it is allowed to use OS must select a page to eject from memory to allow new page The page to eject is selected using the Page Replacement Algo Goal: Select page that minimizes future page faults 34

Page Replacement Algorithms Random: Pick any page to eject at random Used mainly for comparison FIFO: The page brought in earliest is evicted Ignores usage Suffers from “Belady’s Anomaly” Fault rate could increase on increasing number of pages E.g with frame sizes 3 and 4 OPT: Belady’s algorithm Select page not used for longest time LRU: Evict page that hasn’t been used the longest Past could be a good predictor of the future 35

Example: FIFO, OPT 36 Reference stream is A B C A B D A D B C OPTIMAL A B C A B D A D B C B toss A or D toss C 5 Faults FIFO A B C A B D A D B C B toss A ABCDABCABCDABC toss ? 7 Faults

Implementing Perfect LRU On reference: Time stamp each page On eviction: Scan for oldest frame Problems: Large page lists Timestamps are costly Approximate LRU LRU is already an approximation! 37 0xffdcd: add r1,r2,r3 0xffdd0: ld r1, 0(sp) t=4 t=14 t=

LRU: Clock Algorithm Each page has a reference bit Set on use, reset periodically by the OS Algorithm: FIFO + reference bit (keep pages in circular list) Scan: if ref bit is 1, set to 0, and proceed. If ref bit is 0, stop and evict. Problem: Low accuracy for large memory 38 R=1 R=0 R=1 R=0 R=1 R=0 R=1

LRU with large memory Solution: Add another hand Leading edge clears ref bits Trailing edge evicts pages with ref bit 0 What if angle small? What if angle big? 39 R=1 R=0 R=1 R=0 R=1 R=0 R=1

Clock Algorithm: Discussion Sensitive to sweeping interval Fast: lose usage information Slow: all pages look used Clock: add reference bits Could use (ref bit, modified bit) as ordered pair Might have to scan all pages LFU: Remove page with lowest count No track of when the page was referenced Use multiple bits. Shift right by 1 at regular intervals. MFU: remove the most frequently used page LFU and MFU do not approximate OPT well 40

Page Buffering Cute simple trick: (XP, 2K, Mach, VMS) Keep a list of free pages Track which page the free page corresponds to Periodically write modified pages, and reset modified bit 41 add evict modified list (batch writes = speed) unmodified free list used free

Allocating Pages to Processes Global replacement Single memory pool for entire system On page fault, evict oldest page in the system Problem: protection Local (per-process) replacement Have a separate pool of pages for each process Page fault in one process can only replace pages from its own process Problem: might have idle resources 42