Operating Systems Memory management. Memory Management List of Topics 1. Memory Management 2. Memory...

64
Operating Systems Memory management

Transcript of Operating Systems Memory management. Memory Management List of Topics 1. Memory Management 2. Memory...

Operating SystemsOperating Systems

Memory managementMemory management

Memory ManagementMemory Management

List of Topics

1. Memory Management

2. Memory In Systems Design

3. Binding Times

4. Introduction to Memory Management

5. Raw Memory Model

6. Single User Contiguous Memory

7. Relocation - Why and How

8. Overlay Management

List of Topics

1. Memory Management

2. Memory In Systems Design

3. Binding Times

4. Introduction to Memory Management

5. Raw Memory Model

6. Single User Contiguous Memory

7. Relocation - Why and How

8. Overlay Management

Topics (continued)

9. Protection

10. Fixed Partitions

11. Nonuniform Sized Fixed Partitions

12. Uniformly Sized Fixed Partitions

13. Simple Paging

14. Benefits of Simple Paging

15. Page Tables

16. Translation Lookaside Buffers

17. Hierarchical Address Caching

Topics (continued)

9. Protection

10. Fixed Partitions

11. Nonuniform Sized Fixed Partitions

12. Uniformly Sized Fixed Partitions

13. Simple Paging

14. Benefits of Simple Paging

15. Page Tables

16. Translation Lookaside Buffers

17. Hierarchical Address Caching

Topics (continued)

18. Dynamic Partitions

19. Fragmentation

20. Internal Fragmentation

21. External Fragmentation

22. Coalescing Holes

23. Compaction

24. Dynamic Partition Placement

25. Simple Segmentation

26. Memory Layout of A C Program

Topics (continued)

18. Dynamic Partitions

19. Fragmentation

20. Internal Fragmentation

21. External Fragmentation

22. Coalescing Holes

23. Compaction

24. Dynamic Partition Placement

25. Simple Segmentation

26. Memory Layout of A C Program

Topics (continued)

27. malloc

28. sbrk

29. Memory Hierarchy

Topics (continued)

27. malloc

28. sbrk

29. Memory Hierarchy

Memory In Systems designMemory In Systems design

CPU memoryI/O

System

Figure 1: memory Connects CPU and Peripherals

Figure 1: memory Connects CPU and Peripherals

Binding TimesBinding Timessource

program

compiler orassembler

objectmodel

compiletime

linkage editor

load module

loader

Otherobject

modules

system library

loadtime

in-memorybinary

memoryimage

dynamicallyloaded system

library

executiontime

(run time)

Introduction to Memory ManagementIntroduction to Memory Management

Memory management issues:

1. Relocation

2. Protection

3. Sharing

4. Logical Organization

5. Physical Organization

Consider a series of solutions starting

with the most primitive first

Memory management issues:

1. Relocation

2. Protection

3. Sharing

4. Logical Organization

5. Physical Organization

Consider a series of solutions starting

with the most primitive first

Raw Memory ModelRaw Memory Model

The raw memory provides no services

and gives the programmer complete

control.

The raw memory provides no services

and gives the programmer complete

control. 0

1 MB

Figure 3: Raw Machine Model

Figure 3: Raw Machine Model

Single User Contiguous MemorySingle User Contiguous Memory

Primitive operating systems (such as MS-DOS and CP/M) provide some interfaces to the hardware but not much else in the way of services.

Primitive operating systems (such as MS-DOS and CP/M) provide some interfaces to the hardware but not much else in the way of services.

Single User Contiguous Memory(continued)

Single User Contiguous Memory(continued)

Monitor

User

32K

0

fence register

Figure 4: Single User Contiguous MemoryFigure 4: Single User Contiguous Memory

Relocation - Why and HowRelocation - Why and How

Relocation refers to the ability to store a

program at an arbitrary base memory

address.

Actual memory locations have physical

or absolute addresses, user program's

access these locations using logical

addresses.

Relocation refers to the ability to store a

program at an arbitrary base memory

