Upload
shubhamne
View
24
Download
2
Embed Size (px)
DESCRIPTION
memory management lecture
Citation preview
Lecture7:MemoryManagementCSE120:PrinciplesofOpera>ngSystems
UCSanDiego:SummerSessionI,2009FrankUyeda
Announcements
• PeerWiseques>onsduetomorrow.• Project2isdueonFriday.– MilestoneonTuesdaynight.
• Homework3isduenextMonday.
2
GoalsforToday
• Mo>va>onforMemoryManagement• UnderstandPaging– Howtotranslatefromvirtualtophysicaladdress
– Determinesize,structureofpagetables
3
MemoryManagement
• Goalsofmemorymanagement– Provideaconvenientabstrac>onforprogramming– Allocatescarcememoryresourcesamongcompe>ngprocesses
– Maximizeperformancewithminimaloverhead• Mechanisms– Physicalandvirtualaddressing– Techniques:Par>>oning,paging,segmenta>on– Pagetablemanagement,TLBs,VMtricks
• Policies– Pagereplacementalgorithms
4
Inthebeginning…..
• Batchprogrammedsystems– Programsusephysicaladdressesdirectly
– OSloadsjob,runsit,unloadsit– Similartowhatnachosdoesrightnow
(you’llchangethisinProject2)
5
Stack
Heap
DataSegment
TextSegment
SP
PC
Opera>ngSystem
PhysicalMemory
LettherebeMul>programming
• Mul>programmingchangesallofthis– Wantmul>pleprocessesinmemoryatonce
• OverlapI/OandCPUofmul>plejobs
– Candoitanumberofways• Fixedandvariablepar>>oning,paging,segmenta>on
– Requirements• Needprotec>on–restrictwhichaddressesjobscanuse• Fasttransla>on–lookupsneedtobefast• Fastchanges–upda>ngmemoryhardwareoncontextswitch
6
VirtualMemory
• Thebasicabstrac>onprovidedbytheOSmemorymanagementisvirtualmemory– Aprocess’saddressspaceinmemoryisnotnecessarilythesameasthephysicalmemory(RAM)addressinwhichitresides
– Whenaprocessrequestsamemoryaddress,theOSwilltranslatetheaddressfromavirtualaddresstoaphysicaladdress.
7
VirtualAddresses
• Processesaccessmemoryusingavirtualaddress– ThevirtualaddressisnotthesameasthephysicalRAMaddressinwhichitresides– TheOS(hardwareMMU)translatesthevirtualaddressintothephysicalRAMaddress
– Whodeterminesthemappingforthetransla>on?
8
Stack
Heap
DataSegment
TextSegment
SP
PC0x00…….(Star>ngAddress)
0xFFF…..(EndingAddress)
AddressSpace
VirtualAddresses• Processesaccessmemoryusingavirtualaddress
– ThevirtualaddressisnotthesameasthephysicalRAMaddressinwhichitresides– TheOS(hardwareMMU)translatesthevirtualaddressintothephysicalRAMaddress– Whodeterminesthemappingforthetransla>on?
9
Stack
Heap
DataSegment
TextSegment
SP
PC0x00…….(Star>ngAddress)
0xFFF…..(EndingAddress)
AddressSpace
Stack
Heap
DataSegment
TextSegment
PhysicalMemory
MMU
…..
VirtualMemory
VirtualAddresses• Processesaccessmemoryusingavirtualaddress
– ThevirtualaddressisnotthesameasthephysicalRAMaddressinwhichitresides– TheOS(hardwareMMU)translatesthevirtualaddressintothephysicalRAMaddress– Whodeterminesthemappingforthetransla>on?
10
Stack
Heap
DataSegment
TextSegment
SP
PC0x00…….(Star>ngAddress)
0xFFF…..(EndingAddress)
AddressSpace
Stack
Heap
DataSegment
TextSegment
PhysicalMemory
MMU
…..
VirtualMemory
VirtualMemory
• Virtualmemoryenablesprogramstoexecutewithoutrequiringtheiren>readdressspaceresideinphysicalmemory
– Savesspace• Manyprogramsdonotneedalloftheircodeanddataatonce(orever),sothereisnoneedtoallocatememoryforit
– Allowsflexibilityforapplica>onandOS• Indirec>onallowsmovingprogramsaroundinmemory;OScanadjustamountofmemoryallocatedbaseduponitsrun‐>mebehavior
• Allowsprocessestoaddressmoreorlessmemorythanphysicallyinstalledinthemachine
– Isola>onandprotec>on• Oneprocesscannotaccessmemoryaddressesinothers
• Excep>on:sharedmemory,whichwe’vealreadycovered11
MemoryManagementRequirements
• Protec>on– Restrictwhichphysicaladdressesprocessescanuse,sotheycan’tstomponeachother
• Fasttransla>on– Accessingmemorymustbefast,regardlessoftheprotec>onscheme
• Fastcontextswitching– Overheadofupda>ngmemoryhardwareonacontextswitchmustbelow
• Requireshardwaresupportforefficientimplementa>on
12
MMUandTLB
• MemoryManagementUnit(MMU)– Hardwareunitthattranslatesavirtualaddresstoaphysicaladdress
– EachmemoryreferenceispassedthroughtheMMU
– Translateavirtualaddresstoaphysicaladdress
• Transla>onLookasideBuffer(TLB)– Essen>allyacachefortheMMU’svirtual‐to‐physicaltransla>onstable
– Notneededforcorrectnessbutsourceofsignificantperformancegain
13
CPUTransla>on
TableMMU
Memory
VirtualAddress
PhysicalAddress
TLB
MemoryAlloca>on
14
• Howshouldweallocatememorytoprocesses?
PhysicalMemory
MMU
FixedPar>>ons
15
P1
P2
P3
P4
P5
PhysicalMemory
BaseRegisterP4’sBase
VirtualAddressOffset +
• Physicalmemoryisbrokenupintofixedpar>>ons– Hardwarerequirements:baseregister
– Physicaladdress=virtualaddress+baseregister
FixedPar>>ons
• Physicalmemoryisbrokenupintofixedpar>>ons– Hardwarerequirements:baseregister– Physicaladdress=virtualaddress+baseregister– BaseregisterloadedbyOSwhenitswitchestoaprocess– Sizeofeachpar>>onisthesameandfixed– Howdoweprovideprotec>on?
• Advantages– Easytoimplement,fastcontextswitch
• Problems– Internalfragmenta>on:memoryinapar>>onnotusedbya
processisnotavailabletootherprocesses– Par>>onsize:onesizedoesnotfitall(verylargeprocesses?)
16
VariablePar>>ons
• Naturalextension–physicalmemoryisbrokenupintovariablesizedpar>>ons– Hardwarerequirements:baseregisterandlimitregister– Physicaladdress=virtualaddress+baseregister– Whydoweneedthelimitregister?Protec>on
• If(physicaladdress>base+limit)thenprotec>onfault
17
VariablePar>>ons
18
P1
P2
P3
PhysicalMemoryBaseRegister
P4’sBase
VirtualAddressOffset +
LimitRegisterP3’sBase
<Yes?
No?
Protec>onFault
VariablePar>>ons
• Naturalextension–physicalmemoryisbrokenupintovariablesizedpar>>ons– Hardwarerequirements:baseregisterandlimitregister– Physicaladdress=virtualaddress+baseregister– Whydoweneedthelimitregister?Protec>ons
• If(physicaladdress>base+limit)thenexcep>onfault
• Advantages– Nointernalfragmenta>on:allocatejustenoughforprocess
• Problems– Externalfragmenta>on:jobloadingandunloadingproducesemptyholesscaperedthroughoutmemory
19
Paging
• Pagingsolvestheexternalfragmenta>onproblembyusingfixedsizedunitsinbothphysicalandvirtualmemory
20
Page1
Page2
Page3
Page4
Page5
PhysicalMemory
Page1
Page2
PageN
VirtualMemory
…..
User/ProcessPerspec>ve
• Users(andprocesses)viewmemoryasonecon>guousaddressspacefrom0toN– Virtualaddressspace
• Inreality,pagesarescaperedthroughoutphysicalstorage• Themappingisinvisibletotheprogram
• Protec>onisprovidedbecauseaprogramcannotreferencememoryoutsideofitsvirtualaddressspace– Theaddress“0x1000”mapstodifferentphysicaladdressesindifferent
processes
21
Paging
• Transla>ngaddresses– Virtualaddresshastwoparts:virtualpagenumberandoffset
– Virtualpagenumber(VPN)isanindexintoapagetable– Pagetabledeterminespageframenumber(PFN)
– PhysicaladdressisPFN::offset
22
0xBAADF00D=offsetvirtualpagenumber
0xBAADF 0x00D
Transla>onTable
pagetable
0xBAADF 0x900DFphysicalpagenumber(pageframenumber)
virtualpagenumber
virtualaddress
PageLookups
23
Page1
Page2
Page3
PageN
PhysicalMemory…..
Pageframe Offset
PhysicalAddress
Pagenumber Offset
VirtualAddress
Pageframe
PageTable
0xBAADF00D
0xBAADF 0xF00D
0xF00D0x900DF
0x900DF00D
0xFFFFFFFF
0x00000000
Paging
• Transla>ngaddresses– Virtualaddresshastwoparts:virtualpagenumberandoffset
– Virtualpagenumber(VPN)isanindexintoapagetable– Pagetabledeterminespageframenumber(PFN)
– PhysicaladdressisPFN::offset• Pagetables
– Mapvirtualpagenumber(VPN)topageframenumber(PFN)• VPNistheindexintothetablethatdeterminesPFN
– Onepagetableentry(PTE)perpageinvirtualaddressspace• Or,onePTEperVPN
24
PagingExample
• Memoryaddressis32bits
• Pagesare4K– VPNisbits(VPNs),offsetisbits
• Virtualaddressis0x7468– Virtualpageis,offsetis_______
• Pagetableentrycontains0x2– Pageframebaseis0x2<<=_______– _____thvirtualpageisaddress_______(3rdphysicalpage)
• Physicaladdress=_____+_____=______
25
PagingExample
• Memoryaddressis32bits
• Pagesare4K– VPNis20bits(1MVPNs),offsetis12bits
• Virtualaddressis0x7468– Virtualpageis0x7,offsetis_0x468__
• Pagetableentry0x7contains0x2– Pageframebaseis0x2<<12bits=__0x2000__– ___7_thvirtualpageisaddress0x2000(3rdphysicalpage)
• Physicaladdress=_0x2000_+_0x468_=_0x2468_
26
NextTime
• ReadChapter8.3‐8.8• Peerwiseques>onsduetomorrowatmidnight.
• CheckWebsiteforcourseannouncements– hpp://www.cs.ucsd.edu/classes/su09/cse120
27