<<

MemoryManagement

CS217

MemoryManagement

• Problem1:

Twoprogramscan’tcontrolallofmemorysimultaneously • Problem2:

Oneprogramshouldn’tbeallowedtoaccess/change thememoryofanotherprogram 0 OS • Problem3: 0x2000

Machinemayhaveonly256MBofmemory, Text whilevirtualaddressspaceis4GB Data BSS Heap OOppeerraattiinnggssyysstteemmmmuussttmmaannaaggee sshhaarriinnggooffpphhyyssiiccaallmmeemmoorryy bbeettwweeeennmmaannyypprroocceesssseess Stack 0xffffffff

1 VirtualMemory

• Basicidea

Programsdon’t(andcan’t)namephysicaladdresses

Instead,theynamevirtualaddresses (eachhasownaddressspace)

Thekerneltranslateseachvirtualaddressintoaphysicaladdress beforetheoperationiscarriedout • Advantages

Canrunmanyprogramsatonce, withoutthemworryingthattheywillusethesamephysicalmemory

Kernelcontrolsaccesstophysicalmemory,sooneprogramcan’t accessormodifythememoryofanother

Canrunaprogramthatusesmorevirtualmemorythanthe computerhasavailableinphysicalmemory

Segmentation

• Allocatememoryforsegments

Providemappingfromaddressesinsegmentstophysicalmemory • Usebaseandlimitregisterstotranslatevirtualaddresses tophysicaladdresses

1 limit base Baseregister Virtual Physical Address Address cpu < + 2

Limitregister Physical Memory

2 Segmentation

• Allocatememoryforsegments

Providemappingfromaddressesinsegmentstophysicalmemory • Problems:

Segmentsmaygrow Physical Disk Memory Storage Fragmentation

Largeprocesses

Swappingefficiency 1

Baseregister 3

2

Limitregister

Paging

• Motivation

Mappingentiresegmentsistoocoarsegranularity

Mappingindividualbytesistoofinegranularity • Pages

Divideupmemoryintoblocks,calledpages(~4KB)

Eachvirtualpagecanbemappedtoanyphysicalpage

Eachtranslationinvolvestwosteps: – Decidewhichphysicalpage holdsthevirtualaddress – Decideawhatoffset thevirtualaddressisinsidethepage

Thephysicaladdressisformedbygluingtogether thephysicalpagenumberandtheoffsetwithinthepage

3 Paging

• Pagetablemapsvirtualaddressestophysicaladdresses

Silberschatz &Peterson

Paging(cont)

Silberschatz &Peterson

4 PagedSegmentation

Silberschatz &Peterson

Swapping

• Whathappensifcumulativesizesofsegments exceedsvirtualmemory?

5 SwappingtoDisk

• Ifallthevirtualmemorycan’tfitinphysicalmemory, theOScantemporarilystashsomepagesondisk

Cansupportvirtualmemorybiggerthanphysicalmemory

Silberschatz &Peterson

PageTable

• TheOSstoresforeachpage...

Physicalpagenumber(24bits)

Cacheablebit()

Modifiedbit(M)

Referencedbit(R)

Accesspermissions(Readonly,Read/write)

Valid/invalid(V)

6 PageFaults

• Ifprocessaccesses virtualaddressthat mapstoapage notinmemory, thentheOSmust fetchthatpage fromdisk • Sincemost referencesfollow othersonsame page,thecostof readingfromdisk isamortizedacross Silberschatz manyreferences &Peterson

PageReplacement

• Whenreadonepagefromdisk, anotherpagemustbeevicted? • Whichpageshouldbereplaced?

Ideal: – Onethatwillbeaccessedfurthestinfuture

Practicalheuristics: – Leastrecentlyused – Leastfrequentlyused – Etc. void StringArray_read(StringArray_Ts,FILE*fp) { charstring[MAX_STRING_LENGTH];

s->nstrings =0; while(fgets(string,MAX_STRING_LENGTH, fp)){ StringArray_grow(nstrings+1); s->strings[(s->nstrings)++]= strdup(string); } }

7 PageReplacement(cont)

Silberschatz &Peterson

WorkingSets

• Localityofreference

Mostmemoryreferencesarenearbypreviousones • Workingset

Atanypointinaprogram’sexecution,usually asmallregionofmemoryisaccessedfrequently

Theregionofmemory(workingset)changesduring thecourseofexecution int main() { Array_T*strings; strings= ReadStrings(stdin); SortStrings(strings); WriteStrings(strings,stdout);

return0; }

8 Thrashing

• Whathappenswhencumulativesizeofworkingsets exceedscapacityofphysicalmemory?

StorageHierarchy

• Registers ~128,1-5nsaccesstime(CPUcycletime) • Cache 1KB– 4MB,20-100ns(multiplelevels) • Memory 64MB– 2GB,200ns • Disk 1GB– 100GB,10ms • Long-termStorage 1TB,1-10s

9 StorageHierarchyLatency

Jim Gray

Summary

• Memorymanagement

Importantfunctionofoperatingsystem

Understandinghowitworksiscriticalto effectivesystemdevelopment • Virtualmemory

OS&Hardwaresupportformapping virtualaddressestophysicaladdresses

Mappingisusuallyatpagegranularity,whichfacilitates... – Relocation – Swappingtodisk – Protection – Fragmentation – Sharing

10