Operating Systems Principles Memory Management Lecture 7: Physical Memory 主講人:虞台文.

Slides:



Advertisements
Similar presentations
Memory.
Advertisements

Part IV: Memory Management
CSS430 Memory Management Textbook Ch8
Memory Management Chapter 7.
Memory Management Chapter 7. Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated efficiently to pack as.
7. Physical Memory 7.1 Preparing a Program for Execution
Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable supply of ready processes to.
Allocating Memory.
Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic, N.Z. ©2009, Prentice.
CS 311 – Lecture 21 Outline Memory management in UNIX
OS Fall’02 Memory Management Operating Systems Fall 2002.
1 CSE 380 Computer Operating Systems Instructor: Insup Lee University of Pennsylvania, Fall 2002 Lecture Note: Memory Management.
Chapter 7 Memory Management
CS 104 Introduction to Computer Science and Graphics Problems
Memory Management.
Memory Management Chapter 7 B.Ramamurthy. Memory Management Subdividing memory to accommodate multiple processes Memory needs to allocated efficiently.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
Memory Management Chapter 5.
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.
03/05/2008CSCI 315 Operating Systems Design1 Memory Management Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Chapter 7 Memory Management
1 Lecture 8: Memory Mangement Operating System I Spring 2008.
Chapter 91 Memory Management Chapter 9   Review of process from source to executable (linking, loading, addressing)   General discussion of memory.
Chapter 7 Physical Memory Preparing a program for execution Memory partitioning schemes Allocation strategies for variable partitions Managing insufficient.
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.
Memory Management Chapter 7.
Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes.
Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Dr.
Chapter 7 Memory Management
ICS 145B -- L. Bic1 Project: Main Memory Management Textbook: pages ICS 145B L. Bic.
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 Chapter 7.
Memory Management. Roadmap Basic requirements of Memory Management Memory Partitioning Basic blocks of memory management –Paging –Segmentation.
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.
Subject: Operating System.
Silberschatz and Galvin  Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous.
CE Operating Systems Lecture 14 Memory management.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
Operating Systems Principles Memory Management Lecture 7: Physical Memory 主講人:虞台文.
Memory Management. Introduction To improve both the utilization of the CPU and the speed of its response to users, the computer must keep several processes.
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.
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.
Chapter 7 Memory Management Eighth Edition William Stallings Operating Systems: Internals and Design Principles.
1 Memory Management n In most schemes, the kernel occupies some fixed portion of main memory and the rest is shared by multiple processes.
2010INT Operating Systems, School of Information Technology, Griffith University – Gold Coast Copyright © William Stallings /2 Memory Management.
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.
Chapter 7: Main Memory CS 170, Fall Program Execution & Memory Management Program execution Swapping Contiguous Memory Allocation Paging Structure.
Main Memory CSSE 332 Operating Systems Rose-Hulman Institute of Technology.
MEMORY MANAGEMENT. memory management  In a multiprogramming system, in order to share the processor, a number of processes must be kept in memory. 
CompSci 143A1 Part II: Memory Management Chapter 7: Physical Memory Chapter 8: Virtual Memory Chapter 9: Sharing Data and Code in Main Memory Spring, 2013.
Chapter 7 Memory Management
Memory Management Chapter 7.
Chapter 2 Memory and process management
Chapter 8 Main Memory.
Main Memory Management
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.
Main Memory Background Swapping Contiguous Allocation Paging
Lecture 3: Main Memory.
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 9 4/5/2019.
Chapter 7 Memory Management
Presentation transcript:

Operating Systems Principles Memory Management Lecture 7: Physical Memory 主講人:虞台文

Content Preparing a Program for Execution – Program Transformations – Logical-to-Physical Address Binding Memory Partitioning Schemes – Fixed Partitions – Variable Partitions Allocation Strategies for Variable Partitions Dealing with Insufficient Memory

Operating Systems Principles Memory Management Lecture 7: Physical Memory Preparing a Program for Execution

Modules reflecting different functions are designed separately, possibly by different programmers.

Preparing a Program for Execution Compilation External Symbol Table Compilation will produce an object module and a corresponding external symbol table.

