Operating Systems Principles Memory Management Lecture 7: Physical Memory - PowerPoint PPT Presentation

1 / 90
About This Presentation
Title:

Operating Systems Principles Memory Management Lecture 7: Physical Memory

Description:

What operands are needed to be relocated (relocatable)? Indicated by ... Immediate operand (constant)? Offset to a base register? Absolute memory address ... – PowerPoint PPT presentation

Number of Views:377
Avg rating:5.0/5.0
Slides: 91
Provided by: taiwe
Category:

less

Transcript and Presenter's Notes

Title: Operating Systems Principles Memory Management Lecture 7: Physical Memory


1
Operating Systems PrinciplesMemory
ManagementLecture 7 Physical Memory
  • ??????

2
Content
  • Preparing a Program for Execution
  • Program Transformations
  • Logical-to-Physical Address Binding
  • Memory Partitioning Schemes
  • Fixed Partitions
  • Variable Partitions
  • Allocation Strategies for Variable Partitions
  • Dealing with Insufficient Memory
  • Memory Hierarchy

3
Operating Systems PrinciplesMemory
ManagementLecture 7 Physical Memory
  • Preparing a Program for Execution

4
Preparing a Program for Execution
5
Preparing a Program for Execution
Modules reflecting different functions are
designed separately, possibly by different
programmers.
6
Preparing a Program for Execution
Compilation will produce an object module and a
corresponding external symbol table.
External Symbol Table
Compilation
7
Preparing a Program for Execution
Compilation
8
Preparing a Program for Execution
The linker combines several object modules
together to build a load module (EXE) by
resolving external references through symbol
tables.
9
Preparing a Program for Execution
Logical-to-physical address mapping is done by
the loader to transfer the load module from the
secondary storage to the main memory.
10
More on Linking
00000000
Object Module 1
Object Module 1
Object Module 2
Object Module n
Object Module 2
Linking
Object Module n
FFFFFFF
Logical Address Space
11
More on Linking
00000000
Object Module 1
Load Module
Object Module 1
Object Module 2
Object Module n
Object Module 2
Linking
Object Module n
  • Logical address space is used by the linker to
    resolve external references.
  • Binding can be static or dynamic.

FFFFFFF
Logical Address Space
12
Logical-to-Physical Address Binding
Assignment of actual physical addresses to
program instruction and data.
Created by linker (on logical address space)
00000000
00000000
aaaaaaaa
Load Module 1
Load Module 2
Load Module 3
Load Module 3
bbbbbbbb
Load Module 1
cccccccc
Load Module 2
Program relocation needed (done by the loader)
Physical Address Space
13
Address Binding
Assignment of actual physical addresses to
program instruction and data.
  • Static binding
  • Programming time
  • Compilation time
  • Linking time
  • Loading time
  • Dynamic binding
  • Execution time

14
Static Binding
  • Programming time
  • Compilation time
  • Linking time
  • Loading time
  • Programming-time Binding
  • Seldom used
  • Used in low-level environments
  • E.g., OS, real-time and embedded systems, control
    special hardware component.

15
Static Binding
  • Programming time
  • Compilation time
  • Linking time
  • Loading time

SAMPLE PROGRAM FOR MC6802 USING CRS8 The
following source file has been named MC6802.ASM
CPU 6802 6802 processor HOF MOT
Motorola Records ORG 0100H Start of
Data Source DFB 'Hello and Welcome' Length EQU
- Source Length of Source Destin DFS Length
Buffer which has same length as
Source ORG 0120H Start of Code Entry LDX Sou
rce Point Index Reg to Source
string LDAB Length Number of characters to
move Loop LDAA 0,X STAA Length,X INX DECB BNE
Loop Fin JMP Fin END Entry
Programming-time binding
16
Static Binding
  • Programming time
  • Compilation time
  • Linking time
  • Loading time
  • Compile-time Binding
  • The compiler is given the starting address for
    the program to be load for execution.
  • The resulting program can execute only when
    loaded into the specific preassigned memory
    space.
  • Not relocatable
  • Rarely used

