900 likes | 1.05k Views
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
E N D
Operating Systems PrinciplesMemory ManagementLecture 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 • Memory Hierarchy
Operating Systems PrinciplesMemory ManagementLecture 7: Physical Memory Preparing a Program for Execution
Preparing a Program for Execution Modules reflecting different functions are designed separately, possibly by different programmers.
Compilation will produce an object module and a corresponding external symbol table. Preparing a Program for Execution External Symbol Table Compilation
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 00000000 Object Module 1 Object Module 1 Object Module 2 Object Module n Object Module 2 Linking Object Module n FFFFFFF Logical Address Space
More on Linking 00000000 • Logical address space is used by the linker to resolve external references. • Binding can be static or dynamic. Object Module 1 Load Module Object Module 1 Object Module 2 Object Module n Object Module 2 Linking Object Module n FFFFFFF Logical Address Space
Assignment of actualphysical addresses to program instruction and data. Logical-to-Physical Address Binding Created by linker (on logical address space) 00000000 00000000 aaaaaaaa Load Module 1 Load Module 2 Load Module 3 Load Module 3’ bbbbbbbb Load Module 1’ cccccccc Load Module 2’ Program relocation needed (done by the loader) Physical Address Space
Assignment of actualphysical addresses to program instruction and data. Address Binding • Static binding • Programming time • Compilation time • Linking time • Loading time • Dynamic binding • Execution time
Programming time • Compilation time • Linking time • Loading time 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 CPU 6802 ; 6802 processor HOF MOT ; Motorola Records ORG0100H ; Start of Data Source: DFB 'Hello and Welcome' Length: EQU $ - Source ;Length of Source Destin: DFS Length ; 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: LDAA 0,X STAA Length,X INX DECB BNE Loop Fin: JMP Fin END Entry • Programming-time binding
Programming time • Compilation time • Linking time • Loading time Static Binding • 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
Programming time • Compilation time • Linking time • Loading time Static Binding • 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
Programming time • Compilation time • Linking time • Loading time Static Binding • Link-time Binding • The load module is still notrelocatable • 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 • Programming time • Compilation time • Linking time • Loading time Static Binding Object program(s) Object program(s) Library Library Linking loader Linkage editor Memory Simple Loader Memory
Load Module • Programming time • Compilation time • Linking time • Loading time Static Binding Object program(s) Object program(s) Library Library Linking loader Linkage editor Memory Simple Loader Memory
Load Module • Programming time • Compilation time • Linking time • Loading time Static Binding What? • 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? Object program(s) Library Linkage editor relocatable What? How? Loader Memory
Load Module • Programming time • Compilation time • Linking time • Loading time Static Binding What? • What operands are needed to be relocated? • Register? • Immediate operand (constant)? • Offset to a base register? • Absolute memory address • Relative memory address? Object program(s) Library Linkage editor relocatable What? How? Loader Memory
Load Module • Programming time • Compilation time • Linking time • Loading time Static Binding What? • What operands are needed to be relocated? • Register? • Immediate operand (constant)? • Offset to a base register? • Absolute memory address • Relative memory address? Object program(s) Library Linkage editor Keep unchanged relocatable What? How? Loader Ralocatable Memory
x y Load Module • Programming time • Compilation time • Linking time • Loading time Static Binding What? • How to relocate the relocatableoperands? Object program(s) Library Linkage editor Load Module Load Module’ relocatable What? How? Loader Logical Address Space Physical Address Space Memory
x y Load Module • Programming time • Compilation time • Linking time • Loading time Static Binding What? • How to relocate the relocatableoperands? Object program(s) Add all relocatable operands in the load module by the amount ofy x. Library Linkage editor Load Module Load Module’ relocatable What? How? Loader Logical Address Space Physical Address Space Memory
0 0 function f 20 i 200 function g store 20 call g compiler compiler Example f() { . . . } g() { . . . } . . . int i; . . . i = ???; . . . g(); . . . Source Module 1 Source Module 2 Logical Address Space Logical Address Space
0 0 function f 20 i 200 function g store 20 call g compiler compiler Example Relative memory address (relocatable) f() { . . . } g() { . . . } . . . int i; . . . i = ???; . . . g(); . . . External reference Source Module 1 Source Module 2 Logical Address Space Logical Address Space
0 0 function f 20 i 200 function g store 20 call g compiler compiler Example Object Module 1 (obj1) Object Module 2 (obj2) f() { . . . } g() { . . . } . . . int i; . . . i = ???; . . . g(); . . . Source Module 1 Source Module 2 Logical Address Space Logical Address Space
0 Others 0 0 function f 0 20 50 Others function f i i 200 function g store 20 250 function g call g 350 370 store 370 linker call 250 Link obj3+obj2+obj1, myexe Example Obj1 Obj2 Obj3 Logical Address Space
0 Others 0 0 function f 0 20 50 Others function f i i 200 function g store 20 250 function g call g 350 370 store 370 call 250 Link obj3+obj2+obj1, myexe Example Obj1 Obj2 Obj3 relocated External reference resolved Logical Address Space
0 Others 0 0 function f 0 20 50 Others function f i i 200 function g store 20 250 function g call g 350 370 store 370 call 250 Link obj3+obj2+obj1, myexe Example Obj1 Obj2 Obj3 Load Module (myexe) Logical Address Space
0 Others Others 50 function f i i 1050 function f 250 function g 1250 loader function g 350 370 1350 1370 store 370 store 1370 call 250 call 1250 Example 1000 1000 relocated Physical Address Space myexe
Physical address differs from logical address usually by a constant offset. Dynamic Binding • Dynamic Binding = Binding at Execution Time • That is, binding immediatelybefore 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
Operating Systems PrinciplesMemory ManagementLecture 7: Physical Memory Memory Partitioning Schemes
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
OS User OS . . . Fixed Partitions • Single-program systems • 2 partitions (OS/user) • Multi-programmed systems • partitions of different sizes
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 complexqueue 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.
Limitations of Fixed Partitions • Program size limited to largest partition • Internal fragmentation • unused space within partitions OS
Variable Partitions • Memory not partitioned a priori • Each request is allocated portion of free space. • Allocating and releasing memory dynamically cause externalfragmentation.
Issues Over time, memory will consist a sequence of variable size blocks. Some are free, and some are not. • How to allocate memories of sizes All free blocks are applicable. Allocate which one? None free block is applicable. But, the total amount of free memory is large enough.
Adjacent holes must be coalesced to prevent increasingfragmentation. Hole Coalescing A A A A A B B B B’ B’’ B’ B’’ B’’’ C C C D D D D E E E E E F F F F F G G G G G
Adjacent holes must be coalesced to prevent increasingfragmentation. Hole Coalescing Four cases on hole coalescing: no adjacent hole A hole at right A hole at left Holes at two sides
easy checked by starting from the header (inefficient) 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? A B C D E F G free size occupied size occupied size occupied size free size occupied size free size
easy easy 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? E A B C D free size size free occupied size size occupied occupied size occupied size size occupied size occupied free size
Bitmap Implementation Allocated Free • 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] & '11011111' • Allocate:B[i] = B[i] | '11000000' • Search: Repeatedly, Check left-most bit andShift mask right:TEST = B[i] & '10000000'
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] & '11011111' • Allocate:B[i] = B[i] | '11000000' • Search: Repeatedly, Check left-most bit andShift mask right:TEST = B[i] & '10000000'