Preparing a Program for Execution Compilation

Preparing a Program for Execution The linker combines several object modules together to build a load module (EXE) by resolving external references through symbol tables.

Preparing a Program for Execution Logical-to-physical address mapping is done by the loader to transfer the load module from the secondary storage to the main memory.

More on Linking Object Module 1 Object Module 2 Object Module n Logical Address Space Object Module 1 Object Module 1 Object Module 2 Object Module 2 Object Module n Object Module n Linking FFFFFFF

More on Linking Object Module 1 Object Module 2 Object Module n Logical Address Space Object Module 1 Object Module 1 Object Module 2 Object Module 2 Object Module n Object Module n Linking Load Module Logical address space is used by the linker to resolve external references. Binding can be static or dynamic FFFFFFF

Logical-to-Physical Address Binding Load Module 1 Load Module 1 Physical Address Space Load Module 2 Load Module 2 Load Module 3 Load Module 3 Load Module 3 ’ Load Module 2 ’ Load Module 1 ’ Created by linker (on logical address space) aaaaaaaa bbbbbbbb cccccccc Assignment of actual physical addresses to program instruction and data. Program relocation needed (done by the loader)

Address Binding Static binding – Programming time – Compilation time – Linking time – Loading time Dynamic binding – Execution time Assignment of actual physical addresses to program instruction and data.

Static Binding Programming-time Binding – Seldom used – Used in low-level environments – E.g., OS, real-time and embedded systems, control special hardware component. Programming time Compilation time Linking time Loading time

Static Binding SAMPLE PROGRAM FOR MC6802 USING CRS8 The following source file has been named MC6802.ASM CPU6802; 6802 processor HOFMOT ; Motorola Records ORG0100H ; Start of Data Source:DFB'Hello and Welcome' Length:EQU$ - Source ;Length of Source Destin:DFSLength; Buffer which has same ; length as Source ORG0120H; Start of Code Entry:LDX#Source; Point Index Reg to ; Source string LDAB#Length; Number of characters to move Loop:LDAA0,X STAALength,X INX DECB BNELoop Fin:JMPFin ENDEntry SAMPLE PROGRAM FOR MC6802 USING CRS8 The following source file has been named MC6802.ASM CPU6802; 6802 processor HOFMOT ; Motorola Records ORG0100H ; Start of Data Source:DFB'Hello and Welcome' Length:EQU$ - Source ;Length of Source Destin:DFSLength; Buffer which has same ; length as Source ORG0120H; Start of Code Entry:LDX#Source; Point Index Reg to ; Source string LDAB#Length; Number of characters to move Loop:LDAA0,X STAALength,X INX DECB BNELoop Fin:JMPFin ENDEntry Programming-time binding Programming time Compilation time Linking time Loading time

Static Binding Programming time Compilation time Linking time Loading time Compile-time Binding – The compiler is given the starting address for the program to be load for execution. – The resulting program can execute only when loaded into the specific preassigned memory space. – Not relocatable – Rarely used

Static Binding Programming time Compilation time Linking time Loading time Compile-time Binding – Named variables have their addresses hardcoded Global variables given offset from start of global data area Local variables given offset from top of stack Object variables given offset from start of object data – Normal function and method calls are hardcoded Normal functions have specific starting address in object file Object methods have specific starting address in object file

Static Binding Programming time Compilation time Linking time Loading time Link-time Binding – The load module is still not relocatable – The program will be loaded to memory with starting location specified or assumed by the linker (Linkage Editor). – Linking loader: combining linking and loading – Widely used in smaller, single user systems (e.g., CP/M)

Load Module Load Module Static Binding Programming time Compilation time Linking time Loading time Object program(s) Object program(s) Linkage editor Library Simple Loader Memory Object program(s) Object program(s) Linking loader Memory Library

Load Module Load Module Static Binding Programming time Compilation time Linking time Loading time Object program(s) Object program(s) Linkage editor Library Simple Loader Memory Object program(s) Object program(s) Linking loader Memory Library

