780 likes | 951 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 Operating Systems: A Modern Perspective, Chapter 11
Memory Manager • 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 (when no longer needed) • 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
Memory Manager Responsibilities • Abstraction. Provides software with an abstraction of Primary Memory. • Appears to be a large array of contiguously addressed bytes. • Allocation. Allocates primary memory as requested. • Isolation. Ensures that each process has exclusive access to its allocated memory. • Sharing. Permits explicit sharing of memory between multiple processes. • Virtual Memory. Moves info up and down the memory hierarchy automatically. Operating Systems: A Modern Perspective, Chapter 11
Logical vs Physical address space • Logical address space. The set of addresses that a process can use in its program. • Physical address space. The set of physical addresses that the memory manager allocates to a process. • OS must provide a mechanism for binding local primary memory addresses to physical primary memory addresses. 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 Creating an Executable Program Source code C Relocatable Object code • Compile time: Translate elements into relocatable object code. 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 • into an absolute (or load) module • using the link editor. Creating an Executable Program Absolute program stored in file on secondary memory Source code C Relocatable Object code • Compile time: Translate elements 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: • OS allocates primary memory • Loader adjusts addresses in address space • Copy address space from secondary to primary memory Creating an Executable Program Source code C Relocatable Object code • Compile time: Translate elements Operating Systems: A Modern Perspective, Chapter 11
Compile Time • Compiler translates source program into relocatable code. • In C programming model 3 blocks of addresses: • Code segment • Data segment • Stack segment • Compiler must annotate external references • Example: printf library routine address not known Operating Systems: A Modern Perspective, Chapter 11
Link Time • Link editor combines code and data segments of each relocatable object module into one object. • Combines all code segments into one • Combines all data segments into one • Relocates the address in the instructions to reference the updated addresses in the new data segment • Matches entry point addresses to addresses in the new code segment. • Detects all unresolved address references. • Stores resulting object module in secondary storage. Operating Systems: A Modern Perspective, Chapter 11
Load Time • Prior to loading, mem man allocates a block of primary memory to the process • Loader copies absolute program and data into allocated memory. • Must adjust addresses again; first address is no longer 0. • This is static address binding. • Can also defer address binding to run time. Operating Systems: A Modern Perspective, Chapter 11
Reserve space for gVar in data segment and place in symbol table. Put_record is externally defined; don’t know address. 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 (compile) 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) ... static int gVar; ... int proc_a(int arg){ ... gVar = 7; put_record(gVar); ... } Operating Systems: A Modern Perspective, Chapter 11
Table indicates which references must be resolved. Table defines symbol addresses for other files to use The Relocatable Object module (compile) 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) ... static int gVar; ... int proc_a(int arg){ ... gVar = 7; put_record(gVar); ... } Operating Systems: A Modern Perspective, Chapter 11
External libraries added First address is still 0000 Addresses adjusted The Absolute Program (link) 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) All code segments combined All data segments combined 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) Program loaded at address 4000. All other addresses adjusted Operating Systems: A Modern Perspective, Chapter 11
Dynamic Memory • Example: struct ListNode *node; … node = (struct ListNode *) malloc(sizeof(struct ListNode)); Operating Systems: A Modern Perspective, Chapter 11
Dynamic Memory • Dynamic memory not allocated at runtime! • malloc is not really a system call • Linkage editor reserves space for dynamic memory • Called the heap • Max size of stack and heap determined at link time • One block of memory reserved for both • They “grow” towards each other. • When heap is used up • Runtime code calls mem man (sbrk( ) system call) to request more space • May require address space to be rebound to physical memory 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
Modern Memory Binding • Each process allocated 4GB (on 32-bit systems) address space • Address space partitioned into user space (usually 3GB) and supervisor space (1GB) • The absolute program is mapped into the fixed (4GB) address space when process is executed. • Done by loader • Not mapped to physical addresses • Extra mapping done by mem man • Reason: allows dynamic binding and memory mapped files. • At run time, the part of the address space that is used will be mapped onto physical memory. 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
Memory Allocation • Before an address space can be bound to the primary memory, the mem man must allocate space to the process • Mem man must have a strategy for allocating memory: • Fixed-Partition memory strategy • Variable-Partition memory strategy • Contemporary strategies (variations of variable partition) 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 Need to support multiprogramming with memory allocation F G Process 1 R4 H Operating Systems: A Modern Perspective, Chapter 11
Memory Allocation Operating System Unused Issue: fragmentation. Memory blocks to small to be used efficiently. 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 Mem man divides physical memory into N fixed-sized regions. Regions may be different sizes. 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 Memory Mechanism • Main memory usually into two partitions: • Resident operating system, usually held in low memory with interrupt vector • User processes then held in high memory • Single-partition allocation • Relocation-register scheme used to protect user processes from each other, and from changing operating-system code and data • Relocation register contains value of smallest physical address; limit register contains range of logical addresses – each logical address must be less than the limit register Operating Systems: A Modern Perspective, Chapter 11
A base and a limit register define a logical address space Operating Systems: A Modern Perspective, Chapter 11
HW address protection with base and limit registers Operating Systems: A Modern Perspective, Chapter 11
Contiguous Allocation (Cont.) • Multiple-partition allocation • Hole – block of available memory; holes of various size are scattered throughout memory • When a process arrives, it is allocated memory from a hole large enough to accommodate it • Operating system maintains information about:a) allocated partitions b) free partitions (hole) OS OS OS OS process 5 process 5 process 5 process 5 process 9 process 9 process 8 process 10 process 2 process 2 process 2 process 2 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
Variable-Partition Memory(also called dynamic memory allocation) • Memory man dynamically defines regions according to needs of the processes. • No internal fragmentation possible. • Holes: space left when a process terminates. • External fragmentation: When a new process fills a hole, the unused space left. Operating Systems: A Modern Perspective, Chapter 11
Operating System Operating System Process 0 Process 0 Process 6 Process 6 Process 2 Process 2 Process 5 Process 5 Process 4 Process 4 • External fragmentation • Compaction moves program in memory Variable Partition Memory Operating System Operating System 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
Fragmentation • External Fragmentation – total memory space exists to satisfy a request, but it is not contiguous • Internal Fragmentation – allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used Operating Systems: A Modern Perspective, Chapter 11
Fragmentation • Reduce external fragmentation by compaction • Shuffle memory contents to place all free memory together in one large block • Compaction is possible only if relocation is dynamic, and is done at execution time • I/O problem • Latch job in memory while it is involved in I/O • Do I/O only into OS buffers Operating Systems: A Modern Perspective, Chapter 11
Dynamic Storage-Allocation Problem How to satisfy a request of size n from a list of free holes • First-fit: Allocate the first hole that is big 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. First-fit and best-fit better than worst-fit in terms of speed and storage utilization Operating Systems: A Modern Perspective, Chapter 11
Fixed-Partition MemoryBest-Fit Operating System Hole available, but not the best fit p0 External Fragmentation pi p2 Region 3 p1 Operating Systems: A Modern Perspective, Chapter 11
pi Fixed-Partition MemoryWorst-Fit Operating System Idea: leave the largest hole. pj Region 1 pk Operating Systems: A Modern Perspective, Chapter 11
pi Fixed-Partition MemoryWorst-Fit Operating System Idea: leave the largest hole. pj Region 1 pk Operating Systems: A Modern Perspective, Chapter 11
Fixed-Partition MemoryFirst-Fit Operating System Idea: don’t waste time trying to fit. p0 p1 Region 1 pi First large-enough hole Region 2 p2 Operating Systems: A Modern Perspective, Chapter 11
Fixed-Partition MemoryNext-Fit Operating System Treat memory as a circular queue. Idea: spread out the fragmentation. Region 0 pi Region 1 P0 Region 2 Pi+1 Region 3 Next large-enough hole Operating Systems: A Modern Perspective, Chapter 11
3F016010 Program loaded at 0x04000 • Must run loader over program again! • Must use the original absolute file because it • has special flags to locate addresses to change • But must preserve changed data-so first write out Cost of Moving Programs load R1, 0x02010 3F013010 Program loaded at 0x01000 Operating Systems: A Modern Perspective, Chapter 11
Moving an Executable Image 02000 Executable Image Executable Program Loader 06000 Executable Image Loader When a process is moved, the loader must be run again to re-load the process into memory. 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 while executing • Smaller Creates an external fragment • Larger May have to move/relocate the program • Allocate “holes” in memory according to • Best- /Worst- / First- /Next-fit • Mem man must maintain linked list of free blocks. Operating Systems: A Modern Perspective, Chapter 11
Memory Mgmt Strategies • Fixed-Partition used only in batch systems • Variable-Partition used in some form in all contemporary memory man • though VM uses fixed size of memory blocks (pages) • Older systems (MS-DOS, V7 UNIX) used variable-sized blocks of memory • Used strategy like best-fit Operating Systems: A Modern Perspective, Chapter 11