MEMORY MANAGEMENT/1 CS 409, FALL 2013

Similar documents
ECE519 Advanced Operating Systems

Chapter 8 Virtual Memory

Chapter 8 Virtual Memory

Chapter 8 Virtual Memory

a process may be swapped in and out of main memory such that it occupies different regions

Memory Management Virtual Memory

Operating Systems CSE 410, Spring Virtual Memory. Stephen Wagner Michigan State University

Chapter 8. Virtual Memory

Virtual Memory. Chapter 8

Operating Systems: Internals and Design Principles. Chapter 7 Memory Management Seventh Edition William Stallings

Chapter 7 Memory Management

Process size is independent of the main memory present in the system.

Virtual Memory. control structures and hardware support

Memory Management Virtual Memory

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

Role of OS in virtual memory management

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

Operating System Concepts

Chapter 9: Virtual Memory

Chapters 9 & 10: Memory Management and Virtual Memory

First-In-First-Out (FIFO) Algorithm

Virtual Memory Outline

Memory Management. Memory Management

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

Virtual Memory. Reading: Silberschatz chapter 10 Reading: Stallings. chapter 8 EEL 358

Chapter 9: Virtual Memory

Chapter 8: Virtual Memory. Operating System Concepts

Chapter 9: Virtual Memory. Chapter 9: Virtual Memory. Objectives. Background. Virtual-address address Space

Chapter 9: Virtual Memory

Virtual Memory. Chapter 8

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

COMP 346 WINTER 2018 MEMORY MANAGEMENT (VIRTUAL MEMORY)

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

OPERATING SYSTEM. Chapter 9: Virtual Memory

Basic Memory Management. Basic Memory Management. Address Binding. Running a user program. Operating Systems 10/14/2018 CSC 256/456 1

CS6401- Operating System UNIT-III STORAGE MANAGEMENT

Memory Management. Memory Management Requirements

Memory Management Cache Base and Limit Registers base limit Binding of Instructions and Data to Memory Compile time absolute code Load time

Basic Memory Management

Chapter 10: Virtual Memory. Background

Chapter 8. Operating System Support. Yonsei University

8.1 Background. Part Four - Memory Management. Chapter 8: Memory-Management Management Strategies. Chapter 8: Memory Management

CSE 120. Translation Lookaside Buffer (TLB) Implemented in Hardware. July 18, Day 5 Memory. Instructor: Neil Rhodes. Software TLB Management

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Memory Management

Chapter 10: Virtual Memory. Background. Demand Paging. Valid-Invalid Bit. Virtual Memory That is Larger Than Physical Memory

Background. Contiguous Memory Allocation

Virtual Memory COMPSCI 386

CS307: Operating Systems

Operating Systems Lecture 6: Memory Management II

Chapter 8: Main Memory

Part Three - Memory Management. Chapter 8: Memory-Management Strategies

Roadmap. Tevfik Koşar. CSC Operating Systems Spring Lecture - XII Main Memory - II. Louisiana State University

Week 2: Tiina Niklander

Chapter 8: Main Memory

Chapter 3: Virtual Memory ว ตถ ประสงค. Background สามารถอธ บายข อด ในการท ระบบใช ว ธ การจ ดการหน วยความจ าแบบเสม อนได

Operating System Support

Chapter 4 Memory Management

Memory Management and Protection

Chapter 8 & Chapter 9 Main Memory & Virtual Memory

Operating Systems, Fall

Optimal Algorithm. Replace page that will not be used for longest period of time Used for measuring how well your algorithm performs

I.-C. Lin, Assistant Professor. Textbook: Operating System Concepts 8ed CHAPTER 8: MEMORY

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

Chapter 9: Memory Management. Background

Memory Management william stallings, maurizio pizzonia - sistemi operativi

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

Operating System Concepts 9 th Edition

Chapter 9: Virtual Memory. Operating System Concepts 9th Edition

Chapter 4 Memory Management. Memory Management

CISC 7310X. C08: Virtual Memory. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 3/22/2018 CUNY Brooklyn College

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

Chapter 10: Virtual Memory

Where are we in the course?

Demand Paging. Valid-Invalid Bit. Steps in Handling a Page Fault. Page Fault. Transfer of a Paged Memory to Contiguous Disk Space

Chapter 8: Memory Management Strategies

CS370 Operating Systems

