Upload
marvin-gaines
View
215
Download
1
Tags:
Embed Size (px)
Citation preview
Memory Management
2
Basic OS Organization
Processor(s) Main Memory Devices
Process, Thread &Resource Manager
MemoryManager
DeviceManager
FileManager
Operating System
Computer Hardware
3
Storage Hierarchies in the Office
Less Frequently
Used Information
More Frequently
Used Information
4
The Basic Memory Hierarchy
CPU Registers
Primary Memory(Executable Memory)
e.g. RAM
Secondary Memorye.g. Disk or Tape
More Frequently
Used Information
Less Frequently
Used Information
von Neumann architecture
5
Memory System
• Primary memory– Holds programs and data while they are being
used by the CPU– Referenced by byte; fast access; volatile
• Secondary memory– Collection of storage devices– Referenced by block; slow access; nonvolatile
6
Primary & Secondary Memory
CPU
Primary Memory(Executable Memory)
e.g. RAM
Secondary Memorye.g. Disk or Tape
• CPU can load/store• Ctl Unit executes code from this memory•Transient storage
• Access using I/O operations• Persistent storage
Load
Information can be loaded statically or dynamically
7
Classical Memory Manager Tasks
• Memory management technology has evolved
• Early multiprogramming systems– Resource manager for space-multiplexed primary
memory
• As popularity of multiprogramming grew– Provide robust isolation mechanisms
• Still later– Provide mechanisms for shared memory
8
Memory Hierarchies – Dynamic Loading
CPU Registers
“Main” Memory
Rotating Magnetic Memory
Optical Memory
Sequentially Accessed Memory
L1 Cache MemoryL2 Cache Memory
Sec
onda
ryP
rim
ary
(Exe
cuta
ble)
Lar
ger
stor
age/
low
er c
ost
Fas
ter
acce
ss/h
ighe
r co
st
9
Exploiting the Hierarchy
• Upward moves are (usually) copy operations– Require allocation in upper memory
– Image exists in both higher & lower memories
• Updates are first applied to upper memory• Downward move is (usually) destructive
– Destroy image in upper memory
– Update image in lower memory
• Place frequently-used info high, infrequently-used info low in the hierarchy
• Reconfigure as process changes phases
10
Contemporary Memory Manager
• Performs the classic functions required to manage primary memory– Attempts to efficiently use primary memory– Keep programs/data in primary memory only
while they are being used by CPU– Store/restore data in secondary memory soon
after it has been used or created
• Exploits storage hierarchies– Virtual memory manager
11
Requirements on Memory Designs
• The primary memory access time must be as small as possible
• The perceived primary memory must be as large as possible
• The memory system must be cost effective
12
Functions of Memory Manager
• Allocate primary memory space to processes
• Map the process address space into the allocated portion of the primary memory
• Minimize access times using a cost-effective amount of primary memory
• May use static or dynamic techniques
13
External View of the Memory Manager
Hardware
ApplicationProgram
ApplicationProgram
Fil
e M
gr
Dev
ice
Mgr
Mem
ory
Mgr
Pro
cess
Mgr
UNIX
Fil
e M
gr
Dev
ice
Mgr
Mem
ory
Mgr
Pro
cess
Mgr
Windows
VMQuery()VirtualFree()VirtualLock()
ZeroMemory()
VirtualAlloc()
sbrk()exec()
getrlimit()
shmalloc()
14
Memory Manager
• Only a small number of interface functions provided – usually calls to:– Request/release primary memory space– Load programs– Share blocks of memory
• Provides following– Memory abstraction– Allocation/deallocation of memory– Memory isolation– Memory sharing
15
Memory Abstraction
• Process address space– Allows process to use an abstract set of addresses
to reference physical primary memory
Mapped to objectother than memory
Process Address Space Hardware Primary Memory
16
Address Space
• Program must be brought into memory and placed within a process for it to be executed– A program is a file on disk– CPU reads instructions from main memory and
reads/writes data to main memory• Determined by the computer architecture
– Address binding of instructions and data to memory addresses
17
Creating an Executable Program
Sourcecode
Sourcecode
• Compile time: Translate elements
• Load time:• Allocate primary memory• Adjust addresses in address space (relocation)• Copy address space from secondary to primary memory
LoaderLoader Processaddressspace
Primarymemory
C
RelocObjectcode
RelocObjectcode
LinkEdit
LinkEdit
Librarycode
Librarycode
Otherobjects
Otherobjects
Secondary memory
• Link time: Combine elements
18
Bindings
• Compiler– Binds static variables to storage locations relative
to start of data segment– Binds automatic variables to storage locations
relative to bottom of stack
• Linker– Combines data segments and adjusts bindings
accordingly– Same for stack
19
Bindings – cont.
• Loader– Binds logical addresses used by program with
physical memory locations (address binding)
• This type of binding is called static address binding
• The last stage of address binding can be deferred to runtime
dynamic address binding
20
A Sample Code Segment
...static int gVar;...int proc_a(int arg){ ... gVar = 7; put_record(gVar); ...}
21
A Sample Code Segment
• Compiler allocates space for gVar in data segment, saving address in symbol table (binds variable name)
• Assignment statement is translated into instructions storing 7 in that location
• For function call, the parameters are pushed on the stack, but function is external so compiler leaves information so linker can resolve the address
...static int gVar;...int proc_a(int arg){ ... gVar = 7; put_record(gVar); ...}
22
The Relocatable Object module
Code SegmentRelativeAddress Generated Code0000 ......0008 entry proc_a...0220 load =7, R10224 store R1, 00360228 push 00360232 call ‘put_record’...0400 External reference table...0404 ‘put_record’ 0232...0500 External definition table...0540 ‘proc_a’ 0008...0600 (symbol table)...0799 (last location in the code segment)
Data SegmentRelativeAddress Generated variable space...0036 [Space for gVar variable]...0049 (last location in the data segment)
Generated object code
• Linker combines this code with other modules• Causes relative addresses to be adjusted• Fixes up the external reference to function
23
The Absolute ProgramCode SegmentRelativeAddress Generated Code0000 (Other modules)...1008 entry proc_a...1220 load =7, R11224 store R1, 01361228 push 10361232 call 2334...1399 (End of proc_a)... (Other modules)2334 entry put_record...2670 (optional symbol table)...2999 (last location in the code segment)
Data SegmentRelativeAddress Generated variable space...0136 [Space for gVar variable]...1000 (last location in the data segment)
• Loader brings program into memory• Need to relocate• New address bindings
24
The Program Loaded at Location 4000
RelativeAddress Generated Code0000 (Other process’s programs)4000 (Other modules)...5008 entry proc_a...5036 [Space for gVar variable]...5220 load =7, R15224 store R1, 71365228 push 50365232 call 6334...5399 (End of proc_a)... (Other modules)6334 entry put_record...6670 (optional symbol table)...6999 (last location in the code segment)7000 (first location in the data segment)...7136 [Space for gVar variable]...8000 (Other process’s programs)
25
Dynamic Memory
• Static and automatic variables are assigned addresses in the data or stack segments at compile time
• Dynamic memory allocation (e.g., new or malloc) is done at runtime– This is not handled by the memory manager– This merely binds parts of the process’s address
space to dynamic data structures– Memory manager gets involved if the process
runs out of address space
26
Variations in program linking/loading
27
Normal linking and loading
28
Load-time dynamic linking
29
Run-time dynamic linking
30
Data Storage Allocation
• Static variables– stored in programs data segment
• Automatic variables– Stored on stack
• Dynamically allocated space (new or malloc)– Taken from heap storage – no system call
• Note: If heap disappears, kernel memory manager invoked to get more memory for the process
31
C Style Memory Layout
Text SegmentText Segment
Initialized Part Data Segment
Initialized Part Data Segment
Uninitialized Part Data Segment
Uninitialized Part Data Segment
Heap StorageHeap Storage
Stack SegmentStack Segment
EnvironmentVariables, …
EnvironmentVariables, …High Address
Low Address
32
Program and Process Address Spaces
Process Address Space Hardware Primary Memory
AbsoluteProgramAddressSpace
0
3 GB
4 GB
User ProcessAddressSpace
SupervisorProcessAddressSpace
33
Overview of Memory Management Techniques
• Memory allocation strategies– View the process address space and the primary memory as
contiguous address space
• Paging and segmentation based techniques– View the process address space and the primary memory as
a set of pages / segments
– Map an address in process space to a memory address
• Virtual memory– Extension of paging/segmentation based techniques
– To run a program, only the current pages/segments need to in primary memory
34
Memory Allocation Strategies
- There are two different levels in memory allocation
35
Two levels of memory management
36
Memory Management System Calls
• In Unix, the system call is brk– Increase the amount of memory allocated to a
process
37
Malloc and New functions
• They are user-level memory allocation functions, not system calls
38
Memory Management
39
Issues in a memory allocation algorithm
• Memory layout / organization – how to divide the memory into blocks for allocation?
• Fixed partition method: divide the memory once before any bytes are allocated.
• Variable partition method: divide it up as you are allocating the memory.
• Memory allocation– select which piece of memory to allocate to a request
• Memory organization and memory allocation are close related
• It is a very general problem– Variations of this problem occurs in many places.
• For examples: disk space management
40
Static Memory Allocation
OperatingSystem
Process 3
Process 0
Process 2
Process 1
Unused
In Use
Issue: Need a mechanism/policy for loading pi’s address space into primary memory
pi
41
Fixed-Partition Memory allocation
• Statically divide the primary memory into fixed size regions– Regions can have different sizes or same sizes
• A process / request can be allocated to any region that is large enough
42
Fixed-Partition Memory allocation – cont.
• Advantages– easy to implement.
– Good when the sizes for memory requests are known.
• Disadvantage:– cannot handle variable-size requests effectively.
– Might need to use a large block to satisfy a request for small size.
– Internal fragmentation – The difference between the request and the allocated region size; Space allocated to a process but is not used
• It can be significant if the requests vary in size considerably
43
Fixed-Partition Memory Mechanism
OperatingSystem
Region 3
Region 2
Region 1
Region 0 N0
N1
N2
N3
pi
pi needs ni units
ni
44
Which free block to allocate
• How to satisfy a request of size n from a list of free blocks– First-fit: Allocate the first hole that is big enough– Next-fit: Choose the next block that is large enough– Best-fit: Allocate the smallest hole that is big enough;
must search entire list, unless ordered by size. Produces the smallest leftover hole.
– Worst-fit: Allocate the largest hole; must also search entire list. Produces the largest leftover hole.
45
Fixed-Partition Memory -- Best-Fit
OperatingSystem
Region 3
Region 2
Region 1
Region 0 N0
N1
N2
N3
pi
InternalFragmentation
•Loader must adjust every address in the absolute module when placed in memory
46
Fixed-Partition Memory -- Worst-Fit
OperatingSystem
Region 3
Region 2
Region 1
Region 0 N0
N1
N2
N3
pi
47
Fixed-Partition Memory -- First-Fit
OperatingSystem
Region 3
Region 2
Region 1
Region 0 N0
N1
N2
N3
pi
48
Fixed-Partition Memory -- Next-Fit
OperatingSystem
Region 3
Region 2
Region 1
Region 0 N0
N1
N2
N3
pi
Pi+1
49
Variable partition memory allocation
• Grant only the size requested– Example:
• total 512 bytes:
• allocate(r1, 100), allocate(r2, 200), allocate(r3, 200),
• free(r2),
• allocate(r4, 10),
• free(r1),
• allocate(r5, 200)
• External Fragmentation– Memory is divided up into small blocks that none of them can
be used to satisfy any requests.
50
Issues in Variable partition memory allocation
• Where are the free memory blocks?– Keeping trace of the memory blocks– List method and bitmap method
• Which memory blocks to allocate?– There may exist multiple free memory blocks
that can satisfy a request. Which block to use?
• Fragmentation must be minimized• How to keep track of free and allocated
memory blocks?
51
Variable Partition Memory Mechanism
OperatingSystem
OperatingSystem
Process 0
Process 6
Process 2
Process 5Process 4
• Compaction moves program in memory in (d)
OperatingSystem
Process 0
Process 6
Process 2
Process 5
Process 4
• External fragmentation in (c)
OperatingSystem
Process 0
Process 1
Process 2
Process 3
Process 4
Loader adjusts every address in every absolute module when placed in memory
52
Cost of Moving Programs
load R1, 0x02010
3F013010
Program loaded at 0x01000 3F016010
Program loaded at 0x04000
•Must run loader over program again!
Consider dynamic techniques
Compaction requires that a program be moved
53
Dynamic Memory Allocation
• Could use dynamically allocated memory
• Process wants to change the size of its address space– Smaller Creates an external fragment– Larger May have to move/relocate the
program
• Allocate “holes” in memory according to– Best- /Worst- / First- /Next-fit
54
Contemporary Memory Allocation
• Use some form of variable partitioning
• Usually allocate memory in fixed-size blocks (pages)– Simplifies management of free list– Greatly complicates binding problem
55
Dynamic Address Space Binding• Recall: in static binding
– Symbols first bound to relative addresses in a relocatable module at compile time
– Then to addresses in absolute module at link time
– Then to primary memory addresses at load time
• Dynamic binding– Wait to bind absolute program addresses until
run time– Simplest mechanism is dynamic relocation
• Usually implemented by the processor
56
Dynamic Address Relocation
CPU
0x02010
0x10000+
MARload R1, 0x02010
0x12010
•Program loaded at 0x10000 Relocation Register = 0x10000•Program loaded at 0x04000 Relocation Register = 0x04000
Relocation Register
Relative Address
We never have to change the load module addresses!
Performed automatically by processor
57
Dynamic Address Relocation
• Same holds for multiple segment registersCPU (Generated address)
Relative Address
Code register
Stack register
Data register
+
MAR
Primary memory
58
Runtime Bound Checking
CPU
Relative Address
Relocation Register
+
MAR
Limit Register <
Interrupt
•Bound checking is inexpensive to add•Provides excellent memory protection
59
Memory Mgmt 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
• Dynamic Loading (Virtual Memory)– Exploit the memory hierarchy– Paging -- mainstream in contemporary systems
• Shared-memory multiprocessors
60
Swapping
• Special case of dynamic memory allocation
• 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
61
Swapping – cont.
• Objective– Optimize system performance by removing a
process from memory when it is blocked, allowing that memory to be used by other processes
– Block may be caused by a request for a resource, or by the memory manager
• Swapping only becomes necessary when processes are being denied access to memory
62
Swapping – cont.
Image for pj
Image for pi
Swap pi outSwap pi out
Swap pj in
63
Cost of Swapping
• Need to consider time to copy execution image from primary to secondary memory, and back– This is the major part of the swap time
• In addition, there is the time required by the memory manager, and the usual context switching time
64
Swapping Systems
• Standard swapping used in few systems– Requires too much swapping time and provides
too little execution time
• Most systems do use some modified version of swapping– In UNIX, swapping is normally disabled, but will
be enabled if memory usage reaches some threshold limit; when usage drops below the threshold, swapping is again disabled
65
Virtual Memory
• Allows a process to execute when only part of its address space is loaded in primary memory – the rest is in secondary
• Need to be able to partition the address space into parts that can be loaded into primary memory when needed
66
Virtual Memory – cont.
• A characteristic of programs that is very important to the strategy used by virtual memory systems is spatial reference locality– Refers to the implicit partitioning of code and
data segments due to the functioning of the program (portion for initializing data, another for reading input, others for computation, etc.)
• Can be used to select which parts of the process should be loaded into primary memory
67
Virtual Memory Barriers
• Must be able to treat the address space in parts that correspond to the various localities that will exist during the programs execution
• Must be able to load a part anywhere in physical memory and dynamically bind the addresses appropriately
• More on this in next chapter
68
Shared-memory Multiprocessors
• Several processors share an interconnection network to access a set of shared-memory modules
• Any CPU can read/write any memory unit
CPU CPU CPU. . .
Memory Memory Memory. . .
Interconnection Network
69
Shared-memory Multiprocessors – cont.
• Goal is to use processes or threads to implement units of computation on different processors while sharing information via common primary memory locations
• One technique would be to have the address spaces of two processes overlap
• Another would split the address space of a process into a private part and a public part
70
Sharing a Portion of the Address Space
Process 1Process 1
Process 2Process 2
Address Space for Process 2
Address Space for Process 1
Primary Memory
71
Figure 11‑26: Multiple Segments
Relocation
RelocationLimit
Limit
Relocation
RelocationLimit
Limit
CPU Executing Process 1
CPU Executing Process 2
Primary Memory
Shared
Pri
vate
toP
roce
ss 1
Pri
vate
toP
roce
ss 2
72
Shared-memory Multiprocessors – cont.
• A major problem is synchronization– How can one process detect when the other
process has written or read information– Will need to use interprocess communication to
handle the synchronization
• Another problem is overloading the interconnection network– Use cache memories to decrease load on network