Types of information information stored in memory n Kernel management (next lecture) n By role in program: n User-process memory management u Program instructions u internal to process u Constants: F segments F pi, maxnum, strings used by printf/scanf F static allocation u Variables: F dynamic allocation F Locals, globals, function parameters, dynamic storage (from F allocation methods malloc or new) F reclaiming: garbage collection F Initialized or uninitialized u external n By protection status (important for sharing data and code): F stages in address binding u Readable and writable: variables F static relocation u Read-only: code, constants F dynamic relocation n Addresses vs. data: • swapping u Must modify addresses if program is moved (relocation, garbage • compaction collection) F paging (next lecture but one) 1 2

Segments Internal dynamic memory allocation

n Static allocation is not sufficient for heaps and stacks n Process’ memory is divided into logical segments (text, data, bss, u Need dynamic storage — programmer may not know how heap, stack) much memory will be needed when program runs u Some are read-only, others read-write F Use malloc or new to get what’s necessary when it’s u Some are known at , others grow dynamically as necessary program runs F For complex data structures (e.g., trees), allocate space n Who assigns memory to segments? for nodes on demand u and assembler generate an object file (containing u OS doesn’t know in advance which procedures will be called code and data segments) from each source file (would be wasteful to allocate space for every variable in u Linker combines all the object files for a program into a single every procedure in advance) object file, which is complete and self-sufficient u OS must be able to recursive procedures u Loader (part of OS) loads an executable object file into memory n Dynamic memory requires two fundamental operations: at location(s) determined by the u Allocate dynamic storage u Program (as it runs) uses and to dynamically new malloc u Free memory when it’s no longer needed allocate memory, gets space on stack during function calls n Methods vary for stack and heap

3 4

Memory allocation methods Internal fragmentation n Stack (hierarchical) u Good when allocation and freeing are somewhat predictable n eventually end up with many small holes u Typically used: (fragments), each too small to be useful F to pass parameters to procedures u fragmentation, and it leads to wasted F for allocating space for local variables inside a procedure memory F for tree traversal, expression evaluation, parsing, etc. u Fragmentation wasn’t a problem with u Use stack operations: push and pop stack allocation, since we always add/delete from top of stack u Keeps all free space together in a structured organization u Solution goal: reuse the space in the u Simple and efficient, but restricted holes in such a way as to keep the n Heap number of holes small, and their size large u Used when allocation and freeing are not predictable n Compared to stack: more general, less u used for arbitrary list structures, complex data organizations, efficient, more difficult to implement etc. u Use new (malloc) to allocate space, use delete or free to release space u System memory consists of allocated areas and free areas 5 6 (holes) Heap memory allocation Reclaiming memory n Heap-based dynamic memory allocation techniques n typically maintain a free list, which keeps track of all the holes When can memory be freed? u Whenever programmer says to n Algorithms to manage the free list: u u Best fit Any way to do so automatically? F F Keep linked list of free blocks Difficult if that item is shared (i.e., if there are multiple pointers to it) F Search the whole list at each allocation n Potential problems in reclamation F Choose the hole that comes the closest to matching the request u is finished size; any unused space becomes a new (smaller) hole Dangling pointers — have to make sure that everyone using it F When freeing memory, combine adjacent holes u Memory leak — must not “lose” memory by forgetting to free it F Any way to do this efficiently? when appropriate u First fit n Implementing automatic reclamation: F Scan the list for the first hole that is large enough, choose that hole u Reference counts F Otherwise, same as best fit F Used by file systems u Worst fit F OS keeps track of number of outstanding pointers to each F scan for the largest hole (hoping that the remaining hole will be memory item large enough to be useful) F When count goes to zero, free the memory u Which is better? Why?? 7 8

Garbage collection Stages of address binding n u Garbage collection binding - assigning memory addresses to program objects (variables, pointers, etc.) F Used in LISP, Java n physical address - the “hardware” address F Storage isn’t explicitly freed by a free operation; programmer of a memory word (0xb0000) just deletes the pointers and doesn’t worry about what it’s n pointing at relocatable address - relative address (14 bytes from the beginning of this module) F When OS needs more storage space, it recursively searches n absolute code - all addresses are physical through all the active pointers and reclaims memory that no one is using n relocatable code - all addresses are relative F Makes life easier for application programmer, but is difficult to n linking - preparing compiled program to run program the garbage collector u static - all lib-functions included in the F Often expensive — may use 20% of CPU time in systems that code use it u dynamic - lib-functions can be hooked • May spend as much as 50% of time allocating and up at load-time automatically freeing memory n loading - a program into memory u static - all at once u 9 dynamic - on demand 10

Linking Why is linking difficult

