1 / 37

Memory Management

Memory Management. A compiler creates an executable code An executable code must be brought into main memory to run Operating system maps the executable code onto the main memory Hardware accesses the memory locations in the course of the execution. Background.

terrel
Download Presentation

Memory Management

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. Memory Management

  2. A compiler creates an executable code An executable code must be brought into main memory to run Operating system maps the executable code onto the main memory Hardware accesses the memory locations in the course of the execution Background

  3. Applications must request memory through OS calls (or user libs calling certain SVC like page(x)) p=malloc(x) then releases them :free(p) Background

  4. Fetching: When ? On Demand Anticipatory Pre-Allocation: Before request Post-Allocation: Just When reading/writing Placement: From where? How? Memory management

  5. Processes in RAMFixed Partition Multiprogramming 0000: 0010: 01 77 77 00 10 … ; PCB 0 0020: 01 88 88 00 20 … ; PCB 1 PCB 0 St: 1 ;ready AX: 7777 PC: 1000 ;x=[1040] 1000:a1 40 10 ;mov AX, x1003:40 ;inc AX1004:a3 40 10 ;mov x, AX 1040: 00 00 ;add [BX+SI],AL? PCB 1 St: 1 ;ready AX: 8888 PC: 2000 ;y=[2040] 2000:b8 0800 ;mov AX, 82003:a3 40 02 ;mov y,AX 2040: 00 00 ;add [BX+SI],AL?

  6. Processes in RAMFixed Partition Multiprogramming Fixed Partitions AND Absolute translation and loading Bad – what a waste 0000: 0010: 01 77 77 00 10 … ; PCB 0 0020: 01 88 88 00 20 … ; PCB 1 PCB 0 St: 1 ;ready AX: 7777 PC: 1000 ;x=[1040] 1000:a1 40 10 ;mov AX, x1003:40 ;inc AX1004:a3 40 10 ;mov x, AX 1040: 00 00 ;add [BX+SI],AL? PCB 1 St: 1 ;ready AX: 8888 PC: 2000 ;y=[2040] 2000:b8 0800 ;mov AX, 82003:a3 40 02 ;mov y,AX 2040: 00 00 ;add [BX+SI],AL?

  7. Limits # processes Memory Misuse Internal/External Fragments Site Dependent Compilation/Linking Limited possibility of run time dynamic allocation Partition0 Proc 7 Partition 1 Free Partition 0 Proc 7 Fixed PartitionsAbsolute translation and loading

  8. Protection: Processes and OS from malicious references Processes must be unable to reference addresses of other processes or those of the operating system Each memory access must be checked for validity/Enforced by hardware Memory Management Requirements

  9. Processes in RAMFixed Partition Multiprogramming 0000: 0010: 01 77 77 00 10 … ; PCB 0 0020: 01 88 88 00 20 … ; PCB 1 An Exception will be triggered if any address outside [990-1100] is referenced for read or write PCB 0 St: 1 ;ready AX: 7777 PC: 1000 LB: 0990 UP: 1100 ;x=[1040] 1000:a1 40 10 ;mov AX, x1003:40 ;inc AX1004:a3 40 10 ;mov x, AX 1040: 00 00 ;add [BX+SI],AL? PCB 1 St: 1 ;ready AX: 8888 PC: 2000 LB: 2000 UP: 3000 ;y=[2040] 2000:b8 0800 ;mov AX, 82003:a3 40 02 ;mov y,AX 2040: 00 00 ;add [BX+SI],AL?

  10. Protection Relocation: To relax that unacceptable absolute binding ! Compiler generated addresses are relative A process can be loaded at different memory location, swapped in/out, Actual physical mapping is not known at compile time Memory Management Requirements

  11. Processes in RAMDynamic Partition Multiprogramming 0000: 0010: 01 77 77 00 10 … ; PCB 0 0020: 01 88 88 00 20 … ; PCB 1 How Hiding from Proc 0 the later fact that it will load at 990 And P1 at 2000 PCB 0 St: 1 ;ready AX: 7777 PC: 1000 LB: 0990 UP: 1100 ;x=[1040] 1000:a1 40 10 ;mov AX, x1003:40 ;inc AX1004:a3 40 10 ;mov x, AX 1040: 00 00 ;add [BX+SI],AL? PCB 1 St: 1 ;ready AX: 8888 PC: 2000 LB: 2000 UP: 3000 ;y=[2040] 2000:b8 0800 ;mov AX, 82003:a3 40 02 ;mov y,AX 2040: 00 00 ;add [BX+SI],AL?

  12. Processes in RAM Dynamic Partition Multiprogramming 0000: 0010: 01 77 77 00 10 … ; PCB 0 0020: 01 88 88 00 20 … ; PCB 1 PCB 0 St: 1 ;ready AX: 7777 PC: 1000 LB: 0990 UP: 1100 RR: 1000 ;x=[0040] 1000:a1 40 00 ;mov AX, x1003:40 ;inc AX1004:a3 40 00 ;mov x, AX 1040: 00 00 ;add [BX+SI],AL? PCB 1 St: 1 ;ready AX: 8888 PC: 2000 LB: 2000 UP: 3000 RR: 2000 ;y=[0040] 2000:b8 0800 ;mov AX, 82003:a3 40 00 ;mov y,AX 2040: 00 00 ;add [BX+SI],AL?

  13. Processes in RAM Dynamic Partition Multiprogramming 0000: 0010: 01 77 77 00 10 … ; PCB 0 0020: 01 88 88 00 20 … ; PCB 1 PCB 0 St: 1 ;ready AX: 7777 PC: 1000 LB: 0990 UP: 1100 RR: 1000 + ;x=[0040] 1000:a1 40 00 ;mov AX, x1003:40 ;inc AX1004:a3 40 00 ;mov x, AX 1040: 00 00 ;add [BX+SI],AL? 40 PCB 1 St: 1 ;ready AX: 8888 PC: 2000 LB: 2000 UP: 3000 RR: 2000 ;y=[0040] 2000:b8 0800 ;mov AX, 82003:a3 40 00 ;mov y,AX 2040: 00 00 ;add [BX+SI],AL?

  14. Processes in RAM Dynamic Partition Multiprogramming User (Process) address space is disassociated from physical space But contiguity is still preserved 0000: 0010: 01 77 77 00 10 … ; PCB 0 0020: 01 88 88 00 20 … ; PCB 1 PCB 0 St: 1 ;ready AX: 7777 PC: 1000 LB: 0990 UP: 1100 RR: 1000 + ;x=[0040] 1000:a1 40 00 ;mov AX, x1003:40 ;inc AX1004:a3 40 00 ;mov x, AX 1040: 00 00 ;add [BX+SI],AL? 40 PCB 1 St: 1 ;ready AX: 8888 PC: 2000 LB: 2000 UP: 3000 RR: 2000 ;y=[0040] 2000:b8 0800 ;mov AX, 82003:a3 40 00 ;mov y,AX 2040: 00 00 ;add [BX+SI],AL?

  15. Processes in RAM Dynamic Partition Multiprogramming User (Process) address space is disassociated from physical space But contiguity is still preserved Each process has its own address space [0…something] However Separate Ranges {[m..n],[k..l] needs complex management ! 0000: 0010: 01 77 77 00 10 … ; PCB 0 0020: 01 88 88 00 20 … ; PCB 1 PCB 0 St: 1 ;ready AX: 7777 PC: 1000 LB: 0990 UP: 1100 RR: 1000 + ;x=[0040] 1000:a1 40 00 ;mov AX, x1003:40 ;inc AX1004:a3 40 00 ;mov x, AX 1040: 00 00 ;add [BX+SI],AL? 40 PCB 1 St: 1 ;ready AX: 8888 PC: 2000 LB: 2000 UP: 3000 RR: 2000 ;y=[0040] 2000:b8 0800 ;mov AX, 82003:a3 40 00 ;mov y,AX 2040: 00 00 ;add [BX+SI],AL?

  16. Where to allocate: Fixed Partitions Dynamic placement algorithms First Fit Best Fit Worst Fit Buddy Systems The Placement problem

  17. PCB0 PCB1 Free 672 128 448 896 224 320 224 128 null …. null null Dynamic partitioning example 0 OS 128 128 P0 Requests 320 units [h0] P1 Requests 224 units [h1] P0 Requests 224 units [h2] P0 320 448 P1 224 672 P0 224 896 128 1 K

  18. PCB0 Free 896 672 128 448 224 320 224 128 null ... null …. Dynamic partitioning example 0 OS 128 128 P0 Requests 320 units [h0] P1 Requests 224 units [h1] P0 Requests 224 units [h2] P1 exits [releases h1] P0 320 448 224 672 P0 224 896 128 1 K

  19. PCB0 Free 672 128 448 896 224 320 224 128 null …. null ... Dynamic partitioning example 0 OS 128 P0 Requests 320 units [h0] P1 Requests 224 units [h1] P0 Requests 224 units [h2] P1 exits [releases h1] Now P2 Requests 120 ? WF ! 128 P0 320 448 224 672 P0 224 896 From Here? FirstFit Best Fit 128 1 K Or Worst fit

  20. PCB2 PCB0 Free 672 896 128 448 568 120 128 104 320 224 null null null …. ... Dynamic partitioning example 0 OS 128 128 P0 Requests 320 units [h0] P1 Requests 224 units [h1] P0 Requests 288 units [h2] P1 exits [releases h1] P2 request 120 [h3] WF P0 320 448 P2 120 568 104 672 P0 224 896 128 1 K

  21. PCB2 PCB0 Free 448 896 568 672 128 104 128 120 224 320 null ... null … null Dynamic partitioning example 0 OS 128 P0 Requests 320 units [h0] P1 Requests 224 units [h1] P0 Requests 288 units [h2] P1 exits [releases h1] P2 request 128 [h3] WF P0 releases h0 128 320 448 P2 120 568 104 672 P0 224 896 128 1 K

  22. PCB0 PCB2 Free 568 448 672 128 896 120 320 104 224 128 null null null ... … Dynamic partitioning example 0 OS 128 P0 Requests 320 units [h0] P1 Requests 224 units [h1] P0 Requests 288 units [h2] P1 exits [releases h1] P2 request 128 [h3] WF P0 releases h0 P2 Requests 100 ! 128 320 448 P2 120 568 104 672 P0 224 896 FirstFit 128 1 K BestFit WorstFit

  23. PCB0 PCB2 Free 568 448 672 128 896 120 320 104 224 128 null null null ... … Dynamic partitioning example 0 OS 128 P0 Requests 320 units [h0] P1 Requests 224 units [h1] P0 Requests 288 units [h2] P1 exits [releases h1] P2 request 128 [h3] WF P0 releases h0 P2 Requests 100 ! 128 320 448 P2 120 568 104 672 P0 224 896 FirstFit 128 1 K BestFit WorstFit

  24. PCB0 PCB2 Free 896 668 448 672 568 128 224 100 4 128 320 120 … null … null null ... Dynamic partitioning example 0 OS 128 P0 Requests 320 units [h0] P1 Requests 224 units [h1] P0 Requests 288 units [h2] P1 exits [releases h1] P2 request 128 [h3] WF P0 releases h0 P2 Requests 100 ! FF 128 320 448 P2 120 568 P2 100 672 P0 224 896 128 1 K Who wants that

  25. PCB2 PCB0 Free 448 896 568 128 672 128 120 224 320 104 … ... null null null Dynamic partitioning example 0 OS 128 P0 Requests 320 units [h0] P1 Requests 224 units [h1] P0 Requests 288 units [h2] P1 exits [releases h1] P2 request 128 [h3] WF P0 releases h0 P2 Requests 100 ! FF 128 320 448 P2 120 568 P2 104 672 P0 224 Internal Fragment Why ? 896 128 1 K

  26. Case of internal fragments PCB0 Free Free 668 568 448 672 568 896 448 128 896 128 100 128 120 224 320 120 320 4 128 104 null … … null null … … ... ... … Dynamic partitioning example P2 exits Someone wants 541 unit 0 OS 128 Case of exact placement 128 320 448 120 568 104 672 P0 224 896 128 1 K Needs Coalescing

  27. PCB0 Free 128 896 672 554 224 128 null ... null Dynamic partitioning example Someone wants 541 unit – ok But Someone Requests 600 ! External Fragments and Garbage collection 0 OS 128 128 554 672 External Fragments P0 224 896 128 1 K Needs Garbage Collection

  28. 0 0 OS 128 OS 128 128 128 512 512 1 K 1 K Dynamic partitioning example Going WF Going BF

  29. 0 0 OS 128 OS 128 128 128 512 512 1 K 1 K Dynamic partitioning example Going WF Going BF

  30. Preserves large blocks for future External fragments 0 0 OS 128 OS 128 128 128 512 512 1 K 1 K Dynamic partitioning example • More Normally distributed • More predictable Going WF Going BF Random FF

  31. Buddy System • Entire space available is treated as a single block of 2U • If a request of size s such that 2U-1 < s <= 2U, entire block is allocated • Otherwise block is split into two equal buddies • Process continues until smallest block greater than or equal to s is generated

  32. Buddy System

  33. Buddy System 100/128 60/64 64 256 256/256 256

  34. Buddy System • Internal Fragments ! • Overcomes limited processes [Fixed partitioning] and overhead imposed by Dynamic allocation. • Useful in special memory managers such as kernel memory allocation, parallel systems, …etc.

  35. Protection Relocation External Fragments management Sharing Post Allocation ? Why? Managing Requests greater then physically available Memory Management Requirements

  36. Protection Relocation External Fragments management Sharing Post Allocation ? Why? Managing Requests greater then physically available Memory Management Requirements

  37. Thanks && comments

More Related