17
Static Binding
  • Programming time
  • Compilation time
  • Linking time
  • Loading time
  • Compile-time Binding
  • Named variables have their addresses hardcoded
  • Global variables given offset from start of
    global data area
  • Local variables given offset from top of stack
  • Object variables given offset from start of
    object data
  • Normal function and method calls are hardcoded
  • Normal functions have specific starting address
    in object file
  • Object methods have specific starting address in
    object file

18
Static Binding
  • Programming time
  • Compilation time
  • Linking time
  • Loading time
  • Link-time Binding
  • The load module is still not relocatable
  • The program will be loaded to memory with
    starting location specified or assumed by the
    linker (Linkage Editor).
  • Linking loader combining linking and loading
  • Widely used in smaller, single user systems
  • (e.g., CP/M)

19
Static Binding
  • Programming time
  • Compilation time
  • Linking time
  • Loading time

Object program(s)
Object program(s)
Library
Library
Linking loader
Linkage editor
Memory
Simple Loader
Memory
20
Static Binding
  • Programming time
  • Compilation time
  • Linking time
  • Loading time

Object program(s)
Object program(s)
Library
Library
Linking loader
Linkage editor
Memory
Simple Loader
Memory
21
Static Binding
  • Programming time
  • Compilation time
  • Linking time
  • Loading time

What?
  • Load-Time Binding
  • The load module is relocatable
  • What operands are needed to be relocated
    (relocatable)?
  • Indicated by compilers and assemblers
  • Needed for linking and loading
  • How to perform relocation?

Object program(s)
Library
Linkage editor
relocatable
What?
Loader
How?
Memory
22
Static Binding
  • Programming time
  • Compilation time
  • Linking time
  • Loading time

What?
  • What operands are needed to be relocated?
  • Register?
  • Immediate operand (constant)?
  • Offset to a base register?
  • Absolute memory address
  • Relative memory address?

Object program(s)
Library
Linkage editor
relocatable
What?
Loader
How?
Memory
23
Static Binding
  • Programming time
  • Compilation time
  • Linking time
  • Loading time

What?
  • What operands are needed to be relocated?
  • Register?
  • Immediate operand (constant)?
  • Offset to a base register?
  • Absolute memory address
  • Relative memory address?

Object program(s)
Library
Linkage editor
Keep unchanged
relocatable
What?
Loader
How?
Ralocatable
Memory
24
Static Binding
  • Programming time
  • Compilation time
  • Linking time
  • Loading time

What?
  • How to relocate the relocatable operands?

Object program(s)
Library
Linkage editor
Load Module
Load Module
relocatable
What?
Loader
How?
Logical Address Space
Physical Address Space
Memory
25
Static Binding
  • Programming time
  • Compilation time
  • Linking time
  • Loading time

What?
  • How to relocate the relocatable operands?

Object program(s)
Add all relocatable operands in the load module
by the amount of y ? x.
Library
Linkage editor
Load Module
Load Module
relocatable
What?
Loader
How?
Logical Address Space
Physical Address Space
Memory
26
Example
f() . . . g() . . .
. . . int i . . . i ??? . . . g() . . .
Source Module 1
Source Module 2
Logical Address Space
Logical Address Space
27
Example
Relative memory address (relocatable)
f() . . . g() . . .
. . . int i . . . i ??? . . . g() . . .
External reference
Source Module 1
Source Module 2
Logical Address Space
Logical Address Space
28
Example
Object Module 1 (obj1)
Object Module 2 (obj2)
f() . . . g() . . .
. . . int i . . . i ??? . . . g() . . .
Source Module 1
Source Module 2
Logical Address Space
Logical Address Space
29
Example
Link obj3obj2obj1, myexe
Obj1
Obj2
Obj3
Logical Address Space
30
Example
Link obj3obj2obj1, myexe
Obj1
Obj2
Obj3
relocated
External reference resolved
Logical Address Space
31
Example
Link obj3obj2obj1, myexe
Obj1
Obj2
Obj3
Load Module (myexe)
Logical Address Space
32
Example
1000
1000
relocated
Physical Address Space
myexe
33
Dynamic Binding
Physical address differs from logical address
usually by a constant offset.
  • Dynamic Binding Binding at Execution Time
  • That is, binding immediately before each memory
    reference.
  • Hardware support needed for logical-to-physical
    address mapping

