Memory Management Operating Systems Lecture 4, April 3, 2003 Mr. Greg Vogl Uganda Martyrs University.

Slides:



Advertisements
Similar presentations
Chapters 7 & 8 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice,
Advertisements

Chapter 4 Memory Management Basic memory management Swapping
Part IV: Memory Management
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 3 Memory Management Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Fixed/Variable Partitioning
CMPT 300: Final Review Chapters 8 – Memory Management: Ch. 8, 9 Address spaces Logical (virtual): generated by the CPU Physical: seen by the memory.
OS Fall’02 Memory Management Operating Systems Fall 2002.
Virtual Memory Chapter 8.
Virtual Memory Chapter 8. Hardware and Control Structures Memory references are dynamically translated into physical addresses at run time –A process.
CS 104 Introduction to Computer Science and Graphics Problems
Memory Management 2010.
Virtual Memory Chapter 8.
1 Virtual Memory Chapter 8. 2 Hardware and Control Structures Memory references are dynamically translated into physical addresses at run time –A process.
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.
Memory Management Chapter 5.
Memory Management Five Requirements for Memory Management to satisfy: –Relocation Users generally don’t know where they will be placed in main memory May.
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.
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 9 – Real Memory Organization and Management Outline 9.1 Introduction 9.2Memory Organization.
03/05/2008CSCI 315 Operating Systems Design1 Memory Management Notice: The slides for this lecture have been largely based on those accompanying the textbook.
CS364 CH08 Operating System Support TECH Computer Science Operating System Overview Scheduling Memory Management Pentium II and PowerPC Memory Management.
Layers and Views of a Computer System Operating System Services Program creation Program execution Access to I/O devices Controlled access to files System.
Memory Management ◦ Operating Systems ◦ CS550. Paging and Segmentation  Non-contiguous memory allocation  Fragmentation is a serious problem with contiguous.
Chapter 3 Memory Management: Virtual Memory
Operating System Chapter 7. Memory Management Lynn Choi School of Electrical Engineering.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-7 Memory Management (1) Department of Computer Science and Software.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 8: Main Memory.
Operating Systems Chapter 8
MEMORY MANAGEMENT Presented By:- Lect. Puneet Gupta G.P.C.G. Patiala.
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.
Memory Management Techniques
Subject: Operating System.
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.
Virtual Memory Chapter 8. Hardware and Control Structures Memory references are dynamically translated into physical addresses at run time –A process.
Chapter 4 Memory Management Virtual Memory.
Memory. Chapter 8: Memory Management Background Swapping Contiguous Memory Allocation Paging Structure of the Page Table Segmentation.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
CS 241 Section Week #9 (11/05/09). Topics MP6 Overview Memory Management Virtual Memory Page Tables.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
Main Memory. Chapter 8: Memory Management Background Swapping Contiguous Memory Allocation Paging Structure of the Page Table Segmentation Example: The.
Virtual Memory Chapter 8. Hardware and Control Structures Memory references are dynamically translated into physical addresses at run time –A process.
Informationsteknologi Wednesday, October 3, 2007Computer Systems/Operating Systems - Class 121 Today’s class Memory management Virtual memory.
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.
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
Chapter 7 Memory Management Eighth Edition William Stallings Operating Systems: Internals and Design Principles.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 8: Main Memory.
Chapter 8: Memory Management. 8.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 8: Memory Management Background Swapping Contiguous.
Virtual Memory What if program is bigger than available memory?
Chapter 2 Memory and process management
UNIT–IV: Memory Management
Chapter 9 – Real Memory Organization and Management
Main Memory Management
Chapter 8: Main Memory.
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy 11/12/2018.
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.
So far… Text RO …. printf() RW link printf Linking, loading
Main Memory Background Swapping Contiguous Allocation Paging
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.
Operating System Chapter 7. Memory Management
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 9 4/5/2019.
COMP755 Advanced Operating Systems
Chapter 8 & 9 Main Memory and Virtual Memory
CSE 542: Operating Systems
Presentation transcript:

