Memory Management Basics Don Porter Portions courtesy Emmett - - PowerPoint PPT Presentation

memory management basics
SMART_READER_LITE
LIVE PREVIEW

Memory Management Basics Don Porter Portions courtesy Emmett - - PowerPoint PPT Presentation

COMP 530: Operating Systems Memory Management Basics Don Porter Portions courtesy Emmett Witchel and Kevin Jeffay 1 COMP 530: Operating Systems Review: Address Spaces MAX sys Physical address space The address space supported by the


slide-1
SLIDE 1

COMP 530: Operating Systems

Memory Management Basics

Don Porter Portions courtesy Emmett Witchel and Kevin Jeffay

1

slide-2
SLIDE 2

COMP 530: Operating Systems

Program

P

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

Review: Address Spaces

slide-3
SLIDE 3

COMP 530: Operating Systems

  • 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

COMP 530: Operating Systems

  • 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 : ...

Address Space Generation

slide-5
SLIDE 5

COMP 530: Operating Systems

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

COMP 530: Operating Systems

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

2 register translation

slide-7
SLIDE 7

COMP 530: Operating Systems

  • 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

COMP 530: Operating Systems

  • 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

The Fragmentation Problem

slide-9
SLIDE 9

COMP 530: Operating Systems

  • 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

Dynamic Allocation of Partitions

slide-10
SLIDE 10

COMP 530: Operating Systems

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

First Fit Allocation

slide-11
SLIDE 11

COMP 530: Operating Systems

  • Simplicity!
  • 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

First Fit: Rationale and Implementation

slide-12
SLIDE 12

COMP 530: Operating Systems

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

Best Fit Allocation

slide-13
SLIDE 13

COMP 530: Operating Systems

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

Best Fit: Rationale and Implementation

slide-14
SLIDE 14

COMP 530: Operating Systems

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

Worst Fit Allocation

slide-15
SLIDE 15

COMP 530: Operating Systems

  • 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

Worst Fit: Rationale and Implementation

slide-16
SLIDE 16

COMP 530: Operating Systems

Allocation strategies

  • First fit, best fit and worst fit all suffer from

external fragmentation.

– A. True – B. False

slide-17
SLIDE 17

COMP 530: Operating Systems

  • 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

Eliminating Fragmentation

slide-18
SLIDE 18

COMP 530: Operating Systems

2n-1 Program P’s VAS

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

Sharing Between Processes

slide-19
SLIDE 19

COMP 530: Operating Systems

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 (shared) Program Data (not shared) Run-Time Stack (not shared) Heap (not shared) User Code

Multiple (sub) Name Spaces

slide-20
SLIDE 20

COMP 530: Operating Systems

  • 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

Segmentation

Two ways to encode a virtual address

slide-21
SLIDE 21

COMP 530: Operating Systems

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

Implementing Segmentation

slide-22
SLIDE 22

COMP 530: Operating Systems

  • Segmentation allows sharing

– And dead simple hardware

  • Can easily cache all translation metadata on-chip

– Low latency to translate virtual addresses to physical addresses

  • Two arithmetic operations (add and limit check)
  • … 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?

– stay tuned…

Are we done?