CMPT 300 Operating System I

Slides:



Advertisements
Similar presentations
Chapter 4 Memory Management Basic memory management Swapping
Advertisements

Chapter 4 Memory Management Page Replacement 补充:什么叫页面抖动?
Part IV: Memory Management
1 Virtual Memory Management B.Ramamurthy. 2 Demand Paging Main memory LAS 0 LAS 1 LAS 2 (Physical Address Space -PAS) LAS - Logical Address.
1 Memory Management Managing memory hierarchies. 2 Memory Management Ideally programmers want memory that is –large –fast –non volatile –transparent Memory.
Memory Management Chapter 4. Memory hierarchy Programmers want a lot of fast, non- volatile memory But, here is what we have:
Operating System Support Focus on Architecture
Memory Management and Paging CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Memory Management - 2 CS 342 – Operating Systems Ibrahim Korpeoglu Bilkent.
Multiprocessing Memory Management
Memory Management 2010.
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.
CS 333 Introduction to Operating Systems Class 9 - Memory Management
Computer Organization and Architecture
Virtual Memory Management B.Ramamurthy. Paging (2) The relation between virtual addresses and physical memory addres- ses given by page table.
1 Virtual Memory Management B.Ramamurthy Chapter 10.
Chapter 4 Memory Management 4.1 Basic memory management 4.2 Swapping
Layers and Views of a Computer System Operating System Services Program creation Program execution Access to I/O devices Controlled access to files System.
CS 346 – Chapter 8 Main memory –Addressing –Swapping –Allocation and fragmentation –Paging –Segmentation Commitment –Please finish chapter 8.
Review of Memory Management, Virtual Memory CS448.
Memory Management From Chapter 4, Modern Operating Systems, Andrew S. Tanenbaum.
Memory Management By: Omar A. Cruz Salgado ICOM 5007 Sec. 121.
SOCSAMS e-learning Dept. of Computer Applications, MES College Marampally MEMORYMANAGEMNT.
MEMORY MANAGEMENT Presented By:- Lect. Puneet Gupta G.P.C.G. Patiala.
Chapter 5 Operating System Support. Outline Operating system - Objective and function - types of OS Scheduling - Long term scheduling - Medium term scheduling.
Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”
Cosc 2150: Computer Organization Chapter 6, Part 2 Virtual Memory.
1 Chapter 3.2 : Virtual Memory What is virtual memory? What is virtual memory? Virtual memory management schemes Virtual memory management schemes Paging.
1 Memory Management Chapter Basic memory management 4.2 Swapping (εναλλαγή) 4.3 Virtual memory (εικονική/ιδεατή μνήμη) 4.4 Page replacement algorithms.
Operating Systems COMP 4850/CISG 5550 Page Tables TLBs Inverted Page Tables Dr. James Money.
Memory Management – Page 1 of 49CSCI 4717 – Computer Architecture Memory Management Uni-program – memory split into two parts –One for Operating System.
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.
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.
Chapter 10 Memory Management Introduction Process must be loaded into memory before being executed. Input queue – collection of processes on the.
CS 149: Operating Systems March 3 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
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.
Operating Systems COMP 4850/CISG 5550 Basic Memory Management Swapping Dr. James Money.
“ Memory Management Function ” Presented By Lect. Rimple Bala GPC,Amritsar 1.
Main Memory. Chapter 8: Memory Management Background Swapping Contiguous Memory Allocation Paging Structure of the Page Table Segmentation Example: The.
Basic Memory Management 1. Readings r Silbershatz et al: chapters
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Mono an multiprogramming.
CS6502 Operating Systems - Dr. J. Garrido Memory Management – Part 1 Class Will Start Momentarily… Lecture 8b CS6502 Operating Systems Dr. Jose M. Garrido.
Memory Management Program must be brought (from disk) into memory and placed within a process for it to be run Main memory and registers are only storage.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Memory.
CHAPTER 3-1, 3-2 MEMORY MANAGEMENT. MEMORY HIERARCHY Small amount of expensive, fast, volatile cache Larger amount of still fast, but slower, volatile.
Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”
Memory management The main purpose of a computer system is to execute programs. These programs, together with the data they access, must be in main memory.
Memory Management One of the most important OS jobs.
Chapter 2 Memory and process management
From Monoprogramming to multiprogramming with swapping
CSC 322 Operating Systems Concepts Lecture - 12: by
Chapter 9 – Real Memory Organization and Management
Main Memory Management
Chapter 8: Main Memory.
Lecture 28: Virtual Memory-Address Translation
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 8 11/24/2018.
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
CS399 New Beginnings Jonathan Walpole.
Chapter 8: Memory management
Outline Module 1 and 2 dealt with processes, scheduling and synchronization Next two modules will deal with memory and storage Processes require data to.
CSE 451: Operating Systems Autumn 2005 Memory Management
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
COMP755 Advanced Operating Systems
Presentation transcript:

CMPT 300 Operating System I Chapter 4 Memory Management

Why Memory Management? Why money management? Not enough money. Same thing for memory Parkinson’s law: programs expand to fill the memory available to hold them “640KB memory are enough for everyone” – Bill Gates Programmers’ ideal: an infinitely large, infinitely fast memory, nonvolatile Reality: memory hierarchy Registers If main memory is large to hold everything, the arguments in this chapter become obsolete. Cache Main memory Magnetic disk Magnetic tape

What Is Memory Management? Memory manager: the part of the OS managing the memory hierarchy Keep track of memory parts in use/not in use Allocate/de-allocate memory to processes Manage swapping between main memory and disk Basic memory management: every program is put and run in main memory as whole Swapping & paging: move processes back and forth between main memory and disk

Outline Basic memory management Swapping Virtual memory Page replacement algorithms Modeling page replacement algorithms Design issues for paging systems Implementation issues Segmentation

Mono Programming One program at a time Three variations Share memory with OS OS loads the program from disk to memory Three variations User program OS in RAM 0xFFF… OS in ROM User program Device drivers in ROM User program OS in RAM Three variations: choices depend on system design considerations. OS at the bottom of memory in RAM Formerly used on mainframes and minicomputers, rarely used any more. OS in ROM at the top of memory Used on some palmtop computers and embedded systems Device drivers at the top of memory in a ROM and the rest of the system in RAM down below Used by early personal computers (MS-DOS) the portion of the system in ROM is called BIOS

Multiprogramming With Fixed Partitions Advantages of Multiprogramming? Scenario: multiple programs at a time Problem: how to allocate memory? Divide memory up into n partitions, one partition can at most hold one program (process) Equal partitions vs. unequal partitions Each partition has a job queue Can be done manually when system is up A job arrives, put it into the input queue for the smallest partition large enough to hold it Any space in a partition not used by a job is lost Here, we assume that a program always fit in a memory partition.

Example: Multiprogramming With Fixed Partitions 800K Partition 4 Partition 3 Partition 2 Partition 1 OS 700K B 400K A 200K 100K Multiple input queues

Single Input Queue Partition 4 Partition 3 Disadvantage of multiple input queues Small jobs may wait, while a queue with larger memory is empty Solution: single input queue 800K Partition 4 Partition 3 Partition 2 Partition 1 OS 700K 250K B 400K A 10K 200K 100K

How to Pick Jobs? Pick the first job in the queue fitting an empty partition Fast, but may waste a large partition on a small job Pick the largest job fitting an empty partition Memory efficient Smallest jobs may be interactive ones, need best service, slow Policies for efficiency and fairness Have at least one small partition around A job may not be skipped more than k times A trade-off between space and time efficiency

A Naïve Model for Multiprogramming Goal: determine the number of processes in main memory to keep the CPU busy Multiprogramming improves CPU utilization If on average, a process computes 20% of the time it sitting in memory  5 processes can keep CPU busy all the time Assume all processes never wait for I/O at the same time. Too optimistic!

A Probabilistic Model A process spends a fraction p of its time waiting for I/O to complete 0<p<1 At once n processes in memory CPU utilization 1 – pn Probability that all n processes are waiting for I/O: pn Assume processes are independent to each other Not true in reality. A process has to wait another process to give up CPU Using queue theory.

CPU Utilization 1 – pn When 80% I/O wait, if we want CPU utilization >= 80%, at least 7 processes.

Memory Management for Multiprogramming Relocation When program is compiled, it assumes the starting address is 0. (logical address) When it is loaded into memory, it could start at any address. (physical address) How to map logical address to physical address? Protection A program’s access should be confined to proper area

Relocation & Protection Logical address for programming Call a procedure at logical address 100 Physical address When the procedure is in partition 1 (started from physical address 100k), then the procedure is at 100K+100 Relocation problem: translation between logical address and physical address Protection: a malicious program can jump to space belonging to other users Generate a new instruction on the fly that can reads or writes any word in memory