34
Dynamic Binding
Physical address differs from logical address
usually by a constant offset.
35
Example
36
Operating Systems PrinciplesMemory
ManagementLecture 7 Physical Memory
  • Memory Partitioning Schemes

37
Memory Partitioning Schemes
  • Fixed Partitions
  • The number of partitions and the size of each
    partition are determined at the time the OS is
    initialized.
  • Variable Partitions
  • Memory not partitioned a priori
  • Partitioning on demand

38
Fixed Partitions
  • Single-program systems
  • 2 partitions (OS/user)
  • Multi-programmed systems
  • partitions of different sizes

39
How to Assign Processes to Partitions?
40
How to Assign Processes to Partitions?
  • FIFO for each partition
  • Typically, best-fit is used for queue assignment.
  • Problem Some partitions may be unused if no
    processes of appropriate sizes are available.
  • To resolve the problem, more complex queue
    management scheme and process scheduling scheme
    would be required.

41
How to Assign Processes to Partitions?
  • Single FIFO
  • More complex, but more flexible.
  • E.g., rather than leaving a partition empty, the
    scheduler may assigned a process to a partition
    not in best-fit sense.
  • E.g., the actual memory requirement of a process
    may grow and shrink dynamically.

42
Limitations of Fixed Partitions
  • Program size limited to largest partition
  • Internal fragmentation
  • unused space within partitions

OS
43
Variable Partitions
  • Memory not partitioned a priori
  • Each request is allocated portion of free space.
  • Allocating and releasing memory dynamically cause
    external fragmentation.

44
Issues
Over time, memory will consist a sequence of
variable size blocks. Some are free, and some are
not.
  • How to allocate memories of sizes

All free blocks are applicable.
Allocate which one?
None free block is applicable.
But, the total amount of free memory is large
enough.
45
Hole Coalescing
Adjacent holes must be coalesced to prevent
increasing fragmentation.
A
A
A
A
A
B
B
B
B
B
B
B
B
C
C
C
D
D
D
D
E
E
E
E
E
F
F
F
F
F
G
G
G
G
G
46
Hole Coalescing
Adjacent holes must be coalesced to prevent
increasing fragmentation.
Four cases on hole coalescing
no adjacent hole
A hole at right
A hole at left
Holes at two sides
47
Link List Implementation (I)
G
A
B
C
D
E
F
free
free
free
occupied
occupied
occupied
occupied
size
size
size
size
size
size
size
  • Free blocks are kept sorted using a doubly linked
    list.
  • What would be done to release a block of memory?
  • Check both its neighbors for possible coalescing.
  • Problems
  • How to check the right neighbor?
  • How to check the left neighbor?

48
Link List Implementation (II)
E
A
B
C
D
free
occupied
size
size
free
size
occupied
size
occupied
size
occupied
size
occupied
size
occupied
size
free
size
  • Free blocks are linked using a doubly linked list
  • need not be sorted
  • What would be done to release a block of memory?
  • Check both its neighbors for possible coalescing.
  • Problems
  • How to check the right neighbor?
  • How to check the left neighbor?

49
Bitmap Implementation
Allocated
Free
  • Memory divided into fix-size blocks
  • Each block represented by a 0/1 bit in a binary
    string the bitmap
  • Can be implemented as char or int array
  • Operations use bit masks
  • Release Bi Bi '11011111'
  • Allocate Bi Bi '11000000'
  • Search Repeatedly, Check left-most bit andShift
    mask right TEST Bi '10000000'

