0% found this document useful (0 votes)
54 views38 pages

Page Replacement Algorithms Page Replacement Algorithms

Page replacement algorithms aim to efficiently manage the limited memory resources of a computer system. When a page fault occurs and a page needs to be brought into memory, an algorithm is used to select a page to evict from memory. Many algorithms have been developed that aim to minimize the number of page faults by selecting pages that will not be used again for the longest period of time. These include optimal, NRU, FIFO, second chance, clock, LRU, NFU, aging, and working set algorithms. The choice of page replacement algorithm involves balancing factors like implementation cost, page fault rate, and fairness between processes.

Uploaded by

bebo_ha2008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views38 pages

Page Replacement Algorithms Page Replacement Algorithms

Page replacement algorithms aim to efficiently manage the limited memory resources of a computer system. When a page fault occurs and a page needs to be brought into memory, an algorithm is used to select a page to evict from memory. Many algorithms have been developed that aim to minimize the number of page faults by selecting pages that will not be used again for the longest period of time. These include optimal, NRU, FIFO, second chance, clock, LRU, NFU, aging, and working set algorithms. The choice of page replacement algorithm involves balancing factors like implementation cost, page fault rate, and fairness between processes.

Uploaded by

bebo_ha2008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Page replacement algorithms

OS 2008-09

When a page fault occurs


2

y OS has to choose a page to evict from memory


y If the page has been modified, the OS has to schedule

a disk write of the page


