28 -OutlineManaging Free Memory (Base & Limit)AnnouncementsReading:PagingManaging Free MemoryKernel is responsible forkeeping track of whereprocesses are loaded in RAM.How to keep track of the gaps?Virtual Memory1)3)2)MOS 3.1 - 3.2MOS 3.3 - 3.7PA 4 due FridayNo class FridayMemory Management& Virtual MemoryBCAOS?----------------------------------------------bitmapeach allocation unit is a bit in an arraynodes have a type, start index, and length"p" node contains process, "h" node is freelist is sorted by increasing addressFirst Fit (MINIX 3)Base & Limit registersDevised by John Fotheringham in 1961 for Atlassupercomputer to run programs larger than RAM.Process address space: 0x00 to MAXCPU instructions operate on virtual addressesHow do we load only part of a program?Chunks are implemented as:Decouple process address space from physicalmemorySwappingMemory Managementillusion of large, private address spaceMMU translates virtual address to physical32-bit => 2^32 - 1 ~ 4GB64-bit => 2^64 - 1 ~ 16,000,000,000 GBprogram has no awareness of translationbreak virtual address space into chunksSegments (obsolete)Paginge.g. stack, heap, bss, dataeach segment is complete address space,switching between themchunks are fixed size based on MMUusually 4kB chunks (can be smaller or larger)translation performed by MMUdetermines pointer size, e.g. 64 bits16 exabytes is huge and more bits expensivetypically use 48 bits, sometimes 57physical addresses use less (e.g. 39, ~500GB)kernel manages mappingPage = fixed size chunk in virtual memoryPages are mapped to Frames by MMU & kernelApplication sees a single, private, contiguousaddress space (based on number of address bits).Practically, MMU doesn't need full 64 bits.You can find this info on Linux:Pages do not need to be mapped contiguously!Not all pages need to be loaded simultaneously!Process memory does not need to be contiguous!MMU splits address into:Page number used to index into in a page table tolookup corresponding frame number.Offset is appended to frame number to getphysical address.Given 4kB pages size. Virtual addresses are 14bits, how many pages?Physical addresses are 13 bits, how many frames?Frame = same size chunk in physical memoryonly in Intel/AMD for backward compatibilityorigin of "segmentation fault", now misnomermap chunks in virtual space to physicalonly load chunks that are in useif instruction accesses unloaded memory, kernel catcheshardware error, loads chunks from disk, then re-executesinstruction.virtual address space usually much larger than physical memoryallow partially loaded programs to executeenables finer-grained memory protectionprovide relocation & protectionmanage many processesmove processes between RAM and diskfragmentation sucksWhich hole to use when allocating new processNext FitBest FitWorst FitQuick Fitsearch from the beginning and take first fitting hole.simple, allocations distributed toward beginning of memory.start search from the last fit and take the next.much faster, distributes memory usage across full range.memory efficiency worse in some cases.search entire list and take smallest fitting hole.slower, less efficient than expected due to many small holes.maintain separate lists with common hole sizes.very fast allocation, very slow deallocation/merging.search entire list and take largest fitting hole.slower, no real benefit in simulation.need to split and merge nodes whencreating/deleting processesneed to search for sufficientlylong run-length of 0's.runs can straddle word boundaries,which complicates things.bit is 1 if block is allocated, 0 if freeCons:Pros:simplespace efficientperformance concernsDivide memory into allocation units (fewwords to many kB)linked listBitmapLinked Listsneed to know where to loadnew processesMOS Figure 3-6MOS Figure 3-6--Cons:Pros:fast searchingmore to modifyMOS Figure 3-7AlgorithmsRecapBays, 1977Virtual MemoryDetailsPagingExercisePollEvcat /proc/cpuinfo | grep -e "address sizes"MMUMMUstackheapbssdataproc Aproc Bproc Ctextvirtualaddress spacepagesphysicaladdress spacephysicaladdress spacevirtualaddress spacesframesframesMOS Figure 3-8Address Translation----page numberpage table maintained by kernellookup done in hardware, extremely fastoffset (into page)0xdeadbeef0xdeadb0xeefpage numberoffsetExercisePollEvGiven 32-bit address and 4kB page size, how manybits for offset and page number?0xdeadb......0xbframe 0xbframe #......0xeefoffsetpage tablephysicalmemoryvirtual addressphysical address0xbeef