1.07k likes | 1.08k Vues
This article provides an overview of memory management techniques in operating systems, including memory allocation strategies, segmentation, and paging. It also discusses the functions of a memory manager and the requirements of memory designs.
E N D
Outline • Memory management • Introduction • Memory allocation strategies • Segmentation • Paging
Memory Manager in OS COP4610
Background • 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 COP4610
Review: Instruction Execution • Instruction fetch (IF) MAR PC; IR M[MAR] • Instruction Decode (ID) A Rs1; B Rs2; PC PC + 4 • Execution (EXE) • Depends on the instruction • Memory Access (MEM) • Depends on the instruction • Write-back (WB) COP4610
S1 bus Dest bus S2 bus Control unit ALU A R0, r1,... (registers) C B ia(PC) psw... MAR MDR IR MAR memory address register MDR memory data register IR instruction register Memory
Creating a load module COP4610
Linker function • Combine the object modules into a load module • Relocate the object modules as they are being loaded • Link the object modules together as they are being loaded • Search libraries for external references not defined in the object modules COP4610
Relocation COP4610
Linking COP4610
A Sample Code Segment ... static int gVar; ... int proc_a(int arg){ ... gVar = 7; put_record(gVar); ... } COP4610
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) COP4610
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) COP4610
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) COP4610
Normal linking and loading COP4610
Load-time dynamic linking COP4610
Run-time dynamic linking COP4610
Memory Management COP4610
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 COP4610
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 COP4610
Application Program VirtualAlloc() exec() VMQuery() shmalloc() VirtualFree() sbrk() VirtualLock() getrlimit() ZeroMemory() File Mgr Device Mgr Memory Mgr File Mgr Device Mgr Memory Mgr Process Mgr Process Mgr UNIX Windows Hardware The External View of the Memory Manager COP4610
Less Frequently Used Information More Frequently Used Information Storage Hierarchies COP4610
CPU Registers Less Frequently Used Information Primary Memory (Executable Memory) e.g. RAM More Frequently Used Information Secondary Memory e.g. Disk or Tape The Basic Memory Hierarchy COP4610
CPU Registers Primary (Executable) L1 Cache Memory L2 Cache Memory “Main” Memory Larger storage Rotating Magnetic Memory Faster access Secondary Optical Memory Sequentially Accessed Memory Contemporary Memory Hierarchy COP4610
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 COP4610
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 COP4610
Memory Allocation Strategies - There are two different levels in memory allocation COP4610
Two levels of memory management COP4610
Memory Management System Calls • In Unix, the system call is brk • Increase the amount of memory allocated to a process COP4610
Malloc and New functions • They are user-level memory allocation functions, not system calls COP4610
Memory Management in a Process COP4610
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 COP4610
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 COP4610
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 COP4610
Queue for each block size COP4610
Variably-sized memory requests COP4610
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) COP4610
Issues in Variable partition memory allocation • Where are the free memory blocks? • Keeping track 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? COP4610
The block list method COP4610
Information of each memory block • Information of each memory block • Address: Block start address • size: size of the block. • Allocated: whether the block is free or allocated. Struct list_node { int address; int size; int allocated; struct list_node *next; }; COP4610
After allocating P5 COP4610
After freeing P3 COP4610
Design issues in the list method • Linked list or doubly linked list ? • Keep track of allocated blocks in the list? • Two separate lists • Can be used for error checking • Where to keep the block list • Static reserved space for linked list • Use space with each block for memory management COP4610
Block list with list headers COP4610
Bitmap method COP4610
Which free block to allocate • How to satisfy a request of size n from a list of free blocks • First-fit: Allocate the first free block that is big enough • Next-fit: Choose the next free block that is large enough • Best-fit: Allocate the smallest free block that is big enough • Must search entire list, unless ordered by size. • Produces the smallest leftover hole. • Worst-fit: Allocate the largest free block • Must also search entire list. • Produces the largest leftover hole. COP4610
Fragmentation • Without mapping, programs require physically continuous memory • 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. COP4610