Relocation/Protection Using Registers Base register: start of the partition Every memory address generated adds the content of base register Base register: 100K, CALL 100  CALL 100K +100 Limit register: length of the partition Addresses are checked against the limit register Disadvantage: perform addition and comparison on every memory reference

Outline Basic memory management Swapping Virtual memory Page replacement algorithms Modeling page replacement algorithms Design issues for paging systems Implementation issues Segmentation

In Time-sharing/Interactive Systems… Not enough main memory to hold all currently active processes Intuition: excess processes must be kept on disk and brought in to run dynamically Swapping: bring in each process in entirely Assumption: each process can be held in main memory, but cannot finish at one run Virtual memory: allow programs to run even when they are only partially in main memory No assumption about program size

Swapping Time  A OS B A OS C B A OS C B OS C B D OS C D OS C A D OS Hole Time  Swap A out Swap B out

Swapping V.S. Fixed Partitions The number, location and size of partitions vary dynamically in swapping Flexibility, improve memory utilization Complicate allocating, de-allocating and keeping track of memory Memory compaction: combine “holes” in memory into a big one More efficient in allocation Require a lot of CPU time Rarely used in real systems

Enlarge Memory for a Process Fixed size process: easy Growing process Expand to the adjacent hole, if there is a hole Otherwise, wait or swap some processes out to create a large enough hole If swap area on the disk is full, wait or be killed Allocate extra space whenever a process is swapped in or move

Handling Growing Processes B-Stack Room for growth B-Data B-Program A-Stack A-Data A-Program OS Room for growth of B B Room for growth of A A OS Processes with one growing data segment Processes with growing data and stack segments

Memory Management With Bitmaps Two ways to keep track of memory usage Bitmaps and free lists Bitmaps Memory is divided into allocation units One bit per unit: 0-free, 1-occupied A B C D E 1

Size of Allocation Units 4 bytes/unit  1 bit in map for 32 bits of memory  bitmap takes 1/33 of memory Trade-off between allocation unit and memory utilization Smaller allocation unit  larger bitmap Larger allocation unit  smaller bitmap On average, half of the last unit is wasted When bring a k unit process into memory Need find a hole of k units Search for k consecutive 0 bits in the entire map Size of allocation units: a few words ~ several kilobytes

Memory Management With Linked Lists Two types of entries: hole(H)/process(P) Address: 20 A B C D E P 5 H 5 3 P 8 6 P 14 4 H 18 2 P 20 6 P 26 3 H 29 3 X Length 6 List is kept sorted by address. Starts at 20 Process

Updating Linked Lists Combine holes if possible Not necessary for bitmap Before process X terminates After process X terminates A X B A B A X A X B B X

Allocate Memory for New Processes First fit: find the first hole fitting requirement Break the hole into two pieces: P + smaller H Next fit: start search from the place of last fit Empirical evidence: Slightly worse performance than first fit Best fit: take the smallest hole that is adequate Slower Generate tiny useless holes Worst fit: always take the largest hole A H A P H P 2 H 2 6 P 2 P 2 3 H 5 3 Next fit keeps track of where it is whenever it finds a suitable hole. The next time it is called to find a hole, it starts searching the list from the place where it left off last time. It does not always start from the beginning.

Using Distinct Lists Distinct lists for processes and holes List of holes can be sorted on size Best fit becomes faster Problem: how to free a process? Merging holes is very costly Quick fit: grouping holes based on size Different lists for different sizes E.g., List 1 for 4KB holes, List 2 for 8KB holes. How about a 5KB hole? Speed up the searching Merging holes is still costly

Outline Basic memory management Swapping Virtual memory Page replacement algorithms Modeling page replacement algorithms Design issues for paging systems Implementation issues Segmentation

Why Virtual Memory? If the program is too big to fit in memory … Split the program into pieces – overlays Swapping overlays in and out Problem: programmer does the work of splitting the program into pieces. Virtual memory: OS takes care of everything Size of program could be larger than the physical memory available. Keep the parts currently used in memory Put other parts on disk

Virtual and Physical Addresses Virtual addresses (VA) are used/generated by programs Each process has its own VA. E.g, MOV REG, 1000 ;1000 is VA Physical addresses (PA) are used in execution MMU: maps VA to PA Virtual addresses go to MMU. About the figure CPU sends virtual addresses to the MMU The MMU sends physical addresses to the memory Bus Memory Disk controller CPU package CPU MMU

