37
OS/SSS S:1 Memory Management Memory Management

Memory Management

  • Upload
    terrel

  • View
    34

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Memory Management

OS/SSSS:1

Memory ManagementMemory Management

Page 2: Memory Management

OS/SSSS:2

BackgroundBackground

• 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

Page 3: Memory Management

OS/SSSS:3

BackgroundBackground

• Applications must request

memory through OS calls (or

user libs calling certain SVC

like page(x))

p=malloc(x)

then releases them :free(p)

Page 4: Memory Management

OS/SSSS:4

Memory managementMemory management

• Fetching: When ?

– On Demand

– Anticipatory

•Pre-Allocation: Before request

•Post-Allocation: Just When

reading/writing

• Placement: From where? How?

Page 5: Memory Management

OS/SSSS:5

0000:

0010: 01 77 77 00 10 … ; PCB 00020: 01 88 88 00 20 … ; PCB 1

Processes in RAMProcesses in RAMFixed Partition MultiprogrammingFixed Partition Multiprogramming

;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?

;y=[2040]2000:b8 0800 ;mov AX, 82003:a3 40 02 ;mov y,AX

2040: 00 00 ;add [BX+SI],AL?

PCB 0

St: 1 ;readyAX: 7777PC: 1000

PCB 1

St: 1 ;readyAX: 8888PC: 2000

Page 6: Memory Management

OS/SSSS:6

0000:

0010: 01 77 77 00 10 … ; PCB 00020: 01 88 88 00 20 … ; PCB 1

Processes in RAMProcesses in RAMFixed Partition MultiprogrammingFixed Partition Multiprogramming

;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?

;y=[2040]2000:b8 0800 ;mov AX, 82003:a3 40 02 ;mov y,AX

2040: 00 00 ;add [BX+SI],AL?

PCB 0

St: 1 ;readyAX: 7777PC: 1000

PCB 1

St: 1 ;readyAX: 8888PC: 2000

Fixed Partitions ANDAbsolute translation and loadingBad – what a waste

Fixed Partitions ANDAbsolute translation and loadingBad – what a waste

Page 7: Memory Management

OS/SSSS:7

Fixed PartitionsFixed PartitionsAbsolute translation and loadingAbsolute translation and loading

• Limits # processes

• Memory Misuse

• Internal/External Fragments

• Site Dependent Compilation/Linking

• Limited possibility of run time dynamic allocation

Partition 0Proc 7

Partition0Proc 7

Partition 1 Free

Page 8: Memory Management

OS/SSSS:8

Memory Management Memory Management RequirementsRequirements

• 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

Page 9: Memory Management

OS/SSSS:9

0000:

0010: 01 77 77 00 10 … ; PCB 00020: 01 88 88 00 20 … ; PCB 1

Processes in RAMProcesses in RAMFixed Partition MultiprogrammingFixed Partition Multiprogramming

;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?

;y=[2040]2000:b8 0800 ;mov AX, 82003:a3 40 02 ;mov y,AX

2040: 00 00 ;add [BX+SI],AL?

PCB 0

St: 1 ;readyAX: 7777PC: 1000LB: 0990UP: 1100

PCB 1

St: 1 ;readyAX: 8888PC: 2000LB: 2000UP: 3000

An Exception will be triggered if any address outside [990-1100] is referenced for read or write

An Exception will be triggered if any address outside [990-1100] is referenced for read or write

Page 10: Memory Management

OS/SSSS:10

Memory Management Memory Management RequirementsRequirements

• 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

Page 11: Memory Management

OS/SSSS:11

0000:

0010: 01 77 77 00 10 … ; PCB 00020: 01 88 88 00 20 … ; PCB 1

Processes in RAMProcesses in RAMDynamic Partition Dynamic Partition MultiprogrammingMultiprogramming

;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?

;y=[2040]2000:b8 0800 ;mov AX, 82003:a3 40 02 ;mov y,AX

2040: 00 00 ;add [BX+SI],AL?

PCB 0

St: 1 ;readyAX: 7777PC: 1000LB: 0990UP: 1100

PCB 1

St: 1 ;readyAX: 8888PC: 2000LB: 2000UP: 3000

How Hiding from Proc 0 the later fact that it will load at 990

And P1 at 2000

How Hiding from Proc 0 the later fact that it will load at 990

And P1 at 2000

Page 12: Memory Management

OS/SSSS:12

0000:

0010: 01 77 77 00 10 … ; PCB 00020: 01 88 88 00 20 … ; PCB 1

