Memory Memory Management Memory Examples Memory ...

streambabyΛογισμικό & κατασκευή λογ/κού

14 Δεκ 2013 (πριν από 3 χρόνια και 8 μήνες)

80 εμφανίσεις

1
3/31/99 Memory Management 1
Memory
 Memory is an important resource that must
be carefully managed
 Even though most computers have memory
capacities that exceed 32MB, multi-
programming makes memory an even more
critical resource
 The part of the OS that manages memory is
called the memory manager
3/31/99 Memory Management 2
Memory Management
 In order to multi-process several programs
need to be in memory at the same time
 To do this we must manage memory
 Memory includes all parts of the computer
used for storing information
 primary memory is volatile
 second memory is persistent
3/31/99 Memory Management 3
Memory
 Memory can be viewed as a large array of
words or bytes, each with its own address
 The maximum amount of memory that a
CPU can access, the addressing space, is
determined by the size the address that can
be generated by the CPU
 Most machines generate 32-bit addresses
which gives an address space of about 4GB.
3/31/99 Memory Management 4
Examples
 Examples
 VAX, SPARC, 80386 - 32 bit address (4GB)
 8080 - 16 bit address (64KB)
 8088/8086 - 20 bit address (1MB)
 80286 - 24 bit address (16MB protected mode)
 The actual amount of memory in a
computer may be substantially less than
what the computer can address
3/31/99 Memory Management 5
Memory Hierarchies
CPU Registers
Cache Memory
Primary Memory
Rotating Magnetic Memory
Optical Memory
Sequentially Accessed Memory
Executable Memory
Secondary Memory
~500 bytes 1 clock cycle
<10MB 1-2 clock cycle
<250MB 1-4 clock cycle
<10GB 5-50 microseconds
<15GB 25 microseconds -
1 second
<5GB seconds
3/31/99 Memory Management 6
Managing the Hierarchy
 Move across executable-secondary memory
boundary (or lower) requires I/O operation
 Upward moves are copy
operations
 Require allocation in upper memory
 Image exists in both memories
 Updates are first applied to upper memory
 Downward move is (usually) destructive
 Deallocate upper memory
 Updates image in secondary memory
2
3/31/99 Memory Management 7
Memory Manager
 Requirements
 Minimize primary memory access time
 Maximize primary memory size
 Primary memory must be cost-effective
 Today s memory manager:
 Allocates primary memory to processes
 Maps process address space to primary memory
 Minimizes access time using cost-effective
memory configuration
3/31/99 Memory Management 8
Address Binding
 In most cases an OS wants to be able to
load a process anywhere in memory
 The problem is that when the program is
run, the addresses required by the program
must be known or bound
 The binding of instructions and data to
memory addresses can be done in several
places
3/31/99 Memory Management 9
Compile Time Binding
 Addresses are determined at compile time
 Usually referred to as absolute code
 The resulting code must be loaded in the
memory locations specified by the compiler
 The OS usually looses the ability to relocate
the program in memory
 MS-DOS .COM files used this technique
3/31/99 Memory Management 10
Load Time Binding
 The compile produces relocatable code
(typically starting address is 0)
 At load time, when the load address is
know, the addresses are changed to reflect
reality
 Provides flexibility to OS, but slows down
load time
3/31/99 Memory Management 11
Runtime Binding
 Actual addresses are not calculated until the
program runs
 Usually requires some form of hardware
support
 Most systems use this techniques since it
provides the ability to move programs
around more or less at will
3/31/99 Memory Management 12
Building the Address Space
Source
code
Translation
Library
code
 Compose elements
Other
objects
3
3/31/99 Memory Management 13
Building the Address Space
Source
code
Translation
Library
code
Secondary
memory
 Compose elements
 Adjust addresses
 Compile time
 Load time
Other
objects
Process address space
3/31/99 Memory Management 14
Building the Address Space
Source
code
Translation
Library
code
Loader
Process
address
space
Executable
memory
Secondary
memory
 Compose elements
 Adjust addresses
 Translation time
 Load time
 Allocate executable memory space
Other
objects
3/31/99 Memory Management 15
Software Solutions
 Dynamic Loading
 a routine is not loaded until it is called
 Dynamic Linking
 basically the same idea as dynamic loading but
used with system libraries
 Overlays
 keep only those instructions in memory that are
needed at a given period of time
3/31/99 Memory Management 16
Overlays
Symbol
Table
Common
Routines
Overlay Mgr
Pass 2
Pass 1
3/31/99 Memory Management 17
Hardware Solutions
 bank-switching
 Here a section of memory is specially
configured so that under program control
different chunks of memory may be seen
 The Lotus/Intel EMS is a functional definition
of bank-switched memory
 Expanded memory is available to application
software as 16KB pages mapped into a contiguous
64KB area called the page frame
3/31/99 Memory Management 18
Logical and Physical Addresses
 An address generated by the CPU is
