Top Banner
CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt
43

Introduction to Memory Management

Jan 08, 2016

Download

Documents

lilika

Introduction to Memory Management. Lecture 28 Lawrence Angrave/Klara Nahrstedt. CS241 Administrative. Read Stallings Chapter 7 & 7A about Memory Management Regular Quiz 9 this week on Memory Management Discussion Sessions this week (on Memory Management – needed for LMP2) - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Memory Management

 CS 241 Spring 2007System Programming

      

1

Introduction to Memory Management

Lecture 28

Lawrence Angrave/Klara Nahrstedt

Page 2: Introduction to Memory Management

2

CS241 Administrative Read Stallings Chapter 7 & 7A about Memory

Management Regular Quiz 9 this week on Memory Management Discussion Sessions this week (on Memory Management

– needed for LMP2) LMP2 (Long Machine Problem) starts today

LMP 2 is part of three major assignments (LMP1, LMP2, LMP3) LMP2 will be split into two parts, PART I and overall LMP2 delivery. You will need to deliver PART I by Monday, April 9 and the final

overall LMP2 will be due by April 16. For each delivery you will receive points.

LMP2 quiz will be on April 16. The graders will provide feedback on the Part I that should assist you

in the overall LMP2 delivery.

Page 3: Introduction to Memory Management

3

Contents

Addressing Requirements for a ProcessNames and Binding, Loading, Compiling, Run-Time

Storage hierarchySimple Programming

OverlaysResident monitorMulti-Programming

Fixed PartitioningRelocation RegisterSwapping

Page 4: Introduction to Memory Management

4

Names and Binding

Naming = very deep theme that comes up everywhere

IndirectionSymbolic names

Logical names

Physical names

Most used by application programmers

Converted by system programmers of compilers, OS

Page 5: Introduction to Memory Management

5

Symbolic Names

Symbolic names: known in a context or path File names

Program names

Printer/device names

User names

Variables

Convenient to use/reference

Page 6: Introduction to Memory Management

6

Logical Names

Logical names: label a specific entity

… but independent of specific physical entity

Inode number

Job number

Major, minor device numbers

uid, pid, gid

Absolute address in program

Page 7: Introduction to Memory Management

7

Physical Names

Physical names: address of entityPhysical Inode address on disk or memory (track,

cylinder, …)

Entry point or variable address

Memory-mapped register addresses

Process control block address

Difficult to use by programmersTherefore, system software (compilers, loaders, OS)

are usually the ones that convert symbolic names/logic names to physical names

Page 8: Introduction to Memory Management

8

Binding Time of Names

Binding: map names to locations (values)Late binding … Early binding

Different timesSource program (.c file)

Compile time

Object module (.o file)Link time

Load module Load time

Run-time

Page 9: Introduction to Memory Management

9

Binding at Compile & Link time

Create object codegcc -c part1.c

gcc -c part2.c

gcc -o wowee part1.o part2.o

gcc uses linker program 'ld' to link object code together.

Page 10: Introduction to Memory Management

10

What does a process look like? (Unix)

Process address space divided into “segments” text (code), data, heap (dynamic data), and stack

heap

stack

code

initialized data

address 2^n-1

address >= 0

Page 11: Introduction to Memory Management

11

Who Binds What?

Heap: constructed and layout by allocator (malloc)compiler, linker not involved other than saying where it can start namespace constructed dynamically and managed by

programmer (names stored in pointers, and organized using data structures)

Stack: alloc dynamic (proc call), layout by compilernames are relative off of stack pointermanaged by compiler (alloc on proc entry, dealloc on exit)linker not involved because name space entirely local: compiler

has enough information to build it.

Global data & code: allocation static (compiler), layout (linker)compiler emits them and can form symbolic references between

them (“jalr _printf”)linker lays them out, and translates references

Page 12: Introduction to Memory Management

12

Compiling• Compiler:

– doesn’t know where data/code should be placed in the process’s address space

– assumes everything starts at zero– emits symbol table that holds the

name and offset of each created object

– routine/variables exported by the file are recorded global definitionglobal definition

• Simpler perspective:– code is in a big char array– data is in another big char array– compiler creates (object name,

index) tuple for each interesting thing

– linker then merges all of these arrays

40

foo: call printf ret bar: … ret

0

foo: 0bar: 40

Page 13: Introduction to Memory Management

13

Linkers (Linkage editors)

Unix: ldusually hidden behind compiler

Three functions:collect together all pieces of a programcoalesce like segmentsfix addresses of code and data so the program can run

Result: runnable program stored in new object fileWhy can’t compiler do this?

Limited world view: one file, rather than all files

Note *usually*:linkers only shuffle segments, but do not rearrange their internals. E.g., instructions not reordered; routines that are never called are not

removed from a.out

Page 14: Introduction to Memory Management

14

Loading before Running

On Unix systems, read by “loader”

reads all code/data segs into buffer cache; maps code (read only) and initialized data (r/w) into addr space

Optimization opportunities:Zero-initialized data does not need to be read in.Demand load: wait until code used before get from diskCopies of same program running? Share codeMultiple programs use same routines: share code (harder)

ld loader Cache

Compile time runtime

Page 15: Introduction to Memory Management

15

Run-time

Maps virtual addresses to physical addresses

VM, overlaying, recursion, relocation

Page 16: Introduction to Memory Management

16

Binding Time Trade-offs(1)