Processes in RAMProcesses in RAM Dynamic Partition Dynamic Partition MultiprogrammingMultiprogramming

;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?

;y=[0040]2000:b8 0800 ;mov AX, 82003:a3 40 00 ;mov y,AX

2040: 00 00 ;add [BX+SI],AL?

PCB 0

St: 1 ;readyAX: 7777PC: 1000LB: 0990UP: 1100RR: 1000

PCB 1

St: 1 ;readyAX: 8888PC: 2000LB: 2000UP: 3000RR: 2000

Page 13: Memory Management

OS/SSSS:13

0000:

0010: 01 77 77 00 10 … ; PCB 00020: 01 88 88 00 20 … ; PCB 1

Processes in RAMProcesses in RAM Dynamic Partition Dynamic Partition MultiprogrammingMultiprogramming

;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?

;y=[0040]2000:b8 0800 ;mov AX, 82003:a3 40 00 ;mov y,AX

2040: 00 00 ;add [BX+SI],AL?

PCB 0

St: 1 ;readyAX: 7777PC: 1000LB: 0990UP: 1100RR: 1000

PCB 1

St: 1 ;readyAX: 8888PC: 2000LB: 2000UP: 3000RR: 2000

40

+

Page 14: Memory Management

OS/SSSS:14

0000:

0010: 01 77 77 00 10 … ; PCB 00020: 01 88 88 00 20 … ; PCB 1

Processes in RAMProcesses in RAM Dynamic Partition Dynamic Partition MultiprogrammingMultiprogramming

;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?

;y=[0040]2000:b8 0800 ;mov AX, 82003:a3 40 00 ;mov y,AX

2040: 00 00 ;add [BX+SI],AL?

PCB 0

St: 1 ;readyAX: 7777PC: 1000LB: 0990UP: 1100RR: 1000

PCB 1

St: 1 ;readyAX: 8888PC: 2000LB: 2000UP: 3000RR: 2000

40

+

User (Process) address space is disassociated from physical spaceBut contiguity is still preserved

User (Process) address space is disassociated from physical spaceBut contiguity is still preserved

Page 15: Memory Management

OS/SSSS:15

0000:

0010: 01 77 77 00 10 … ; PCB 00020: 01 88 88 00 20 … ; PCB 1

Processes in RAMProcesses in RAM Dynamic Partition Dynamic Partition MultiprogrammingMultiprogramming

;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?

;y=[0040]2000:b8 0800 ;mov AX, 82003:a3 40 00 ;mov y,AX

2040: 00 00 ;add [BX+SI],AL?

PCB 0

St: 1 ;readyAX: 7777PC: 1000LB: 0990UP: 1100RR: 1000

PCB 1

St: 1 ;readyAX: 8888PC: 2000LB: 2000UP: 3000RR: 2000

40

+

User (Process) address space is disassociated from physical spaceBut contiguity is still preserved

