370 likes | 406 Views
Memory Management. 11. VirtualAlloc(). exec(). VMQuery(). shmalloc(). VirtualFree(). sbrk(). VirtualLock(). getrlimit(). ZeroMemory(). The External View of the Memory Manager. Application Program. File Mgr. Device Mgr. Memory Mgr. File Mgr. Device Mgr. Memory Mgr. Process Mgr.
E N D
MemoryManagement 11 Operating Systems: A Modern Perspective, Chapter 11
VirtualAlloc() exec() VMQuery() shmalloc() VirtualFree() sbrk() VirtualLock() getrlimit() ZeroMemory() The External View of the Memory Manager Application Program File Mgr Device Mgr Memory Mgr File Mgr Device Mgr Memory Mgr Process Mgr Process Mgr UNIX Windows Hardware Operating Systems: A Modern Perspective, Chapter 11
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
Storage Hierarchies Less Frequently Used Information More Frequently Used Information Operating Systems: A Modern Perspective, Chapter 11
The Basic Memory Hierarchy CPU Registers Less Frequently Used Information Primary Memory (Executable Memory) e.g. RAM More Frequently Used Information Secondary Memory e.g. Disk or Tape Operating Systems: A Modern Perspective, Chapter 11
Contemporary Memory Hierarchy &Dynamic Loading CPU Registers Primary (Executable) L1 Cache Memory L2 Cache Memory “Main” Memory Larger storage Rotating Magnetic Memory Faster access Optical Memory Secondary Sequentially Accessed Memory Operating Systems: A Modern Perspective, Chapter 11
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
Address Space vs Primary Memory Hardware Primary Memory Process Address Space Mapped to object other than memory Operating Systems: A Modern Perspective, Chapter 11
Library code Other objects Secondary memory Link Edit Primary memory Loader Process address space • Link time: Combine elements • Load time: • Allocate primary memory • Adjust addresses in address space • Copy address space from secondary to primary memory Creating an Executable Program Source code C Reloc Object code • Compile time: Translate elements Operating Systems: A Modern Perspective, Chapter 11
A Sample Code Segment ... static int gVar; ... int proc_a(int arg){ ... gVar = 7; put_record(gVar); ... } Operating Systems: A Modern Perspective, Chapter 11
The Relocatable Object module Code Segment RelativeAddress Generated Code 0000 ... ... 0008 entry proc_a ... 0220 load =7, R1 0224 store R1, 0036 0228 push 0036 0232 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 Segment RelativeAddress Generated variable space ... 0036 [Space for gVar variable] ... 0049 (last location in the data segment) Operating Systems: A Modern Perspective, Chapter 11
The Absolute Program Code Segment RelativeAddress Generated Code 0000 (Other modules) ... 1008 entry proc_a ... 1220 load =7, R1 1224 store R1, 0136 1228 push 1036 1232 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 Segment RelativeAddress Generated variable space ... 0136 [Space for gVar variable] ... 1000 (last location in the data segment) Operating Systems: A Modern Perspective, Chapter 11
The Program Loaded at Location 4000 Relative Address Generated Code 0000 (Other process’s programs) 4000 (Other modules) ... 5008 entry proc_a ... 5036 [Space for gVar variable] ... 5220 load =7, R1 5224 store R1, 7136 5228 push 5036 5232 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
Static Memory Allocation Operating System Unused In Use Process 3 Process 0 pi Process 2 Issue: Need a mechanism/policy for loading pi’s address space into primary memory Process 1 Operating Systems: A Modern Perspective, Chapter 11
Fixed-Partition Memory Mechanism Operating System pi needs ni units Region 0 N0 pi ni Region 1 N1 N2 Region 2 Region 3 N3 Operating Systems: A Modern Perspective, Chapter 11
Fixed-Partition MemoryBest-Fit Operating System • Loader must adjust every address in the absolute module when placed in memory Region 0 N0 Region 1 N1 Internal Fragmentation pi N2 Region 2 Region 3 N3 Operating Systems: A Modern Perspective, Chapter 11
Fixed-Partition MemoryWorst-Fit Operating System pi Region 0 N0 Region 1 N1 N2 Region 2 Region 3 N3 Operating Systems: A Modern Perspective, Chapter 11
Fixed-Partition MemoryFirst-Fit Operating System pi Region 0 N0 Region 1 N1 N2 Region 2 Region 3 N3 Operating Systems: A Modern Perspective, Chapter 11
Fixed-Partition MemoryNext-Fit Operating System Region 0 N0 pi Region 1 N1 Pi+1 N2 Region 2 Region 3 N3 Operating Systems: A Modern Perspective, Chapter 11
Operating System Operating System Operating System Process 0 Process 0 Process 0 Process 1 Process 6 Process 6 Process 2 Process 2 Process 2 Process 5 Process 3 Process 5 Process 4 Process 4 Process 4 • External fragmentation Loader adjusts every address in every absolute module when placed in memory • Compaction moves program in memory Variable Partition Memory Operating System Operating Systems: A Modern Perspective, Chapter 11
3F016010 Program loaded at 0x04000 • Must run loader over program again! Cost of Moving Programs load R1, 0x02010 3F013010 Program loaded at 0x01000 Consider dynamic techniques Operating Systems: A Modern Perspective, Chapter 11
C-Style Memory Layout Text Segment Initialized Part Data Segment Uninitialized Part Data Segment Heap Storage Stack Segment Environment Variables, … Operating Systems: A Modern Perspective, Chapter 11
Program and Process Address Spaces Process Address Space Primary Memory Absolute Program Address Space 0 User Process Address Space Supervisor Process Address Space 3GB 4GB Operating Systems: A Modern Perspective, Chapter 11
Multiprogramming Memory Support 0 Operating System R0 Unused A In Use B Process 1 R1 C Process 3 R2 D E Process 0 R3 F G Process 1 R4 H Operating Systems: A Modern Perspective, Chapter 11
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
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
Moving an Executable Image 02000 Executable Image Executable Program Loader 06000 Executable Image Loader Operating Systems: A Modern Perspective, Chapter 11
+ Dynamic Address Relocation CPU Relative Address 0x02010 0x12010 Relocation Register 0x10000 load R1, 0x02010 MAR • Program loaded at 0x10000 Relocation Register = 0x10000 • Program loaded at 0x04000 Relocation Register = 0x04000 We never have to change the load module addresses! Operating Systems: A Modern Perspective, Chapter 11
+ Multiple Segment Relocation Registers CPU Relative Address 0x12010 Code Register Stack Register MAR Data Register Operating Systems: A Modern Perspective, Chapter 11
+ Runtime Bound Checking CPU Relative Address Relocation Register < Limit Register • Bound checking is inexpensive to add • Provides excellent memory protection MAR Interrupt Operating Systems: A Modern Perspective, Chapter 11
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 Operating Systems: A Modern Perspective, Chapter 11
Swapping Image for pi Swap pi out Swap pj in Image for pj Operating Systems: A Modern Perspective, Chapter 11
Sharing a Portion of the Address Space Address Space for Process 1 Process 1 Process 2 Primary Memory Address Space for Process 2 Operating Systems: A Modern Perspective, Chapter 11
Figure 11‑26: Multiple Segments CPU Executing Process 1 Private to Process 1 Limit Relocation Limit Relocation Shared CPU Executing Process 2 Private to Process 2 Limit Relocation Limit Relocation Primary Memory Operating Systems: A Modern Perspective, Chapter 11