Paging Virtual address space is divided into pages Memories are allocated in the unit of page Page frames in physical memory Pages and page frames are always the same size Usually, from 512B to 64KB #Pages > #Page frames On a 32-bit PC, VA could be as large as 4GB, but PA < 1GB In hardware, a present/absent bit keeps track of which pages are physically present in memory. Page fault: an unmapped page is requested OS picks up a little-used page frame and write its content back to hard disk Fetch the wanted page into the page frame just freed

physical address space Paging: An Example Virtual address space 60-64K X 56-6K 52-56K 48-52K 44-48K 7 40-44K 36-40K 5 32-36K 28-32K 24-28K 20-24K 3 16-20K 4 12-16K 8-12K 6 4-8K 1 0-4K 2 Pages Page 0: 0—4095 VA: 0 page 0 page frame 2 PA: 8192 0—4095 8192--12287 VA: 8192 page 2 page frame 6 PA: 24567 VA: 8199 page 2, offset 7 page frame 6, offset 7 PA: 24567+7=24574 VA:32789 page 8 unmapped page fault Page frames physical address space 28-32K 24-28K 20-24K 16-20K 12-16K 8-12K 4-8K 0-4K 8 2 1

The Magic in MMU An address  4-bit page number + 12 bit offset 24 = 16 pages 212 = 4096 bytes/page Page table: yielding the number of the page frame corresponding to a virtual page Replace the virtual page number by the physical page frame number

Page Table Map virtual pages onto page frames VA is split into page number and offset. Each page number has one entry in page table. Page table can be extremely large 32 bits virtual addresses, 4kb/page 1M pages. How about 64 bits VA? Each process needs its own page table

Typical Page Table Entry Entry size: usually 32 bits Page frame number: goal of page mapping Present/absent bit: page in memory? Protection: what kinds of access permitted Modified: Has the page been written? (If so, need to write back to disk later) Dirty bit Referenced: Has the page been referenced? Caching disable: read from the disk? Caching disabled Modified Present/absent Page frame number Referenced Protection

Fast Mapping Virtual to physical mapping must be fast several page table references/instruction Unacceptable to store the entire page table in main memory Have to seek for hardware solutions

Two Simple Designs for Page Table Use fast hardware registers for page table Single physical page table in MMU: an array of fast registers: one entry for each virtual page Requires no memory reference during mapping Load registers at every process switching Expensive if the page table is large Cost of hardware and overhead of context switching Put the whole table in main memory Only one register pointing to the start of table Fast switching Several memory references/instruction Pure memory solution is slow, pure register solution is expensive, so …

Translation Lookaside Buffers (TLBs) Observation: Most programs tend to make a large number of references to a small number of pages Put the heavily read fraction in registers TLB/associative memory check Page table TLB Virtual address found Not found Physical address

Outline Basic memory management Swapping Virtual memory Page replacement algorithms Modeling page replacement algorithms Design issues for paging systems Implementation issues Segmentation

Page Replacement When a page fault occurs, and all page frames are full Choose one page to remove, if modified (called dirty page), update its disk copy Better choose an unmodified page Better choose a rarely used page Many similar problems in computer systems Memory cache page replacement Web page cache replacement in web server Revisit: page table entry

Typical Page Table Entry Entry size: usually 32 bits Page frame number: goal of page mapping Present/absent bit: page in memory? Protection: what kinds of access permitted Modified: Has the page been written? (If so, need to write back to disk later) Dirty bit Referenced: Has the page been referenced? Caching disable: read from the disk? Caching disabled Modified Present/absent Page frame number Referenced Protection

Optimal Algorithm Label each page in the main memory with number of instructions will be executed before next reference E.g, a page labeled by “1” means this page will be referenced by the next instruction. Remove the page with highest label Put off page faults as long as possible Unrealizable! Why? SJF process scheduling, Banker’s Algorithm for deadlock avoidance Could be used as a benchmark

Remove Not Recently Used Pages R and M are initially 0 Set R when a page is referenced Set M when a page is modified Done by hardware Clear R bit periodically by software (OS) Four classes of pages when a page fault Class 0 (R0M0): not referenced, not modified Class 1 (R0M1): not referenced, modified Class 2 (R1M0): referenced, not modified Class 3 (R1M1): referenced, modified NRU removes a page at random from the lowest numbered nonempty class R bit can be cleared every clock interrupt Advantage of NRU Easy to understand, moderately efficient to implement, not optimal but adequate