Page Replacement Algorithms Page Replacement Algorithms
Page Replacement Algorithms Page Replacement Algorithms
OS 2008-09
OS 2008-09
Benchmarking
3
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
OS 2008-09
OS 2008-09
Example: optimal
5
Sequence
PF
6 page faults
OS 2008-09
Beladys anomaly
6
OS 2008-09
reference to memory
OS 2008-09
FIFO replacement
8
1
Next removal
OS 2008-09
Latest load
Example (FIFO)
9
Sequence
PF
12 page faults
OS 2008-09
2
Latest load
OS 2008-09
y Like second
second chance
chance but
y implemented differently:
{ Check starting
g from the
latest visited page
{ More efficient:
doesntt have to move
doesn
lists entries all the
time
a
h
d
f
OS 2008-09
average)
y Idea! Get a counter, maybe a 64bit counter
y Store the
h value
l off the
h counter iin each
h entry off the
h
page table (last access time to the page)
y When is time to remove a page,
page find the lowest
counter value (this is the LRU page)
y Nice & good but expensive: it requires dedicated
hardware
OS 2008-09
Example LRU
13
Sequence
PF
9 page faults
OS 2008-09
NFU algorithm
14
OS 2008-09
Process behavior
16
OS 2008-09
Working set
Working set
17
OS 2008-09
WS based algorithm
18
them)
y At
A page ffault,
l scan entries:
i
memoryy references
OS 2008-09
WSClock algorithm
19
OS 2008-09
Summary
20
Algorithm
OS 2008-09
Comment
Optimal
Very crude
FIFO
Second chance
Clock
NFU
Aging
Working set
Expensive to implement
WSClock
21
Design issues
OS 2008-09
Design issues
22
y Local vs. g
global allocation p
policyy
{ When a page fault occurs, whose page should the OS evict?
y Which process should get more or less pages?
{ Monitor the number of page faults for every process (PFF
page fault frequency)
{ For many page replacement algorithms
algorithms, the more pages the
less page faults
OS 2008-09
23
Thrashing
OS 2008-09
Load control
24
y If the WS of all p
processes > memory,
y, theres
thrashing
y E.g. the PFF says a process requires more memory
but none require less
y Solution: swapping swap a process out of memory
and
d re-assign
i it
its pages tto others
th
OS 2008-09
Page size
25
y Page
g size p, n p
pages
g of memoryy
y Average process size s, in pages s/p
y Each entry in the page table requires e bytes
y On average p/2 is lost (fragmentation)
y Internal fragmentation: how much memory is not
Two memories
26
y Separate
p
data and p
program
g
address spaces
p
y Two independent spaces, two paging systems
y The linker must know about the two address spaces
p
OS 2008-09
Other issues
27
y Shared p
pages,
g , handle shared p
pages
g ((e.g.
g p
program
g
code)
{
y Cleaning policy
{ Paging algorithms work better if there are a lot of free
pages available
il bl
{ Pages need to be swapped out to disk
{ Paging daemon (write pages to disk during spare time
and evict pages if there are to few)
OS 2008-09
OS 2008-09
29
Segmentation
OS 2008-09
Why?
30
code,
d and
d many others
h
if needed)
d d)
Each segment is separate (e.g. addresses from 0 to some
MAX))
Segments might have different lengths
Segment number + address within segment
Li ki is
Linking
i simplified
i lifi d (libraries
(lib i within
ithi diff
differentt segments
t
can assume addresses starting from 0) e.g. if a part of the
libraries is recompiled the remainder of the code is
unaffected
ff t d
Shared library (DLLs) implementation is simpler (the
g is simpler)
p
sharing
OS 2008-09
Paging
Segmentation
No
Yes
Many
Yes
Yes
No
Yes
No
Yes
IIs sharing
h i off procedures
d
between
b t
users facilitated?
N
No
Y
Yes
OS 2008-09
Pure segmentations
32
Operating system
Operating system
Operating system
OS 2008-09
Operating system
Fragmentation
33
y External fragmentation:
{ Memory fragments not used (weve already seen this)
{ Memory wasted in unused holes
OS 2008-09
y 16K segments
g
y 1G 32bit words (DoubleWords)
y Two tables: LDT, GDT Local (to the process) and
OS 2008-09
Base 24-31
Limit 16-19
Base 0-15
Page size is 4K
Privilege level
DPL
Type
Base 16-23
Limit 0-15
Segment type
protection
8 bytes
System/application
Segment present in memory
Limit (20 bits)
CS/DS
I d
Index
G/L
Selector
OS 2008-09
P i il
Privilege
Selector
Offset
Base address
Limit
Oth fi
Other
fields
ld
Descriptor
32 bit linear address
32-bit
OS 2008-09
y 2-level p
page
g table in memoryy
10 bits
Dir
Page
Offset
3
2
1
0
1023
1023
Dir
12 bits
10 bits
Page
2
1
0
1023
5
4
3
2
1
0
1023
5
4
3
2
1
0
1023
5
4
3
2
1
0
1023
5
4
Offset
3
2
1
0
OS 2008-09