Chapter 9: Virtual Memory. Operating System Concepts 9 th Edition

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

Main Memory. Electrical and Computer Engineering Stephen Kim ECE/IUPUI RTOS & APPS 1

Chapter 8: Memory Management

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

CS450/550 Operating Systems

Perform page replacement. (Fig 8.8 [Stal05])

I.-C. Lin, Assistant Professor. Textbook: Operating System Principles 7ed CHAPTER 8: MEMORY

CS307: Operating Systems

stack Two-dimensional logical addresses Fixed Allocation Binary Page Table

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

PAGE REPLACEMENT. Operating Systems 2015 Spring by Euiseong Seo

Lecture 7. Memory Management

Virtual or Logical. Logical Addr. MMU (Memory Mgt. Unit) Physical. Addr. 1. (50 ns access)

Virtual Memory: Page Replacement. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

Module 8: Memory Management

CPE300: Digital System Architecture and Design

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

Memory Management. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Module 9: Memory Management. Background. Binding of Instructions and Data to Memory

VII. Memory Management

Chapter 8: Main Memory

Virtual Memory III. Jo, Heeseung

Chapter 9: Virtual Memory

Transcription:

MEMORY MANAGEMENT Requirements: Relocation (to different memory areas) Protection (run time, usually implemented together with relocation) Sharing (and also protection) Logical organization Physical organization Key terms: Frame a fixed-size block of the main memory Page a fixed-size block of data (secondary memory) A page may temporarily be copied into a frame Segment a variable-size block of data (secondary memory) An entire segment may temporarily be copied into the main memory (segmentation) or the segment may be divided into pages which can be individually copied into the main memory (combined segmentation and paging) MEMORY MANAGEMENT/1

LOGICAL AND PHYSICAL ORGANIZATION Logical organization is linear Different parts of the memory can have different permissions (read-write, read-only, etc.) Best implemented using segmentation Physical organization cannot be linear Cannot leave the responsibility of managing memory to the programmer Memory requirements may exceed physical memory, are unknown at build time anyway MEMORY MANAGEMENT/2

MEMORY MANAGEMENT TECHNIQUES External fragmentation = memory exists to satisfy a request, but is not contiguous Internal fragmentation = allocated memory may be slightly larger than requested memory; difference not used Fixed Partitioning: Main memory is divided into a number of static partitions; a process may be loaded into a partition of equal or greater size Simple to implement, but inefficient (internal fragmentation) Maximum number of active processes is fixed Dynamic Partitioning: Partitions are created dynamically, so that each process is loaded into a partition of exactly the same size as that process No internal fragmentation, more efficient use of main memory Need for compaction to counter external fragmentation (increased CPU load) Simple Paging: Main memory divided into a number of equal-size frames; each process is divided into a number of equal-size pages (same size as frames); a process is loaded by loading all of its pages into available, not necessarily contiguous, frames No external fragmentation; some (small) internal fragmentation MEMORY MANAGEMENT/3

MEMORY MANAGEMENT TECHNIQUES (CONT D) Simple Segmentation: Process divided into a number of segments; process is loaded by loading all of its segments into dynamic partitions (need not be contiguous) No internal fragmentation, improved memory utilization, reduced overhead vs. dynamic partitioning External fragmentation Virtual Memory Paging: As with simple paging, except that it is not necessary to load all of the pages of a process; nonresident pages are brought in later, as needed No external fragmentation, higher degree of multiprogramming, large virtual address space Overhead of complex memory management Virtual Memory Segmentation: As with simple segmentation, but is not necessary to load all of the segments; nonresident segments are brought in later, as needed No internal fragmentation, higher degree of multiprogramming, large virtual address space, protection and sharing support Overhead of complex memory management MEMORY MANAGEMENT/4

PARTITIONING Same-size partitions All programs occupy the same amount of space no matter their size = inefficient, internal fragmentation (whenever program data smaller than partition size) Unequal size partitions Existence of smaller partitions reduce the internal fragmentation Memory assignment: queues of processes waiting to be brought into main memory A single queue or one queue per partition size The fixed number of partitions limits the number of active processes in the system Small jobs will not utilize partition space efficiently Programs larger than any partition loaded via overlays OS can only swap out whole processes if needed MEMORY MANAGEMENT/5

