Page-replacement policies On some standard algorithms for the management of resident

advertisement
Page-replacement policies
On some standard algorithms for
the management of resident
virtual memory pages
Use of backing storage
• The Pentium’s virtual memory architecture
supports the ‘swapping’ of memory pages
between physical ram and disk storage
• More processes can be in the ‘running’
state if some of their pages are ‘swapped’
More processes can be running
6 processes
3 processes
PHYSICAL RAM
PHYSICAL RAM
DISK STORAGE
Replacing a page
• If a running process needs to reference a
page that is not current in physical RAM,
then that page must be read from the disk
• But then some other page current in RAM
must be overwritten (i.e., “replaced”)
• It’s the Operating System’s job is to decide
which page will be the one to get replaced
What could go wrong?
• If the OS replaces a page that will soon be
referenced again, then that page will have
to be re-read back into memory (and some
other page will need to be replaced)
• Disk-I/O is slow compared to RAM access
• A task’s progress is delayed when it needs
to wait for disk-I/O operations to complete
• A worst-case senario is disk “thrashing”
Intelligent replacement
• The OS should try to avoid “replacing” any
page that will very soon be needed again
• Belady showed (in 1966) that an “optimal”
page-replacement policy is to discard the
page that won’t be needed again for the
greatest amount of time
• But of course the OS has no way to know
which pages will be needed soonest
The LRU policy
• Although the OS can’t know the future, it
can keep track of what’s occurred in past
• OS can assume a task’s future behavior
will probably resemble its past behavior
• So a page that has gone unreferenced for
the longest amount of time is “most likely”
to continue to go unreferenced in future
• This is the “Least Recently Used” policy
LRU is too costly
• Tracking the time when each page gets
referenced is prohibitively expensive
• For example, with a 4GB physical memory
there would be one million physical pages
• Hardware would need to support pagetracking in parallel with task execution
• So more practical approaches are needed
The FIFO policy
• A fixed-size array of pages is organized as
a ring-buffer (FIRST-IN, FIRST-OUT)
• Whenever a new page must be brought in
from the disk, it will replace the page that
has been in RAM for the longest time
• This is simple to implement (and can work
well if all pages are accessed with roughly
the same frequency – but that’s unusual!
The CLOCK policy
• An easy way to modify the FIFO policy, so
as to avoid replacing frequently accessed
pages that got loaded into RAM very early,
is the so-called “clock” policy (also called
known as the “second chance” algorithm)
• It uses a single bit of data for each page,
called the ‘accessed’ bit (which the CPU
automatically sets upon a page-access),
plus a revolving software ‘pointer’
The ‘clockface’ pointer
0
0
1
0
0 = unaccessed
1 = accessed
How it works
• When a page-replacement is needed, the
page pointed to is examined
– If its access-bit is 0 (unaccessed), it will get
replaced, and the pointer will be advanced
– If its access-bit is 1 (accessed), its access-bit
will be cleared, and the pointer will advance,
and this will be repeated until an unaccessed
page is reached (maybe requiring a full circle)
• So accessed pages get a second chance!
Clock-policy enhancement?
• The Pentium supports an enhancement to
this clock page-replacement policy
• Each page-table entry has two ‘sticky’ bits:
31
Page-table entry
6
5
D
A
0
A = Accessed – the page has been read ( 1 = yes, 0 = no )
D = Dirty – the page has been modified ( 1 = yes, 0 = no )
Pages: ‘clean’ versus ‘dirty’
• When a page is selected for replacement,
it does not need to be written back to disk
unless it has been modified (i.e., it’s dirty)
• So it’s more efficient if the OS replaces a
‘clean’ page instead of a ‘dirty’ one
• This leads to an improvement in the clock
page-replacement policy
Keep track of (D,A) bits
1,1
0,0
0,1
0,0
0,0 = unaccessed
0,1 = accessed (clean)
1,1 = accessed (dirty)
How enhancement works
• When a page-replacement is needed, the page
pointed to is examined: (D,A)-bits checked
– If sticky-bits are 0,0 (unaccessed), page is replaced
and the pointer is advanced
– If sticky-bits are 0,1 (accessed, but clean), bits are
not changed, but the pointer will be advanced
– After a full circle, a second scan begins; if a ‘clean’
page is encountered, it gets replaced; otherwise, after
a second full scan (all page are dirty), the initial page
gets replaced
In-class exercise
• Our ‘clockalg.cpp’ demo simulates three
page-replacement algorithms: optimal,
CLOCK, and FIFO
• Try adding your own code to implement a
simulation for the ‘enhanced’ clock policy
• NOTE: you’ll need to associate a ‘read’ or
‘write’ attribute with every page-access
Download