Segment-Based Memory Management
E N D
Presentation Transcript
Segment-Based Memory Management • stages of address assignment • memory management unit • differentiation of RAM information storage • segments • allocation • internal • external • segment table • usage
Stages of Address Binding • binding - assigning memory addresses to program objects (variables, pointers, etc.) • physical address - the “hardware” address of a memory word (0xb0000) • logical address (virtual address, relative address) - used by the program • relocatable address - relative address (14 bytes from the beginning of this module) • absolute code - all addresses are physical • relocatable code - all addresses are relative • linking - preparing compiled program to run • static – all lib-functions included in the code • dynamic - lib-functions can be connected at load-time • loading - a program into memory • static - all at once • dynamic - on demand
Memory-Management Unit • hardware device that translates logical memory addresses generated by program running on CPU to physical address • MMU differs from architecture to architecture • relocation register MMU • loads beginning (physical address) of the module to the register • logical address is added tothe registerto produce physicaladdress
Types of Information Stored in RAM differs by • role in program: • program instructions • constants: • pi, maxnum, strings used by printf/scanf • variables: • locals, globals, function parameters, dynamic storage (from malloc or new) • initialized or uninitialized • by protection status (important for sharing data and code): • readable and writable: variables • read-only: code, constants • addresses vs. data: • must modify addresses if program is moved (relocation, garbage collection)
Segments • process’ memory is divided into logical segments (text, data, bss, heap, stack) • some are read-only, others read-write • some are known at compile time, others grow dynamically as program runs • lately not used directly but helpful to app programmers • who assigns memory to segments? • compiler and assembler generate anobject file (containing code and data segments) from each source file • linker combines all the object files for a program into a single executable object file,which is complete and self-sufficient • loader (part of OS) loads an executable object file into memory at location(s) determined by the operating system • os for program as program runs and uses new/delete to dynamically allocate memory, gets/releases space on stack during function calls
Internal Allocation in Variable-Size Segments • stack • good when allocation and freeing are somewhat predictable • typically used: • to pass parameters to /return values form procedures • for allocating space for local variables inside a procedure • use stack operations: push and pop • simple and efficient, but restrictive • keeps all free space together in one place • heap • used when allocation and freeing are not predictable • used for arbitrary list structures, complex data organization, etc. • more general, less efficient, more difficult to implement • system memory consists of allocated areas and free areas (holes)
Fragmentation • fragmentation – free memory is spread in multiple places of various sizes (fragments) which makes further allocation difficult/impossible and thus wastes memory • internal – unused memory fragments are inside the allocation units • external – unused memory fragments are outside the allocation units • example: heap – arbitrary dynamic allocation/deallocation leads to fragmentation • is this fragmentation internal or external? • how to reuse the fragments? • does stack allocation lead to fragmentation?
Segment Allocation • static – physical memory addressesare encoded in program (MS DOS .com executables) • dynamic – uses logical addresses needs per process segment table • segment-table base register (STBR) points to the segment table’s location in memory • segment-table length register (STLR) indicates number of segments used by a program • each segment table entry has • base – contains the starting segment physical memory address • limit – specifies the length of the segment • protection bits • read/write/execute privileges • validation bit = 0 illegal segment • table entries of several processes may point to the same physical segment, why?
Segment Use • logical memory access request is – <segment number, offset> it has to be translated to physical address and checkedfor validity • validity check • if segment number < STLR • if offset is less than segment limit • if access type is compatible withprotection bits (no writing to read only segment) • if fails – memory access violation trap to kernel • dynamic allocation of segments may lead to fragmentation • why? is it internal or external? • compaction – relocating segments to free unused space • how can it be done? can compaction be used with static relocation?is compaction efficient?