Professional Documents
Culture Documents
Unit 5
Unit 5
Unit 5
Topics:
Virtual memory and demand paging, page faults,
page replacement algorithms, thrashing and working
set model.
Background
Programs may have :
● Code needs to be in memory to execute, but entire program
rarely used.
● Error code, unusual routines, large data structures(Ex:
declared array of size 100 cells but only 10 cells are used)
● Entire program code not needed at same time
● Consider ability to execute partially-loaded program
● Program no longer constrained by limits of physical memory.
● Each program takes less memory while running -> more
programs run at the same time
DTEL 4
❑ Increased CPU utilization and throughput with
no increase in response time or turnaround
time(more multi-programming and time-sharing)
❑ Less I/O needed to load or swap programs into
memory -> each user program runs faster
DTEL 5
Virtual Memory
•CPU executes a process if it resides in main memory
•A Process is a collection of pages
•Each page is a collection of instructions
•CPU executes instruction that resides in main memory
•But if the size of process is larger than the size of the
main memory
•For ex process size = 10 GB
• main memory size = 5 GB
•In such case Virtual memory is implemented using demand
paging.
•i.e whenever there is a demand for any page then only OS
loads that page in the main memory.
DTEL 6
Virtual Memory
•Virtual memory–separation of user’s logical memory
from physical memory.
•As perceived by users; that programs exist in
contiguous memory.
•Abstracts physical memory: need not worry about
memory requirements.
•Only part of the program needs to be in memory for
execution.
•Logical address space can therefore be much larger
than physical address space.
•Programmers can work as if memory is an unlimited
resource
7
DTEL 7
•Virtual Memory is a storage allocation scheme used
by the Memory Management Unit(MMU) to
compensate for the shortage of physical memory by
transferring data from RAM to disk storage.
DTEL 8
•Virtual address space–logical view of how process is
stored in memory
I. Process starts at address 0 with contiguous
addresses until end of its address space
II. Meanwhile, physical memory organized in page
frames; not contiguous.
III. MMU maps logical pages to physical pages (i.e.,
frames) in memory.
DTEL 9
Virtual Memory That is Larger Than Physical Memory
10
DTEL 10
Demand Paging
12
DTEL 12
•Could bring an entire process into memory at load
time.
•Bring a process’s page into memory only when it
is needed
Less I/O needed, no unnecessary I/O
Less memory needed
Faster response
More users
•Similar to a paging system with swapping.
•A lazy swapper never swaps a page into memory
unless that page will be needed.
13
DTEL 13
•A swapper manipulates entire processes, whereas a
pager is concerned with the individual pages of a
process.
•When swapping in a process, the pager guesses
which pages will be used before swapping it out
again.
•The pager brings in only those needed pages into
memory.
•Thus, decreases swap time and amount of needed
physical memory.
•Dirty page: pages that have been modified and need
to be written back to disk.
14
DTEL 14
Valid-Invalid Bit scheme (for protection of paging system)
15
DTEL 15
16
DTEL 16
Page Fault
• What if the process refers to (i.e., tries to access) a page
not in-memory ?
• When an executing process refers to a page, it is first
searched in the main memory. If it is not present in the main
memory, a page fault occurs.
DTEL 17
Procedure for handling a page fault
1.OS checks an internal table to see if reference is
valid or invalid memory access
2.If
>>Invalid reference--abort the process
• address is not in logical address space of process.
>>Just not in memory--page in the referred page from
the disk
• logical address is valid but page is simply not
in-memory
18
DTEL 18
3.Find a free frame;
4.Read the referred page into this allocated frame via
scheduled disk operation
5.Update both internal table and page-table by setting
validation bit = v
6.Restart the instruction that caused the page fault and
resume process execution
19
DTEL 19
Steps in Handling a Page Fault
20
DTEL 20
What happens if there is no free frame?
Page replacement –
The main objective of all the Page replacement is to
decrease the maximum number of page faults.
DTEL 21
Basic Page Replacement Algorithm
1.Find the location of the desired page on disk
2.Find a free frame:
•If there is a free frame, use it
•If there is no free frame, use a page-replacement
algorithm to select a victim frame
3.Write the victim frame to the disk [if dirty]; change
the page and the frame tables accordingly
3.Read the desired page into the newly freed frame;
change the page and frame tables
4.Continue the user process from where the page
fault occurred (Restart the user processes)
22
DTEL 22
23
DTEL 23
Page-Replacement Algorithms
Various page replacement algorithms
are-
24
DTEL 24
Page-Replacement Algorithms
25
DTEL 25
Page-Replacement Algorithms
First In First Out Page Replacement
Algorithm
Consider the reference string 6, 1, 1, 2, 0, 3, 4, 6,
0, 2, 1, 2, 1, 2, 0, 3, 2, 1, 2, 0 for a memory with
three frames and calculate number of page
faults by using FIFO (First In First Out) Page
replacement algorithms.
Remember
Page Not Found - - - > Page Fault (F or PF,or M)
Page Found - - - > Page Hit (H)
26
DTEL 26
Page-Replacement Algorithms
Reference String: 6, 1, 1, 2, 0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3,
2, 1, 2, 0
s.no 1 2 3 4 5 6 7 8 9
F3
F2
F1
or
s.n 1 2 3 4 5 6 7 8 9
o
F1
F2
F3
27
DTEL 27
Page-Replacement Algorithms
Reference String: 6, 1, 1, 2, 0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3,
2, 1, 2, 0
s.no 1 2 3 4 5 6 7 8 9 10 11 12 13
F3 2 2 2 4 4 4 2 2 2 2
F2 1 1 1 1 3 3 3 0 0 0 0 0
F1 6 6 6 6 0 0 0 6 6 6 1 1 1
F/H
28
DTEL 28
Page-Replacement Algorithms
Reference String:
29
DTEL 29
Page-Replacement Algorithms
Number of Page Hits = 8
Number of Page Faults = 12
The Ratio of Page Hit to the Page Fault =
8 / 12 = 2 / 3 = 0.66
Hit ratio = total hit / total string pages
= 8/20 = 0.4
Miss ratio = 12/20= 0.6
The Page Hit Percentage = 8 *100 / 20 =
40%
The Page Fault Percentage = 100 - Page
Hit Percentage = 100 - 40 = 60%
30
DTEL 30
First-In-First-Out (FIFO) Algorithm
In this algorithm, the operating system keeps track of all pages in the
memory in a queue, the oldest page is in the front of the queue. When a
page needs to be replaced page in the front of the queue is selected for
removal.
Reference string:
7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
3 frames (3 pages can be in memory at a time per
process)
31
DTEL 31
Total=15 page faults
32
DTEL 32
Belady’s anomaly
Belady’s anomaly proves that it is possible to have
more page faults when increasing the number of page
frames while using the First in First Out (FIFO) page
replacement algorithm.
For example, if we consider reference string 3, 2, 1, 0,
3, 2, 4, 3, 2, 1, 0, 4 and 3 frame size,
we get 9 total page faults,
but if we increase frame size to 4,
we get 10 page faults.
33
DTEL 33
Optimal Page replacement Algorithm
DTEL 34
Optimal Algorithm
Example:
Suppose we have a reference page ( 7, 0, 1, 2, 0, 3, 0, 4,
2, 3, 0, 3, 2, 3) and number of frames is 4 and calculate
number of page faults by using OPTIMAL Page
replacement algorithms.
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3
F4
F3
F2
F1
M /H
35
DTEL 35
Optimal Algorithm
Example:
Suppose we have a reference page ( 7, 0, 1, 2, 0, 3, 0, 4,
2, 3, 0, 3, 2, 3) and number of frames is 4 and calculate
number of page faults by using OPTIMAL Page
replacement algorithms.
36
DTEL 36
Optimal Algorithm
37
DTEL 37
Optimal Algorithm
● An Optimal page-replacement algorithm has the lowest
page-fault rate of all algorithms.
● An optimal algorithm will never suffer from Belady's
anomaly.
● Replace the page that will not be used for the longest period
of time.
38
DTEL 38
Optimal Algorithm
● An Optimal page-replacement algorithm has the lowest
page-fault rate of all algorithms.
● An optimal algorithm will never suffer from Belady's
anomaly.
● Replace the page that will not be used for the longest period
of time.
39
DTEL 39
Optimal Algorithm
Advantages
1. Easy to Implement.
2. Simple data structures are used.
3. Highly efficient.
Disadvantages
1. Requires future knowledge of the program.
2. Time-consuming.
40
DTEL 40
Least Recently Used (LRU) Algorithm
•If we use the recent past as an approximation of the
near future, then we will replace the page that has not
been used for the longest period of time
DTEL 41
Consider the following reference string:
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
Find the number of page faults using FIFO,Optimal
and LRU page replacement algorithm with 3 page
frames
42
DTEL 42
Consider the following page reference string:
1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3, 7, 6, 3, 2, 1, 2, 3, 6.
How many page faults would occur for the following
replacement algorithms, assuming one, two, three,
four, five, six, or seven frames? Remember all frames
are initially empty, so your first unique pages will
all cost one fault each.
• LRU replacement
• FIFO replacement
• Optimal replacement
43
DTEL 43
44
DTEL 44
Most Recently Used (MRU) Algorithm
Page fault = 12
45
DTEL 45
OTHER Page Replacement Algorithm
46
DTEL 46
OTHER Page Replacement
Algorithm
MFU - Most Frequently Used Page Replacement
Algorithm
Actually MFU algorithm thinks that the page which
was used most frequently will not be needed
immediately so it will replace the MFU page.
47
DTEL 47
Buffer size:4 String 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0
1
7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
0 0 0 0 3 0 4 4 4 4 4 4 4 4 4 4 4 4 4
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 3 0 3 2 2 2 0 0 0 0 0
48
DTEL 48
LFU Page Replacement
Algorithm
The LFU page replacement algorithm stands for
the Least Frequently Used.
In the LFU, the page with the least visits in a
given period of time is removed.
It replaces the least frequently used pages.
If the frequency of pages remains constant, the
page that comes first is replaced first.
49
DTEL 49
LFU Page Replacement
Algorithm
Example:
Let's take the following reference string to understand
the LFU Page Replacement algorithm.
7024314720430327
Find the number of page faults when the LFU page
replacement policy is used. Also, consider the page
frame size to be three.
50
DTEL 50
LFU Page Replacement Algorithm
Solution:
String 7 0 2 4 3 1 4 7 2 0 4 3 0 3 2 7
Frame
3
2 2 2 1 1 1 2 2 2 3 3 3 3 3
Frame
2
0 0 0 3 3 3 7 7 0 0 0 0 0 2 7
Frame
1
7 7 7 4 4 4 4 4 4 4 4 4 4 4 4 4
Miss/H M M M M M M H M M M H M H H M M
it
51
DTEL 51
LFU Page Replacement Algorithm
53
DTEL 53
LIFO
54
DTEL 54
Example: Letʼs see how the LIFO performs for our example string of 3, 1, 2, 1, 6, 5, 1,
3 with 3-page frames:
string : 3, 1, 2, 1, 6, 5, 1, 3
55
DTEL 55
Random Page Replacement in OS
56
DTEL 56
Random Page Replacement in OS
57
DTEL 57
Random Page Replacement in OS
•Initially, since all the slots are empty, page 3,1,2 causes a page fault and takes the
empty slots
Page faults = 3
•When page 1 comes, it is in the memory and no page fault occurs.
Page faults = 3
•When page 6 comes, the page fault occurs, we replace the middle element i.e 1 is
removed.
Page faults = 4
•When page 5 comes, the page fault occurs again and middle element 6 is removed
Page faults = 5
•When page 1 comes, there is again a page fault and again the middle element 5 is
removed
Page faults = 6
•When page 3 comes, it is in memory, hence no page fault occurs.
Total page faults = 6
As we can see, the performance is not the best, but it's also not the worst. The
performance in the random replacement algorithm depends on the choice of the page
chosen at random.
58
DTEL 58
Random Page Replacement in OS
Advantages
•Easy to understand and implement
•No extra data structure needed to implement
•No overhead
Disadvantages
•Can not be analyzed, may produce different
performances for the same series
•Can suffer from Beladyʼs anomaly
59
DTEL 59
SOLVE:
1) A process refers to 5 pages A,B,C,D,E in order:
A,B,C,D,A,B,E,A,C,D,E
If the page replacement algorithm is FIFO,OPTIMAL, LRU the number of page
transfers with an empty internal store of 3 frames is:
60
DTEL 60
Thrashing
A process is said to be thrashing if the CPU spends more
time serving page faults than executing the pages,
61
DTEL 61
Thrashing
• If a process does not have “enough” pages, the
page-fault rate is very high
– Page fault to get page
– Replace existing frame
– But quickly need replaced frame back
– This leads to:
• Low CPU utilization
• Operating system thinking that it needs to
increase the degree of multiprogramming
• Another process added to the system
62
DTEL 62
• Thrashing ≡ a process is busy swapping
pages in and out (due to page fault)
63
DTEL 63
•As the degree of multiprogramming increases, CPU
utilization also increases, although more slowly, until a
maximum is reached.
•If the degree of multiprogramming is increased even
further, thrashing sets in and CPU utilization drops
sharply.
•At this point, to increase CPU utilization and stop
thrashing, we must decrease the degree of
multiprogramming.
DTEL 64
•With local replacement, if one process starts
thrashing, it cannot steal frames from another
process and cause the latter to thrash also.
65
DTEL 65
Methods to Handle Thrashing
•By suspending the execution of current processes and
preventing the execution of new process to start.
•Thrashing can be removed by selecting an
appropriate page replacement algorithm.
•Thrashing can be avoided by using local page
replacement algorithm.
•With local replacement, the process cannot take
frames from another process. It just selects and swaps
the page which belongs to the same process.
•Thrashing can also be prevented by allocating a
process as many frames as it needs.
66
DTEL 66
Solution to Trashing : Working-Set Model
Working set model: To prevent thrashing we must provide frames to
process as per its needs.
The Working set strategy is a dynamic page replacement algorithm
that starts by looking at how many frames a process is actually using.
It reduces the page fault rate.
Working set model provides frames for deciding how many and
which page of process should be in memory to obtain a good
performance of process.
Definition:
Working set model is a dynamic page replacement
algorithm that allocates frames to a particular process
assuming that the nearest future of pages will be used is a
closed approximation of the recent past pages in memory.
67
DTEL 67
Solution to Trashing : Working-Set Model
•The pages used by a process within a
window of time are called its working
set(set of active pages)
•The working-set model is based on the
assumption of locality of ref.
(A locality is a set of pages that are actively
used together. i.e A short period of time certain
instructions are executed repeatedly)
68
DTEL 68
Solution to Trashing : Working-Set Model
DTEL 70
•If D is the total demand for frames and WSS_i
is the working set size for process i,
• D = Σ WSSi,
DTEL 71
The operating system monitors the working-set of
each process and allocates them enough frames
Disadvantage
•Keeping track of working set.
•Working set is a moving window.
72
DTEL 72
THANK YOU
73
DTEL 73