18 March 2002 Operating Systems -------- Read Nutt, Chapter 11 -------- Memory Management Main memory: OS memory and user memory Motivation: sharing of available memory generally do not know where program will be put put where memory is available want to allow swapping do not want to require to be put in same spot in mem when swapped back in Protection shared among users protect procs from one another protect os from users usually some h/w/ support (too expensive all in sw) caching and virtualization if proc requires more memory than machine has only small amount of mem in use at a given time need process to allow prog to execute with only some parts in mem ------------------------------------------ Physical vs. virtual addresses Lifetime of a process: compile time->link time->load time compile time Data: static variables allocated space normal variables will be put onto runtime stack Code: procedure calls to different module (printf), not definable Link time sequence modules, as if starting at memory location 0 fill in "blanks" from earlier, w.r.t cross module proc calls adjust memory locations of static variables w.r.t. offset of new location of module in linked executable Load time most likely not loaded into memory location 0 so, adjust addresses of both data and code to actual physical memory locations ------------------------------------------ Memory Allocation Strategy Need to load the program into main memory before it can be executed Memory manager basically in charge of space multiplexing all of memory Part of preparing a process to run is requesting memory from the memory management part of the os Simple scheme: partitioning Fixed partitioning divide memory into fixed sized pieces, of varying size allocate a process to a big enough partition Internal fragmentation: space allocated to a process which it will not use generally not used outside of batch systems batch ok because size of program is generally known in advance time sharing programs are more dynamic Variable partition "eliminate" internal fragmentation by giving exactly the requested amount not "exact", but rather on word boundaries minimal fragmentation now, need to track the unused memory initially, all unused after processes finish, start to have holes in the memory space these holes are "External fragmentation: pieces of memory not allocated to any process, but too small for a new process to fit into to track free memory, use a few bytes of the actual memory to maintain a "free list", basically a linked list of unused blocks and their sizes Question: how to select the free block to put your program into: Best fit worst fit: idea: increase possibility that another proc can use the remainder first fit : tends to fragment blocks near beginning of the list next fit: make list circular When a process needs more memory, move it to a larger available segment - re run the loader to adjust the addresses When no more "big enough" chunks of memory left, COMPACTION To save some time when relocating a program, can use a relative address in the program, and a relocation register. Relative address + relocation register is the location of memory to use. Typically done with hardware support (fast) Typically break program into code/stack/data segments Can do runtime bounds checking easily, check if resulting address is outside the bounds of the process requesting it. what to do when detect violation? Segment violation/ out-of-bounds reference - exception ------------------------------------------ Swapping: not enough memory for all processes idea: time multiplex the processes take out of main memory when not in use only take out if it is worth it...it will be out for long enough introduces noticeable user delays to bring processes back into main memory Virtual Memory: program viewed as generally one continuous address space generally access "close" data - spatial locality originally motivated by cost - memory was expensive, programs were bigger Allow process to use CPU when only part of its address space is loaded into memory than could fit, or wanted to run too many programs at the same time and not all would fit. contemporary motivation reduce process's execution time by reducing the memory access time delays Instead of changing the memory accesses in the code, memory manager translates the virtual address into a physical address. If the physical address is not loaded in memory, it is brought into memory. This mapping (virtual address translation map) can change over time ------------------------- Segmentation and Paging - different allocation strategies real systems are a combination Paging ------ divide virtual address space into "small" equal sized, logical "pages" physical address of machine is similarly divided into "frames" Then, map from pages to frames, and "nice, linear" virtual space is composed of a bunch of scattered frames