Memory Management Operating Systems Lecture 4, April 3, 2003 Mr. Greg Vogl Uganda Martyrs University

April 3, 2003 Operating Systems: Memory Management 2 Overview Fixed and Variable Partition Paging Segmentation Virtual Memory and Page Replacement Segmented Paging Protection and Sharing DOS and UNIX Memory Management

April 3, 2003 Operating Systems: Memory Management 3 Sources Ritchie Ch. 5-7 Burgess Solomon Part 6

April 3, 2003 Operating Systems: Memory Management 4 What is stored in memory? Operating system code and data User program code User program data Process Control Blocks Stack for executing subroutines Memory mapped I/O: device drivers Screen/display memory (Video RAM)

April 3, 2003 Operating Systems: Memory Management 5 Memory management goals/tasks Manage several processes at same time Load into memory, swap out to disk Run processes quickly, use available memory Protect most processes from each other But allow some processes to share memory Ease memory management for programmer Allocate memory in contiguous logical blocks Map logical addresses to physical addresses

April 3, 2003 Operating Systems: Memory Management 6 Fixed partition memory Each process gets fixed partition of memory Usually the OS is put at the bottom (address 0) Use different partition sizes Accommodates different possible process sizes Don’t let a process harm another’s memory Check that the addresses are in its partition  Every partition has unused (wasted) space  Not enough space for big new processes?

April 3, 2003 Operating Systems: Memory Management 7 Variable partition memory Allocate the memory each process needs Free the space of a terminated process Put new processes in empty “holes” Adjacent holes can be merged About 2x as many processes as holes  Holes maybe not right size for new processes How to choose a hole to put in a new process?

April 3, 2003 Operating Systems: Memory Management 8 Storage placement policies Best fit Put new process in smallest possible hole Remaining hole is as small as possible Worst fit Put new process in largest possible hole Reduces number of big holes, creates few small ones First fit (or next fit) Put new process in first (or next) hole big enough to fit No overhead of finding min/max hole sizes Which is best? Tradeoff: speed vs. space

April 3, 2003 Operating Systems: Memory Management 9 Memory implementation Functions to allocate, deallocate, reallocate UNIX uses malloc(), free(), realloc() C++ uses new and delete operators Lists keep track of allocated and free blocks Keep lists of various-sized holes (powers of 2)

April 3, 2003 Operating Systems: Memory Management 10 Fragmentation Gaps of unused storage space Occurs in all storage devices (memory, disk)  Wastes space, may also reduce performance Internal fragmentation Unused space within a process or block Can occur if word size > smallest data size External fragmentation Unused space between processes or blocks

April 3, 2003 Operating Systems: Memory Management 11 Compaction (defragmentation) Join together used memory; combine holes Move allocated blocks to an end of memory Calculate distance the block will move Add to pointers, then move the data  Need to move a lot of things in memory  Need to find and move all pointers  Need to suspend processes until done  Cannot use in time-critical systems

April 3, 2003 Operating Systems: Memory Management 12 When/how often to compact? When any process terminates When there is no more free memory At fixed intervals At user(s) request

April 3, 2003 Operating Systems: Memory Management 13 Coalescing holes Don’t coalesce Give entire hole (maybe used later by realloc) Buddy system Combined buddies align in powers of 2 When (de)allocating do the buddy block too

April 3, 2003 Operating Systems: Memory Management 14 Garbage Collection Find inaccessible blocks, add to free list Conservative Treat pointer-like memory addresses as pointers  Not all garbage found Reference count Each block stores a count of pointers to itself When a block’s count is 0, free the block  Does not detect circular lists of garbage

April 3, 2003 Operating Systems: Memory Management 15 Mark and sweep Algorithm Mark used blocks using depth-first search Sweep (free) unused blocks and compact Disadvantages  Not helpful if memory is almost full  Must load many swapped pages into memory