50
Bitmap Implementation
  • Memory divided into fix-size blocks
  • Each block represented by a 0/1 bit in a binary
    string the bitmap
  • Can be implemented as char or int array
  • Operations use bit masks
  • Release Bi Bi '11011111'
  • Allocate Bi Bi '11000000'
  • Search Repeatedly, Check left-most bit andShift
    mask right TEST Bi '10000000'

51
Bitmap Implementation
52
Bitmap Implementation
How to allocate a block of continuous memory of a
given size?
4K
4K
4K
4K
4K
4K
4K
4K
4K
4K
4K
4K
4K
4K
4K
4K
4K
4K
53
Buddy Systems
  • Compromise between fixed and variable partitions
  • Fixed number of possible hole sizes typically,
    2i.
  • Each hole of size 2i can be divided (equally)
    into 2 buddies of size 2i?1.
  • Two buddies can be combined into the original
    hole of size 2i.
  • Track holes by size on separate lists
  • List entries for holes of sizes 20, 21, , 2m.

54
Buddy Systems
  • Compromise between fixed and variable partitions
  • Fixed number of possible hole sizes typically,
    2i.
  • Each hole of size 2i can be divided (equally)
    into 2 buddies of size 2i?1.
  • Two buddies can be combined into the original
    hole of size 2i.
  • Track holes by size on separate lists
  • List entries for holes of sizes 20, 21, , 2m.

55
Buddy Systems
  • When n bytes requested, find smallest i so that n
    ? 2i
  • If hole of this size available, allocate it
  • otherwise, consider larger holes.
  • Recursively split each hole into two buddies
  • until smallest adequate hole is created
  • Allocate it and place other holes on appropriate
    lists
  • On release, recursively coalesce buddies
  • Buddy searching for coalescing can be inefficient

56
Example
Hole Sizes 1, 2, 4, 8, 16
3 blocks allocated 3 holes left
void pmalloc(1blocksize)
free(12blocksize)
57
Operating Systems PrinciplesMemory
ManagementLecture 7 Physical Memory
  • Allocation Strategies for Variable Partitions

58
Allocation Strategies
  • Problem
  • Given a request for n bytes, find hole n
  • Goal
  • Maximize memory utilization
  • (Minimize external fragmentation)
  • Minimize search time

59
Common Allocation Strategies
  • First-fit
  • Always start from the beginning of free-list
  • Simplest
  • Generally the best choice
  • Next-fit
  • Rotating-first-fit (Resume search)
  • Improves distribution of holes
  • Best-fit
  • Closest fit
  • Avoid breaking up large holes.
  • Worst-fit
  • Largest fit
  • Avoid leaving tiny hole fragments

60
Measure of Memory Fragmentation
What measurement could be used?
61
Measure of Memory Fragmentation
What measurement could be used?
m blocks occupied
n holes
62
Measure of Memory Fragmentation
What measurement could be used?
m blocks occupied
n holes
63
Measure of Memory Fragmentation
What measurement could be used?
m blocks occupied
n holes
holes unchanged on release
Decrease one hole on release.
Increase one hole on release.
Four types of occupied memory blocks.
64
Measure of Memory Fragmentation
What measurement could be used?
m blocks occupied
n holes
Let a, b, c, d be the average number of occupied
blocks of each type in memory during equilibrium.
Fact b c
holes unchanged on release
Decrease one hole on release.
Increase one hole on release.
65
Measure of Memory Fragmentation
Fact b c
holes unchanged on release
Decrease one hole on release.
Increase one hole on release.
66
Measure of Memory Fragmentation
?
Fact b c
holes unchanged on release
Decrease one hole on release.
Increase one hole on release.
b
c
a
b
d
c
b
d
c
b
d
a
b
c
d
67
Measure of Memory Fragmentation
?
Fact b c
holes unchanged on release
Decrease one hole on release.
Increase one hole on release.
b
c
a
b
d
c
b
d
c
b
d
a
c
n 7
a
b
c
d
68
Measure of Memory Fragmentation
holes unchanged on release
Decrease one hole on release.
Increase one hole on release.
69
Measure of Memory Fragmentation
p The probability that the request memory size
doesnt match any hole size. Fact p ? 1.
holes unchanged on release
Decrease one hole on release.
Increase one hole on release.
70
Measure of Memory Fragmentation
In equilibrium,
71
Measure of Memory Fragmentation
In Equilibrium, of the total number of occupied
blocks and holes, 1/3 are holes.
In equilibrium,
50 rule (Knuth 1968)
72
Measure of Memory Utilization
In Equilibrium, of the total number of occupied
blocks and holes, 1/3 are holes.
In equilibrium,
How much memory is wasted?(average) hole_size
is not the same as(average) block_size
50 rule (Knuth 1968)
73
Measure of Memory Utilization
b average block size h average hole size
Assume
74
Measure of Memory Utilization
b average block size h average hole size
Assume
75
Measure of Memory Utilization
We want to minimize the value of f.
It is equivalent to minimize k.
How to know the value of k?
b average block size h average hole size
Assume
76
Measure of Memory Utilization
How to know the value of k?
  • Simulations by Knuth (1968) provide an answer.
  • k depends on the ratio of b/M, e.g.,
  • b ? M/10, k 0.22 ? f ? 0.1
  • b ? M/3, k 2 ? f ? 0.5

