Operating Systems Lecture - Memory Management
Operating Systems Lecture - Memory Management
DanielHagimont(INPT)
ThankstoNolDePalma,FabienneBoyerand
ArnaudLegrand(UJF)
Introduction
Memoryisaressourcerequiredbyallprocesses
Everyprogramneedstobeloadedinmemorytobe
running
Problems
Addresstranslation
SymbolLogicaladdressphysicaladdress
Memoryallocationandexhaustion
Memorysharing
Memoryprotection
Source
file
symbol
compiler
Relocatable
Object
logical address
loader
physical address
data
Absolute
Objet
Machine
result
Source
Source
Source
compiler
compiler
Relocatable
Object
Relocatable
Object
compiler
logical address
Object
relogeable
Library
Linker
logical address
Relocatable
Object
Physical Address
loader
Absolute
Object
Load-time translation
Translationbetweenlogicalandphysicaladresses
Determinewhereprocesswillresideinmemory
Translateallreferenceswithinprogram
Establishedonceforall
Monoprogramming
Oneprograminmemory
Easy(couldevenbedonebeforeloadtime)
Multiprogramming
Nprogramsinmemory
Compilerandlinkerdonotknowtheimplantationofprocessesinmemory
Needtotrackopcodesthatmustbeupdatedatloadtime
Compile
Text
Text
Data
Data
Symbol
table
Relocation
table
load
Symbol
Table
Text1
Data1
Compile
prg1
ST1
RT1
Compile
prg2
Text2
Text1
Text2
Data
Data1
linker
Text
Data2
ST1
Data2
ST2
ST2
RT1
RT2
RT2
loader
Symbol
Table
Load-time translation
summary
Remainingproblems
Howtoenforceprotection?
Howtomoveprogramonceinmemory?
Whatifnocontiguousfreeregionfitsprogramsize?
Canweseparatelinkingfrommemorymanagement
problems?
Virtual memory
Separatelinkingproblemfrommemory
management
Giveeachprogramitsownvirtualaddressspace
Linkerworksonvirtualaddresses
Virtualaddresstranslationdoneatruntime
Relocateeachload/storetoitsphysicaladdress
Requirespecifichardware(MMU)
Virtual memory
P2
P1
P0
0
Addresses
translation
2 n-1
Virtual memory
2 m -1
Physical memory
Programscanberelocatedwhilerunning
Enforceprotection
Easeswapin/swapout
Preventoneappfrommessingwithanother's
memory
Programscanseemorememorythanexists
Mostofaprocess'smemorywillbeidle
Writeidleparttodiskuntilneeded(swap)
Contiguousallocationofvariablesize
Twospecialprivilegedregisters:baseandbound
Oneachload/store:
Check0<=virtualaddress<bound,elsetraptokernel
Physicaladdress=virtualaddress(plus)base
yes
<
no
Trap to kernel
app2
virtual
address
base
register
app1
bound
register
Movingaprocessinmemory
Contextswitch
Changebaseregister
OSmustreloadbaseandboundregister
Advantages
Cheapintermsofhardware:onlytworegisters
Cheapintermsofcycles:doaddandcompareinparallel
Disadvantages
Stillcontiguousallocation
Growingaprocessisexpensiveorimpossible
Hardtosharecodeordata
Segmentation
Noncontiguousallocation
Splitaprogramindifferentnoncontiguous
segmentsofvariablesize
Letprocesseshavemanybase/boundregisters
Addressspacebuiltfrommanysegments
Canshare/protectmemoryatsegmentgranularity
Mustspecifysegmentaspartofvirtualaddress
Segmentation
Segmentation mechanism
Eachprocesshasasegmenttable
Eachvirtualaddressindicatesasegmentandoffset:
Topbitsofaddrselectsegment,lowbitsselectoffset
Segmentation example
4bitsegmentnumber(1stdigit),12bitoffset(last3)
Whereis0x0240?0x1108?0x265c?0x3002?0x1600?
Segmentation tradeoffs
Advantages
Multiplesegmentsperprocess
Allowssharing
Disadvantages
NbytesegmentneedsNcontiguousbytesof
physicalmemory
Fragmentation
Remember fragmentation
problem
Fragmentation=>inabilitytousefreememory
Overtime:
Variablesizepieces=manysmallholes(external
fragmentation)
Paging
Virtualmemoryisdividedintosmallpages
Pagesarefixedsize
Apageiscontiguous
Mapvirtualpagestophysicalblock
Noncontiguousallocationofblocks
Eachprocesshasaseparatemappingbutcansharethe
samephysicalblock
MMU
OSgainscontroloncertainoperations
NonallocatedpagestraptoOSonaccess
ReadonlypagestraptoOSonwrite
OScanchangethemapping
Paging
Pagetable
Globalorperprocess
Requireextramemoryreferencesoneachload/store
Cacherecentlyusedtranslations
Localityprinciple
Highprobabilitythatthenextrequiredaddressisclose
TranslationLookasideBuffer(TLB)
Fast(small)associativememorywhichcanperforma
parallelsearch
TypicalTLB
Hittime:1clockcycle
Missrate1%
TLBmanagement:hardwareorsoftware
TLB
Whattodowhenswitchaddressspace?
FlushtheTLB
Tageachentrywiththeprocess'sid
UpdateTLBonpagefault(add/removeTLBentries)
Flatpagetablesarehuge
Example
4GBofvirtualmemory(32bitsaddress)
4KBpages
20bitspagenumber,12bitsoffset
1MBpagetablesize:<
Reducethesizeofpagetablesinmemory
Structuredpagetablesin2ormorelevels
Allthepagetablesarenotpresentinmemoryall
thetime
Somepagetablesarestoredondiskandfetchedif
necessary
Basedonaondemandpagingmechanism
On demand paging
Virtualmemory>physicalmemory
Somepagesarenotpresentinmemory(X)
Storedondisk
Physical
memory
Virtual
memory
0-4K
0-4K
4-8K
8-12K
12-16K
16-20K
20-24K
4-8K
8-12K
Page fault
Accesstoanabsentpage
Presencebit
Pagefault(TraptoOS)
Pagefaultmanagement
Findafreephysicalframe
Ifthereisafreeframe;useit
Else,selectapagetoreplace(tofreeaframe)
Savethereplacedpageondiskifnecessary(dirtypage)
Loadthepagefromdiskinthephysicalframe
Updatepagetable
Restartinstruction
Requireapresencebit,adirtybit,adisk@inthepagetable
Differentpagereplacementalgorithms
On demand paging
Page replacement
algorithms
Workingsetmodel
Algorithms
Optimal
FIFO
Secondchance
LRU
Diskmuchmuchslowerthanmemory(RAM)
Goal:runatmemory(notdisk)speed
90/10rule:10%ofmemorygets90%ofmemoryrefs
So,keepthat10%inrealmemory,theother90%ondisk
Whatisoptimal(ifyouknewthefuture)?
Replacepagesthatwillnotbeusedforlongest
periodoftime
Example
Referencestring:0,1,2,3,0,1,4,0,1,2,3,4,1,2
4physicalsframes:
6 pages faults
FiFo
Evictoldestpageinsystem
Example
Referencestring:0,1,2,3,0,1,4,0,1,2,3,4,1,2
4physicalsframes:
10 page faults
Implementation:justalist
Approximateoptimalwithleastrecentlyused
Becausepastoftenpredictsthefuture
Example
Referencestring:0,1,2,3,0,1,4,0,1,2,3,4,1,2
4physicalsframes:
0
8 page faults
LRU implementation
Expensive
Needspecifichardware
ApproximateLRU
Theagingalgorithm
Addacounterforeachpage(thedate)
Onapageaccess,allpagecountersareshiftedright,
inject1fortheaccessedpage,else0
Onapagefault,removethepagewiththelowest
counter
Aging : example
Accessed Date
page
Page0
Date
Page1
Date
Page2
000
000
000
Page 0
100
000
000
P0,P1=P2
Page 1
010
100
000
P1,P0,P2
Page 2
001
010
100
P2,P1,P0
Page 1
000
101
010
P1,P2,P0
P0 is the oldest
Order
pages /date
Second chance
SimpleFIFOmodification
UseanaccessbitRforeachpage
Setto1whenpageisreferenced
Periodicallyresetbyhardware
InspecttheRbitoftheoldestpage(oftheFIFOlist)
If0:replacethepage
If1:clearthebit,putthepageattheendofthelist,
andrepeat
AppromixationofLRU
Page buffering
Navepaging
Pagereplacement:2diskIOperpagefault
ReducetheIOonthecriticalpath
Keepapooloffreeframes
Fetchthepageinanalreadyfreepage
Swapoutapageinbackgound
Paging
Separatelinkingfrommemoryconcern
Simplifiesallocation,freeandswap
Eliminateexternalfragmentation
Mayleverageinternalfragmentation
http://en.wikipedia.org/wiki/Page_table
Wikipediacanalwaysbeuseful
OperatingSystemConcepts,8thEdition,Abraham
Silberschatz,PeterB.Galvin,GregGagne
http://osbook.com/
Chapters7&8
ModernOperatingSystems,AndrewTanenbaum
http://www.cs.vu.nl/~ast/books/mos2/
Chapter4