Memory Management S:1 OS/SSS Background • 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 S:2 OS/SSS Background • Applications must request memory through OS calls (or user libs calling certain SVC like page(x)) p=malloc(x) then releases them :free(p) S:3 OS/SSS Memory management • Fetching: When ? – On Demand – Anticipatory • Pre-Allocation: Before request • Post-Allocation: Just When reading/writing • Placement: From where? How? S:4 OS/SSS Processes in RAM Fixed Partition Multiprogramming 0000: 0010: 01 77 77 00 10 … ; PCB 0 0020: 01 88 88 00 20 … ; PCB 1 S:5 1000:a1 40 10 1003:40 1004:a3 40 10 ;x=[1040] ;mov AX, x ;inc AX ;mov x, AX 1040: 00 00 ;add [BX+SI],AL? 2000:b8 0800 2003:a3 40 02 ;y=[2040] ;mov AX, 8 ;mov y,AX 2040: 00 00 ;add [BX+SI],AL? PCB 0 St: 1 ;ready AX: 7777 PC: 1000 PCB 1 St: 1 ;ready AX: 8888 PC: 2000 OS/SSS Processes in RAM Fixed Partition Multiprogramming 0000: 0010: 01 77 77 00 10 … ; PCB 0 0020: 01 88 88 00 20 … ; PCB 1 S:6 1000:a1 40 10 1003:40 1004:a3 40 10 ;x=[1040] ;mov AX, x ;inc AX ;mov x, AX 1040: 00 00 ;add [BX+SI],AL? 2000:b8 0800 2003:a3 40 02 ;y=[2040] ;mov AX, 8 ;mov y,AX 2040: 00 00 ;add [BX+SI],AL? Fixed Partitions AND Absolute translation and PCB 0 loading St: 1 ;ready Bad – what a AX: 7777 waste PC: 1000 PCB 1 St: 1 ;ready AX: 8888 PC: 2000 OS/SSS Fixed Partitions Absolute translation and loading • Limits # processes • Memory Misuse • Internal/External Fragments • Site Dependent Compilation/Linking Partition0 Proc 7 Partition 1 Free Partition 0 Proc 7 • Limited possibility of run time dynamic allocation S:7 OS/SSS Memory Management Requirements • 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 S:8 OS/SSS Processes in RAM Fixed Partition Multiprogramming 0000: 0010: 01 77 77 00 10 … ; PCB 0 0020: 01 88 88 00 20 … ; PCB 1 S:9 1000:a1 40 10 1003:40 1004:a3 40 10 ;x=[1040] ;mov AX, x ;inc AX ;mov x, AX 1040: 00 00 ;add [BX+SI],AL? 2000:b8 0800 2003:a3 40 02 ;y=[2040] ;mov AX, 8 ;mov y,AX 2040: 00 00 ;add [BX+SI],AL? PCB 0 St: 1 ;ready AX: 7777 PC: 1000 LB: 0990 UP: 1100 PCB 1 An Exception will be triggered if any address outside [9901100] is referenced for read or write St: 1 ;ready AX: 8888 PC: 2000 LB: 2000 UP: 3000 OS/SSS Memory Management Requirements • 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 S:10 OS/SSS Processes in RAM Dynamic Partition Multiprogramming 0000: 0010: 01 77 77 00 10 … ; PCB 0 0020: 01 88 88 00 20 … ; PCB 1 S:11 1000:a1 40 10 1003:40 1004:a3 40 10 ;x=[1040] ;mov AX, x ;inc AX ;mov x, AX 1040: 00 00 ;add [BX+SI],AL? 2000:b8 0800 2003:a3 40 02 ;y=[2040] ;mov AX, 8 ;mov y,AX 2040: 00 00 ;add [BX+SI],AL? PCB 0 St: 1 ;ready AX: 7777 PC: 1000 LB: 0990 UP: 1100 How Hiding from Proc 0 the later fact that it will load at 990 And P1 at 2000 PCB 1 St: 1 ;ready AX: 8888 PC: 2000 LB: 2000 UP: 3000 OS/SSS Processes in RAM Dynamic Partition Multiprogramming S:12 0000: PCB 0 0010: 01 77 77 00 10 … ; PCB 0 0020: 01 88 88 00 20 … ; PCB 1 St: 1 ;ready AX: 7777 PC: 1000 LB: 0990 UP: 1100 RR: 1000 1000:a1 40 00 1003:40 1004:a3 40 00 ;x=[0040] ;mov AX, x ;inc AX ;mov x, AX 1040: 00 00 ;add [BX+SI],AL? 2000:b8 0800 2003:a3 40 00 ;y=[0040] ;mov AX, 8 ;mov y,AX 2040: 00 00 ;add [BX+SI],AL? PCB 1 St: 1 ;ready AX: 8888 PC: 2000 LB: 2000 UP: 3000 RR: 2000 OS/SSS Processes in RAM Dynamic Partition Multiprogramming 0000: PCB 0 0010: 01 77 77 00 10 … ; PCB 0 0020: 01 88 88 00 20 … ; PCB 1 St: 1 ;ready AX: 7777 PC: 1000 LB: 0990 UP: 1100 RR: 1000 + S:13 1000:a1 40 00 1003:40 1004:a3 40 00 ;x=[0040] ;mov AX, x ;inc AX ;mov x, AX 1040: 00 00 ;add [BX+SI],AL? 40 2000:b8 0800 2003:a3 40 00 ;y=[0040] ;mov AX, 8 ;mov y,AX 2040: 00 00 ;add [BX+SI],AL? PCB 1 St: 1 ;ready AX: 8888 PC: 2000 LB: 2000 UP: 3000 RR: 2000 OS/SSS Processes in RAM Dynamic Partition Multiprogramming 0000: 0010: 01 77 77 00 10 … ; PCB 0 0020: 01 88 88 00 20 … ; PCB 1 + S:14 1000:a1 40 00 1003:40 1004:a3 40 00 ;x=[0040] ;mov AX, x ;inc AX ;mov x, AX 1040: 00 00 ;add [BX+SI],AL? 40 2000:b8 0800 2003:a3 40 00 ;y=[0040] ;mov AX, 8 ;mov y,AX 2040: 00 00 ;add [BX+SI],AL? User (Process) address PCB 0space is disassociated from St:physical 1 ;ready space AX: 7777 ButPC: contiguity is 1000 stillLB: preserved 0990 UP: 1100 RR: 1000 PCB 1 St: 1 ;ready AX: 8888 PC: 2000 LB: 2000 UP: 3000 RR: 2000 OS/SSS Processes in RAM Dynamic Partition Multiprogramming User (Process) address space is PCB 0 disassociated from physical St: 1 ;ready space AX: 7777 But contiguity is PC: 1000 still preserved + LB: 0990 0000: 0010: 01 77 77 00 10 … ; PCB 0 0020: 01 88 88 00 20 … ; PCB 1 S:15 1000:a1 40 00 1003:40 1004:a3 40 00 ;x=[0040] ;mov AX, x ;inc AX ;mov x, AX 1040: 00 00 ;add [BX+SI],AL? 2000:b8 0800 2003:a3 40 00 ;y=[0040] ;mov AX, 8 ;mov y,AX 2040: 00 00 ;add [BX+SI],AL? 40 UP: 1100 RR: 1000 Each process has its own address space PCB 1 [0…something] St:However 1 ;ready Separate AX: 8888 Ranges PC:{[m..n],[k..l] 2000 LB:needs 2000complex ! UP:management 3000 RR: 2000 OS/SSS The Placement problem • Where to allocate: – Fixed Partitions – Dynamic placement algorithms • First Fit • Best Fit • Worst Fit – Buddy Systems S:16 OS/SSS Dynamic partitioning example 0 128 OS 128 P0 320 448 P1 224 672 P0 224 896 1K PCB0 128 P0 Requests 320 units [h0] P1 Requests 224 units [h1] P0 Requests 224 units [h2] 128 …. 672 224 PCB1 448 224 null Free 896 S:17 320 128 null OS/SSS null Dynamic partitioning example 0 128 PCB0 OS 128 P0 320 448 224 P0 Requests 320 units [h0] P1 Requests 224 units [h1] P0 Requests 224 units [h2] P1 exits [releases h1] 128 320 …. 672 224 128 ... 672 P0 224 896 1K Free 128 896 S:18 448 224 null OS/SSS null Dynamic partitioning example 0 128 OS 128 P0 320 448 224 PCB0 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 320 …. 672 224 128 ... 672 P0 224 896 1K S:19 128 From Here? First Fit Best Fit Or Worst fit Free 896 448 224 null OS/SSS null Dynamic partitioning example 0 128 OS 128 P0 320 448 568 672 P2 120 104 PCB0 P0 Requests 320 units [h0] P1 Requests 224 units [h1] P0 Requests 288 units [h2] P1 exits [releases h1] P2 request 120 [h3] WF 896 …. 224 PCB2 120 null Free 128 896 S:20 320 672 448 P0 224 1K 128 568 104 128 ... null OS/SSS null Dynamic partitioning example 0 128 OS 128 320 448 568 672 P2 120 104 PCB0 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 S:21 448 Free 128 896 128 224 null PCB2 P0 224 896 1K 672 568 104 320 null 120 128 null ... … OS/SSS Dynamic partitioning example 0 128 PCB0 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 ! 320 448 568 672 P2 120 104 P0 224 First Fit 128 null PCB2 448 120 896 Best Fit S:22 224 null Free 896 1K 672 Worst Fit 128 568 104 320 null 128 ... … OS/SSS Dynamic partitioning example 0 128 PCB0 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 ! 320 448 568 672 P2 120 104 P0 224 First Fit 128 null PCB2 448 120 896 Best Fit S:23 224 null Free 896 1K 672 Worst Fit 128 568 104 320 null 128 ... … OS/SSS Dynamic partitioning example 0 128 OS 128 320 448 568 672 P2 120 P2 100 PCB0 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 S:24 448 Free 128 896 Who wants that 128 224 null 120 … PCB2 P0 224 896 1K 672 668 4 320 null 568 128 100 ... … OS/SSS null Dynamic partitioning example 0 128 OS 128 320 448 568 672 P2 120 P2 104 P0 224 896 1K 128 PCB0 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 224 null 120 … PCB2 448 568 Free Internal Fragment Why ? 896 128 S:25 672 320 128 104 ... null OS/SSS null Dynamic partitioning example 0 128 OS 128 P2 exits Someone wants 541 unit 568 672 fragments 120 896 128 128 448 568 placement 128 320 120 104 ... 896 … … null 668 128 448 568 128 4 … 320 120 100 … … null Needs Coalescing S:26 null Free 896 104 224 Case of exact Free P0 224 1K 672 Case of internal 320 448 PCB0 OS/SSS ... Dynamic partitioning example PCB0 0 128 Someone wants 541 unit – ok OS 128 672 Free 896 External Fragments P0 224 128 554 128 null 896 1K 128 Needs Garbage Collection S:27 null But Someone Requests 600 ! External Fragments and Garbage collection 554 672 224 OS/SSS ... Dynamic partitioning example 0 128 OS 128 512 1K Going BF S:28 0 WorstFit BestFit 10 1 1 20 1 2 40 3 3 60 6 3 80 8 2 100 4 1 120 2 2 140 1 2 160 1 6 2090 2090 Free 128 OS 128 512 1K Going WF OS/SSS Dynamic partitioning example 0 128 0 OS 128 128 OS 128 10 512 512 5 0 0 1K Going BF S:29 50 100 Worst-Fit 150 Best-Fit 200 1K Going WF OS/SSS Dynamic partitioning example 0 128 OS 128 512 0 • Preserves • More large Normally blocks for distributed future • More • External 128 OS 128 512 predictable fragments 1K 1K Going BF Going WF Random FF S:30 OS/SSS 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 H0->128 128 256 512 H1Req 240 H0->128 128 H1->256 512 H2Req 60 H0->128 H2->64 64 H1->256 512 H3Req 256 H0->128 H2->64 64 H1->256 H3->256 256 Release H1 H0->128 H2->64 64 256 H3->256 256 Release H0 128 H2->64 64 256 H3->256 256 H4Req 100 H4->128 H2->64 64 256 H3->256 256 Release H2 H4->128 256 H3->256 256 H3->256 256 Release H3 Release H3 S:31 1M H0Req 100 128 512 1M OS/SSS Buddy System 1M H0Req 100 H0->128 128 256 512 H1Req 240 H0->128 128 H1->256 512 H2Req 60 H0->128 H2->64 64 H1->256 512 H3Req 256 H0->128 H2->64 64 H1->256 H3->256 256 Release H1 H0->128 H2->64 64 256 H3->256 256 Release H0 128 H2->64 64 256 H3->256 256 H4Req 100 H4->128 H2->64 64 256 H3->256 256 Release H2 H4->128 256 H3->256 256 H3->256 256 Release H3 Release H3 S:32 128 512 1M OS/SSS Buddy System 1M H0Req 100 H0->128 128 256 512 H1Req 240 H0->128 128 H1->256 512 H2Req 60 H0->128 H2->64 64 H1->256 512 H3Req 256 H0->128 H2->64 64 H1->256 H3->256 256 Release H1 H0->128 H2->64 64 256 H3->256 256 Release H0 128 H2->64 64 256 H3->256 256 H4Req 100 H4->128 H2->64 64 256 H3->256 256 Release H2 H4->128 256 H3->256 256 H3->256 256 256/256 256 128 Release H3 512 Release H3 1M 100/128 S:33 60/64 64 256 OS/SSS Buddy System •Internal Fragments ! 1M H0Req 100 H0->128 128 256 512 H1Req 240 H0->128 128 H1->256 512 H2Req 60 H0->128 H2->64 64 H1->256 512 H3Req 256 H0->128 H2->64 64 H1->256 H3->256 256 Release H1 H0->128 H2->64 64 256 H3->256 256 Release H0 128 H2->64 64 256 H3->256 256 H4Req 100 H4->128 H2->64 64 256 H3->256 256 Release H2 H4->128 256 H3->256 256 H3->256 256 Release H3 Release H3 128 512 1M •Overcomes limited processes [Fixed partitioning] and overhead imposed by Dynamic allocation. •Useful in special memory managers such as kernel memory allocation, parallel systems, …etc. S:34 OS/SSS Memory Management Requirements • Protection • Relocation • External Fragments management • Sharing • Post Allocation ? Why? • Managing Requests greater then physically available S:35 OS/SSS Memory Management Requirements • Protection • Relocation • External Fragments management • Sharing • Post Allocation ? Why? • Managing Requests greater then physically available S:36 OS/SSS Thanks && comments S:37 OS/SSS