Download - Computer Science 37 Lecture 25
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 1/23
1
Lecture25
VirtualMemory
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 2/23
2
Case1: Myprogramis
largerthanphysical
memory.Nowwhat?
Physicalmemory
WhygoVirtual ?Becausesometimesreal isnotgoodenough.
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 3/23
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 4/23
4
Case1: Myprogramis
largerthanphysical
memory.Nowwhat?
Physicalmemory
OVERLAY#1
Fact:
Theuserprogramwill
not needtheentirephysical
memoryallatonce.
Solution: overlays.
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 5/23
5
Case1: Myprogramis
largerthanphysical
memory.Nowwhat?
Physicalmemory
OVERLAY#2
Fact:
Theuserprogramwill
not needtheentirephysical
memoryallatonce.
Problem: theburdenisallontheprogrammer(loading,unloading,
partitioning,etc).
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 6/23
6
1) Whenamemoryitemis
referencedonce,it’slikelyto
bereferencedagaininthenear
future.
2)WhenIaccessamemory
location,itislikelythatIwill
accessalsootherlocationsin
thevicinityofthefirst.
TemporalLocality
SpatialLocality
ThePrincipleofLocality
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 7/23
7
Case2: Severalprogramsare
runningconcurrently.
Physicalmemory
XWindow
Acommandshell
(csh,bash,tcsh,etc)
xemacs
xspim
XWindowAwindowmanager
(twm,fvwm,sawfish,etc)Windowmanager
shell
emacs
xspim
OS(Unixkernel)
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 8/23
8
Case2: Severalprogramsare
runningconcurrently.
Physicalmemory
XWindow
Acommandshell
(csh,bash,tcsh,etc)
xemacs
xspim
XWindowAwindowmanager
(twm,fvwm,sawfish,etc)Windowmanager
shell
emacs
xspim
OS(Unixkernel)
Problem: IhavelessphysicalmemorythanIneed.
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 9/23
9
Case2: Severalprogramsare
runningconcurrently.
Physicalmemory
XWindow
Acommandshell
(csh,bash,tcsh,etc)
xemacs
xspim
XWindowAwindowmanager
(twm,fvwm,sawfish,etc)Windowmanager
shell
xemacs
xspim
OS(Unixkernel)
Fact: Idon’tneedallofeverythingatthesametime.
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 10/23
10
1) Whenamemoryitemis
referencedonce,it’slikelyto
bereferencedagaininthenear
future.
2)WhenIaccessamemory
location,itislikelythatIwill
accessalsootherlocationsin
thevicinityofthefirst.
TemporalLocality
SpatialLocality
ThePrincipleofLocality
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 11/23
11
Themainideaistodesignaschemethat:
1) Supportsalargeraddressspacethanthephysical,
2) Achievesthisbyswappingdatainandoutofdisk,
3) Giveseachprogramtheillusionofowningallmemory,
4) Istransparenttotheprogrammer,
5) Ontopofitall,isefficient !
TheBasicsofVirtualMemory
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 12/23
12
TheBasicsofVirtualMemory
$0.10-$0.2010-20msMag.Disk
$5-$1060-120nsDRAM
$100-$2505-25nsSRAM
CostperMbyteAccessTimeTechnology
Rememberthecostsofdiskusageintermsof accesstimeandmonetaryunits:
Diskspaceischeap,but diskaccessisnot!
Sidecomment: Diskaccesscosthasthreecomponents:thetimetomovethediskheadtotherightcylinder,the
timetoreachthesectorinthecylinderandtransfertime.
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 13/23
13
TheBasicsofVirtualMemory
Physicalmemory
Dividephysicalmemory
intoequallysizedportions
calledpages.
Pagesmayresideinphysicalmemorywhentheircontents
arebeingactivelyusedorin
diskwhentheircontentshave
notbeenusedforawhile.
pagei
DISK
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 14/23
14
Physicalmemory
Aprogrammayusealargenumber N ofpages,evenif(N*page
size)isgreaterthanthephysicalmemory.
Thepagesthataprogramusesneednotbecontiguousnorinorderinphysical
memory.
xemacs 3
xemacs 0
xemacs 1
xemacs 2Question: Whatdoweneedtocreate
thisillusionfortheprogrammer?
Theprogrammerdoesn’tneedto
explicitlymovepagestoandfrom
disk.
Theprogrammerseestheaddressspace
asifitwereinamemorymuchlargerthan
thephysicalmemory.
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 15/23
15
Physicalmemory
Weneedamappingmechanism
xemacs 3
xemacs 0
xemacs 1
xemacs 2
Virtualmemory
xemacs 0
xemacs 1
xemacs 2
xemacs 3Address
Translation:
from
virtualaddresstophysicaladdress
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 16/23
16
Virtualmemory
xemacs 0
xemacs 1
xemacs 2
xemacs 3
Pointstonotice:
1) Virtualmemorydefinesavirtual
addressspace.
2) Virtualaddressspaceismadeof
pagesofthesamesizeasthephysical
addressspace.
3) Thepagesinvirtualaddress
spacearenicelycontiguousand
ordered.
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 17/23
17
AddressTranslationforVirtualMemory
3 2 1 011 10 9 815 14 13 1231 30 29 28 27
Page offset Virtual page number
Virtual address
3 2 1 011 10 9 815 14 13 1229 28 27
Page offsetPhysical page number
Physical address
Translation
Virtualaddress
Example: Pagesize=4KB=2^12Bytes
Physicalmemorysize=2^18pages=2^30Bytes=1GB
Virtualmemorysize=2^32Bytes=4GB
Question: Whataboutthismappingfromalargertoasmallerspace?
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 18/23
18
Address (showing bit positions)
20 10
Byteoffset
Valid Tag DataIndex
0
1
2
1021
1022
1023
Tag
Index
Hit Data
20 32
31 30 13 12 11 2 1 0
Address(showingbitpositions)
Remembercachemapping?
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 19/23
19
ImportantConsiderations
Cachemiss
PageFault:howdothepenaltiescompare?
Pagesshouldbelargetoamortizeaccesstime,butnot
toolarge.Why?)
Whataboutwriting?Shouldwethinkwritethrough or
writeback ?
Shouldweusea fullyassociative oradirectmapping
scheme?Why?
Shouldweleavepagefaulthandlingtosoftwareorshould
wemakeitahardwaretask?
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 20/23
20
Page offset Virtual page number
Virtual address
Page offsetPhysical page number
Physical address
Physical page number Valid
If 0 then page is notpresent in memory
Page table register
Page table
20 12
18
31 30 29 28 27 15 14 13 12 11 10 9 8 3 2 1 0
29 28 27 15 14 13 12 11 10 9 8 3 2 1 0
MappingMechanismforVirtualMemory
Pagetable
Weusefullyassociativemapping.
(eachprogramhasitsown)
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 21/23
21
Page offset Virtual page number
Virtual address
Page offsetPhysical page number
Physical address
Physical page number Valid
If 0 then page is notpresent in memory
Page table register
Page table
20 12
18
31 30 29 28 27 15 14 13 12 11 10 9 8 3 2 1 0
29 28 27 15 14 13 12 11 10 9 8 3 2 1 0
MappingMechanismforVirtualMemory
Pagetable
Question: Howmanyentriesinthepagetable?
(eachprogramhasitsown)
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 22/23
22
Interlude:Program,ProcessandContext
EachprogramusestheCPUregistersasitwants.
Eachprogramhasitsownpagetable,itsownvaluefor
thepagetableregister..
Ifthere’smorethanoneprogramexecutingunderthesame
CPU, theysharetheCPU .
Sharingmeansthatalltherepresentsoneprocessisswapped
inandoutoftheCPU,dependingonwhethertheprocessis
activeorinactive.
Theoperatingsystem handlesCPUsharingand,thus,
alsoprocesscontextmanagement.
8/4/2019 Computer Science 37 Lecture 25
http://slidepdf.com/reader/full/computer-science-37-lecture-25 23/23
TheVirtualMemorySystem
Physical memory
Disk storage
Valid
1
1
1
1
0
1
1
0
1
1
0
1
Page table
Virtual pagenumber
Physical page ordisk address
Virtualpagenumber
Pagefaultsareexceptionsbydefinition.