DYNAMIC PARTITIONING Process is allocated exactly as much memory as it requires = partitions of variable length and number Used by IBM s mainframe operating system OS/MVT Placement algorithms: Best-fit: chooses the block that is closest in size to the request First-fit: first block that is large enough Next-fit: next large enough block since the last allocation Main disadvantage: external fragmentation Memory utilization declines as memory becomes more and more fragmented Solution: compacting = OS shifts processes in memory so that they are contiguous and free memory is in one block; costly MEMORY MANAGEMENT/6

DYNAMIC PARTITIONING Process is allocated exactly as much memory as it requires = partitions of variable length and number Used by IBM s mainframe operating system OS/MVT Placement algorithms: Best-fit: chooses the block that is closest in size to the request First-fit: first block that is large enough Next-fit: next large enough block since the last allocation Main disadvantage: external fragmentation Memory utilization declines as memory becomes more and more fragmented Solution: compacting = OS shifts processes in memory so that they are contiguous and free memory is in one block; costly MEMORY MANAGEMENT/6

DYNAMIC PARTITIONING (CONT D) MEMORY MANAGEMENT/7

RELOCATION Logical address = reference to memory location independent on the assignment of data to memory Relative address = address expressed as a location relative to some known point Physical/Absolute address: actual location in main memory Relocation: Logical address relative (to the beginning of the process) address physical address Needs hardware support = Memory Management Unit (MMU) MEMORY MANAGEMENT/8

PAGING Partition memory into equal fixed-size chunks that are relatively small (pages) Process is also divided into small fixed-size chunks of the same size (frames) Key data structure: page table Maintained by operating system for each process Contains the frame location for each page in the process Processor must know how to access for the current process Used by processor to produce a physical address MEMORY MANAGEMENT/9

LOGICAL ADDRESS AND PAGING/SEGMENTATION MEMORY MANAGEMENT/10

LOGICAL ADDRESS AND PAGING MEMORY MANAGEMENT/11

SEGMENTATION A program can be subdivided into segments May vary in length Maximum length usually given Addressing consists of two parts: Segment number Offset Similar to dynamic partitioning Eliminates internal fragmentation MEMORY MANAGEMENT/12

LOGICAL ADDRESS AND SEGMENTATION MEMORY MANAGEMENT/13

VIRTUAL MEMORY Not necessary that all the process is in memory as long as all references are logical addresses and address space can be split into pages OS brings into main memory a few pieces of the program = resident set An interrupt is generated when an address is needed but is in the virtual memory (usually on disk) instead of the main memory OS places the process in a Blocked state while the missing piece is brought into the main memory Operating system issues a disk I/O Read request (usually) Another process is dispatched to run while the disk I/O takes place When disk I/O is complete (interrupt), the affected process is placed back in the Ready state More processes can be maintained in the main memory Only some of the pieces of each process are loaded With so many processes in main memory it is very likely a process will be in the Ready state at any particular time A process can even be larger than all of the main memory! MEMORY MANAGEMENT/14

PRINCIPLE OF LOCALITY Main problem: Thrashing = the system spends most of its time swapping process pieces rather than executing instructions To avoid this, the OS tries to guess based on recent history which pieces are least likely to be used in the near future Guess is based on the principle of locality Program and data references within a process tend to cluster Only a few pieces of a process will be needed over a short period of time Therefore it is possible to make intelligent guesses about which pieces will be needed in the future MEMORY MANAGEMENT/15

HARDWARE SUPPORT FOR VIRTUAL MEMORY Hardware must support paging and segmentation OS must include software for managing the movement of pages and/or segments between secondary memory and main memory Virtual memory is usually associated with systems that employ paging Each process has its own page table Each page table entry contains the frame number of the corresponding page in main memory MEMORY MANAGEMENT/16

MEMORY MANAGEMENT FORMATS MEMORY MANAGEMENT/17

ADDRESS TRANSLATION MEMORY MANAGEMENT/18

TWO-LEVEL HIERARCHICAL PAGE TABLE MEMORY MANAGEMENT/19

ADDRESS TRANSLATION IN A TWO-LEVEL PAGING SYSTEM MEMORY MANAGEMENT/20

INVERTED PAGE TABLE Page number portion of a virtual address is mapped into a hash value Hash value points to inverted page table Fixed proportion of real memory is required for the tables regardless of the number of processes or virtual pages supported Structure is called inverted because it indexes page table entries by frame number rather than by virtual page number MEMORY MANAGEMENT/21