address.

Actual memory locations have physical

or absolute addresses, user program's

access these locations using logical

addresses.

Relocation - Why and How (continued)

Relocation - Why and How (continued)

+CPU

14000

memory

Base register

Logical address

346

Physical address

14346

Figure 5: Address TranslationFigure 5: Address Translation

Overlay ManagementOverlay Management

Overlays have gone out of fashion with

cheaper memory, users (and compilers)

determine which code to swap in and out.

Overlays have gone out of fashion with

cheaper memory, users (and compilers)

determine which code to swap in and out.

symboltable

commonroutines

overlaydriver

pass 1 pass 2

20K

30K

10K

80K70K

Figure 6:OverlayManagementExample [2]

Figure 6:OverlayManagementExample [2]

ProtectionProtection

It is undesirable to permit user programs

(accidentally or intentionally) to accesses

memory outside of their partition.

It is undesirable to permit user programs

(accidentally or intentionally) to accesses

memory outside of their partition.

Protection (continued)Protection (continued)

Monitor

User

32K

0

fence register

Figure 7:Protectionin Resident

MonitorModel

Figure 7:Protectionin Resident

MonitorModel

Fixed PartitionsFixed Partitions

Fixed partitioning refers to memory being

split into contiguous non overlapping

regions of precomputed sizes.

Fixed sized partitions make the selection

of a partition for a job easy.

Fixed partitioning refers to memory being

split into contiguous non overlapping

regions of precomputed sizes.

Fixed sized partitions make the selection

of a partition for a job easy.

Nonuniform Sized Fixed PartitionsNonuniform Sized Fixed Partitions

Fixed Partitions may have differing sizes.Fixed Partitions may have differing sizes.

Operatingsystem

NewProcess

Figure 8:Nonuniform

fixed Partitions [3]

Figure 8:Nonuniform

fixed Partitions [3]

Uniformly Sized Fixed PartitionsUniformly Sized Fixed Partitions

Memory is frequently partitioned into

uniformly sized regions.

Memory is frequently partitioned into

uniformly sized regions.

Operatingsystem512 K

512 K

512 K

512 K

512 K

512 K

512 K

512 K

Figure 9:UniformFixed PartitionAllocation [3]

Figure 9:UniformFixed PartitionAllocation [3]

Simple PagingSimple Paging

Paging provides relocation, and splits

memory into fixed length partitions

called frames.

Paging provides relocation, and splits

memory into fixed length partitions

called frames.

Page frame 0

Page frame 1

Page frame 2

Page frame 3

0

p-1p

2p-12p

3p-13p

4p-1

Figure 10:SimplePaging

[1]

Figure 10:SimplePaging

[1]

4p

5p-15p

6p-16p

7p-17p

8p-1

Page frame 4

Page frame 5

Page frame 6

Page frame 7

Figure 10:SimplePaging(continued)

[1]

Figure 10:SimplePaging(continued)

[1]

Pageframe

number

01234567

Pageframesize

pppppppp

Range of realstorage

addresses

0 p-1p 2p-12p 3p-13p 4p-14p 5p-15p 6p-16p 7p-17p 8p-1

Benefits of Simple PagingBenefits of Simple Paging

Simple paging allows discontiguous

storage for memory objects exceeding

the page frame size.

Simple paging allows discontiguous

storage for memory objects exceeding

the page frame size.

Virtualmemory

...

...

Addressmapping

mechanism

Realstorage

Contiguousvirtual

storagelocations

Page TablesPage Tables

One simple mechanism is to allocate

some real memory space for a table, and

hash page addresses using the high

order address bits as pointers into the

page table. There are 2 real memory

accesses per virtual memory access.

One simple mechanism is to allocate

some real memory space for a table, and

hash page addresses using the high

order address bits as pointers into the

page table. There are 2 real memory

accesses per virtual memory access.

Page Tables (continued)Page Tables (continued)

d

b

p

p1 dp1

p

bPage # Displacement

Virtual address} v=(p,d)

Page

bMap Table