Static Binding Programming time Compilation time Linking time Loading time Load-Time Binding – The load module is relocatable – What operands are needed to be relocated (relocatable)? Indicated by compilers and assemblers Needed for linking and loading – How to perform relocation? Load Module Load Module Object program(s) Object program(s) Linkage editor Library Loader Memory relocatable What? How? What?

Static Binding Programming time Compilation time Linking time Loading time What operands are needed to be relocated? – Register? – Immediate operand (constant)? – Offset to a base register? – Absolute memory address – Relative memory address? Load Module Load Module Object program(s) Object program(s) Linkage editor Library Loader Memory relocatable What? How? What?

Static Binding Programming time Compilation time Linking time Loading time What operands are needed to be relocated? – Register? – Immediate operand (constant)? – Offset to a base register? – Absolute memory address – Relative memory address? Load Module Load Module Object program(s) Object program(s) Linkage editor Library Loader Memory relocatable What? How? What? Keep unchanged Ralocatable

Static Binding Programming time Compilation time Linking time Loading time How to relocate the relocatable operands? Load Module Load Module Object program(s) Object program(s) Linkage editor Library Loader Memory relocatable What? How? What? Logical Address Space Physical Address Space Load Module Load Module’ x y

Static Binding How to relocate the relocatable operands? Load Module Load Module Object program(s) Object program(s) Linkage editor Library Loader Memory relocatable What? How? What? Logical Address Space Physical Address Space Load Module Load Module’ x y Add all relocatable operands in the load module by the amount of y  x. Programming time Compilation time Linking time Loading time

Example... int i;... i = ???;... g();... int i;... i = ???;... g();... Logical Address Space 0 20 i store 20 call g f() {... } g() {... } f() {... } g() {... } Logical Address Space Source Module 1 Source Module function f function g compiler

Example... int i;... i = ???;... g();... int i;... i = ???;... g();... Logical Address Space 0 20 i store 20 call g f() {... } g() {... } f() {... } g() {... } Logical Address Space Source Module 1 Source Module function f function g compiler Relative memory address (relocatable) Relative memory address (relocatable) External reference

Example... int i;... i = ???;... g();... int i;... i = ???;... g();... Logical Address Space 0 20 i store 20 call g f() {... } g() {... } f() {... } g() {... } Logical Address Space Source Module 1 Source Module function f function g compiler Object Module 1 (obj1) Object Module 2 (obj2)

Example 0 20 i store 20 call g function f function f function g function g Obj1 Obj2 0 Others Obj3 Link obj3+obj2+obj1, myexe Logical Address Space 0 Others function f function g i store 370 call 250 linker

Example 0 20 i store 20 call g function f function f function g function g Obj1 Obj2 0 Others Obj3 Link obj3+obj2+obj1, myexe Logical Address Space 0 Others function f function g i store 370 call 250 relocated External reference resolved

Example 0 20 i store 20 call g function f function f function g function g Obj1 Obj2 0 Others Obj3 Logical Address Space 0 Others function f function g i store 370 call 250 Load Module ( myexe ) Link obj3+obj2+obj1, myexe

Example 0 Others function f function g i store 370 call 250 myexe Physical Address Space 1000 Others function f function g i store 1370 call 1250 loader 1000 relocated

Dynamic Binding Dynamic Binding = Binding at Execution Time That is, binding immediately before each memory reference. Hardware support needed for logical-to-physical address mapping Physical address differs from logical address usually by a constant offset.

Dynamic Binding Physical address differs from logical address usually by a constant offset.

Example

Operating Systems Principles Memory Management Lecture 7: Physical Memory Memory Partitioning Schemes

Fixed Partitions – The number of partitions and the size of each partition are determined at the time the OS is initialized. Variable Partitions – Memory not partitioned a priori – Partitioning on demand

Fixed Partitions Single-program systems – 2 partitions (OS/user) Multi-programmed systems – partitions of different sizes OS User OS

How to Assign Processes to Partitions?

FIFO for each partition – Typically, best-fit is used for queue assignment. – Problem: Some partitions may be unused if no processes of appropriate sizes are available. – To resolve the problem, more complex queue management scheme and process scheduling scheme would be required.

