Memory - New Mexico Institute of Mining and Technologycs325/spring 11/Lectures/Lec13-Memory.pdf ·...

36
CSE325 Principles of Operating Systems Memory David P. Duggan [email protected] March 24, 2010

Transcript of Memory - New Mexico Institute of Mining and Technologycs325/spring 11/Lectures/Lec13-Memory.pdf ·...

CSE325 Principles of Operating Systems

Memory

David P. Duggan [email protected]

March 24, 2010

3/24/11 CSE325 - Main Memory 2

Where is Memory?

•  Characteristics? •  Issues/challenges?

3/24/11 CSE325 - Main Memory 3

Outline •  Memory management requirements •  Memory hierarchy •  Memory partitioning

– Best-fit, worst-fit, first-fit, next-fit •  Dynamic linking, address relocation,

swapping

3/24/11 4

The External View of the Memory Manager

Hardware

Application Program

File

Mgr

Dev

ice

Mgr

Mem

ory

Mgr

Pr

oces

s Mgr

UNIX

File

Mgr

Dev

ice

Mgr

Mem

ory

Mgr

Pr

oces

s Mgr

Windows

VMQuery() VirtualFree() VirtualLock()

ZeroMemory()

VirtualAlloc()

sbrk() exec()

getrlimit() shmalloc()

3/24/11 CSE325 - Main Memory 5

Memory Manager •  Requirements

– Minimize executable memory access time – Maximize executable memory size – Executable memory must be cost-effective

•  Today’s memory manager: – Allocates primary memory to processes – Maps process address space to primary memory – Minimizes access time using cost-effective

memory configuration – May use static or dynamic techniques

3/24/11 CSE325 - Main Memory 6

Storage Hierarchies

3/24/11 CSE325 - Main Memory 7

The Basic Memory Hierarchy

CPU Registers

Primary Memory (Executable Memory)

e.g. RAM

Secondary Memory e.g. Disk or Tape

3/24/11 CSE325 - Main Memory 8

Exploiting the Hierarchy •  Upward moves are (usually) copy operations

–  Require allocation in upper memory –  Image exists in both higher & lower memories

•  Updates are first applied to upper memory •  Downward move is (usually) destructive

–  Update image in lower memory –  Destroy image in upper memory

•  Place frequently-used info high, infrequently-used info low in the hierarchy

•  Reconfigure as process changes phases

3/24/11 CSE325 - Main Memory 9

Address Space vs Primary Memory

Mapped to object other than memory

Process Address Space Hardware Primary Memory

10

Creating an Executable Program

Source code

• Compile time: Translate elements

• Load time: • Allocate primary memory • Adjust addresses in address space • Copy address space from secondary to primary memory

Loader Process address space

Primary memory

C

Reloc Object code

Link Edit

Library code

Other objects

Secondary memory

• Link time: Combine elements

3/24/11 CSE325 - Main Memory 11

Dynamic Linking •  Linking postponed until execution time

•  Small piece of code, stub, used to locate the appropriate memory-resident library routine

•  Stub replaces itself with the address of the routine, and executes the routine

•  Operating system needed to check if routine is in processes’ memory address

•  Dynamic linking is particularly useful for libraries

3/24/11 CSE325 - Main Memory 12

A Sample Code Segment ... static int gVar; ... int proc_a(int arg){ ... gVar = 7; put_record(gVar); ... }

13

The Relocatable Object module Code Segment Relative���Address Generated Code 0000 ... ... 0008 entry proc_a ... 0220 load =7, R1 0224 store R1, 0036 0228 push 0036 0232 call ‘put_record’ ... 0400 External reference table ... 0404 ‘put_record’ 0232 ... 0500 External definition table ... 0540 ‘proc_a’ 0008 ... 0600 (symbol table) ... 0799 (last location in the code segment)

Data Segment Relative���Address Generated variable space ... 0036 [Space for gVar variable] ... 0049 (last location in the data segment)

... static int gVar; ... int proc_a(int arg){ ... gVar = 7; put_record(gVar); ... }

3/24/11 CSE325 - Main Memory 14

The Linked Program Code Segment Relative���Address Generated Code 0000 (Other modules) ... 1008 entry proc_a ... 1220 load =7, R1 1224 store R1, 0136 1228 push 1036 1232 call 2334 ... 1399 (End of proc_a) ... (Other modules) 2334 entry put_record ... 2670 (optional symbol table) ... 2999 (last location in the code segment)

Data Segment Relative���Address Generated variable space ... 0136 [Space for gVar variable] ... 1000 (last location in the data segment)

15

The Program Loaded at

Location 4000

Relative Address Generated Code 0000 (Other process’s programs) 4000 (Other modules) ... 5008 entry proc_a ... 5036 [Space for gVar variable] ... 5220 load =7, R1 5224 store R1, 7136 5228 push 5036 5232 call 6334 ... 5399 (End of proc_a) ... (Other modules) 6334 entry put_record ... 6670 (optional symbol table) ... 6999 (last location in the code segment) 7000 (first location in the data segment) ... 7136 [Space for gVar variable] ... 8000 (Other process’s programs)

16

Dynamic Relocation

•  Hardware device that maps virtual to physical address •  The value in the relocation register is added to every address

generated by a user process at the time it is sent to memory •  The user program deals with logical addresses; it never sees

