Post on 15-Dec-2015
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-1
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-2
Copyright © 2004 Pearson Education, Inc.
11MemoryManagement
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-3
Copyright © 2004 Pearson Education, Inc.
The External View of the Memory Manager
Hardware
ApplicationProgram
ApplicationProgram
Fil
e M
gr
Dev
ice
Mgr
Mem
ory
Mgr
Pro
cess
Mgr
UNIXF
ile
Mgr
Dev
ice
Mgr
Mem
ory
Mgr
Pro
cess
Mgr
Windows
VMQuery()VirtualFree()VirtualLock()
ZeroMemory()
VirtualAlloc()
sbrk()exec()
getrlimit()
shmalloc()
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-4
Copyright © 2004 Pearson Education, Inc.
Memory Manager
• Requirements– Minimize executable memory access time– Maximize executable memory size– Executable 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– May use static or dynamic techniques
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-5
Copyright © 2004 Pearson Education, Inc.
Storage Hierarchies
Less Frequently
Used Information
More Frequently
Used Information
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-6
Copyright © 2004 Pearson Education, Inc.
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
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-7
Copyright © 2004 Pearson Education, Inc.
Contemporary Memory Hierarchy &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
Fas
ter
acce
ss
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-8
Copyright © 2004 Pearson Education, Inc.
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
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-9
Copyright © 2004 Pearson Education, Inc.
Address Space vs Primary Memory
Mapped to objectother than memory
Process Address Space Hardware Primary Memory
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-10
Copyright © 2004 Pearson Education, Inc.
Creating an Executable Program
Sourcecode
Sourcecode
•Compile time: Translate elements
•Load time:•Allocate primary memory•Adjust addresses in address space•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
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-11
Copyright © 2004 Pearson Education, Inc.
A Sample Code Segment
...static int gVar;...int proc_a(int arg){ ... gVar = 7; put_record(gVar); ...}
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-12
Copyright © 2004 Pearson Education, Inc.
The Relocatable Object moduleCode 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)
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-13
Copyright © 2004 Pearson Education, Inc.
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)
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-14
Copyright © 2004 Pearson Education, Inc.
The Program Loaded at Location 4000RelativeAddress 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)
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-15
Copyright © 2004 Pearson Education, Inc.
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
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-16
Copyright © 2004 Pearson Education, Inc.
Fixed-Partition Memory Mechanism
OperatingSystem
Region 3
Region 2
Region 1
Region 0 N0
N1
N2
N3
pi
pi needs ni units
ni
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-17
Copyright © 2004 Pearson Education, Inc.
Fixed-Partition MemoryBest-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
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-18
Copyright © 2004 Pearson Education, Inc.
Fixed-Partition MemoryWorst-Fit
OperatingSystem
Region 3
Region 2
Region 1
Region 0 N0
N1
N2
N3
pi
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-19
Copyright © 2004 Pearson Education, Inc.
Fixed-Partition MemoryFirst-Fit
OperatingSystem
Region 3
Region 2
Region 1
Region 0 N0
N1
N2
N3
pi
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-20
Copyright © 2004 Pearson Education, Inc.
Fixed-Partition MemoryNext-Fit
OperatingSystem
Region 3
Region 2
Region 1
Region 0 N0
N1
N2
N3
pi
Pi+1
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-21
Copyright © 2004 Pearson Education, Inc.
Variable Partition Memory
OperatingSystem
OperatingSystem
Process 0
Process 6
Process 2
Process 5Process 4
•Compaction moves program in memory
OperatingSystem
Process 0
Process 6
Process 2
Process 5
Process 4
•External fragmentation
OperatingSystem
Process 0
Process 1
Process 2
Process 3
Process 4
Loader adjusts every address in every absolute module when placed in memory
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-22
Copyright © 2004 Pearson Education, Inc.
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
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-23
Copyright © 2004 Pearson Education, Inc.
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, …
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-24
Copyright © 2004 Pearson Education, Inc.
Multiprogramming Memory Support
OperatingSystem
Process 1
Process 3
Process 0
Process 1
Unused
In Use
0
AB
C
DE
F
G
H
R0
R1
R2
R3
R4
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-25
Copyright © 2004 Pearson Education, Inc.
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
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-26
Copyright © 2004 Pearson Education, Inc.
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– Now dated
• Dynamic Loading (Virtual Memory)– Exploit the memory hierarchy– Paging -- mainstream in contemporary systems– Segmentation -- the future
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-27
Copyright © 2004 Pearson Education, Inc.
Moving an Executable Image
ExecutableProgram
ExecutableProgram
LoaderLoader
LoaderLoader
ExecutableImage
ExecutableImage
ExecutableImage
ExecutableImage
02000
06000
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-28
Copyright © 2004 Pearson Education, Inc.
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!
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-29
Copyright © 2004 Pearson Education, Inc.
Multiple Segment Relocation Registers
CPU
Relative Address
Code Register
+
MAR
0x12010
Stack Register
Data Register
Operating Systems: A Modern Perspective, Chapter 11
Slide 11-30
Copyright © 2004 Pearson Education, Inc.
Special Case: 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