TRANSLATION LOOKASIDE BUFFER (TLB) Problem: Each virtual memory reference can cause two physical memory accesses: one to fetch the page table entry one to fetch the data Solution: use a special highspeed cache called a translation lookaside buffer MEMORY MANAGEMENT/22

TLB ALGORITHM MEMORY MANAGEMENT/23

ASSOCIATIVE MAPPING TLB cannot be indexed on page number (it contains only some page table entries) TLB entry must include the page number as well as the complete page table entry Hardware that allows simultaneously interrogation of a number of TLB entries to determine if there is a match on page number = associative mapping MEMORY MANAGEMENT/24

TLB AND CACHE MEMORY MANAGEMENT/25

PAGE SIZE The smaller the page size, the lesser the amount of internal fragmentation However, more pages are required per process More pages per process means larger page tables For large programs in a heavily multiprogrammed environment some portion of the page tables of active processes must be in virtual memory instead of main memory The physical characteristics of most secondary-memory devices favor a larger page size for more efficient block transfer of data Computer Atlas Honeywell-Multics IBM 370/XA IBM 370/ESA VAX family IBM AS/400 DEC Alpha MIPS UltraSPARC Pentium IBM POWER Itanium Page Size 512 48-bit words 1024 36-bit words 4 KB 512 bytes 512 bytes 8 KB 4 KB to 16 MB 8 KB to 4 MB 4 KB or 4 MB 4 KB 4 KB to 256 MB MEMORY MANAGEMENT/26

COMBINED PAGING AND SEGMENTATION Segmentation allows programmer to view memory as consisting of multiple address spaces (or segments) Simplifies handling of large data structures Facilitates sharing data among processes Facilitates protection Each segment table entry contains the starting address of the corresponding segment in main memory and the length of the segment A bit is needed to determine if the segment is already in main memory Another bit is needed to determine if the segment has been modified since it was loaded in main memory In a combined paging/segmentation system a user s address space is broken up into a number of segments Each segment is broken up into a number of fixed-sized pages which are equal in length to a main memory frame Segmentation is visible to the programmer, paging is transparent MEMORY MANAGEMENT/27

ADDRESS TRANSLATION MEMORY MANAGEMENT/28

PROTECTION RELATIONSHIP Segmentation lends itself to the implementation of protection and sharing policies Each entry has a base address and length so inadvertent memory access can be controlled Sharing can be achieved by segments referencing multiple processes Each entry in segment table features: Validation bit (0 = illegal segment) Read/write/execute privileges MEMORY MANAGEMENT/29

OS MEMORY MANAGEMENT SOFTWARE The design of the memory management portion of an operating system depends on three fundamental areas of choice: Whether or not to use virtual memory techniques The use of paging or segmentation or both The algorithms employed for various aspects of memory management MEMORY MANAGEMENT/30

VIRTUAL MEMORY FETCH POLICIES Fetch policy: Determines when a page should be brought into memory Demand paging only brings pages into main memory when a reference is made to a location on the page Many page faults when process is first started But drops as more and more pages are brought in (principle of locality most future references will be to pages that have recently been brought in) Prepaging once a page is demanded, more are brought in Exploits the characteristics of most secondary memory devices If pages of a process are stored contiguously in secondary memory it is more efficient to bring in a number of pages at one time Ineffective if extra pages are not referenced MEMORY MANAGEMENT/31

VIRTUAL MEMORY PLACEMENT AND REPLACEMENT POLICIES Placement determines where in real memory a process piece is to reside Important design issue in a segmentation system But irrelevant to paging or combined paging with segmentation (hardware performs functions with equal efficiency) Replacement deals with the selection of a page in main memory to be replaced when a new page must be brought in Objective: the page that is removed is the page least likely to be referenced in the near future The more elaborate the better at guessing, but at the cost of greater hardware and software overhead Frame Locking: When a frame is locked the page currently stored in that frame may not be replaced The kernel as well as key control structures are held in locked frames I/O buffers and time-critical areas may be locked into main memory frames Achieved by associating a lock bit with each frame MEMORY MANAGEMENT/32

