0% found this document useful (0 votes)
47 views

Operating Systems Lecture - Memory Management

The document discusses memory management techniques used in operating systems. It covers topics like address translation, memory allocation, virtual memory, paging, segmentation, page replacement algorithms like FIFO, LRU and discusses how they address issues like memory protection and efficient use of limited physical memory.

Uploaded by

Nhu-Tung Doan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Operating Systems Lecture - Memory Management

The document discusses memory management techniques used in operating systems. It covers topics like address translation, memory allocation, virtual memory, paging, segmentation, page replacement algorithms like FIFO, LRU and discusses how they address issues like memory protection and efficient use of limited physical memory.

Uploaded by

Nhu-Tung Doan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Memory management

DanielHagimont(INPT)
ThankstoNolDePalma,FabienneBoyerand
ArnaudLegrand(UJF)

Introduction

Memoryisaressourcerequiredbyallprocesses

Everyprogramneedstobeloadedinmemorytobe
running

Problems

Addresstranslation

SymbolLogicaladdressphysicaladdress

Memoryallocationandexhaustion

Memorysharing

Memoryprotection

Life cycle of a single


program

Source
file
symbol

compiler

Relocatable
Object

logical address

loader

physical address
data

Logical address (e.g. offet in code)


Physical address: decided at load-time

Absolute
Objet

Machine
result

Lifecycle of a program assembled


from multiple parts

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

Simple program binary


structure
?

Compile

Text

Text

Data

Data

Symbol
table
Relocation
table

load

Symbol
Table

for dynamic libraries

Complex program binary


structure
0

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

Ideally we want to enable n > m and non contiguous allocation

Virtual memory expected


benefits

Programscanberelocatedwhilerunning

Enforceprotection

Easeswapin/swapout
Preventoneappfrommessingwithanother's
memory

Programscanseemorememorythanexists

Mostofaprocess'smemorywillbeidle

Writeidleparttodiskuntilneeded(swap)

1st idea : Base + bound


registers

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

Base + bounds 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

Virtual address translation

Problem : translation speed

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)

Problem : page table size

Flatpagetablesarehuge

Example

4GBofvirtualmemory(32bitsaddress)

4KBpages

20bitspagenumber,12bitsoffset

1MBpagetablesize:<

Multi-level page tables

Reducethesizeofpagetablesinmemory

Structuredpagetablesin2ormorelevels

Allthepagetablesarenotpresentinmemoryall
thetime
Somepagetablesarestoredondiskandfetchedif
necessary

Basedonaondemandpagingmechanism

Example: two level pages

Example: two level pages

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

Working set model

Diskmuchmuchslowerthanmemory(RAM)

Goal:runatmemory(notdisk)speed

90/10rule:10%ofmemorygets90%ofmemoryrefs

So,keepthat10%inrealmemory,theother90%ondisk

Optimal page replacement

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

LRU page replacement

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

Resources you can read

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

You might also like