April 3, 2003 Operating Systems: Memory Management 16 Generational Divide memory into spaces Objects are usually short- or long-lived Keep long-lived objects in their own spaces Clean out mostly empty spaces Copy objects to other spaces when accessed

April 3, 2003 Operating Systems: Memory Management 17 Paging Each process composed of fixed-size pages Memory is also divided into pages (frames) Process pages can go anywhere in memory No external fragmentation  Internal fragmentation ~ page size (a process will not usually use all of each page)  Need to keep track of page locations

April 3, 2003 Operating Systems: Memory Management 18 Implementing paging Logical address = page number + displacement E.g. 32 bit addr. = 20 bit page no bit disp. 4 GB of addresses: a million pages, 4 KB each Page table translates page no. to frame no. Implemented as array of memory page numbers Page address + displacement  physical address

April 3, 2003 Operating Systems: Memory Management 19 Segmentation Each process has variable length pieces Segment size determined by programmer Each subroutine or data takes one or more Reflects logical/modular process structure No internal fragmentation Improved performance (locality of reference)  External fragmentation

April 3, 2003 Operating Systems: Memory Management 20 Implementing segmentation Logical address = segment reference + displacement Process segment table is like page table Each segment entry has base address and length Base address + displacement  physical address  If displacement > length, segmentation fault!

April 3, 2003 Operating Systems: Memory Management 21 Virtual memory

April 3, 2003 Operating Systems: Memory Management 22 Virtual memory Process loaded in separate parts Dynamic address translation at run time Not need all of process in memory to run it Only currently accessed code & data pages Rest of process stays in secondary storage Windows reserves a swap file (win386.swp) UNIX/Linux often uses a swap partition

April 3, 2003 Operating Systems: Memory Management 23 Virtual memory benefits More processes keeping processor busy Memory and disk space more fully used Few pages of a process needed at one time Modular programs have locality of reference Virtual memory > real memory A process memory can be > real memory Programmer not limited by real memory

April 3, 2003 Operating Systems: Memory Management 24 Virtual memory using paging Resident set = a process’s pages in memory Demand paging: only load pages when needed When a required page is not in memory A page fault generates an interrupt to request it If no free page frames, replace an existing one Separate page table for each process Maps page numbers to frame numbers Page table register points to process page table

April 3, 2003 Operating Systems: Memory Management 25 Virtual memory costs  Complexity, hardware and OS support  Page table takes a lot of space Must itself be stored in virtual memory  Overhead of swapping is large  Too many page faults can cause thrashing Find optimum number of active processes Resident sets proportional to process sizes

April 3, 2003 Operating Systems: Memory Management 26 Page size How large should pages be? Tradeoffs: If too small, page table is too big If too large, internal fragmentation is too big Must be a power of 2 for easy addressing Pages in most systems are 2 or 4 KB

April 3, 2003 Operating Systems: Memory Management 27 Page replacement policies A page is removed from memory, replaced Present bit: 1 if in real memory Modified bit: 1 if page is modified (“dirty”) write to secondary storage before replacing Optimal policy can be known in retrospect If performance near optimal, good enough Policies: LRU, NRU, FIFO, Clock

April 3, 2003 Operating Systems: Memory Management 28 Least recently used (LRU) Replace page not referenced the longest Frame is given time stamp when referenced  Overhead for time stamp and finding oldest  Linked list would also have big overhead

April 3, 2003 Operating Systems: Memory Management 29 Not recently used (NRU) “page referenced” bit All bits are set to 0 periodically Bit is set to 1 when page is used Any page with 0 can be replaced

April 3, 2003 Operating Systems: Memory Management 30 First-in first-out (FIFO) Remove page in memory longest Easy to implement using linked list queue  Bad performance: evicts heavily used pages

April 3, 2003 Operating Systems: Memory Management 31 Clock or second chance Circular linked list similar to queue Add used bit, set to 0 when loaded Set used to 1 when referenced Use pointer to head of list When replacement needed, look for a 0 Set any 1s to 0 Same as FIFO but leave recently used pages

