P Starting at address 0, going to address MAX prog 0 But where do - - PowerPoint PPT Presentation

p
SMART_READER_LITE
LIVE PREVIEW

P Starting at address 0, going to address MAX prog 0 But where do - - PowerPoint PPT Presentation

Memory Management Basics 1 Basic Memory Management Concepts Address spaces MAX sys Physical address space The address space supported by the hardware Starting at address 0, going to address MAX sys MAX prog Logical/virtual address space


slide-1
SLIDE 1

1

Memory Management Basics

slide-2
SLIDE 2

2

Program

P

Basic Memory Management Concepts

Address spaces

Physical address space — The address space supported by the hardware

Ø Starting at address 0, going to address MAXsys

Logical/virtual address space — A process’s view of its own memory

Ø Starting at address 0, going to address MAXprog

MAXsys

MAXprog

MOV r0, @0xfffa620e But where do addresses come from?

slide-3
SLIDE 3

3

Which is bigger, physical or virtual address space?

Ø A. Physical address space Ø B. Virtual address space Ø C. It depends on the system.

slide-4
SLIDE 4

4

Basic Concepts

Address generation

The compilation pipeline

prog P : : foo() : : end P P: : push ... inc SP, x jmp _foo : foo: ... : push ... inc SP, 4 jmp 75 : ... 75 1100 1175

Library Routines

1000 175

Library Routines

100

Compilation Assembly Linking Loading

: : : jmp 1175 : ... : : : jmp 175 : ...

slide-5
SLIDE 5

5

Program Relocation Program issues virtual addresses Machine has physical addresses. If virtual == physical, then how can we have multiple programs resident concurrently? Instead, relocate virtual addresses to physical at run time.

Ø While we are relocating, also bounds check addresses for safety.

I can relocate that program (safely) in two registers…

slide-6
SLIDE 6

6

Basic Concepts (Cont’d.)

Address Translation

MAXsys Program Program P’s logical address space

MAXprog

1000 1500 CPU

+

1000 Base Register

Logical Addresses

500 Limit Register

MEMORY EXCEPTION Physical Addresses yes no

Instructions P’s physical address space

slide-7
SLIDE 7

7

With base and bounds registers, the OS needs a hole in physical memory at least as big as the process.

Ø A. True Ø B. False

slide-8
SLIDE 8

8

Evaluating Dynamic Allocation Techniques

The fragmentation problem

External fragmentation

Ø Unused memory between units of allocation Ø E.g, two fixed tables for 2, but a party of 4

Internal fragmentation

Ø Unused memory within a unit of allocation Ø E.g., a party of 3 at a table for 4

MAX Program R’s PAS Program Q’s PAS

Execution Stack Program Code (“text”) Data Execution Stack

slide-9
SLIDE 9

9

Simple Memory Management Schemes

Dynamic allocation of partitions

Simple approach:

Ø Allocate a partition when a process is admitted into the system Ø Allocate a contiguous memory partition to the process

MAX Program P2 Program P3 Program P1 P5 Program P4

OS keeps track of... Full-blocks Empty-blocks (“holes”) Allocation strategies First-fit Best-fit Worst-fit

slide-10
SLIDE 10

10

First Fit Allocation

To allocate n bytes, use the first available free block such that the block size is larger than n. 500 bytes 1K bytes 2K bytes To allocate 400 bytes, we use the 1st free block available 2K bytes 500 bytes

slide-11
SLIDE 11

11

Rationale & Implementation

Simplicity of implementation Requires:

Ø Free block list sorted by address Ø Allocation requires a search for a suitable partition Ø De-allocation requires a check to see if the freed partition could be merged with adjacent free partitions (if any)

Advantages

Simple

Tends to produce larger free blocks toward the end

  • f the address space

Disadvantages

Slow allocation

External fragmentation

slide-12
SLIDE 12

12

Best Fit Allocation To allocate n bytes, use the

smallest available free block such that the block size is larger than n. 500 bytes 1K bytes 2K bytes To allocate 400 bytes, we use the 3rd free block available (smallest) 1K bytes 2K bytes

slide-13
SLIDE 13

13

Rationale & Implementation

To avoid fragmenting big free blocks To minimize the size of external fragments produced Requires:

Ø Free block list sorted by size Ø Allocation requires search for a suitable partition Ø De-allocation requires search + merge with adjacent free partitions, if any

Advantages

Works well when most allocations are of small size

Relatively simple

Disadvantages

External fragmentation

Slow de-allocation

Tends to produce many useless tiny fragments (not really great)

Doug Lea’s malloc “In most ways this malloc is a best-fit allocator”

slide-14
SLIDE 14

14

Worst Fit Allocation

To allocate n bytes, use the largest available free block such that the block size is larger than n. 500 bytes 1K bytes 2K bytes To allocate 400 bytes, we use the 2nd free block available (largest) 1K bytes

slide-15
SLIDE 15

15

Rationale & Implementation

To avoid having too many tiny fragments Requires:

Ø Free block list sorted by size Ø Allocation is fast (get the largest partition) Ø De-allocation requires merge with adjacent free partitions, if any, and then adjusting the free block list

Advantages

Works best if allocations are of medium sizes

Disadvantages

Slow de-allocation

External fragmentation

Tends to break large free blocks such that large partitions cannot be allocated

slide-16
SLIDE 16

16

Allocation strategies

First fit, best fit and worst fit all suffer from external fragmentation.

Ø A. True Ø B. False

slide-17
SLIDE 17

17

Dynamic Allocation of Partitions

Eliminating Fragmentation

Compaction

Ø Relocate programs to coalesce holes

MAX Program P2 Program P3 Program P1 Program P4

Suspended

suspended queue ready queue semaphore/condition queues

Waiting Running Ready

?

Swapping Ø Preempt processes & reclaim their memory

slide-18
SLIDE 18

18

2n-1 Program P’s VAS Memory Management

Sharing Between Processes

Schemes so far have considered only a single address space per process

Ø A single name space per process Ø No sharing

Program P’s VAS Program Data Program Text Heap Run-Time Stack

How can one share code and data between programs without paging?

slide-19
SLIDE 19

19

Multiple Name Spaces

Example — Protection/Fault isolation & sharing

2n-1 2n1-1 2n2-1 2n3-1 2n4-1

2n6-1

Libraries

2n5-1

Program Data Program Text Heap Run-Time Stack Program Text Program Data Run-Time Stack Heap User Code

slide-20
SLIDE 20

20

Supporting Multiple Name Spaces

Segmentation

New concept: A segment — a memory “object”

Ø A virtual address space

A process now addresses objects —a pair (s, addr)

Ø s — segment number Ø addr — an offset within an object

❖ Don’t know size of object, so 32 bits for offset?

Segment + Address register scheme

s addr

Single address scheme n1 n2

s

n

addr

slide-21
SLIDE 21

21

Implementing Segmentation

Base + Limit register scheme

Program 1000 1500

+

1000

Base Register Logical Addresses

500

Limit Register

MEMORY EXCEPTION

Physical Memory yes no

P’s Segment

Segment Table

s

CPU

n 32

s

  • Program

P

base limit

STBR Add a segment table containing base & limit register values

slide-22
SLIDE 22

22

Memory Management Basics

Are We Done?

Segmentation allows sharing … but leads to poor memory utilization

Ø We might not use much of a large segment, but we must keep the whole thing in memory (bad memory utilization). Ø Suffers from external fragmentation Ø Allocation/deallocation of arbitrary size segments is complex

How can we improve memory management?

Ø Paging