y The
Th page jjustt read
d overwrites
it a page iin memory ((e.g.
4Kbytes)
y Clearly,
Clearly its
it s better not to pick a page at random
y Same problem applies to memory caches

OS 2008-09

Benchmarking
3

y Tests are done by


y generating
g
gp
page
g references ((either

from real code or random)


y Sequences of page numbers (no real address, no
offset)
y Example:

7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
OS 2008-09

Optimal page replacement


4

y At the moment of page fault:


{ Label each page in memory is labeled with the number of
instructions that will be executed before that page is first
referenced
{ Replace the page with the highest number: i.e. postpone as
much as possible the next page fault
y Nice
Nice, optimal,
optimal but unrealizable
{ The OS cant look into the future to know how long itll take to
reference every page again

OS 2008-09

Example: optimal
5

Sequence

PF

6 page faults

OS 2008-09

Beladys anomaly
6

Try this sequence

With 3 page frames


With 4 page frames
With FIFO, with the optimal algorithm, (later) with the LRU

OS 2008-09

Not recently used algorithm


7

y Use Referenced and Modified bits


y R&M are in hardware, potentially changed at each

reference to memory

R&M are zero when process is started

y On clock interrupt the R bit is cleared


y On page fault, to decide which page to evict:
{ Classify:
Class 0 R=0,M=0
Class 1 R=0,M=1
Class 2 R=1,M=0
Class 3 R=1,M=1

Replace a page at random from the lowest class

OS 2008-09

FIFO replacement
8

y FIFO,, first in first out for pages


p g
y Clearly not particularly optimal
y It might
g end up
p removing
gap
page
g that is still

referenced since it only looks at the pages age


y Rarely used in pure form

1
Next removal
OS 2008-09

Latest load

Example (FIFO)
9

Sequence

PF

12 page faults

OS 2008-09

Second chance algorithm


10

y Like FIFO but


y Before throwing out a page checks the R bit:
{ If 0 remove it
{ If 1 clear it and move the page to the end of the list (as it were
just been loaded)
{ If all pages have R
R=11, eventually the algorithm degenerates to
FIFO (why?)

2
Latest load

OS 2008-09

Clock page algorithm


11

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

Least recently used (LRU)


12

y Pages recently used tend to be used again soon (on

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

y Since LRU is expensive


p
y NFU: Not Frequently Used algorithm
y At each clock interrupt
p add the R bit to a counter for

each page: i.e. count how often a page is referenced


y Remove page with lowest counter value
y Unfortunately, this version tends not to forget
anything

OS 2008-09

Aging (NFU + forgetting)


15

y Take NFU but


y At each clock interrupt:
{ Right
g shift the counters (divide byy 2)
{ Add the R bit to the left (MSB)
y As for NFU remove pages with lowest counter
y Note: this is different from LRU since the time

granularity is a clock tick and not every memory


reference!
OS 2008-09

Process behavior
16

y Locality of reference: most of the time the last k

references are within a finite set of pages < a large


address space
y The set of pages a process is currently using is called
the working set of the process
y Knowing
g the working
g set of p
processes we can do veryy
sophisticate things (e.g. pre-paging)

OS 2008-09

Working set
Working set

17

k-most-recent memory references

OS 2008-09

WS based algorithm
18

y Store time information in the table entries


y At clock interrupt handle R bits as usual (clear

them)
y At
A page ffault,
l scan entries:
i

If R=1 just store current time in the entry


{ If R=0 compute current-last
current-last time page was
referenced and if > threshold the page can be removed
since its no longer in the working set (not used for
threshold time)
{

y Note: were using time rather than actual

memoryy references

OS 2008-09

WSClock algorithm
19

y Use the circular structure (as seen earlier)


y R=1, page in the WS dont remove it
y R=0, M=0 no problem (as before)
y M=1, schedule disk write appropriately to

procrastinate as long as possible a process switch


{

No write is schedulable (R=1 always),


always) just choose a clean page

OS 2008-09

Summary
20

Algorithm

OS 2008-09

Comment

Optimal

Not implementable, useful for


benchmarking

NRU (Not recently used)

Very crude

FIFO

Might throw out important pages

Second chance

Big improvement over FIFO

Clock

Realistic (better implementation)

LRU (Least Recently Used)

Excellent but difficult to


implement

NFU

Crude approx to LRU

Aging

Efficient in approximating LRU

Working set

Expensive to implement

WSClock

Good and efficient

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

Page fault behavior


Pagee faults/secc

23

Thrashing

Optimal (fair to others)

Too many pages

Number of page frames assigned

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

used within pages


y Wasted memory: p/2 + se/p
y Minimizing it yields the optimal page size (under
simplifying
p y g assumptions)
p
)
OS 2008-09

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)
{

Sharing data (e.g. shared memory)

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

Page fault handling


28
1. Page fault, the HW traps to the kernel
1
1.
Perhaps registers are saved (e.g.
(e g stack)
2. Save general purpose microprocessor information (registers, PC, PSW, etc.)
3. The OS looks for which page caused the fault (sometimes this information is
4
4.
5.
6
6.
7.
8.
8
9.

already somewhere within the MMU)


The system checks whether the process has access to the page (otherwise a
protection fault is generated, and the process killed)
The OS looks for a free page frame, if none is found then the replacement
algorithm is run
If the selected page is dirty (M=1)
(M 1) a disk write is scheduled (suspending the
calling process)
When the page frame is clean, the OS schedules another transfer to read in the
required page from disk
When the load is completed,
completed the page table is updated consequently
The faulting instruction is backed up, the situation before the fault is restored,
the process resumes execution

OS 2008-09

29

Segmentation

OS 2008-09

Why?
30

y Many separate address spaces (segments) (e.g. data, stack,


y
y
y
y

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

Comparing paging and segmentation


31
Consideration

Paging

Segmentation

Need the programmer be aware that


this technique is being used?

No

Yes

How many linear address spaces are


there?

Many

Can the total address space exceed


the size of physical memory

Yes

Yes

Can procedures and data be


distinguished and separately
protected?

No

Yes

Can tables whose size fluctuate be


accommodated easily?

No

Yes

IIs sharing
h i off procedures
d
between
b t
users facilitated?

N
No

Y
Yes

Why was this technique invented?

To get a large linear address space


without having to buy more physical
y
memory

To allow programs and data to be


broken up into logically independent
p
and to aid sharing
g
address spaces
and protection

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

Segmentation + paging (Pentium)


34

y 16K segments
g
y 1G 32bit words (DoubleWords)
y Two tables: LDT, GDT Local (to the process) and

global (to the processor) descriptor table


y To work with a segment
g
the machine loads the
segment number into a special register (CS, DS,
etc.) CS, DS are 16 bit registers
y The descriptor of the segment (see next slide)

OS 2008-09

The segment descriptor


35

y This is used by the microcode within the Pentium

to work with segments


Limit in pages/bytes
16/32 bit segment

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

Getting the address


36

Selector

Offset

Base address
Limit

Oth fi
Other
fields
ld

Descriptor
32 bit linear address
32-bit

OS 2008-09

Paging on the Pentium


37

y 2-level p
page
g table in memoryy
10 bits
Dir

Page

Offset

3
2
1
0

Address to the page

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

Each points to 4Mbytes of pages


OS 2008-09

More on the Pentiums


38

y TLB,, to avoid repeated


p
accesses to memoryy
y The whole thing can be used with just a single

segment to obtain a linear 32bit address space


y Set base and limit appropriately
y Protection (a few bits)

OS 2008-09

You might also like