Operating System
Operating System
• Background
• Demand Paging
• Copy-on-Write
• Page Replacement
• Allocation of Frames
• Thrashing
• Memory-Mapped Files
• Allocating Kernel Memory
• Other Considerations
• Operating-System Examples
Objectives
• Lazy swapper – never swaps a page into memory unless page will be
needed
• Swapper that deals with pages is a pager
Transfer of a Paged Memory to Contiguous Disk Space
Valid-Invalid Bit
• With each page table entry a valid–invalid bit is associated
(v in-memory, i not-in-memory)
• Initially valid–invalid bit is set to i on all entries
• Example of a page table snapshot:
Frame # valid-invalid bit
v
v
v
v
i
….
i
i
page table
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
Graph of Page Faults Versus The Number of Frames
First-In-First-Out (FIFO) Algorithm
• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
• 3 frames (3 pages can be in memory at a time per process)
1 1 4 5
2 2 1 3 9 page faults
3 3 2 4
• 4 frames
1 1 5 4
2 2 1 5 10 page faults
3 3 2
4 4 3
• Belady’s Anomaly: more frames more page faults
FIFO Page Replacement
Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,3,2,2,0,1,7,0,1
3 frames (3 pages can be in memory at a time per process)
FIFO Illustrating Belady’s Anomaly
Optimal Algorithm
• Replace page that will not be used for longest period of time
• 4 frames example
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1 4
2 6 page faults
3
4 5
• How do you know this?
• Used for measuring how well your algorithm performs
Optimal Page Replacement
Least Recently Used (LRU) Algorithm
• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1 1 1 1 5
2 2 2 2 2
3 5 5 4 4
4 4 3 3 3
• Counter implementation
• Every page entry has a counter; every time page is
referenced through this entry, copy the clock into the
counter
• When a page needs to be changed, look at the counters
to determine which are to change
LRU Page Replacement
LRU Algorithm (Cont.)
• Second chance
• Need reference bit
• Clock replacement
• If page to be replaced (in clock order) has reference bit = 1
then:
• set reference bit 0
• leave page in memory
• replace next page (in clock order), subject to same rules
Second-Chance (clock)
Page-Replacement Algorithm
FIFO EXAMPLE:
Consider page reference string 1, 3, 0, 3, 5, 6, 3 with 3 page frames. Find the number of
page faults.
• A file is initially read using demand paging. A page-sized portion of the file
is read from the file system into a physical page. Subsequent reads/writes
to/from the file are treated as ordinary memory accesses.
• Simplifies file access by treating file I/O through memory rather than
read() write() system calls.
• Also allows several processes to map the same file allowing the pages in
memory to be shared.
Memory Mapped Files
Memory-Mapped Shared Memory
in Windows
Allocating Kernel Memory
• Prepaging
• To reduce the large number of page faults that occurs at process
startup
• Prepage all or some of the pages a process will need, before they
are referenced
• But if prepaged pages are unused, I/O and memory was wasted
• Assume s pages are prepaged and α of the pages is used
• Is cost of s * α save pages faults > or < than the cost of prepaging
s * (1- α) unnecessary pages?
• α near zero prepaging loses
Other Issues – Page Size
• Consider I/O - Pages that are used for copying a file from
a device must be locked from being selected for eviction
by a page replacement algorithm
Operating System Examples
• Windows XP
• Solaris
Windows XP
• Uses demand paging with clustering. Clustering brings in pages
surrounding the faulting page.
• Processes are assigned working set minimum and working set
maximum.
• Working set minimum is the minimum number of pages the process is
guaranteed to have in memory.
• A process may be assigned as many pages up to its working set
maximum.
• When the amount of free memory in the system falls below a threshold,
automatic working set trimming is performed to restore the amount of
free memory.
• Working set trimming removes pages from processes that have pages in
excess of their working set minimum.
Solaris
• Maintains a list of free pages to assign faulting processes
• Lotsfree – threshold parameter (amount of free memory) to begin paging
• Desfree – threshold parameter to increasing paging
• Minfree – threshold parameter to being swapping
• Paging is performed by pageout process
• Pageout scans pages using modified clock algorithm
• Scanrate is the rate at which pages are scanned. This ranges from slowscan to
fastscan
• Pageout is called more frequently depending upon the amount of free memory
available
Solaris 2 Page Scanner
End of Chapter 9