the real physical addresses

3/24/11 CSE325 - Main Memory 17

Memory Partitioning

Operating System

Process 3

Process 0

Process 2

Process 1

Unused In Use

Issue: Need a mechanism/policy for loading Pi’s address space into primary memory

Pi

3/24/11 CSE325 - Main Memory 18

Memory Partitioning (Cont.) •  Multiple-partition allocation

–  Hole – block of available memory; holes of various size are scattered throughout memory

–  When a process arrives, it is allocated memory from a hole large enough to accommodate it

–  Operating system maintains information about: a) allocated partitions b) free partitions (hole)

OS"

process 5"

process 8"

process 2"

OS"

process 5"

process 2"

OS"

process 5"

process 2"

OS"

process 5"process 9"

process 2"

process 9"

process 10"

3/24/11 CSE325 - Main Memory 19

Fixed-Partition Memory Mechanism

Operating System

Region 3

Region 2

Region 1

Region 0 N0

N1

N2

N3

Pi

Pi needs ni units

ni

Operating System

Region 0 N

N

N

N

Region 1

Region 2

Region 3

3/24/11 CSE325 - Main Memory 20

Fixed-Partition Memory Best-Fit

Operating System

Region 3

Region 2

Region 1

Region 0 N0

N1

N2

N3

Pi

Internal Fragmentation

• Allocate the smallest hole that is big enough.

Pros and Cons?

3/24/11 CSE325 - Main Memory 21

Fixed-Partition Memory Worst-Fit

Operating System

Region 3

Region 2

Region 1

Region 0 N0

N1

N2

N3

Pi

Pros and Cons?

• Allocate the largest hole.

3/24/11 CSE325 - Main Memory 22

Fixed-Partition Memory First-Fit

Operating System

Region 3

Region 2

Region 1

Region 0 N0

N1

N2

N3

Pi

Pros and Cons?

• Allocate the first hole that is big enough.

3/24/11 CSE325 - Main Memory 23

Fixed-Partition Memory Next-Fit

Operating System

Region 3

Region 2

Region 1

Region 0 N0

N1

N2

N3

Pi

Pi+1

Fixed-Partition strategies were used in batch multiprogramming systems (why?)

Not good for timesharing systems

Pros and Cons?

• Allocate the first hole that is big enough starting from the last placement.

24

Variable Partition Memory Operating System

Operating System

Process 0

Process 6

Process 2

Process 5 Process 4

• Compaction moves program in memory

Operating System

Process 0

Process 6

Process 2

Process 5 Process 4

• External fragmentation

Operating System

Process 0

Process 1

Process 2

Process 3 Process 4

Loader adjusts every address in every absolute module when placed in memory

3/24/11 CSE325 - Main Memory 25

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 •  The process continues until the smallest block

greater than or equal to s is generated

3/24/11 CSE325 - Main Memory 26

3/24/11 CSE325 - Main Memory 27

Program and Process Address Spaces

Relocatable Program Address Space

0

4GB

3GB

Process Address Space

Supervisor Process Address Space

User Process Address Space

Primary Memory

Runtime binding

3/24/11 CSE325 - Main Memory 28

Dynamic Memory Allocation

•  Could use dynamically allocated memory •  Process wants to change the size of its

address space – Smaller ⇒ Creates an external fragment – Larger ⇒ May have to move/relocate the

program •  Allocate “holes” in memory according to

– Best- /Worst- / First- /Next-fit

3/24/11 CSE325 - Main Memory 29

Moving an Executable Image

Executable Program

Loader

Loader

Executable Image

Executable Image

02000

06000

3/24/11 CSE325 - Main Memory 30

A base and a limit register define a logical address space

•  Mapping

•  Protection Relocation register

Limit register

3/24/11 CSE325 - Main Memory 31

Addresses •  Logical

–  reference to a memory location independent of the current mapping of data to memory

–  translation must be made to the physical address •  Relative

–  address expressed as a location relative to some known point

•  Physical –  the absolute address or actual location in main

memory

32

Dynamic Address Relocation CPU

0x02010

0x10000 +

MAR load R1, 0x02010

0x12010

• Program loaded at 0x10000 ⇒ Relocation Register = 0x10000 • Program loaded at 0x04000 ⇒ Relocation Register = 0x04000

Relocation Register

Relative Address

We never have to change the load module addresses!

3/24/11 CSE325 - Main Memory 33

HW address protection with base and limit registers

3/24/11 CSE325 - Main Memory 34

Special Case: Swapping •  Special case of dynamic memory allocation •  Suppose there is high demand for executable

memory •  Equitable policy might be to time-multiplex

processes into the memory (also space-mux) •  Means that process can have its address space

unloaded when it still needs memory –  Usually only happens when it is blocked

3/24/11 CSE325 - Main Memory 35

Swapping

Image for Pj

Image for Pi

Swap Pi out

Swap Pj in

3/24/11 CSE325 - Main Memory 36

Memory Mgmt Strategies •  Fixed-Partition used only in batch systems •  Variable-Partition used everywhere (except

in virtual memory) •  Swapping systems

– Popularized in timesharing – Relies on dynamic address relocation

•  Dynamic Loading (Virtual Memory) – Exploit the memory hierarchy – Paging – Segmentation