Early binding Compiler

+ Produces efficient code

+ Allows checking to be done early

+ Allows estimates of running time and space

- inflexible

- require fixed hardware

- difficult to support multiprogramming

Page 17: Introduction to Memory Management

17

Binding Time Trade-offs(2)

Delayed binding Linker, loader

+ Produces efficient code

+ Allows separate compilation

+ Provides libraries, system interfaces

+ Portability and sharing of object code

+ Checking of consistency

- inflexible

- medium difficult to support multiprogramming

Page 18: Introduction to Memory Management

18

Binding Time Trade-offs (3)

Late binding VM, dynamic linking and loading, overlaying, interpreting

- Code less efficient

- Checks must be done at run time

+ Flexible

+ Provides abstraction of hardware

+ Allows dynamic reconfiguration

Page 19: Introduction to Memory Management

19

Storage Hierarchy

Cost

$400 a chip

$10-4 per byte

$10-8 per byte

$10-9 per byte

Size

213 bytes

227 bytes

230 bytes

240 bytes

CPU Reg

Cache

Memory

Secondary Storage

32-64 bits

4-128 words

512-16k words

Page 20: Introduction to Memory Management

20

Single Programming

Overlay Manager

Overlay Area

Main Program

Overlay 1

Overlay 2

Overlay 3

Secondary Storage

Overlay 1Overlay 2Overlay 3Overlay 1

0K

5k

7k

12k

Page 21: Introduction to Memory Management

21

User

Resident Monitor

Free

8k

32k

User

UserUser

Monitor

UserUserUser

0K

Page 22: Introduction to Memory Management

22

Multiprogramming

How to keep multiple programs in memory?

Page 23: Introduction to Memory Management

23

Multiprogramming with Fixed Partitions

Divide memory into n (possible unequal) partitions.

Problem:Fragmentation

Free Space

0k

4k

16k

64k

128k

Page 24: Introduction to Memory Management

24

Fixed Partitions

Legend

Free Space0k

4k

16k

64k

128k

Internalfragmentation

(cannot be reallocated)

Page 25: Introduction to Memory Management

25

Fixed Partitions

Legend

Free Space0k

4k

16k

64k

128k

Page 26: Introduction to Memory Management

26

Fixed Partitions

Legend

Free Space0k

4k

16k

64k

128k

Page 27: Introduction to Memory Management

27

Fixed Partitions

Legend

Free Space0k

4k

16k

64k

128k

Page 28: Introduction to Memory Management

28

Fixed Partitions

Legend

Free Space0k

4k

16k

64k

128k

Page 29: Introduction to Memory Management

29

Fixed Partitions

Legend

Free Space0k

4k

16k

64k

128k

Page 30: Introduction to Memory Management

30

Fixed Partition Allocation Implementation Issues

Separate input queue for each partitionRequires sorting the incoming jobs and putting them

into separate queues

Inefficient utilization of memorywhen the queue for a large partition is empty but

the queue for a small partition is full. Small jobs have to wait to get into memory even though plenty of memory is free.

One single input queue for all partitions. Allocate a partition where the job fits in.

Best Fit

Available Fit

Page 31: Introduction to Memory Management

31

Relocation

Correct starting address when a program should start in the memory

Different jobs will run at different addressesWhen a program is linked, the linker must know at what address the

program will begin in memory.

Logical addresses, Virtual addresses Logical address space , range (0 to max)

Physical addresses, Physical address spacerange (R+0 to R+max) for base value R.

User program never sees the real physical addresses

Memory-management unit (MMU)map virtual to physical addresses.

Relocation register Mapping requires hardware (MMU) with the base register

Page 32: Introduction to Memory Management

32

Relocation Register

Memory

Base Register

CPU Instruction

Address

+

BA

MA MA+BA

PhysicalAddress

LogicalAddress

Page 33: Introduction to Memory Management

33

Question 1 - Protection

Problem:How to prevent a malicious process to write or jump

into other user's or OS partitions

Solution:Base bounds registers

Page 34: Introduction to Memory Management

34

Base Bounds Registers

Memory

Bounds Register Base Register

CPUAddress

< +MemoryAddress MA

LogicalAddress LA

Physical AddressPA

Fault

Base Address

Limit Address

MA+BA

BaseAddressBA

Page 35: Introduction to Memory Management

35

Question 2

What if there are more processes than what could fit into the memory?

Page 36: Introduction to Memory Management

36

Swapping

Monitor

Disk

UserPartition

Page 37: Introduction to Memory Management

37

Swapping

Monitor

Disk

User 1

UserPartition

Page 38: Introduction to Memory Management

38

Swapping

Monitor

User 1

Disk

User 1

UserPartition

Page 39: Introduction to Memory Management

39

Swapping

Monitor

User 2

User 1

Disk

User 1

UserPartition

Page 40: Introduction to Memory Management

40

Swapping

Monitor

Disk

User 2

User 2

UserPartition

User 1

Page 41: Introduction to Memory Management

41

Swapping

Monitor

Disk

User 2

User 2

UserPartition

User 1

Page 42: Introduction to Memory Management

42

Swapping

Monitor

Disk

User 1

User 2

UserPartition

User 1

Page 43: Introduction to Memory Management

43

Summary

Concept of Names/Addresses during CompileLinkLoadRunning

Simple Programming Overlays

Relocation Register

Multi-Programming Fixed Partitions

Swapping