April 3, 2003 Operating Systems: Memory Management 32 Translation lookaside buffer A memory cache for page table entries Hardware buffer in fast storage If not in buffer, look in page table as usual Holds both virtual and real page numbers Associative lookaside buffer often also used Maps virtual page no. to real page frame no.

April 3, 2003 Operating Systems: Memory Management 33 Virtual memory using segments Facilitates use of dynamic memory Segments can grow or be relocated Facilitates process sharing of code and data Logical structure ~ physical structure Reinforces locality principle, good performance

April 3, 2003 Operating Systems: Memory Management 34 Implementing virtual segments Virtual address = segment number + displacement Seg. table register points to current process Segment descriptor (table entries) include Segment base address Segment size (limit) to check for address errors Bits: in memory, used, rwx access protection

April 3, 2003 Operating Systems: Memory Management 35 Paged segmented memory Used in many modern operating systems Each segment has whole number of pages Logical pages mapped to physical pages Programmer works with segments Operating system manages pages Virtual address = segment no. + page no. + displacement One segment table per process, s.t. register One page table per segment

April 3, 2003 Operating Systems: Memory Management 36 Paged segmented memory

April 3, 2003 Operating Systems: Memory Management 37 Sharing Threads share process info. (PCB, code) Shared libraries e.g. dlls in Windows, stdio in C Segments shared by processes

April 3, 2003 Operating Systems: Memory Management 38 Memory Protection Protection violations that produce errors: address < base address address > limit register + base address displacement > page/segment size page/segment no. > no. of pages/segments read/write/execute segment not permitted

April 3, 2003 Operating Systems: Memory Management 39 MS-DOS Designed for Intel CPUs w/ 16-bit registers DOS limited at first to 64 KB, then 1 MB 16 bit addressing, left shifted 4 bits Processes have at least 4 64-KB segments segment registers: code, data, stack, extra Process switching is possible One awakens, the other goes to sleep First-fit to find free memory for each segment

April 3, 2003 Operating Systems: Memory Management 40 MS-DOS memory map Early DOS memory had fixed uses KB DOS files, device drivers, user program(s) 640 KB-1 MB Video RAM, ROM BIOS 1 MB-1MB + 64 KB High Memory Area: parts of OS

April 3, 2003 Operating Systems: Memory Management 41 Overlays Used to divide up a large program Process root module is always loaded Infrequently used routines put in overlays Separate modules use same memory area Only one can be loaded at a time

April 3, 2003 Operating Systems: Memory Management 42 Extended/expanded memory How can DOS access > 1 MB memory? Extended memory: above 1 MB Expanded Memory System (EMS): use memory board, expanded memory manager 1 MB has KB page frames Up to 32 MB of additional 16-KB pages Address references redirected above 1MB

April 3, 2003 Operating Systems: Memory Management 43 Windows 3.1 Modes Real: Intel 8086, 640 KB Standard: 286, up to 16 MB, task switching Enhanced: 386 virtual memory, multitasking 16-bit segmented addressing (like DOS) Win16 API DLLs used by applications and Windows

April 3, 2003 Operating Systems: Memory Management 44 Windows 95, NT 32 bit memory, 4 GB total address space not segmented 2 GB process memory 2 GB system memory paged, non-paged, physical addressing 64-bit processors and OS are now in use Win32 API Win32s has same interface but uses 16 bit code

April 3, 2003 Operating Systems: Memory Management 45 UNIX A process has three segments Text (executable code) Data (initialised, uninitialised) Stack (local procedure data and parameters) Processes can share segments (text, data)

April 3, 2003 Operating Systems: Memory Management 46 UNIX virtual memory pages (typically 4 KB) Page daemon counts number of free frames If too few, remove pages using clock If many page faults, remove LRU processes Reload processes swapped out a long time