Unix memory management

35,887 views 35 slides Sep 03, 2012
Slide 1
Slide 1 of 35
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35

About This Presentation

No description available for this slideshow.


Slide Content

UNIX MEMORY
MANAGEMENT
1

AGENDA
Introduction
History of UNIX
Swapping
Demand Paging
Page Replacement Algorithm
Kernel Memory Allocator
Conclusion
2

Introduction
UNIXisaportable,multi-taskingandmulti-useroperating
system.
–Portable:runsonmanydifferenthardwarearchitectures(Intel
x86andIA-64,Alpha,MIPS,HPPA-RISC,PowerPC,IBM
S/390,SPARC,Motorola680x0,etc.).
–Preemptivemulti-tasking:severalprogramscanrunatthe
sametime(timeslices,interrupts,andtaskswitching).
–Multi-user:manyuserscansharethecomputersystematthe
sametime.
3

Other Features…
•Usesasimple,uniformfilemodelwhichincludesdevicesand
accesstootherservicesinaflexible,hierarchicalfilesystem.
•Writteninahigh-levellanguage(“C”)makingiteasyto
read,understand,changeandport.
•Thecommandpromptisasimpleuserprocess,theUnixshell,
whichisalsoaconvenientjobprogramminglanguage.
•Includessupportforregularexpressionswhichareconvenient
forcomplexsearching.
4

History of UNIX
•1964jointprojectbetweenAT&TBellLabs,GE,andMITto
developanewOS.
•Goal:developanOSthatcouldprovidecomputational
power,datastorageandtheabilitytosharedataamong
multipleusers.
•Result:MultiplexedInformation&ComputerService-
MULTICS.
5

•1969BellLabswithdrawsfromgroup.
•TwoBellLabscientists,KenThompsonandDennisRitchie,
continueresearch.Theywerestillleftwithouta“Convenient
interactivecomputingservice”*.
•AtthesametimeKenThompsonwroteagame“space
travel”inFortrantorunonGECOSOS.
•Thespaceshipwashardtocontrolanditwasexpensiveto
run.Hewastoldtogetthegameoffhisworkcomputer.
6

•ThompsonportedthegametoalittleusedPDP-7computer.
•Unics(laterUnix)wasbornasapunonMultics.
•DennisRitchiedeveloped“B”.Thenwrote“C”acompiled
language.
•In1973entireOSportedto“C”.
•1991Linux0.02isfirstreleasedtothepublic.
•1994Linux1.0isreleased.
7

Memory Management
UNIXismachineindependentsoitsmemorymanagement
schemewillvaryfromonesystemtonext.
EarlyversionsofUNIXusedvariablepartitioningwithno
virtualmemoryscheme.
CurrentimplementationsofUNIXmakeuseofpagedvirtual
memory.
Therearetwomemorymanagementschemes:
PagingSystem
KernalMemoryAllocator
8

Virtual Memory
9

Memory Management policies
•Swapping
–Easy to implement
–Less system overhead
•Demand Paging
–Greater Flexibility
10

Swapping
•Theprocessofmovingsomepagesoutofmainmemoryand
movingothersin,iscalledswapping.
•ApagefaultoccurswhentheCPUtriestoaccessapagethat
isnotinmainmemory,thusforcingtheCPUtowaitforthe
pagetobeswappedin.
•Sincemovingdatatoandfromdiskstakesasignificant
amountoftime,thegoalofthememorymanageristo
minimizethenumberofpagefaults.
11

•SwapSpace-Diskmemoryusedtoholddatathatisnotin
RealorFileSystemmemory.Swapspaceismostefficient
whenitisonaseparatediskorpartition,butsometimesitis
justalargefileintheFileSystem.
•Allocationofbothmainmemoryandswapspaceisdone
first-fit.
•ApagefaultoccurswhentheCPUtriestoaccessapage
thatisnotinmainmemory,thusforcingtheCPUtowaitfor
thepagetobeswappedin.
12

•Sincemovingdatatoandfromdiskstakesasignificant
amountoftime,thegoalofthememorymanageristo
minimizethenumberofpagefaults.
•Whenthesizeofaprocess'memoryimageincreases(dueto
eitherstackexpansionordataexpansion),anewpieceof
memorybigenoughforthewholeimageisallocated.
•Ifnosinglepieceofmainmemoryislargeenough,the
processisswappedoutsuchthatitwillbeswappedbackin
withthenewsize.
13

•Decisionsregardingwhichprocessestoswapinorswapout
aremadebytheschedulerprocess(alsoknownasthe
swapper).
•Aprocessismorelikelytobeswappedoutifitisidleorhas
beeninmainmemoryforalongtime,orislarge;ifno
obviouscandidatesarefound,otherprocessesarepickedby
age.
•Aprocessismorelikelytobeswappedinifitshasbeen
swappedoutalongtime,orissmall.
14

15

Allocating Swap Space
16
1 10000 101 9900
151 9750251 9850
Allocate 100 unit
Allocate 100 unit
Allocate 50 unit
Map
AddressUnit

Freeing Swap Space
17
251 9750
50 unit free at 101
Map
Address Unit
101 50
251 9750
Case 1: Free resources fill a hole,
but not contiguous to any resources in the map

