Download - Op Sy 03 Ch 33
1
Ceng 334 - Operating Systems 3.3-1
Chapter 3.3 : OS Policies for Virtual Memory
• Fetch policy
• Placement policy
• Replacement policy
• Resident set management
• Cleaning policy
• Load control
From : Operating Systems by W. Stallings, Prentice-Hall, 1995
Chapter 3.3: Operating System Policies for VM
2
Ceng 334 - Operating Systems 3.3-2
Fetch Policy
• Demand Paging: Pages are fetched when needed (ie., when a page fault occurs)– Process starts with a flurry of page faults,
eventually locality takes over• Prepaging (anticipatative): Pages other than the
one needed are brought in– Prepaging makes use of disk storage
characteristics. If pages are stored contiguously it may be more efficient to fetch them in one go
– The policy is ineffective if the extra pages are not referenced
Fetch Policy
3
Ceng 334 - Operating Systems 3.3-3
Placement Policy
• The placement policy is concerned with determining where in real memory a process piece is to reside
• Uni-Processor systems
– With anything other than pure segmentation this is not an issue (refer to best-fit, first-fit etc.)
• NUMA multiprocessors
– Non-uniform memory access multiprocessorshave distributed, shared memory
Placement Policy
4
Ceng 334 - Operating Systems 3.3-4
Replacement Policy
• All page frames are used. A page fault has occurred. New page must go into a frame.
• Which one do you take out?
Replacements Policy
5
Ceng 334 - Operating Systems 3.3-5
Replacement Algorithm Objectives
• The page being replaced should be the page least likely to be referenced in the near future
• There is a link between past history and the future because of locality
• Thus most algorithms base their decision on past history
Replacements Algorithm Objectives
6
Ceng 334 - Operating Systems 3.3-6
Replacement Algorithms
• Optimal
• Not-recently-used (NRU)
• First-in, first-out (FIFO)
• Least recently used (LRU)
• Not frequently used (NFU)
• Modified NFU (~LRU)
Placement Algorithms
7
Ceng 334 - Operating Systems 3.3-7
Scope of Replacements
• The set of frames from which these algorithms choose is based on the scope
• Local Scope: Only frames belonging to the faulting process can be replaced.
• Global Scope: All frames can be replaced
• Some frames will be locked (e.g. Kernel, system buffers etc.,)
Scope of Replacements
8
Ceng 334 - Operating Systems 3.3-8
Optimal Replacement Algorithm
• Replace the page which is least likely to be referenced or for which the time to the next reference is the longest
• This algorithm is impossible to implement because OS must have perfect knowledge of future events
• This algorithm is used to compare other algorithms
Optimal Replacement
9
Ceng 334 - Operating Systems 3.3-9
Optimal Replacement Example
F
F F
22
23
2231
123
3235
5235
2
435
4435
5435
3235
2235
5235
2
Optimal Replacement
10
Ceng 334 - Operating Systems 3.3-10
Optimal Replacement Example (Cont.)
• 1’st page fault : page 1 is replaced by page 5 because page 1 does not appear in the page address stream in the future
• 2’nd page fault: page 2 is replaced by page 4 because page 2 will be referenced after two (pages 5 and 3) references
• 3’rd page fault: page 4 is replaced by 2 because page 4 is not in the stream any more
Optimal Replacement Example
11
Ceng 334 - Operating Systems 3.3-11
Not-Recently-Used (NRU)
• Replace the page which is not used recently
• Use the referenced (R) & modified (M) bits in the page table entry
• R bit is set when the page is referenced (read or written)
• M bit is set when the page is modified (contents changed - written)
NRU
12
Ceng 334 - Operating Systems 3.3-12
NRU Implementation
• Hardware
– R & M bits are set by hardware on every memory reference
• Software
– R & M bits in the page table entry is modified at page faults
NRU Implementation
13
Ceng 334 - Operating Systems 3.3-13
NRU Algorithm
• When process starts both R & M bits are cleared
• R bit is cleared on every clock interrupt• At a page fault, a page from the lowest
numbered nonempty class is removed:– Class 0 : not referenced, not modified– Class 1 : not referenced, modified– Class 2 : referenced, not modified– Class 3 : referenced, modified
• Class 1 appears when clock interrupt clears R bits of class 3 processes
NRU Algorithm
14
Ceng 334 - Operating Systems 3.3-14
First-in First-out (FIFO)
• Replace the page which has been in
memory for the longest time
• Simple to implement (use a circular buffer)
• There is a chance that the oldest page may be used heavily (thrashing - page moves back & forth between memory & disk)
• Inspect R & M bits (ie., classes) to skip over heavily used pages to decrease thrashing
FIFO
15
Ceng 334 - Operating Systems 3.3-15
FIFO Example
F F
22
23
2231
123
3531
5521
2
524
4524
5324
3324
2354
5352
2
F
F F
F
FIFO Example
16
Ceng 334 - Operating Systems 3.3-16
Least Recently Used (LRU)
• Replace the page which has been unused for
the longest time
• Does almost as well as optimal
• Implementation poses overheads
• Implementation uses a time stamp for each
page (time of last reference)
LRU
17
Ceng 334 - Operating Systems 3.3-17
LRU Example
F
F F
22
23
2231
123
3251
5251
2
254
4254
5354
3352
2352
5352
2
F
LRU Example
18
Ceng 334 - Operating Systems 3.3-18
LRU Example (Cont.)
• 1’st page fault: page 5 replaces page 3 because page 3 hasn’t been referenced in the last two references
• 2’nd page fault: page 4 replaces page 1 because page 1 hasn’t been referenced in the last two references
• 3’rd page fault: page 3 replaces page 2 because page 2 hasn’t been referenced in the last two references
• 4’th page fault: page 2 replaces page 4 because page 4 hasn’t been referenced in the last two references
LRU Example
19
Ceng 334 - Operating Systems 3.3-19
Hardware Implementation of LRU
• Maintain a linked list of all pages ordered from the most recently used to the least recently used. Maintaining a list on every instruction execution is very expensive and time consuming
• An approximate solution– A hardware counter is incremented after each
instruction– Page table also has a field to store the number
of references (counter value)– At a page fault remove the page which has the
minimum number of references
Hardware LRU
20
Ceng 334 - Operating Systems 3.3-20
Software Implementation of LRU : Not Frequently Used (NFU)
• At every clock interrupt the R bit value is added to the fields in page tables
• At a page fault, replace the page with the minimum counter value
• Since the counter update is done at every clock interrupt, the algorithm is an approximation
Software LRU: NFU
21
C e n g 3 3 4 - O p e r a t i n g S y s t e m s 3 .3 - 2 1
N F U E x a m p l e
P r o c e s s # 1 2 3 4 50 0 1 2 3 4 41 0 0 1 2 2 32 0 1 1 1 1 23 0 0 0 1 1 14 0 1 2 2 3 35 0 1 1 2 2 2
R b i t s 1 0 1 0 1 1 1 1 0 0 1 0 1 1 0 1 0 1 1 0 0 0 1 0 0 1 1 0 0 0
NFU Example
22
Ceng 334 - Operating Systems 3.3-22
Modified NFU : ~LRU
• Aging of pages
• Algorithm– Shift right counter values (in page table) by 1
– Put R bit value (0 - not referenced or 1-referenced ) as the new leftmost bit at every clock tick
– At a page fault replace the page with the lowest counter value (this is the least recently used page)
Modified NFU : ~LRU
23
Ceng 334 - Operating Systems 3.3-23
Modified NFU : ~LRU (Cont.)
• Example
Suppose counter is 1 0 1 1 0
& R bit = 0
New counter becomes 0 1 0 1 1
• Leading zeros represent the number of clock ticks that the page has not been referenced
Modified NFU : ~LRU
24
Ceng 334 - Operating Systems 3.3-24
Differences Between NFU & ~LRU
• NFU counts the number of times that a
page is accessed in a given period
• ~LRU incorporates a time factor by
shifting right (aging) the counter value
Compare NFU and ~LRU
25
Ceng 334 - Operating Systems 3.3-25
Resident Set Management
• Resident Set: Set of a process' pages which are in main memory
• OS must manage the size and allocation policies which effect the resident set
Resident Set Mangement
26
Ceng 334 - Operating Systems 3.3-26
Factors Include
• Smaller resident set per process, implies more processes in memory, implies OS will always find at least one ready process (if not swapping must occur)
• If process' resident set is small, page fault frequency will be higher
• Increasing the resident set size beyond some limit does not effect page fault frequency
Factor Include
27
Ceng 334 - Operating Systems 3.3-27
Resident Set Management Policies
• Fixed allocation policy
Each process has a fixed number of pages
• Variable allocation policy
The number of pages per process can change
Resident set Mnag. Polcy.
28
Ceng 334 - Operating Systems 3.3-28
Allocation vs ScopeLocal Replacement Global Replacement
FixedAllocation
Number of framesper process is fixed
Replacement ischosen from process'frames
Not Possible
VariableAllocation
Number of framesper process willchange to maintainthe working set
Replacement ischosen from process'frames
Replacement is chosenfrom all frames of allprocesses
This may cause the sizeof the resident set of aprocess (which hasn’tcaused the page fault)to vary
Allocation Vesus Scope
29
Ceng 334 - Operating Systems 3.3-29
Fixed Allocation, Local Scope
• Frame number per process is decided beforehand and can't be changed
• Too Small: High page faults, poor performance
• Too Large: Small number of processes, high processor idle time and/or swapping
Fixed Allocation, Local Scope
30
Variable Allocation, Global Scope
Ceng 334 - Operating Systems 3.3-30
Variable Allocation, Global Scope
• Easiest to implement
• Processes with high page fault rates will tend to grow. However replacement problems exist
31
Variable Allocation, Local Scope
Ceng 334 - Operating Systems 3.3-31
Variable Allocation, Local Scope
• Allocate new process a resident set size.
• Prepaging or demand to fill up allocation
• Select replacement from within faulting process
• Re-evaluate allocation occasionally
32
Working Set
Ceng 334 - Operating Systems 3.3-32
Working Set (Denning’s)
• Program Execution– Start with main program
(page 8)– Main program loads the
main loop (pages 4,5,6,7)– Program executes in the
main loop for 20 seconds– Then routine 1 is called
(page 1) which executes 2 seconds
– Finally routine 2 (pages 2, 3) is called to execute for 3seconds
1
23
567
8
4
Routine 2
Routine 1
Main program
Main loop
33
Ceng 334 - Operating Systems 3.3-33
Working Set (Cont.)
• In the previous example, the process needs pages 4, 5, 6, 7 for most of the time (20 seconds in a total of 25 seconds execution time)
• If these pages are kept in memory the number of page faults will decrease. Otherwise, thrashing may occur
• The set of pages that a process is currently using is called its working set
• Rule: Do not run a process if its working set can not be kept in memory
Working Set
34
Working Set Implementation
Ceng 334 - Operating Systems 3.3-34
Working Set Implementation
• Use aging as in ~LRU
• Pages with 1 (referenced) in “n” clock ticks are assumed to be a member of the working set
• “n” is experimentally determined
35
Ceng 334 - Operating Systems 3.3-35
Cleaning Policy
• Cleaning Policy: Deciding when a modified page should be written out to secondary memory
• Demand Precleaning: Page is written out only when it has been selected for replacement
• Means a page fault may imply two I/O operations which severely effects performance
Cleaning Policy
36
Ceng 334 - Operating Systems 3.3-36
• Precleaning: Pages written before frames are needed (so they can be written out in batches)
• No sense writing out batches of pages and then finding them changed again
• Page Buffering is a nice compromise
37
Load Control
Ceng 334 - Operating Systems 3.3-37
Load Control
• Load Control: Controlling the number of
processes that are resident in memory
• Too few processes imply lack of ready
processes, implies swapping
• Too many processes implies high page fault
frequency which leads to thrashing