n Functions of a linker: n When assembler assembles a file, it may find external references u Combine all files and libraries of a program — symbols it doesn’t know about (e.g., printf, scanf) u Regroup all the segments from each file together (one big data u Compiler just puts in an address of 0 when producing the object segment, etc.) code u Adjust addresses to match regrouping u Compiler records external symbols and their location (in object file) in a cross-reference list, and stores that list in the object file u Result is an executable program u Linker must resolve those external references as it links the n Contents of object files: files together u File header — size and starting address (in memory) of each n segment Compiler doesn’t know where program will go in memory (if u multiprogramming, always 0 for uniprogramming) Segments for code and initialized data u u Compiler just assumes program starts at 0 Symbol table (symbols, addresses) u u Compiler records relocation information (location of addresses External symbols (symbols, location) to be adjusted later), and stores it in the object file u Relocation information (symbols, location) n for details type man ld u information u For UNIX details, type man a.out 11 12 Loading Goals in memory usage for multiprogramming n The loader loads the completed program into memory where it can be executed u Loads code and initialized data segments into memory at n Transparency: specified location u Multiple processes must coexist in memory u Leaves space for uninitialized data (bss) u No process should be aware that the memory is shared u Returns value of start address to operating system u Each process should execute regardless of where it is n Alternatives in loading located in memory u Absolute loader — loads executable file at fixed location n Safety: u Relocatable loader — loads the program at an arbitrary u Processes must not be able to corrupt each other, or the OS memory location specified by OS u Protection mechanisms are used to enforce safety u Assembler and linker provide relocatable addreses, loader n Efficiency: translates them to physical addresses u The performance of the CPU and memory should not F When program is loaded, loader modifies all addresses degrade very much as a result of sharing by adding the real start location to those addresses

13 14

Static external relocation Static external relocation (cont.)

n Problems with static relocation: u Safety — not satisfied — one process can access / corrupt n Put the OS in the highest another’s memory, can even corrupt OS’s memory main main memory u Processes can not change size (why…?) address memory address memory 2400 2400 n Compiler and linker u Processes can not move after beginning to run (why would they OS OS assume each process want to?) 2200 2200 1900 starts at address 0 u Used by MS-DOS, Windows, Mac OS n B At load time, the OS: n An alternative: dynamic relocation u Allocates the process u The basic idea is to change each memory address dynamically as 1200 1200 a segment of memory the process runs in which it fits u This translation is done by hardware — between the CPU and the completely A A memory is a (MMU) (also called a u Adjusts the translation unit ) that converts virtual addresses to physical addresses in the addresses 0 0 processes to reflect F This translation happens for every memory reference the its assigned location process makes in memory 15 16

Dynamic relocation Dynamic relocation (cont.) n There are now two different views of the address space: u The physical address space — seen only by the OS — is as large as there is physical memory on the machine u The virtual (logical) address space —seen by the process — can be as large as the instruction set architecture allows F For now, we’ll assume it’s much smaller than the physical address space u Multiple processes share the physical memory, but each can see only its own virtual address space n The OS and hardware must now manage two different addresses: u Virtual address — seen by the process u Physical address — address in physical memory (seen by OS)

17 18 Implementing dynamic relocation Swapping

virtual address n If there isn’t room enough in memory for all processes, some MMU processes can be swapped out to make room u OS swaps a process out by storing its complete state to disk base limit (relocation) + > u register register OS can reclaim space used by ready or blocked processes n When process becomes active again, OS must swap it back in (into memory) u physical address address error With static relocation, the process must be replaced in the exception — trap to OS same location u With dynamic relocation, OS can place the process in any n MMU protects address space, and translates virtual addresses free partition (must update the relocation and limit registers) u Base register holds lowest virtual address of process, limit n Swapping and dynamic relocation make it easy to increase the register holds highest size of a process and to compact memory (although slow!) u Translation: physical address = virtual address + base u Protection: if virtual address > limit, then trap to the OS with an address 19 20 exception

Compaction Evaluation of dynamic relocation

n Dynamic relocation leads to external fragmentation n Advantages: - unused space is left between processes u OS can easily move a process n compaction - overcomes this problem by moving u OS can allow processes to grow the processes so that memory allocation is u Hardware changes are minimal, but fairly fast and contiguous efficient n in previous example we can compact the n Disadvantages: processes to free up 256K of contiguous memory u space (enough to load additional process) by Compared to static relocation, memory addressing is moving the total of 416K of memory slower due to translation u Memory allocation is complex (partitions, holes, n how is it done? fragmentation, etc.) n Can compaction be used with static relocation? u If process grows, OS may have to move it n Is compaction efficient? u Process limited to physical memory size u Not possible to share code or data between processes

21 22