How to Assign Processes to Partitions? Single FIFO – More complex, but more flexible. – E.g., rather than leaving a partition empty, the scheduler may assigned a process to a partition not in best-fit sense. – E.g., the actual memory requirement of a process may grow and shrink dynamically.

OS Limitations of Fixed Partitions Program size limited to largest partition Internal fragmentation – unused space within partitions

Variable Partitions Memory not partitioned a priori Each request is allocated portion of free space. Allocating and releasing memory dynamically cause external fragmentation.

Issues How to allocate memories of sizes All free blocks are applicable. None free block is applicable. But, the total amount of free memory is large enough. Allocate which one? Over time, memory will consist a sequence of variable size blocks. Some are free, and some are not.

Hole Coalescing A B D E C F A B D E C F A B D E C F B’B’ A D E F B’B’ B ’’ GGGG A E F G B ’’’ Adjacent holes must be coalesced to prevent increasing fragmentation.

Hole Coalescing Adjacent holes must be coalesced to prevent increasing fragmentation. Four cases on hole coalescing: no adjacent holeA hole at right A hole at leftHoles at two sides

Link List Implementation (I) Free blocks are kept sorted using a doubly linked list. What would be done to release a block of memory? – Check both its neighbors for possible coalescing. Problems: – How to check the right neighbor? – How to check the left neighbor? G A B C D E F free occupied size easy checked by starting from the header (inefficient)

E Link List Implementation (II) Free blocks are linked using a doubly linked list – need not be sorted What would be done to release a block of memory? – Check both its neighbors for possible coalescing. Problems: – How to check the right neighbor? – How to check the left neighbor? AB freeoccupiedsize easy freesizeoccupiedsize C occupiedsize occupiedsize D occupiedsize occupiedsizefreesize easy

Bitmap Implementation Memory divided into fix-size blocks Each block represented by a 0/1 bit in a binary string: the “bitmap” Can be implemented as char or int array Operations use bit masks – Release: B[i] = B[i] & ' ' – Allocate: B[i] = B[i] | ' ' – Search: Repeatedly, Check left-most bit and Shift mask right: TEST = B[i] & ' '

Bitmap Implementation 4K K a b c d e f000 4K K a b c d e f000 4K K a b c d e f000                                                                                

Bitmap Implementation 4K K a b c d e f000 4K K a b c d e f000 4K K a b c d e f000                                                                                 4K How to allocate a block of continuous memory of a given size?

Buddy Systems Compromise between fixed and variable partitions Fixed number of possible hole sizes; typically, 2 i. – Each hole of size 2 i can be divided (equally) into 2 buddies of size 2 i  1. – Two buddies can be combined into the original hole of size 2 i. Track holes by size on separate lists – List entries for holes of sizes 2 0, 2 1, …, 2 m. 2i2i 2i12i1 2i12i1

Buddy Systems Compromise between fixed and variable partitions Fixed number of possible hole sizes; typically, 2 i. – Each hole of size 2 i can be divided (equally) into 2 buddies of size 2 i  1. – Two buddies can be combined into the original hole of size 2 i. Track holes by size on separate lists – List entries for holes of sizes 2 0, 2 1, …, 2 m m2m

Buddy Systems When n bytes requested, find smallest i so that n  2 i – If hole of this size available, allocate it; – otherwise, consider larger holes. Recursively split each hole into two buddies until smallest adequate hole is created Allocate it and place other holes on appropriate lists On release, recursively coalesce buddies – Buddy searching for coalescing can be inefficient

Example void p=malloc(1*blocksize); free(12*blocksize); Hole Sizes: 1, 2, 4, 8, 16 3 blocks allocated & 3 holes left

Operating Systems Principles Memory Management Lecture 7: Physical Memory Allocation Strategies for Variable Partitions

Allocation Strategies Problem: Given a request for n bytes, find hole ≥ n Goal: – Maximize memory utilization (Minimize “external fragmentation”) – Minimize search time

Common Allocation Strategies First-fit: – Always start from the beginning of free-list – Simplest – Generally the best choice Next-fit: – Rotating-first-fit (Resume search) – Improves distribution of holes Best-fit: – Closest fit – Avoid breaking up large holes. Worst-fit: – Largest fit – Avoid leaving tiny hole fragments

