2Main Memory Large array of words or bytes each having its own address Several processes must be kept in main memory to improve utilization and system response: memory sharingMemory management algorithms vary from simple approach to paging and segmentation strategies
3Memory Basics Types of memory addresses Symbolic (e.g. program variables)Relocatable (e.g. offset + 100)Absolute (e.g. 255)Address binding: Which data goes where. May be determined in any of the following phasesCompile time: compiler generates absolute code for execution in a fixed addressLoad time: compiler generates relocatable code, link- loader computes relocation addresses during link timeExecution time: allows executing processes to move, by simply changing values of segment registers.
4Logical & Physical Addresses Logical or virtual address – address generated by the CPU.Physical address – address loaded into the memory-address register of RAMCompile-time and load-time binding generate identical logical and physical addressesExecution-time binding generate differing logical and physical addressesMemory management unit – maps virtual to physical addresses during runtime.
5Improving Memory Utilization Dynamic loadingSubroutines are stored on disk in relocatable format, and are not loaded until calledDynamic linkingLinking to subroutine libraries postponed until run-time; stub used as pointer to routine in a shared DLL.Conserves disk space – a single copy of library is shared by all executing processes. Versioning issue.OverlaysKeeps only needed instructions in memoryOther instructions overwrite previously occupied address when needed. May be implemented by programmer.
6Swapping Used if memory is no longer sufficient The roll out (swap out to disk) and roll in (swap to MM) drastically increases time for context switch
7SwappingNeeds a fast backing store (secondary memory) for efficient implementationWaiting processes are good candidates to be swapped out to diskProcesses waiting on I/O should not be swapped out, since the I/O could be into their address space, or I/O should be done into OS buffers only.
8SwappingModified version of swapping used in Unix. Swapping normally disabled. Enabled when memory runs out due to many running processes.In Linux, only R/W data segment needs to be swapped out, R/O code segment are just overwritten, since it can be reread from disk.Microsoft Windows provides partial swapping - if not enough memory for a new program, current program is swapped out to disk. User determines swap rather than scheduler
9Memory ProtectionOS must protect itself from user processes, and must protect user processes from each other.Each process is assigned a relocation register and a limit register.Relocation register contains the value of the smallest physical address.Logical address must be less than the limit register
11Contiguous Allocation Type of allocation where each process occupies only a single continuous block in main memorySimple two-partition schemeLow memory: usually contains resident O/S since interrupt vectors are hereRest of memory: for user processes
12Contiguous Allocation Multiple-Partition AlgorithmSimplest is multiple fixed partitionEach partition is allocated to a processMultiprogramming limited to number of partitionsDynamic partitioningStarts with one large memory block called a “hole”Processes arrive and are allocated a blockHoles become available as processes terminate
14Contiguous Allocation Dynamic storage allocation problemlooking for freed memory for waiting processesset of holes searched to see w/c one to allocatefirst-fit - allocate first hole big enough for process (search may start from beginning or end)best-fit - allocate smallest hole that is big enoughworst-fit - allocate largest hole -> produces largest leftover (sometimes useful than smaller ones)first/best better in time/storage utilization, respectivelyfirst-fit is generally faster
15Contiguous Allocation FragmentationExternalEnough memory exists but not contiguous50% rule - given N allocated blocks, the next 0.5N blocks will be lost due to fragmentation (1/3 is unusable) when first-fit is used.InternalAllocate memory in fixed-sized units, say 4k blocks. Memory actually allocated to a process may be slightly larger than requested memory. The difference is internal fragmentation.
16Contiguous Allocation CompactionA solution to external fragmentation – shuffle memory contents to place all free memory together in one large block.Not always possible if relocatable addressing at execution time is not supportedAlgorithm 1: move processes to one end (expensive if many processes are moved)Algorithm 2: create big hole in the middleSwapping may be combined w/ compactionprocesses rolled out to backing store then back incompaction not possible w/ disk due to slow access
17PagingAllows non-contiguous allocation -> solves external fragmentationLogical memoryFixed sized blocks called pagesPhysical memoryFixed-sized blocks called framesPage table converts pages to frames, using address translation hardwarepage# + offset -> frame# + offset, using page tableBacking store (swap partition in Linux)same structure as logical memory
19Internal Fragmentation in Paging External fragmentation is eliminated but internal fragmentation is not since processes rarely take up all the memory space allocated to themWorst case:Process needs (n pages + 1 byte)Wasted space of (page size bytes – 1 byte)
20Page Table StructureEach O/S has own method for storing page tables -> most allocate a page table per processContext-switch time increases with paging due to need to store page tables in PCB of each processEach page table entry might include access type as r/o (constant data), r/w (variable data), or x/o (code), to implement a further level of memory protectionPage table structure: linear, multilevel hierarchical, hashed, inverted.
21Shared Pages Shared read-only/execute-only code When several instances of a single program are running, only one copy of the program code is stored in memory, but each instance has its own program data. Code must be reentrant.Possibility of sharing common code is another advantage of pagingSimilar to sharing of address space of a task by threads.
23Segmentation Scheme divides logical memory into segments More intuitive view of memory from user’s point of view (e.g. program divided into segments -- subroutines, procedures, data -- that have different length)
24Logical View of Segmentation 14231234user spacephysical memory space
25Segmentation Hardware Logical (2-dimensional) vs. physical (1- dimensional) -> mapping effected through a segment tableEntry consists of segment base (=starting address in physical memory) and limit (= length of segment)Similar to concept of pages except segments do not have fixed lengthIntel 80x86 is based on segmentation
27Segments and Fragmentation Segmentation may cause external fragmentationHappens when all free blocks are too small to accommodate a segmentCompaction may be used to solve the problemProcess may wait if segment cannot be found
28Segmentation with Paging Solves the external fragmentation problem of pure segmentationEach segment is composed of several equal-sized pages