Each process has its own address space [0…something]However Separate Ranges {[m..n],[k..l] needs complex management !

User (Process) address space is disassociated from physical spaceBut contiguity is still preserved

Each process has its own address space [0…something]However Separate Ranges {[m..n],[k..l] needs complex management !

Page 16: Memory Management

OS/SSSS:16

The Placement problemThe Placement problem

• Where to allocate:

– Fixed Partitions

– Dynamic placement algorithms

•First Fit

•Best Fit

•Worst Fit

– Buddy Systems

Page 17: Memory Management

OS/SSSS:17

Dynamic partitioning exampleDynamic partitioning example

224 null672

320 ….128

PCB0

224 null448

PCB1

128 null896

Free

P0 Requests 320 units [h0]P1 Requests 224 units [h1]P0 Requests 224 units [h2]

OS 128

P0 320

128

P1 224

P0 224

0

128

448

672

896

1 K

Page 18: Memory Management

OS/SSSS:18

Dynamic partitioning exampleDynamic partitioning example

224 null672

320 ….128

PCB0

224 null448

128 ...896

Free

P0 Requests 320 units [h0]P1 Requests 224 units [h1]P0 Requests 224 units [h2]P1 exits [releases h1]

OS 128

P0 320

128

224

P0 224

0

128

448

672

896

1 K

Page 19: Memory Management

OS/SSSS:19

Dynamic partitioning exampleDynamic partitioning example

224 null672

320 ….128

PCB0

224 null448

128 ...896

Free

P0 Requests 320 units [h0]P1 Requests 224 units [h1]P0 Requests 224 units [h2]P1 exits [releases h1]NowP2 Requests 120 ? WF !

OS 128

P0 320

128

224

P0 224

0

128

448

672

896

1 K

From Here?First FitBest Fit

OrWorst fit

Page 20: Memory Management

OS/SSSS:20

Dynamic partitioning exampleDynamic partitioning example

224 null672

320 ….128

PCB0

104 null568

128 ...896

Free

P0 Requests 320 units [h0]P1 Requests 224 units [h1]P0 Requests 288 units [h2]P1 exits [releases h1]P2 request 120 [h3] WF

120 null448

PCB2

OS 128

P0 320

128

104

P0 224

0

128

448

672

896

1 K

P2 120568

Page 21: Memory Management

OS/SSSS:21

Dynamic partitioning exampleDynamic partitioning example

224 null672

PCB0

P0 Requests 320 units [h0]P1 Requests 224 units [h1]P0 Requests 288 units [h2]P1 exits [releases h1]P2 request 128 [h3] WFP0 releases h0

104 …568

128 ...896

Free

120 null448

PCB2

320 null128

OS 128

320

128

104

P0 224

0

128

448

672

896

1 K

P2 120568

Page 22: Memory Management

OS/SSSS:22

Dynamic partitioning exampleDynamic partitioning example

224 null672

PCB0P0 Requests 320 units [h0]P1 Requests 224 units [h1]P0 Requests 288 units [h2]P1 exits [releases h1]P2 request 128 [h3] WFP0 releases h0P2 Requests 100 !

104 …568

128 ...896

Free

120 null448

PCB2

320 null128

OS 128

320

128

104

P0 224

0

128

448

672

896

1 K

P2 120568

First Fit

Best Fit

Worst Fit

Page 23: Memory Management

OS/SSSS:23

Dynamic partitioning exampleDynamic partitioning example

224 null672

PCB0P0 Requests 320 units [h0]P1 Requests 224 units [h1]P0 Requests 288 units [h2]P1 exits [releases h1]P2 request 128 [h3] WFP0 releases h0P2 Requests 100 !

104 …568

128 ...896

Free

120 null448

PCB2

320 null128

OS 128

320

128

104

P0 224

0

128

448

672

896

1 K

P2 120568

First Fit

Best Fit

Worst Fit

Page 24: Memory Management

OS/SSSS:24

Dynamic partitioning exampleDynamic partitioning example

224 null672

PCB0P0 Requests 320 units [h0]P1 Requests 224 units [h1]P0 Requests 288 units [h2]P1 exits [releases h1]P2 request 128 [h3] WFP0 releases h0P2 Requests 100 ! FF

4 …668

128 ...896

Free

120 …448

PCB2

320 null128

OS 128

320

128

P2 100

P0 224

0

128

448

672

896

1 K

P2 120568

Who wants that

100 null568

Page 25: Memory Management

OS/SSSS:25

Dynamic partitioning exampleDynamic partitioning example

224 null672

PCB0P0 Requests 320 units [h0]P1 Requests 224 units [h1]P0 Requests 288 units [h2]P1 exits [releases h1]P2 request 128 [h3] WFP0 releases h0P2 Requests 100 ! FF

320 null128

128 ...896

Free

120 …448

PCB2

OS 128

320

128

P2 104

P0 224

0

128

448

672

896

1 K

P2 120568

104 null568

Internal FragmentInternal FragmentWhy ?Why ?

Page 26: Memory Management

OS/SSSS:26

104

Dynamic partitioning exampleDynamic partitioning example

224 null672

PCB0

OS 128

320

128

P0 224

0

128

448

672

896

1 K

120568

320 …128

128 ...896

Free

104 null568

120 …448

4 …668

128 ...896

Free

120 …448

320 …128

100 null568

Needs CoalescingNeeds Coalescing

Case of internal

fragments

Case of exact

placement

P2 exitsSomeone wants 541 unit

Page 27: Memory Management

OS/SSSS:27

Dynamic partitioning exampleDynamic partitioning example

224 null672

PCB0

OS 128

554

128

P0 224

0

128

672

896

1 K

128 ...896

Free

554 null128

Someone wants 541 unit – ok

But Someone Requests 600 !External Fragments and Garbage collection

Needs GarbageNeeds GarbageCollectionCollection

External External FragmentsFragments

Page 28: Memory Management

OS/SSSS:28

Dynamic partitioning exampleDynamic partitioning example

OS 1280

128

512

1 K

OS 1280

128

512

1 K

Going BFGoing BF Going WFGoing WF

 Worst-

FitBest-

Fit

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

     

Free 2090 2090

Page 29: Memory Management

OS/SSSS:29

Dynamic partitioning exampleDynamic partitioning example

OS 1280

128

512

1 K

OS 1280

128

512

1 K

Going BFGoing BF Going WFGoing WF

0

5

10

0 50 100 150 200

Worst-Fit Best-Fit

Page 30: Memory Management

OS/SSSS:30

Dynamic partitioning exampleDynamic partitioning example

OS 1280

128

512

1 K

OS 1280

128

512

1 K

Going BFGoing BF Going WFGoing WF

Random Random FFFF

• Preserves Preserves

large large

blocks for blocks for

futurefuture

• External External

fragmentfragment

ss

• More More

Normally Normally

distributedistribute

dd

• More More

predictablpredictabl

ee

Page 31: Memory Management

OS/SSSS:31

Buddy SystemBuddy System•Entire space available is treated as a single block Entire space available is treated as a single block of 2of 2UU

•If a request of size s such that 2If a request of size s such that 2U-1 U-1 < s <= 2< s <= 2UU, , entire block is allocatedentire block is allocated

Otherwise block is split into two equal buddiesOtherwise block is split into two equal buddiesProcess continues until smallest block greater than or Process continues until smallest block greater than or equal to s is generatedequal to s is generated

H0 Req 100 H0->128H1 Req 240 H0->128

H2 Req 60 H0->128 H2->64 64

H3 Req 256 H0->128 H2->64 64

Release H1 H0->128 H2->64 64

Release H0 128 H2->64 64

H4 Req 100 H4->128 H2->64 64

Release H2 H4->128

Release H3

Release H3

H1->256 512

H1->256

256 512

H1->256 512

256

256 256

256

256 256

H3->256 256

H3->256

H3->256 256

1M

1M

512

H3->256 256

H3->256 256

H3->256

128

128

128

Page 32: Memory Management

OS/SSSS:32

Buddy SystemBuddy System

H0 Req 100 H0->128H1 Req 240 H0->128

H2 Req 60 H0->128 H2->64 64

H3 Req 256 H0->128 H2->64 64

Release H1 H0->128 H2->64 64

Release H0 128 H2->64 64

H4 Req 100 H4->128 H2->64 64

Release H2 H4->128

Release H3

Release H3

H1->256 512

H1->256

256 512

H1->256 512

256

256 256

256

256 256

H3->256 256

H3->256

H3->256 256

1M

1M

512

H3->256 256

H3->256 256

H3->256

128

128

128

Page 33: Memory Management

OS/SSSS:33

H0 Req 100 H0->128H1 Req 240 H0->128

H2 Req 60 H0->128 H2->64 64

H3 Req 256 H0->128 H2->64 64

Release H1 H0->128 H2->64 64

Release H0 128 H2->64 64

H4 Req 100 H4->128 H2->64 64

Release H2 H4->128

Release H3

Release H3

H1->256 512

H1->256

256 512

H1->256 512

256

256 256

256

256 256

H3->256 256

H3->256

H3->256 256

1M

1M

512

H3->256 256

H3->256 256

H3->256

128

128

128

Buddy SystemBuddy System

100/128 60/64 64 256 256/256 256

Page 34: Memory Management

OS/SSSS:34

Buddy SystemBuddy System

H0 Req 100 H0->128H1 Req 240 H0->128

H2 Req 60 H0->128 H2->64 64

H3 Req 256 H0->128 H2->64 64

Release H1 H0->128 H2->64 64

Release H0 128 H2->64 64

H4 Req 100 H4->128 H2->64 64

Release H2 H4->128

Release H3

Release H3

H1->256 512

H1->256

256 512

H1->256 512

256

256 256

256

256 256

H3->256 256

H3->256

H3->256 256

1M

1M

512

H3->256 256

H3->256 256

H3->256

128

128

128

•Internal Fragments !Internal Fragments !

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

Page 35: Memory Management

OS/SSSS:35

Memory Management Memory Management RequirementsRequirements

• Protection

• Relocation

• External Fragments management

• Sharing

• Post Allocation ? Why?

• Managing Requests greater then

physically available

Page 36: Memory Management

OS/SSSS:36

Memory Management Memory Management RequirementsRequirements

• Protection

• Relocation

• External Fragments management

• Sharing

• Post Allocation ? Why?

• Managing Requests greater then

physically available

Page 37: Memory Management

OS/SSSS:37

Thanks Thanks && comments&& comments