commonly referred to as a logical address
 An address seen by the memory unit is
referred to as a physical address
 In compile time and load time binding, both
addresses are the same
 In runtime binding they can be different
4
3/31/99 Memory Management 19
Memory Management Unit
 A MMU is a hardware device that maps
logical addresses to physical addresses at
runtime
Memory
CPU
MMU
Relocation Register
+
3/31/99 Memory Management 20
Dynamic Address Relocation
CPU
Relative Address
Relocation Register
+
MAR
 Program loaded at 0x01000  Relocation Register = 0x01000
 Program loaded at 0x04000  Relocation Register = 0x04000
3/31/99 Memory Management 21
Runtime Bound Checking
CPU
Relative Address
Relocation Register
+
MAR
Limit Register
<

Interrupt
3/31/99 Memory Management 22
Address Space vs Primary Memory
Mapped to object
other than memory
Process Address Space
Primary Memory
Process never
sees the real
addresses
Logical addresses 0 - max
3/31/99 Memory Management 23
Memory Allocation
Operating
System
Process 3
Process 0
Process 2
Process 1
Unused
In Use
Somehow main memory
must be split up for the
various processes that
are currently running
3/31/99 Memory Management 24
Fixed-Partition Memory
Operating
System
Region 3
Region 2
Region 1
Region 0 N
0
N
1
N
2
N
3
p
i
p
i
needs n
i
units
n
i
5
3/31/99 Memory Management 25
Fixed-Partition Memory -- Best-Fit
Operating
System
Region 3
Region 2
Region 1
Region 0 N
0
N
1
N
2
N
3
p
i
Internal
Fragmentation
 Loader must
adjust every
address in the
absolute module
when placed in
memory
3/31/99 Memory Management 26
Fixed-Partition Memory -- Worst-Fit
Operating
System
Region 3
Region 2
Region 1
Region 0 N
0
N
1
N
2
N
3
p
i
Internal
Fragmentation
3/31/99 Memory Management 27
Fixed-Partition Memory -- First-Fit
Operating
System
Region 3
Region 2
Region 1
Region 0 N
0
N
1
N
2
N
3
p
i
3/31/99 Memory Management 28
Variable Partition Memory
Operating
System
3/31/99 Memory Management 29
Variable Partition Memory
Operating
System
Operating
System
Process 0
Process 1
Process 2
Process 3
Process 4
 Loader must
adjust every
address in every
absolute module
when placed in
memory
3/31/99 Memory Management 30
Variable Partition Memory
Operating
System
Operating
System
Process 0
Process 1
Process 2
Process 3
Operating
System
Process 4
Process 0
Process 6
Process 2
Process 5
Process 4
External fragmentation
6
3/31/99 Memory Management 31
Variable Partition Memory
Operating
System
Operating
System
Process 0
Process 1
Process 2
Process 3
Operating
System
Operating
System
Process 4
Process 0
Process 6
Process 2
Process 5
Process 4
Process 0
Process 6
Process 2
Process 5
Process 4
 Compaction
moves program in memory
3/31/99 Memory Management 32
Cost of Moving Programs
 Moving programs can be expensive
 in absolute code programs all addresses must be
changed
 relative mode programming allows most of the
program to be moved without changes
 bottom line is almost always something needs
to be changed
 Much better to use relocation hardware
3/31/99 Memory Management 33
Dynamic Memory Allocation
 Common to use dynamically allocated
memory
 Process wants to change the size of its
address space
 Smaller  Creates an external fragment
 Larger  Have to move/relocate the program
 Allocate  holes in memory according to
 Best- /Worst- / First- /Next-fit
3/31/99 Memory Management 34
Managing the Holes
 An OS typically uses one of the following
techniques to keep track of memory usage
 bit maps
 lists
3/31/99 Memory Management 35
Bit Maps & Linked Lists
11111000
01111111
11111111
11000000
00000111
11111111
11111100
00000111
111
A B C D
P
0
5
H
5
4
P
9
17
H
26
11
P
37
17
H
54
7
P
61
6
Keeping this list sorted by addresses
makes list updates straightforward
3/31/99 Memory Management 36
List Updates
A
BX
A
B
A
X
A
BX
B
X
7
3/31/99 Memory Management 37
Swapping
 Suppose there is high demand for
executable memory
 Equitable policy might be to time-multiplex
processes into the memory (also space-mux)
 Means that process can have its address
space unloaded when it still needs memory
 Usually only happens when it is blocked
 Have same problems as dynamic memory
allocation
3/31/99 Memory Management 38
Strategies
 Fixed-Partition used only in batch systems
 Variable-Partition used everywhere (except
in virtual memory)
 Swapping systems
 Popularized in timesharing
 Relies on dynamic address relocation
 Virtual Memory
 Paging -- mainstream in contemporary systems
 Segmentation -- the future