html5-img
1 / 90

Operating Systems Principles Memory Management Lecture 7: Physical Memory

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

dolph
Download Presentation

Operating Systems Principles Memory Management Lecture 7: Physical Memory

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operating Systems PrinciplesMemory ManagementLecture 7: Physical Memory 主講人:虞台文

  2. 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

  3. Operating Systems PrinciplesMemory ManagementLecture 7: Physical Memory Preparing a Program for Execution

  4. Preparing a Program for Execution

  5. Preparing a Program for Execution Modules reflecting different functions are designed separately, possibly by different programmers.

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

  7. Preparing a Program for Execution Compilation

  8. 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.

  9. 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.

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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.

  15. 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

  16. 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

  17. 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

  18. 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)

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

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

  35. Example

  36. Operating Systems PrinciplesMemory ManagementLecture 7: Physical Memory Memory Partitioning Schemes

  37. 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

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

  39. How to Assign Processes to Partitions?

  40. 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.

  41. 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.

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

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

  44. 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.

  45. 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

  46. 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

  47. 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

  48. 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

  49. 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'

  50. 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'

More Related