lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk,...

32
Memory Management CSC501 Operating Systems Principles 1

Transcript of lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk,...

Page 1: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Memory Management

CSC501 Operating Systems Principles

1

Page 2: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Previous Lectures

q Linker & Loader

q TodayQ Memory Management

2

Page 3: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Why? Why?

Layer

user

filesysteminter-machine net.

device mgr + driversreal-time clock mgr

IPCprocess coordprocess mgr

memory mgr (low)

hardware

user/kernel boundary

memory mgr (high)

Page 4: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Memory Managementq Low-level memory managementQ Manages memory within kernel address spaceQ Allocates address spaces for processesQ Treats memory as a single, exhaustible resourceQ In layered-OS design, it is positioned in the

hierarchy below process managerq High-level memory managementQ Manages pages within address spaceQ Divides memory into abstract resourcesQ Positioned in the hierarchy above device

manager

4

Page 5: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Low-level Memory Management

q Conceptual UsesQ Allocation of process stackv getstk, freestkv Used by OS

Q Allocation of heap storagev getmem, freememv Used by applications and OS

5

Page 6: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Possible Allocation Strategies

q Single free listQ First-fit: scan free list and allocate first hole that

is large enoughQ Next-fit: start search from end of last allocationQ Best-fit: find smallest hole that is adequate Q Worst fit: find largest hole

q Multiple listsQ By exact size (static / dynamic)

q HierarchyQ Binary size allocation

q FIFO cache with above methods6

Page 7: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Example Implementation in Xinu

q Single free listQ Ordered by increasing addressQ Singly-linkedQ Initialized at system startup to all free memory

q Allocation policyQ Heap: first-fitQ Stack: last-fitQ Minimizes fragmentation

7

Page 8: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Result of Xinu Allocation Policy

q Stack allocation from highest free memoryq Heap allocation from lowest free memory

8

Page 9: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Illustration of Xinu Free List

q List in order of addressq Related Files: h/mem.h sys/getmem.c sys/freemem.c

9

Page 10: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

High-level Memory Managementq Memory is cheap today, and getting cheaperQ But applications are demanding more and more

memory, there is never enough! q Let’s take a look at the history

Page 11: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

q Batch systemsQ One program loaded in physical memoryQ Runs to completion

q If job larger than physical memory, use overlaysQ Identify sections of program that v Can run to a resultv Can fit into the available memory

Q Add statement after result to load a new section

Q Like passes in a compiler

In the Beginning (prehistory)…

Page 12: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

q Multiple processes in physical memory at the same timeQ Allows fast switching to a ready processQ Divide physical memory into multiple pieces –

partitioningq Partition requirementsQ Protection – keep processes from smashing

each otherQ Fast execution – memory accesses can’t be

slowed by protection mechanismsQ Fast context switch – can’t take forever to

setup mapping of addresses

In the Beginning (multi-programming) …

Page 13: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

0x00000000

0x0000FFFF

Physical

address space

OS Kernel

Process 1

Process 2

Empty

Process 3

Physical Memory

Page 14: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Loading a Process

q Relocate all addresses relative to start of partitionQ See Linking and Loading

q Memory protection assigned by OSQ Block-by-block to physical memory

q Once process startsQ Partition cannot be moved in memoryQ Why?

Page 15: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

0x00000000

0x0001FFFF

Physical

address space

OS Kernel

Process 1

Empty

Empty

Process 3

Physical Memory

Page 16: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Problem

q What happens when Process 4 comes along and requires space larger than the largest empty partition? v Waitv Complex resource allocation problem for OSv Potential starvation

Page 17: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

OS Kernel

Process 1

Empty

Empty

Process 3

Process 4

Physical Memory

Page 18: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Solution

q Logical Address: an address used by the program that is translated by computer into a physical address each time it is used

q When the program utters 0x00105C, …Q The machine accesses 0x01605C instead

Page 19: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

q Base and Limit registersQ Base added to all addressesQ Limit checked on all memory references

q Loaded by OS at each context switch

logicaladdress

Limit Reg

<

error

no

Reloc Reg

+yes

physicaladdress

PhysicalMemoryCPU

Simplest Implementation

Page 20: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

0x00000000

0x0003FFFF

Physical

address space

OS Kernel

Process 1

Empty

Empty

Process 3

Base

Limit

Physical Memory

Page 21: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Advantagesq No relocation of program addresses at load timeQ All addresses relative to zero

q Built-in protection provided by LimitQ No physical protection per page or block

q Fast executionQ Addition and limit check at hardware speeds within

each instructionq Fast context switchQ Need only change base and limit registers

q Partition can be suspended and moved at any timeQ Process is unaware of changeQ Expensive for large processes!

Page 22: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

0x00000000

0x0003FFFF

Physical

address space

OS Kernel

Process 1

Process 3

Base

Limit

Process 4

Physical Memory

Page 23: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Challenge – Memory Allocation

q Fixed partitionsq Variable partitions

Page 24: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Partitioning Strategies – Fixed

q Fixed Partitions – divide memory into equal sized pieces (except for OS)Q Degree of multiprogramming = number of partitionsQ Simple policy to implement v All processes must fit into partition spacev Find any free partition and load the process

q Problem – Internal Fragmentation Q Unused memory in a partition that is not available to

other processesQuestion:

What is the “right” partition size?

Page 25: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Partitioning Strategies – Variable

q Memory is dynamically divided into partitions based on process needsQ More complex management problemv Need data structures to do tracking of free and

used memoryv New process is allocated memory from hole large

enough to fit itq Problem – External Fragmentation Q Unused memory between partitions that is not too

small to be used by any processes

Page 26: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Partitioning Strategies – Variable

26

OS

process 1

process 2

process 3

OS

process 1

process 3

Process 2Terminates

OS

process 1

process 3

Process 4Starts

process 4

Page 27: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

User’s View of a Program

Question: Can we have multiple sets of “base and limit” registers?

Page 28: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Logical View of Segmentation

1

3

2

4

14

2

3

user space physical memory space

Page 29: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Segmentation

q Logical address consists of a pair:<segment-number, offset>

q Segment table where each entry has:Q Base: contains the starting physical address

where the segments reside in memory.Q Limit: specifies the length of the segment.

Page 30: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

segment 0

segment 1

segment 2

segment 3

segment 4

physical memory

segment #

+

virtual address

<?

raiseprotection fault

no

yes

offset

baselimit

Segment register tableIndex to segment

register table

Physical Address

Segment Lookup

RWXR

X

Page 31: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Segmentationq Common in early minicomputersQ Small amount of additional hardware – 4 or 8

segmentsQ Used effectively in Unix

q Good idea that has persisted and supported in current hardware and OSsQ X86 supports segmentsQ Linux supports segments

Question: Do we still have external

fragmentation problem? If yes, can we further improve it?

Page 32: lec11-mm14. Low-level Memory Management qConceptual Uses QAllocation of process stack vgetstk, freestk vUsed by OS QAllocation of heap storage vgetmem, freemem vUsed by applications

Next Lecture

q Paging

32