BASIC PAGE REPLACEMENT ALGORITHMS Optimal: Selects page for which the time to the next reference is the longest (ideal) Least Recently Used (LRU): Replaces the page that has not been referenced for the longest time Principle of locality LRU = page least likely to be referenced in the near future Difficult to implement, substantial overhead One approach is to tag each page with the time of last reference MEMORY MANAGEMENT/33

BASIC PAGE REPLACEMENT ALGORITHMS (CONT D) First-in-First-out (FIFO): Treats page frames allocated to a process as a circular buffer; pages are removed in round-robin style Simple to implement, page that has been in memory the longest is replaced Clock: Associates a use bit with each frame, initialized with 1 on load Any frame with a use bit of 1 is passed over by the algorithm (but the bit is reset) Page frames visualized as laid out in a circle MEMORY MANAGEMENT/34

THE CLOCK REPLACEMENT ALGORITHM (CONT D) MEMORY MANAGEMENT/35

COMPARISON OF PAGE REPLACEMENT ALGORITHMS MEMORY MANAGEMENT/36

ADDITIONAL REPLACEMENT POLICIES Page buffering: A replaced page is assigned to the free page list or modified page list (as the case might be) Pages are written to disk in clusters to improve efficiency Replacement Policy and Cache Size: With large caches replacement of pages can have a performance impact; if the page frame to be replaced is in the cache then that cache block is lost as well Resident Set Management: The OS must decide how many pages to bring into main memory The smaller the amount of memory allocated to each process, the more processes can reside in memory But small number of pages loaded increases page faults But beyond a certain size further allocation of pages will not effect page fault rate Fixed-allocation gives a process a fixed number of frames in main memory within which to execute (page fault one of the pages of that process is replaced) Variable-allocation allows the number of frames allocated to a process to vary MEMORY MANAGEMENT/37

ADDITIONAL REPLACEMENT POLICIES (CONT D) Replacement Scope Local: chooses only among the resident pages of the process that generated the page fault Global: considers all unlocked pages in main memory (not possible for fixed allocation) Most used policy: variable allocation, global scope Cleaning policy: determine when a modified page should be written out to secondary memory Demand Cleaning: a page is written out only when it has been selected for replacement Precleaning: allows the writing of pages in batches MEMORY MANAGEMENT/38

LOAD CONTROL Determines the number of processes that will be resident in main memory Critical in effective memory management Too few processes, many occasions when all processes will be blocked and much time will be spent in swapping Too many processes will lead to thrashing If the degree of multiprogramming is to be reduced, one or of the currently resident processes must be swapped out; six possibilities exist: Lowest-priority process Faulting process Last process activated Process with the smallest resident set Largest process Process with the largest remaining execution window Do not confuse swapping with page replacement! MEMORY MANAGEMENT/39

LINUX MEMORY MANAGEMENT Two main aspects: process virtual memory and kernel memory allocation Three-level page table structure for the virtual memory Page replacement based on the clock algorithm, with an 8-bit age variable (incremented each time a page is accessed, decremented periodically) An age of 0 not accessed in a long time, best candidate for replacement (LRU) MEMORY MANAGEMENT/40

KERNEL MEMORY ALLOCATION IN LINUX Page allocator inefficient because the kernel usually requires small chunks in odd sizes A buddy algorithm (power-of-two allocator) and slab allocation are used so that memory for the kernel can be allocated and deallocated in units of one or more pages MEMORY MANAGEMENT/41

OTHER ADVANTAGES OF VIRTUAL MEMORY Process creation: Copy-on-write (COW) Allows both parent and child processes to initially share the same pages If either process modifies a shared page, only then is the page copied Allows more efficient process creation as only modified pages are copied Memory-mapped files Memory-mapped file I/O allows file I/O to be treated as routine memory access by mapping a disk block to a page in memory A file is initially read using demand paging. A page-sized portion of the file is read from the file system into a physical page. Subsequent reads/writes to/from the file are treated as ordinary memory accesses. Simplifies file access by treating file I/O through memory rather than read() and write() system calls Also allows several processes to map the same file allowing the pages in memory to be shared Mapping of shared libraries MEMORY MANAGEMENT/42

COPY ON WRITE Before any writing takes place: MEMORY MANAGEMENT/43

COPY ON WRITE (CONT D) As soon as Process 1 modifies Page C: MEMORY MANAGEMENT/44

SHARED LIBRARIES MEMORY MANAGEMENT/45

MEMORY MAPPED FILES MEMORY MANAGEMENT/46