Freeing Swap Space
18
251 9750
50 unit free at 101
Map
Address Unit
101 50
251 9750
100 unit free at 1
1 150
251 9750
Case 2: Free resources fill a hole,
and immediately precedes an entry in the map

19
Freeing Swap Space
251 9750
50 unit free at 101
Allocate 200 unit
Map
AddressUnit
101 50
251 9750
100 unit free at 1
1 150
251 9750
1 150
451 9550
1 10000
300 unit free at 151
Case 3: Free resources fill a
hole, and completely fills the
gap between entries in the
map

Demand Paging
•DemandpagingtounixwithBSD(Berkleysystem)which
transferredmemorypagesinsteadofprocesstoandfroma
secondarydevice.
•Whenaprocessneedsapageandthepageisnotthere,a
pagefaulttothekerneloccursaframeofmainmemoryis
allocated,andtheprocessisloadedintotheframebythe
kernel.
20

PageTable
•frame#containsthephysicalframewherethevirtualpage
isstored
•ageisprocessordependant,andismeanttomaintainhow
longithasbeensincethepagewasaccessed.
•CopyonWritestorethecopyonwritebit,whichisusedin
UNIXsystemsto,amongotherthings,renderforkefficient.
PageFrame
Number
AgeCopy on
write
ModifyReferenceValidProtect
21

•Dirtyisasinglebitthatindicateswhetherapagehasbeen
modifiedsincelastswappedin(theoppositeofdirtyisclean,
andacleanpageneednotbewrittenouttodiskif
swapped).
•RefcontainstheusageinformationnecessaryforaCLOCK-
stylealgorithm.
•ValidisthestandardUNIXjargonforresident.Avalidpage
isinmainmemory,aninvalidoneisswappedout.
•Protectcontainsthepermissioninformationforthepage
andisalsohardwaredependant.
22

Diskblockdescriptor
•Thediskblockdescriptorcontainstheinformationmapping
avirtualpagetoaspotondisk.
•TheOSmaintainsatableofdescriptorsforeachprocess.
23
Swap device NumberDevice Block No. Typeof storage

•Device#isbasicallyapointertothediskthatthispagewas
swappedto.
•Block#istheactualblockthatthepageisstoredon.Thisis
whymostUNIXsystemsprefertohaveaseparateswap
partition,sothattheblocksizecanbesettothepagesize.
•Typespecifieswhetherthepageisneworpre-existing.This
letstheOSknowifithastocleartheframefirst.
24

Page frame data table
•The page frame data table holds information about each
physical frame of memory (indexed by frame number).
•This table is of primary importance for the replacement
algorithm.
Page State Reference
Count
Logical
device
Block
number
Pf data
pointer
25

•Page state indicates whether or not the frame is available or
has an associated page (i.e. whether its been allocated to a
process or not).
•Ref. Count holds the number of processes that refer to this
page (remember, processes can share the same physical
page).
•Logical device contains the device number of the disk that
holds a physical copy of the page.
26

•Block # holds the block number on that disk where the
page data is located.
•Pfdata pointeris a pointer field that is used to thread a
singly-linked list of free frames through the frame table. If
the page is free, this points to the next free page (useful for
free list-style allocation).
27

Swap use table
•Reference Count: Number of page table entries that point
to a page on the swap device.
•Page/storage unit number : Page identifier on storage unit
28
Reference
count
Page/storage
unit number

Page Replacement Algorithm
•The Page frame data table is used for page replacement.
•All of the available frames are linked together in a list of free
frames available for bringing in pages.
•The two-handed clock algorithm uses the reference bit in the
page table entry for each page in memory that is eligible
(not locked) to be swapped out.
•This bit is set to 0 when when the page is first brought in and
set to 1 when the page is refernced for a read or write .
29

Two-Handed clock Page Replacement Algorithm
30

•Twoparametersdeterminetheoperationofthealgorithm:
–Scanrate:Therateatwhichthetwohandsscanthroughthe
pagelist,inpagespersecond.
–Handspread:Thegapbetweenfronthandandbackhand
•Thesetwoparametershavedefaultvaluessetatboottime
basedontheamountofphysicalmemory.
31

•The parameter varies linearly between the values slowscan
and fastscan as the amount of free memory varies between
the values lotsfree and minfree.
•The handspread parameter determines the gap between the
fronthand and the backhand and therefore, together with
scanrate,determines the window of oppurtunity to use a
page before it is swapped out due to lack of use.
32

Kernal Memory Allocator
•Kernelmemoryallocator:providesbuffersofmemoryto
variouskernelsubsytems.
EvolutionCriteria:
–mustbespace-efficienti.e.minimizewastage.
–Canbemeasuredbyutilizationfactor
–mustbefast
–musthaveasimpleprogramminginterface
–shouldnotforcetofreetheentireallocatedareaallatonce
–mustguardagainstthewastageofmemory
–mustbeabletointeractwiththepagingsystem
33

34
Advantages
•easy to implement
•not restricted to memory allocation
•no wastage of space
•can release any part of the region
•allows reuse of memory by coalescing

References
Books :
*********
1. Os Internals & design
---William Stallings
2.The design of the unix operating system
---Maurice J. Bach prentice hall
35