p

}Real

address

b+p

Translation Lookaside BuffersTranslation Lookaside Buffers

Translation lookaside buffers (TLB)

eliminate one physical memory reference

using special associative memory, which

addressed by its contents in O(1) parallel

search time.

Translation lookaside buffers (TLB)

eliminate one physical memory reference

using special associative memory, which

addressed by its contents in O(1) parallel

search time.

d }Virtual addressV=(p,d)

p p1

Page # Displacement

p

p1 d }Real

Address r

Frame # Displacement

Associative map

Associative Memory LookupAssociative Memory Lookup

Hierarchical Address CachingHierarchical Address Caching

Rather than placing all addresses in the

TLB recently/frequently used addresses

are stored in associative memory, with

misses being serviced by the page table.

Rather than placing all addresses in the

TLB recently/frequently used addresses

are stored in associative memory, with

misses being serviced by the page table.

Hierarchical Address Caching (continued)Hierarchical Address Caching (continued)

Address ofPage table b

Page tableorigin register

p1

Page #p

Displacement

d

Virtual address v=(p,d)

+

Partial associative

p p1

p

Try this first

Performed only if no match in associative map b+p Real address

Displacementd

Frame# p1

Only if match in associative map

Only if no match in associative map

Direct map (all pages)

Map (most active pages only

Dynamic PartitionsDynamic Partitions

Dynamically partitioned memory allows

placement of relocatable code in variable

size contigous memory regions.

Dynamically partitioned memory allows

placement of relocatable code in variable

size contigous memory regions.

Dynamic Partitions (continued)Dynamic Partitions (continued)

user needs

IHGFEDCBA

9K.18K.11K.32K.14K.25K.10K.20K.15K.

Operatingsystem

UserA 15K

Operating

User

User

UserUser

User

User

User

User

User

Operating Operatingsystem systemsystem

Free

B 20K

A 15KA 15KA 15K

B 20K B 20K

FreeFree

C 10K C 10K

D 25KFree

FragmentationFragmentation

Fragmentation makes available memory useless by breaking it into discontiguous pieces too small to use.

Fragmentation makes available memory useless by breaking it into discontiguous pieces too small to use.

Fragmentation (continued)Fragmentation (continued)

There are two categories of memory

fragmentation:

1. Internal Fragmentation --- A fixed partition contains more memory than required by the user, and some is wasted.

2. External Fragmentation --- Results from the holes left by dynamic partitions.

There are two categories of memory

fragmentation:

1. Internal Fragmentation --- A fixed partition contains more memory than required by the user, and some is wasted.

2. External Fragmentation --- Results from the holes left by dynamic partitions.

Internal FragmentationInternal Fragmentation

Internal fragmentation occurs when fixed

size partitions are too large.

Internal fragmentation occurs when fixed

size partitions are too large.

Pag

e 2

Pag

e 1

Pag

e 0

Inte

rna

lfr

ag

me

nta

tio

n

Logical AddressPage#=1,Offset=478

000001 01110111110

478

Figure 16:InternalFragmentation[3]

Figure 16:InternalFragmentation[3]

External FragmentationExternal Fragmentation

External fragmentation happens when

dynamic partitions are released. The

fragments are frequently called holes.

External fragmentation happens when

dynamic partitions are released. The

fragments are frequently called holes.

operatingsystem

User A

User B

User C

User D

User E

Hole

operatingsystem

User A

User C

User D

User E

Hole

operatingsystem

User A

User C

User E

Hole

Hole Hole

Hole

User B finishes and frees its storage. User D

finishes and frees its storage.

Coalescing HolesCoalescing Holes

Adjacent holes in dynamic partitions

should be coalesced into a single larger

hole.

Adjacent holes in dynamic partitions

should be coalesced into a single larger

hole.

operatingsystem

other users

5K user A

otherusers

2K Hole 2K Hole

5K Hole

otherusers

other users

operatingsystem

7K Hole

otherusers

other users

operatingsystem

User A finishes and frees its storage

Operating system combines adjacent

holes to form a single larger hole.

CompactionCompaction

If the amount of memory available in the

holes is large enough to service a

request, the holes may made contiguous

by compacting storage.

If the amount of memory available in the

holes is large enough to service a

request, the holes may made contiguous

by compacting storage.

operatingsystems

In use

In use

In use

Free

Free

Free

operatingsystems

Free

In use

In use

In use

Operating system places all “in use” blocks together leaving free storage as a single, large hole.

(a) First-Fit Strategy

Place job in first storage hole on free storage place list in which it will fit

(a) First-Fit Strategy

Place job in first storage hole on free storage place list in which it will fit

Dynamic Partition PlacementDynamic Partition Placement

Operating systems

16K hole

14K hole

5K hole

30K hole

In use

In use

In use

a 16K

c 14K

e 5K

g 30K...

Request for 13K

Free Storage List

Start Address

Length

(kept in storage address order, or something in random order.)

0

a

b

c

d

e

f

g

h

(b) Best-Fit Strategy

Place job in the smallest possible hole in which it will fit.

(b) Best-Fit Strategy

Place job in the smallest possible hole in which it will fit.

Operating systems

16K hole

14K hole

5K hole

30K hole

In use

In use

In use

e 5K

c 14K

a 16K

g 30K...

Request for 13KStart

Address Length

(Kept in ascending order by hole size.)

0

a

b

c

d

e

f

g

h

Free Storage List

(c) Worst-Fit Strategy

Place job in the largest possible hole in which it will fit.

(c) Worst-Fit Strategy

Place job in the largest possible hole in which it will fit.

Operating systems

16K hole

14K hole

5K hole

30K hole

In use

In use

In use

g 30K

a 16K

c 14K

e 5K...

Request for 13KStart

Address Length

(Kept in descending order by hole size.)

0

a

b

c

d

e

f

g

h

Free Storage List

Simple SegmentationSimple Segmentation

Segmentation provides relocation, and

supports contiguous variable length

partitions.

Segmentation often provides

protection (counterexample Intel 8086).

Segmentation provides relocation, and

supports contiguous variable length

partitions.

Segmentation often provides

protection (counterexample Intel 8086).

CPU <

Logical address

no

Trap; address error

+yes

limit base

memory

Memory Layout of A C ProgramsMemory Layout of A C Programs

Traditional Unix/C memory images of

programs use segments.

Traditional Unix/C memory images of

programs use segments.

high address

low address text

uninitialized data(bss)

heap

stack

Initialized data

Command-line arguments and environment variables

initialized to zero by exec

read from program file by exec

mallocmalloc

Programmers often want to allocate data objects which persist beyond the function call creating them (e.g. constructors in OOP).

In C and C++ the malloc operator maintains a linked list of data objects, in the user program's Data segment (on the Heap).

Programmers often want to allocate data objects which persist beyond the function call creating them (e.g. constructors in OOP).

In C and C++ the malloc operator maintains a linked list of data objects, in the user program's Data segment (on the Heap).

user data

user data

user data

user data

user data

user data

In Use List

Free List

Key

Memory management Information

sbrksbrk

A user program can exhaust its default

heap space allocation.

The Unix sbrk system call increases data

segment allocation at run time.

A user program can exhaust its default

heap space allocation.

The Unix sbrk system call increases data

segment allocation at run time.

Memory allocationfunction malloc

applicationcode

sbrksystem call

kernel

userprocess

Memory HierarchyMemory Hierarchy

Users want to:

1. Increase their address space, using slow cheaper memory to extend their more expensive faster memory.

2. Increase the speed at which they can the extended memory by using small amounts of expensive fast memory.

Users want to:

1. Increase their address space, using slow cheaper memory to extend their more expensive faster memory.

2. Increase the speed at which they can the extended memory by using small amounts of expensive fast memory.

Memory HierarchyMemory Hierarchy

Registers

Cache

Main Memory

Magnetic Disk (Cache)

Backups Optical Juke Box Remote Access