Measure of Memory Fragmentation What measurement could be used?

Measure of Memory Fragmentation What measurement could be used? m : #blocks occupied n : #holes

Measure of Memory Fragmentation What measurement could be used? m : #blocks occupied n : #holes

Measure of Memory Fragmentation What measurement could be used? m : #blocks occupied n : #holes Four types of occupied memory blocks. Increase one hole on release. #holes unchanged on release Decrease one hole on release.

Measure of Memory Fragmentation What measurement could be used? m : #blocks occupied n : #holes Increase one hole on release. #holes unchanged on release Decrease one hole on release. Let a, b, c, d be the average number of occupied blocks of each type in memory during equilibrium. Fact: b = c

Measure of Memory Fragmentation Increase one hole on release. #holes unchanged on release Decrease one hole on release.

Measure of Memory Fragmentation Increase one hole on release. #holes unchanged on release Decrease one hole on release. p : The probability that the request memory size doesn’t match any hole size. Fact: p  1. p : The probability that the request memory size doesn’t match any hole size. Fact: p  1.

Measure of Memory Fragmentation In equilibrium,

Measure of Memory Fragmentation In equilibrium, 50% rule (Knuth 1968) In Equilibrium, of the total number of occupied blocks and holes, 1/3 are holes.

Measure of Memory Utilization In equilibrium, 50% rule (Knuth 1968) In Equilibrium, of the total number of occupied blocks and holes, 1/3 are holes. How much memory is wasted? (average) hole_size is not the same as (average) block_size

Measure of Memory Utilization Assume b : average block size h : average hole size

Measure of Memory Utilization Assume b : average block size h : average hole size 50% rule (Knuth 1968)

Measure of Memory Utilization Assume b : average block size h : average hole size We want to minimize the value of f. It is equivalent to minimize k. How to know the value of k ?

Measure of Memory Utilization Assume b : average block size h : average hole size How to know the value of k ? Simulations by Knuth (1968) provide an answer. k depends on the ratio of b/M, e.g., b  M/10, k = 0.22  f  0.1 b  M/3, k = 2  f  0.5 Simulations by Knuth (1968) provide an answer. k depends on the ratio of b/M, e.g., b  M/10, k = 0.22  f  0.1 b  M/3, k = 2  f  0.5

Measure of Memory Utilization Assume b : average block size h : average hole size Conclusion: M must be large relative to b, otherwise much of main memory will remain unused.

Operating Systems Principles Memory Management Lecture 7: Physical Memory Dealing with Insufficient Memory

Managing Insufficient Memory What to be done? – when the total size of free memory is large enough while the memory requirement of a process exceeds the size of the largest partition – when new process arrives with insufficient free memory? – When the memory requirement is larger than the physical memory size.

Dealing with Insufficient Memory Memory compaction – How much and what to move? Swapping – Temporarily move process to disk – Requires dynamic relocation Overlays – Allow programs larger than physical memory – Programs loaded as needed according to calling structure.

Memory Compaction Request for a memory block of size 10. Initial Complete Compaction Partial Compaction Minimal data Movement

Swapping Swap in/out More efficient than memory compaction. – Memory compaction may fail to create a large enough hole. – Effect only small number of processes each time, thus requiring fewer memory accesses. – Swapping can be overlapped with process execution. – Swapping can be used with both fixed and variable size partition, whereas memory compaction is applicable only for variable size partition. What are needed to be swapped out? Code? Data?

Overlaps Allow programs large than physical memory Programs loaded as needed, according to calling structure. A BC DE A k1k1 B

Overlaps A BC DE A k1k1 B C k2k2 D Allow programs large than physical memory Programs loaded as needed, according to calling structure.

Overlaps A BC D E A k1k1 B C k2k2 D E Allow programs large than physical memory Programs loaded as needed, according to calling structure.

Overlaps A BC D E A k1k1 B A C k2k2 D A B C E Allow programs large than physical memory Programs loaded as needed, according to calling structure.