b average block size h average hole size
Assume
77
Measure of Memory Utilization
Conclusion M must be large relative to b,
otherwise much of main memory will remain unused.
b average block size h average hole size
Assume
78
Operating Systems PrinciplesMemory
ManagementLecture 7 Physical Memory
  • Dealing with Insufficient Memory

79
Managing Insufficient Memory
  • What to be done?
  • when the total size of free memory is large
    enough while the memory requirement of a process
    exceeds the size of the largest partition
  • when new process arrives with insufficient free
    memory?
  • When the memory requirement is larger than the
    physical memory size.

80
Dealing with Insufficient Memory
  • Memory compaction
  • How much and what to move?
  • Swapping
  • Temporarily move process to disk
  • Requires dynamic relocation
  • Overlays
  • Allow programs larger than physical memory
  • Programs loaded as needed according to calling
    structure.

81
Memory Compaction
Request for a memory block of size 10.
Complete Compaction
Partial Compaction
Minimal data Movement
Initial
82
Swapping
  • What are needed to be swapped out?
  • Code?
  • Data?
  • More efficient than memory compaction.
  • Memory compaction may fail to create a large
    enough hole.
  • Effect only small number of processes each time,
    thus requiring fewer memory accesses.
  • Swapping can be overlapped with process
    execution.
  • Swapping can be used with both fixed and variable
    size partition, whereas memory compaction is
    applicable only for variable size partition.

Swap in/out
83
Overlaps
  • Allow programs large than physical memory
  • Programs loaded as needed, according to calling
    structure.

A
A
k1
B
B
C
D
E
84
Overlaps
  • Allow programs large than physical memory
  • Programs loaded as needed, according to calling
    structure.

A
A
k1
B
C
B
C
k2
D
D
E
85
Overlaps
  • Allow programs large than physical memory
  • Programs loaded as needed, according to calling
    structure.

A
A
k1
B
C
B
C
k2
D
E
D
E
86
Overlaps
  • Allow programs large than physical memory
  • Programs loaded as needed, according to calling
    structure.

A
B
C
D
E
87
Operating Systems PrinciplesMemory
ManagementLecture 7 Physical Memory
  • Memory Hierarchy

88
Storage Hierarchy
registers
cache
main memory
electronic disk
magnetic disk
optical disk
magnetic tapes
89
Storage Hierarchy
Speed
registers
Size
cache
main memory
electronic disk
magnetic disk
optical disk
magnetic tapes
90
Memory Hierarchy Design Principles
  • Make the Common Case Fast
  • Principle of Locality
  • Smaller is Faster
Write a Comment
User Comments (0)
About PowerShow.com