Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation -...

22
1 Operating Systems Memory management Memory 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

Transcript of Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation -...

Page 1: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

1

Operating Systems

Memory management

Memory Management

List of Topics1. Memory Management2. Memory In Systems Design3. Binding Times4. Introduction to Memory Management5. Raw Memory Model6. Single User Contiguous Memory7. Relocation - Why and How8. Overlay Management

Topics (continued)

9. Protection10. Fixed Partitions11. Nonuniform Sized Fixed Partitions12. Uniformly Sized Fixed Partitions13. Simple Paging14. Benefits of Simple Paging15. Page Tables16. Translation Lookaside Buffers17. Hierarchical Address Caching

Page 2: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

2

Topics (continued)

18. Dynamic Partitions19. Fragmentation20. Internal Fragmentation21. External Fragmentation22. Coalescing Holes23. Compaction24. Dynamic Partition Placement25. Simple Segmentation26. Memory Layout of A C Program

Topics (continued)

27. malloc28. sbrk29. Memory Hierarchy

Memory In Systems design

CPU memoryI/O

System

Figure 1: memory Connects CPU and Peripherals

Page 3: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

3

Binding 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)

Page 4: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

4

Introduction to Memory Management

Memory management issues:1. Relocation 2. Protection3. Sharing4. Logical Organization5. Physical Organization

Consider a series of solutions startingwith the most primitive first

Raw Memory Model

The raw memory provides no services and gives the programmer complete control. 0

1 MB

Figure 3: Raw Machine Model

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

Page 5: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

5

Single User Contiguous Memory(continued)

Monitor

User

32K

0

fence register

Figure 4: Single User Contiguous Memory

Relocation - Why and How

Relocation refers to the ability to store aprogram at an arbitrary base memory address.

Actual memory locations have physicalor absolute addresses, user program's access these locations using logicaladdresses.

Relocation - Why and How (continued)

+CPU

14000

memory

Base register

Logical address

346

Physical address

14346

Figure 5: Address Translation

Page 6: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

6

Overlay Management

Overlays have gone out of fashion withcheaper 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]

Protection

It is undesirable to permit user programs (accidentally or intentionally) to accesses memory outside of their partition.

Page 7: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

7

Protection (continued)

Monitor

User

32K

0

fence register

Figure 7:Protectionin Resident

MonitorModel

Fixed Partitions

Fixed partitioning refers to memory being split into contiguous non overlapping regions of precomputed sizes.

Fixed sized partitions make the selectionof a partition for a job easy.

Nonuniform Sized Fixed Partitions

Fixed Partitions may have differing sizes.

Page 8: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

8

Operatingsystem

NewProcess

Figure 8:Nonuniform

fixed Partitions [3]

Uniformly Sized Fixed Partitions

Memory is frequently partitioned into uniformly sized regions.

Operatingsystem512 K

512 K512 K

512 K512 K

512 K512 K512 K

Figure 9:UniformFixed PartitionAllocation [3]

Page 9: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

9

Simple Paging

Paging provides relocation, and splitsmemory into fixed length partitionscalled 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]

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]

Page 10: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

10

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 Paging

Simple paging allowsdiscontiguous storage for memory objects exceeding the page frame size.

Virtualmemory

...

...

Addressmapping

mechanism

Realstorage

Contiguousvirtual

storagelocations

Page 11: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

11

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

Page Tables (continued)

d

b

p

p1 dp1

p

bPage # Displacement

Virtual address}v=(p,d)

Page

b Map Table

p

}Real

address

b+p

Translation Lookaside Buffers

Translation lookaside buffers (TLB) eliminate one physical memory reference using special associative memory, which addressed by its contents in O(1) parallelsearch time.

Page 12: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

12

d }Virtual

addressV=(p,d)

p p1

Page # Displacement

p

p1 d }Real

Address r

Frame # Displacement

Associative map

Associative Memory Lookup

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

Hierarchical Address Caching (continued)

Address ofPage table b

Page tableorigin register

p1

Page #p

Displacementd

Virtual address v=(p,d)

+

Partial associative

p p1p

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

Page 13: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

13

Dynamic Partitions

Dynamically partitioned memory allows placement of relocatable code in variable size contigous memory regions.

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

Fragmentation

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

Page 14: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

14

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.

Internal Fragmentation

Internal fragmentation occurs when fixed size partitions are too large.

Pag

e 2

Pag

e 1

Pag

e 0

Inte

rnal

frag

men

tati

on

Logical AddressPage#=1,Offset=478

000001 01110111110

478

Figure 16:InternalFragmentation[3]

Page 15: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

15

External Fragmentation

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 Holes

Adjacent holes in dynamic partitionsshould be coalesced into a single largerhole.

Page 16: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

16

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.

Compaction

If the amount of memory available in theholes is large enough to service arequest, the holes may made contiguousby compacting storage.

operatingsystems

In use

In use

In use

Free

Free

Free

operatingsystems

Free

In useIn use

In use

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

Page 17: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

17

(a) First-Fit Strategy

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

Dynamic Partition Placement

Operating systems

16K hole

14K hole

5K hole

30K hole

In use

In use

In use

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

Page 18: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

18

Operating systems

16K hole

14K hole

5K hole

30K hole

In use

In use

In use

e 5Kc 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.

Operating systems

16K hole

14K hole

5K hole

30K hole

In use

In use

In use

g 30Ka 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

Page 19: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

19

Simple Segmentation

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 Programs

Traditional Unix/C memory images of programs use segments.

Page 20: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

20

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 fileby exec

malloc

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

Page 21: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

21

sbrk

A user program can exhaust its defaultheap space allocation.

The Unix sbrk system call increases data segment allocation at run time.

Memory allocationfunction malloc

applicationcode

sbrksystem call

kernel

userprocess

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

Page 22: Operating Systems - Computer Sciencemoorthy/courses/os01/Lectures/OSLecture9.pdf · Relocation - Why and How Relocation refers to the ability to store a program at an arbitrary base

22

Memory Hierarchy

Registers

Cache

Main Memory

Magnetic Disk (Cache)

Backups Optical Juke Box Remote Access