SlideShare a Scribd company logo
Page replacement algorithms
1
Page replacement algorithms
OS 2009-10
When a page fault occurs
2
ī‚— OS has to choose a page to evict from memory
ī‚— If the page has been modified, the OS has to schedule
a disk write of the page
Th j t d it i (
ī‚— The page just read overwrites a page in memory (e.g.
4Kbytes)
ī‚— Clearly it’s better not to pick a page at random
Clearly, it s better not to pick a page at random
ī‚— Same problem applies to memory caches
OS 2009-10
Benchmarking
3
ī‚— Tests are done by generating page references (either
y g g p g (
from real code or random)
ī‚— Sequences of page numbers (no real address, no
offset)
ī‚— Example:
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
OS 2009-10
Optimal page replacement
4
ī‚— 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
ī‚— Nice optimal but unrealizable
Nice, optimal, but unrealizable
ī‚Ą The OS can’t look into the future to know how long it’ll take to
reference every page again
OS 2009-10
Example: optimal
5
Sequence
PF
PF
6 page faults
OS 2009-10
Belady’s anomaly
6
Try this sequence
With 3 page frames
With 4 page frames
With 4 page frames
With FIFO, with the optimal algorithm, (later) with the LRU
OS 2009-10
“Not recently used” algorithm
7
ī‚— Use Referenced and Modified bits
ī‚— R&M are in hardware, potentially changed at each
reference to memory
ī‚Ą R&M are zero when process is started
ī‚Ą R&M are zero when process is started
ī‚— On clock interrupt the R bit is cleared
ī‚— 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 2009-10
FIFO replacement
8
ī‚— FIFO, first in first out for pages
, p g
ī‚— Clearly not particularly optimal
ī‚— It might end up removing a page that is still
g p g p g
referenced since it only looks at the page’s age
ī‚— Rarely used in pure formâ€Ļ
1 8 3 7 2
Latest load
OS 2009-10
Next removal
Example (FIFO)
9
Sequence
PF
12 page faults
OS 2009-10
“Second chance” algorithm
10
ī‚— Like FIFO butâ€Ļ
ī‚— 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=1 eventually the algorithm degenerates to
ī‚Ą If all pages have R 1, eventually the algorithm degenerates to
FIFO (why?)
1 8 3 7 2
OS 2009-10
Latest load
Clock page algorithm
11
ī‚— Like “second chance” butâ€Ļ
Like second chance butâ€Ļ
ī‚— â€Ļimplemented differently:
ī‚Ą Check starting from the
g
latest visited page
ī‚Ą More efficient:
doesn’t have to move c
h
b
a
doesn t have to move
list’s entries all the
time
c
d
h
g
e
f
OS 2009-10
Least recently used (LRU)
12
ī‚— Why: pages recently used tend to be used again
soon (on average)
i f ll i ( l i h
ī‚— List of all pages in memory (most recently in the
head, least recently used in the tail)
ī‚— List update at each memory reference!
ī‚— List update at each memory reference!
ī‚— List operations are expensive (e.g. find a page)
ī‚— So, difficultâ€Ļ
OS 2009-10
Least recently used (LRU)
13
ī‚— Idea! Get a counter, maybe a 64bit counter
ī‚— The counter is incremented after each instruction
and stored into the page entry at each reference
h l f h i h f h
ī‚— Store the value of the counter in each entry of the
page table (last access time to the page)
ī‚— When is time to remove a page find the lowest
ī‚— When is time to remove a page, find the lowest
counter value (this is the LRU page)
ī‚— Nice & good but expensive: it requires dedicated
hardware
OS 2009-10
Example LRU
14
Sequence
PF
9 page faults
OS 2009-10
NFU algorithm
15
ī‚— Since LRU is expensive
p
ī‚— NFU: “Not Frequently Used” algorithm
ī‚— At each clock interrupt add the R bit to a counter for
p
each page: i.e. count how often a page is referenced
ī‚— Remove page with lowest counter value
ī‚— Unfortunately, this version tends not to forget
anything
OS 2009-10
Aging (NFU + forgetting)
16
ī‚— Take NFU butâ€Ļ
ī‚— At each clock interrupt:
ī‚Ą Right shift the counters (divide by 2)
g y
ī‚Ą Add the R bit to the left (MSB)
ī‚— As for NFU remove pages with lowest counter
ī‚— Note: this is different from LRU since the time
granularity is a clock tick and not every memory
reference!
OS 2009-10
NFU+ageing
17
OS 2009-10
Process’ behavior
18
ī‚— Locality of reference: most of the time the last k
references are within a finite set of pages < a large
address space
ī‚— The set of pages a process is currently using is called
ī‚— The set of pages a process is currently using is called
the working set (WS) of the process
ī‚— Knowing the working set of processes we can do very
g g p y
sophisticate things (e.g. pre-paging)
ī‚— Pre-paging: load pages before letting the process to
th t th f lt t i l l if I k
run so that the page-fault rate is low, also, if I know
the WS when I swap the process then I can expect it
to be the same in the future time when I reload the
OS 2009-10
process in memory
Working set
19
set
Working
k-most-recent memory references
OS 2009-10
WS based algorithm
20
ī‚— Store execution time information in the table
entries (storing reference is too expensive!)
ī‚— At clock interrupt handle R bits as usual (clear
them)
them)
ī‚— At page fault, scan entries:
ī‚Ą If R=1 just store current time in the entry
ī‚Ą If R=1 just store current time in the entry
ī‚Ą If R=0 compute “current-last time page was
referenced” and if > threshold the page can be removed
since it’s no longer in the working set (not used for
since it s no longer in the working set (not used for
threshold time)
ī‚— Note: we’re using time rather than actual
OS 2009-10
g
memory references
e.g.: WS based algorithm
21
OS 2009-10
WSClock algorithm
22
ī‚— Use the circular structure (as seen earlier)
ī‚— At page fault examine the page pointed by the handle
ī‚— R=1, page in the WS – don’t remove it (set R to zero)
ī‚— R=0 M=0 no problem (as before check age page clean
ī‚— R=0, M=0 no problem (as before, check age, page clean
and decide depending on age)
ī‚— If M=1, schedule disk write appropriately to procrastinate
l ibl it h
as long as possible a process switch
ī‚Ą If return to starting point, then one page will eventually be clean
(maybe after a context switch)
S h d li l i l di k i b ffi i i ffi i
ī‚Ą Scheduling multiple disk write can be efficient in efficient systems
(with disk scheduling and multiple disks)
ī‚Ą No write is schedulable (R=1 always), just choose a clean page
ī‚Ą N l s th t d th h dl
OS 2009-10
ī‚Ą No clean page, use the current page under the handle
WSClock
23
R=1
R=0
OS 2009-10
Summary
Summary
24
Algorithm 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)
Clock Realistic (better implementation)
LRU (Least Recently Used) Excellent but difficult to
implement
NFU Crude approx to LRU
NFU Crude approx to LRU
Aging Efficient in approximating LRU
Working set Expensive to implement
OS 2009-10
WSClock Good and efficient
25
Design issues
OS 2009-10
Design issues
26
ī‚— Local vs. global allocation policy
g p y
ī‚Ą When a page fault occurs, whose page should the OS evict?
ī‚— 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 the more pages the
ī‚Ą For many page replacement algorithms, the more pages the
less page faults
OS 2009-10
Page fault behavior
27
c
e
faults/sec
Thrashing
Page
Optimal (fair to others)
Too many pages
Number of page frames assigned
OS 2009-10
Load control
28
ī‚— If the WS of all processes > memory, there’s
p y,
thrashing
ī‚— E.g. the PFF says a process requires more memory
but none require less
ī‚— Solution: swapping – swap a process out of memory
d i it t th
and re-assign its pages to others
OS 2009-10
Page size
29
ī‚— Page size p, n pages of memory
g p, p g y
ī‚— Average process size s, in pages s/p
ī‚— Each entry in the page table requires e bytes
Each entry in the page table requires e bytes
ī‚— On average p/2 is lost (fragmentation)
ī‚— Internal fragmentation: how much memory is not
Internal fragmentation: how much memory is not
used within pages
ī‚— Wasted memory: p/2 + se/p
Wasted memory: p/2 + se/p
ī‚— Minimizing it yields the optimal page size (under
simplifying assumptions)
OS 2009-10
p y g p )
Two memories
30
ī‚— Separate data and program address spaces
p p g p
ī‚— Two independent spaces, two paging systems
ī‚— The linker must know about the two address spaces
p
OS 2009-10
Other issues
31
ī‚— Shared pages, handle shared pages (e.g. program
p g , p g ( g p g
code)
ī‚Ą Sharing data (e.g. shared memory)
ī‚— Cleaning policy
ī‚Ą Paging algorithms work better if there are a lot of free
il bl
pages available
ī‚Ą Pages need to be swapped out to disk
ī‚Ą Paging daemon (write pages to disk during spare time
ī‚Ą Paging daemon (write pages to disk during spare time
and evict pages if there are to few)
OS 2009-10
Page fault handling
32
1. Page fault, the HW traps to the kernel
1 Perhaps registers are saved (e g stack)
1. Perhaps registers are saved (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
already somewhere within the MMU)
4 The system checks whether the process has access to the page (otherwise a
4. The system checks whether the process has access to the page (otherwise a
protection fault is generated, and the process killed)
5. The OS looks for a free page frame, if none is found then the replacement
algorithm is run
6 If the selected page is dirty (M 1) a disk write is scheduled (suspending the
6. If the selected page is dirty (M=1) a disk write is scheduled (suspending the
calling process)
7. When the page frame is clean, the OS schedules another transfer to read in the
required page from disk
8 When the load is completed the page table is updated consequently
8. When the load is completed, the page table is updated consequently
9. The faulting instruction is backed up, the situation before the fault is restored,
the process resumes execution
OS 2009-10
33
Segmentation
OS 2009-10
Why?
34
ī‚— Many separate address spaces (segments) (e.g. data, stack,
d d h if d d)
code, and many others if needed)
ī‚— Each segment is separate (e.g. addresses from 0 to some
MAX)
)
ī‚— Segments might have different lengths
ī‚— Segment number + address within segment
Li ki i i lifi d (lib i ithi diff t t
ī‚— Linking is simplified (libraries within different segments
can assume addresses starting from 0) – e.g. if a part of the
libraries is recompiled the remainder of the code is
ff t d
unaffected
ī‚— Shared library (DLL’s) implementation is simpler (the
sharing is simpler)
OS 2009-10
g p
Comparing paging and segmentation
35
Consideration Paging Segmentation
Consideration Paging Segmentation
Need the programmer be aware that
this technique is being used?
No Yes
How many linear address spaces are 1 Many
there?
Can the total address space exceed
the size of physical memory
Yes Yes
Can procedures and data be No Yes
distinguished and separately
protected?
Can tables whose size fluctuate be
accommodated easily?
No Yes
I h i f d b t N Y
Is sharing of procedures between
users facilitated?
No Yes
Why was this technique invented? To get a large linear address space
without having to buy more physical
memory
To allow programs and data to be
broken up into logically independent
address spaces and to aid sharing
OS 2009-10
y p g
and protection
Pure segmentations
36
C C
B
C
B
C
B
A A A
D
Operating system Operating system Operating system Operating system
OS 2009-10
Fragmentation
37
ī‚— External fragmentation:
ī‚Ą Memory fragments not used (we’ve already seen this)
ī‚Ą Memory wasted in unused holes
OS 2009-10
Segmentation + paging (Pentium)
38
ī‚— 16K segments
g
ī‚— 1G 32bit words (DoubleWords)
ī‚— Two tables: LDT, GDT – Local (to the process) and
Two tables: LDT, GDT Local (to the process) and
global (to the processor) descriptor table
ī‚— To work with a segment the machine loads the
g
segment number into a special register (CS, DS,
etc.) – CS, DS are 16 bit registers
ī‚— The descriptor of the segment (see next slide)
OS 2009-10
The segment descriptor
39
ī‚— This is used by the microcode within the Pentium
to work with segments
Limit in pages/bytes
Base 24-31 G D 0 Limit 16-19 P DPL S Type Base 16-23
16/32 bit segment
8 bytes
Privilege level Segment type
protection
Base 0-15 Limit 0-15
8 bytes
Segment present in memory
Page size is 4K System/application
Limit (20 bits)
I d G/L P i il
CS/DS
OS 2009-10
Index G/L Privilege
Selector
CS/DS
Getting the address
40
Selector Offset
Base address
Limit
Oth fi ld
+
Other fields
Descriptor
32 bit linear address
32-bit linear address
OS 2009-10
Paging on the Pentium
41
ī‚— 2-level page table in memory
p g y
Dir Page Offset
10 bits 10 bits 12 bits
Dir Page Offset
1023
1023
â€Ļ
1023
1023
Address to the page
â€Ļ
5
4
3
Dir 5
4
3
2
â€Ļ
5
4
3
2
â€Ļ
5
4
3
1023
â€Ļ
5
4
3
1023
â€Ļ
5
4
Page
Offset
3
2
1
0
1
0
1
0
2
1
0
3
2
1
0
3
2
1
0
Page
OS 2009-10
Each points to 4Mbytes of pages
More on the Pentiums
42
ī‚— TLB, to avoid repeated accesses to memory
, p y
ī‚— The whole thing can be used with just a single
segment to obtain a linear 32bit address space
ī‚— Set base and limit appropriately
ī‚— Protection (a few bits)
OS 2009-10

More Related Content

Similar to page replacement algorithms.pdf

Galvin-operating System(Ch10)
Galvin-operating System(Ch10)Galvin-operating System(Ch10)
Galvin-operating System(Ch10)
dsuyal1
 
Virtualmemorypre final-formatting-161019022904
Virtualmemorypre final-formatting-161019022904Virtualmemorypre final-formatting-161019022904
Virtualmemorypre final-formatting-161019022904
marangburu42
 

Similar to page replacement algorithms.pdf (20)

Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating System
 
Mem mgt
Mem mgtMem mgt
Mem mgt
 
Mca ii os u-4 memory management
Mca  ii  os u-4 memory managementMca  ii  os u-4 memory management
Mca ii os u-4 memory management
 
CH09.pdf
CH09.pdfCH09.pdf
CH09.pdf
 
Virtualmemoryfinal 161019175858
Virtualmemoryfinal 161019175858Virtualmemoryfinal 161019175858
Virtualmemoryfinal 161019175858
 
Virtual memoryfinal
Virtual memoryfinalVirtual memoryfinal
Virtual memoryfinal
 
VM Page Replacement
VM Page ReplacementVM Page Replacement
VM Page Replacement
 
Virtual memory pre-final-formatting
Virtual memory pre-final-formattingVirtual memory pre-final-formatting
Virtual memory pre-final-formatting
 
Virtual memory This is the operating system ppt.ppt
Virtual memory This is the operating system ppt.pptVirtual memory This is the operating system ppt.ppt
Virtual memory This is the operating system ppt.ppt
 
Galvin-operating System(Ch10)
Galvin-operating System(Ch10)Galvin-operating System(Ch10)
Galvin-operating System(Ch10)
 
Memory management in Linux
Memory management in LinuxMemory management in Linux
Memory management in Linux
 
Virtualmemorypre final-formatting-161019022904
Virtualmemorypre final-formatting-161019022904Virtualmemorypre final-formatting-161019022904
Virtualmemorypre final-formatting-161019022904
 
basics of virtual memory
basics of virtual memorybasics of virtual memory
basics of virtual memory
 
Unit 2chapter 2 memory mgmt complete
Unit 2chapter 2  memory mgmt completeUnit 2chapter 2  memory mgmt complete
Unit 2chapter 2 memory mgmt complete
 
Ch9
Ch9Ch9
Ch9
 
LRU_Replacement-Policy.pdf
LRU_Replacement-Policy.pdfLRU_Replacement-Policy.pdf
LRU_Replacement-Policy.pdf
 
Operating System
Operating SystemOperating System
Operating System
 
Pge Replacement Algorithm.pdf
Pge Replacement Algorithm.pdfPge Replacement Algorithm.pdf
Pge Replacement Algorithm.pdf
 
Operating System chapter 9 (Virtual Memory)
Operating System chapter 9 (Virtual Memory)Operating System chapter 9 (Virtual Memory)
Operating System chapter 9 (Virtual Memory)
 
Chapter 11 from Operating system by silberschartz
Chapter 11 from Operating system by silberschartzChapter 11 from Operating system by silberschartz
Chapter 11 from Operating system by silberschartz
 

More from SonaliAjankar (7)

ch5_CPU Scheduling_part1.pdf
ch5_CPU Scheduling_part1.pdfch5_CPU Scheduling_part1.pdf
ch5_CPU Scheduling_part1.pdf
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
assem.ppt
assem.pptassem.ppt
assem.ppt
 
part2.pdf
part2.pdfpart2.pdf
part2.pdf
 
l1.ppt
l1.pptl1.ppt
l1.ppt
 
Unit 5 Streams2.pptx
Unit 5 Streams2.pptxUnit 5 Streams2.pptx
Unit 5 Streams2.pptx
 
Mobile transport layer
 Mobile transport layer Mobile transport layer
Mobile transport layer
 

Recently uploaded

Introduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
C Sai Kiran
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
AbrahamGadissa
 

Recently uploaded (20)

Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptx
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
 
Natalia Rutkowska - BIM School Course in KrakÃŗw
Natalia Rutkowska - BIM School Course in KrakÃŗwNatalia Rutkowska - BIM School Course in KrakÃŗw
Natalia Rutkowska - BIM School Course in KrakÃŗw
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
Danfoss NeoCharge Technology -A Revolution in 2024.pdf
Danfoss NeoCharge Technology -A Revolution in 2024.pdfDanfoss NeoCharge Technology -A Revolution in 2024.pdf
Danfoss NeoCharge Technology -A Revolution in 2024.pdf
 
2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
fluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answerfluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answer
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
 
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdfA CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
 
Explosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdfExplosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdf
 
İTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering WorkshopİTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering Workshop
 
Online resume builder management system project report.pdf
Online resume builder management system project report.pdfOnline resume builder management system project report.pdf
Online resume builder management system project report.pdf
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 

page replacement algorithms.pdf

  • 1. Page replacement algorithms 1 Page replacement algorithms OS 2009-10
  • 2. When a page fault occurs 2 ī‚— OS has to choose a page to evict from memory ī‚— If the page has been modified, the OS has to schedule a disk write of the page Th j t d it i ( ī‚— The page just read overwrites a page in memory (e.g. 4Kbytes) ī‚— Clearly it’s better not to pick a page at random Clearly, it s better not to pick a page at random ī‚— Same problem applies to memory caches OS 2009-10
  • 3. Benchmarking 3 ī‚— Tests are done by generating page references (either y g g p g ( from real code or random) ī‚— Sequences of page numbers (no real address, no offset) ī‚— Example: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 OS 2009-10
  • 4. Optimal page replacement 4 ī‚— 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 ī‚— Nice optimal but unrealizable Nice, optimal, but unrealizable ī‚Ą The OS can’t look into the future to know how long it’ll take to reference every page again OS 2009-10
  • 6. Belady’s anomaly 6 Try this sequence With 3 page frames With 4 page frames With 4 page frames With FIFO, with the optimal algorithm, (later) with the LRU OS 2009-10
  • 7. “Not recently used” algorithm 7 ī‚— Use Referenced and Modified bits ī‚— R&M are in hardware, potentially changed at each reference to memory ī‚Ą R&M are zero when process is started ī‚Ą R&M are zero when process is started ī‚— On clock interrupt the R bit is cleared ī‚— 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 2009-10
  • 8. FIFO replacement 8 ī‚— FIFO, first in first out for pages , p g ī‚— Clearly not particularly optimal ī‚— It might end up removing a page that is still g p g p g referenced since it only looks at the page’s age ī‚— Rarely used in pure formâ€Ļ 1 8 3 7 2 Latest load OS 2009-10 Next removal
  • 10. “Second chance” algorithm 10 ī‚— Like FIFO butâ€Ļ ī‚— 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=1 eventually the algorithm degenerates to ī‚Ą If all pages have R 1, eventually the algorithm degenerates to FIFO (why?) 1 8 3 7 2 OS 2009-10 Latest load
  • 11. Clock page algorithm 11 ī‚— Like “second chance” butâ€Ļ Like second chance butâ€Ļ ī‚— â€Ļimplemented differently: ī‚Ą Check starting from the g latest visited page ī‚Ą More efficient: doesn’t have to move c h b a doesn t have to move list’s entries all the time c d h g e f OS 2009-10
  • 12. Least recently used (LRU) 12 ī‚— Why: pages recently used tend to be used again soon (on average) i f ll i ( l i h ī‚— List of all pages in memory (most recently in the head, least recently used in the tail) ī‚— List update at each memory reference! ī‚— List update at each memory reference! ī‚— List operations are expensive (e.g. find a page) ī‚— So, difficultâ€Ļ OS 2009-10
  • 13. Least recently used (LRU) 13 ī‚— Idea! Get a counter, maybe a 64bit counter ī‚— The counter is incremented after each instruction and stored into the page entry at each reference h l f h i h f h ī‚— Store the value of the counter in each entry of the page table (last access time to the page) ī‚— When is time to remove a page find the lowest ī‚— When is time to remove a page, find the lowest counter value (this is the LRU page) ī‚— Nice & good but expensive: it requires dedicated hardware OS 2009-10
  • 15. NFU algorithm 15 ī‚— Since LRU is expensive p ī‚— NFU: “Not Frequently Used” algorithm ī‚— At each clock interrupt add the R bit to a counter for p each page: i.e. count how often a page is referenced ī‚— Remove page with lowest counter value ī‚— Unfortunately, this version tends not to forget anything OS 2009-10
  • 16. Aging (NFU + forgetting) 16 ī‚— Take NFU butâ€Ļ ī‚— At each clock interrupt: ī‚Ą Right shift the counters (divide by 2) g y ī‚Ą Add the R bit to the left (MSB) ī‚— As for NFU remove pages with lowest counter ī‚— Note: this is different from LRU since the time granularity is a clock tick and not every memory reference! OS 2009-10
  • 18. Process’ behavior 18 ī‚— Locality of reference: most of the time the last k references are within a finite set of pages < a large address space ī‚— The set of pages a process is currently using is called ī‚— The set of pages a process is currently using is called the working set (WS) of the process ī‚— Knowing the working set of processes we can do very g g p y sophisticate things (e.g. pre-paging) ī‚— Pre-paging: load pages before letting the process to th t th f lt t i l l if I k run so that the page-fault rate is low, also, if I know the WS when I swap the process then I can expect it to be the same in the future time when I reload the OS 2009-10 process in memory
  • 20. WS based algorithm 20 ī‚— Store execution time information in the table entries (storing reference is too expensive!) ī‚— At clock interrupt handle R bits as usual (clear them) them) ī‚— At page fault, scan entries: ī‚Ą If R=1 just store current time in the entry ī‚Ą If R=1 just store current time in the entry ī‚Ą If R=0 compute “current-last time page was referenced” and if > threshold the page can be removed since it’s no longer in the working set (not used for since it s no longer in the working set (not used for threshold time) ī‚— Note: we’re using time rather than actual OS 2009-10 g memory references
  • 21. e.g.: WS based algorithm 21 OS 2009-10
  • 22. WSClock algorithm 22 ī‚— Use the circular structure (as seen earlier) ī‚— At page fault examine the page pointed by the handle ī‚— R=1, page in the WS – don’t remove it (set R to zero) ī‚— R=0 M=0 no problem (as before check age page clean ī‚— R=0, M=0 no problem (as before, check age, page clean and decide depending on age) ī‚— If M=1, schedule disk write appropriately to procrastinate l ibl it h as long as possible a process switch ī‚Ą If return to starting point, then one page will eventually be clean (maybe after a context switch) S h d li l i l di k i b ffi i i ffi i ī‚Ą Scheduling multiple disk write can be efficient in efficient systems (with disk scheduling and multiple disks) ī‚Ą No write is schedulable (R=1 always), just choose a clean page ī‚Ą N l s th t d th h dl OS 2009-10 ī‚Ą No clean page, use the current page under the handle
  • 24. Summary Summary 24 Algorithm 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) Clock Realistic (better implementation) LRU (Least Recently Used) Excellent but difficult to implement NFU Crude approx to LRU NFU Crude approx to LRU Aging Efficient in approximating LRU Working set Expensive to implement OS 2009-10 WSClock Good and efficient
  • 26. Design issues 26 ī‚— Local vs. global allocation policy g p y ī‚Ą When a page fault occurs, whose page should the OS evict? ī‚— 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 the more pages the ī‚Ą For many page replacement algorithms, the more pages the less page faults OS 2009-10
  • 27. Page fault behavior 27 c e faults/sec Thrashing Page Optimal (fair to others) Too many pages Number of page frames assigned OS 2009-10
  • 28. Load control 28 ī‚— If the WS of all processes > memory, there’s p y, thrashing ī‚— E.g. the PFF says a process requires more memory but none require less ī‚— Solution: swapping – swap a process out of memory d i it t th and re-assign its pages to others OS 2009-10
  • 29. Page size 29 ī‚— Page size p, n pages of memory g p, p g y ī‚— Average process size s, in pages s/p ī‚— Each entry in the page table requires e bytes Each entry in the page table requires e bytes ī‚— On average p/2 is lost (fragmentation) ī‚— Internal fragmentation: how much memory is not Internal fragmentation: how much memory is not used within pages ī‚— Wasted memory: p/2 + se/p Wasted memory: p/2 + se/p ī‚— Minimizing it yields the optimal page size (under simplifying assumptions) OS 2009-10 p y g p )
  • 30. Two memories 30 ī‚— Separate data and program address spaces p p g p ī‚— Two independent spaces, two paging systems ī‚— The linker must know about the two address spaces p OS 2009-10
  • 31. Other issues 31 ī‚— Shared pages, handle shared pages (e.g. program p g , p g ( g p g code) ī‚Ą Sharing data (e.g. shared memory) ī‚— Cleaning policy ī‚Ą Paging algorithms work better if there are a lot of free il bl pages available ī‚Ą Pages need to be swapped out to disk ī‚Ą Paging daemon (write pages to disk during spare time ī‚Ą Paging daemon (write pages to disk during spare time and evict pages if there are to few) OS 2009-10
  • 32. Page fault handling 32 1. Page fault, the HW traps to the kernel 1 Perhaps registers are saved (e g stack) 1. Perhaps registers are saved (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 already somewhere within the MMU) 4 The system checks whether the process has access to the page (otherwise a 4. The system checks whether the process has access to the page (otherwise a protection fault is generated, and the process killed) 5. The OS looks for a free page frame, if none is found then the replacement algorithm is run 6 If the selected page is dirty (M 1) a disk write is scheduled (suspending the 6. If the selected page is dirty (M=1) a disk write is scheduled (suspending the calling process) 7. When the page frame is clean, the OS schedules another transfer to read in the required page from disk 8 When the load is completed the page table is updated consequently 8. When the load is completed, the page table is updated consequently 9. The faulting instruction is backed up, the situation before the fault is restored, the process resumes execution OS 2009-10
  • 34. Why? 34 ī‚— Many separate address spaces (segments) (e.g. data, stack, d d h if d d) code, and many others if needed) ī‚— Each segment is separate (e.g. addresses from 0 to some MAX) ) ī‚— Segments might have different lengths ī‚— Segment number + address within segment Li ki i i lifi d (lib i ithi diff t t ī‚— Linking is simplified (libraries within different segments can assume addresses starting from 0) – e.g. if a part of the libraries is recompiled the remainder of the code is ff t d unaffected ī‚— Shared library (DLL’s) implementation is simpler (the sharing is simpler) OS 2009-10 g p
  • 35. Comparing paging and segmentation 35 Consideration Paging Segmentation Consideration Paging Segmentation Need the programmer be aware that this technique is being used? No Yes How many linear address spaces are 1 Many there? Can the total address space exceed the size of physical memory Yes Yes Can procedures and data be No Yes distinguished and separately protected? Can tables whose size fluctuate be accommodated easily? No Yes I h i f d b t N Y Is sharing of procedures between users facilitated? No Yes Why was this technique invented? To get a large linear address space without having to buy more physical memory To allow programs and data to be broken up into logically independent address spaces and to aid sharing OS 2009-10 y p g and protection
  • 36. Pure segmentations 36 C C B C B C B A A A D Operating system Operating system Operating system Operating system OS 2009-10
  • 37. Fragmentation 37 ī‚— External fragmentation: ī‚Ą Memory fragments not used (we’ve already seen this) ī‚Ą Memory wasted in unused holes OS 2009-10
  • 38. Segmentation + paging (Pentium) 38 ī‚— 16K segments g ī‚— 1G 32bit words (DoubleWords) ī‚— Two tables: LDT, GDT – Local (to the process) and Two tables: LDT, GDT Local (to the process) and global (to the processor) descriptor table ī‚— To work with a segment the machine loads the g segment number into a special register (CS, DS, etc.) – CS, DS are 16 bit registers ī‚— The descriptor of the segment (see next slide) OS 2009-10
  • 39. The segment descriptor 39 ī‚— This is used by the microcode within the Pentium to work with segments Limit in pages/bytes Base 24-31 G D 0 Limit 16-19 P DPL S Type Base 16-23 16/32 bit segment 8 bytes Privilege level Segment type protection Base 0-15 Limit 0-15 8 bytes Segment present in memory Page size is 4K System/application Limit (20 bits) I d G/L P i il CS/DS OS 2009-10 Index G/L Privilege Selector CS/DS
  • 40. Getting the address 40 Selector Offset Base address Limit Oth fi ld + Other fields Descriptor 32 bit linear address 32-bit linear address OS 2009-10
  • 41. Paging on the Pentium 41 ī‚— 2-level page table in memory p g y Dir Page Offset 10 bits 10 bits 12 bits Dir Page Offset 1023 1023 â€Ļ 1023 1023 Address to the page â€Ļ 5 4 3 Dir 5 4 3 2 â€Ļ 5 4 3 2 â€Ļ 5 4 3 1023 â€Ļ 5 4 3 1023 â€Ļ 5 4 Page Offset 3 2 1 0 1 0 1 0 2 1 0 3 2 1 0 3 2 1 0 Page OS 2009-10 Each points to 4Mbytes of pages
  • 42. More on the Pentiums 42 ī‚— TLB, to avoid repeated accesses to memory , p y ī‚— The whole thing can be used with just a single segment to obtain a linear 32bit address space ī‚— Set base and limit appropriately ī‚— Protection (a few bits) OS 2009-10