You are on page 1of 73

DTEL (Department for Technology Enhanced Learning)

The Centre for Technology enabled Teaching & Learning , N Y S S, India

Teaching Innovation - Entrepreneurial - Global


DEPARTMENT OF COMPUTER TECHNOLOGY
4TH -SEMESTER
OPERATING SYSTEM

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.

• It addresses secondary memory as though it is a part


of the main memory.

•Virtual Memory makes the memory appear larger than


actually present which helps in the execution of
programs that are larger than the physical memory.

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.

• Virtual memory can be implemented via:


I. Demand paging
II. Demand segmentation
9

DTEL 9
Virtual Memory That is Larger Than Physical Memory
10

DTEL 10
Demand Paging

Demand Paging is a technique in which a page is usually


brought into the main memory only when it is needed or
demanded by the CPU.
The demand paging system is similar to the swapping paging
system in that processes are mostly stored in the main 11
memory (usually on the hard disk).
DTEL 11
Demand Paging

A pager is a kind of swapper that deals with the


individual pages of a process.
Demand Paging is a method in which a page is only
brought into main memory when the CPU requests
it.
At first, just those pages are loaded directly required by
the operation.
Pages that are never accessed are thus never loaded
into physical memory.

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)

•When this bit is set to "valid," this value indicates


that the associated page is both legal and in memory.

• If the bit is set to "invalid,“ this value indicates that


the page either is not valid (that is, not in the logical
address space of the process), or is valid but is
currently on the disk.

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.

• Page Fault is the condition in which a running process refers to a


page that is not loaded in the main memory.

• In such a case, the OS has to bring the page from the


secondary storage into the main memory. This may cause
some pages in the main memory to be replaced due to limited
storage. A Page Replacement Algorithm is required to decide
which page needs to be replaced.
17

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.

find some page in memory,which is not really in use &


swap it out
– In order to swap use one of the Page replacement
algorithm
– Select an algorithm which will result in minimum
number of page faults & improves the performance

• Same page may be brought into memory several


times 21

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

FIFO Page Replacement Algorithm-

•As the name suggests, this algorithm works on the


principle of “First in First out“.
•It replaces the oldest page that has been present in
the main memory for the longest time.
•It is implemented by keeping track of all the pages in
a queue.

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

Total number of reference strings = 20


Total number of page misses or page faults = 15
∵We know that;
Total number of page hits = Total number of reference strings – Total number of
page misses or page faults
∴ Total number of page hits (from figure 1) = 20 – 15 = 5
Thus, Page Hit ratio = Total no. of page hits/Total no. of reference = 5/20

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

In this algorithm, pages are replaced which would not be


used for the longest duration of time in the future, i.e.,
the pages in the memory which are going to be referred
farthest in the future are replaced.
The replacement occurs when the page fault appears.
Optimal Page Replacement algorithm is the best page
replacement algorithm as it gives the least number of
page faults.
This algorithm was introduced long back and is difficult
to implement because it requires future knowledge of
the program behavior.
34

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

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
The Page Hit Percentage = 8 *100 / 20 =
40%
The Page Fault Percentage = 100 - Page
Hit Percentage = 100 - 40 = 60%

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

12 faults – better than FIFO but worse than OPT


41

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

FIFO-Total number of page faults = 9


Optimal-Total number of page faults = 7
LRU-Total number of page faults = 10

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

MFU - Most Frequently Used


LFU- Least Frequently Used
LIFO - Last In First Out (LIFO)
Page Replacement Algorithms

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.

Example: consider the following reference string:7


0120304230321201701
Buffer size:4 String :7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1
701

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:

Reference String:7 024314720430327

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

Total number of reference strings = 16


Total number of page faults or page misses
= 12
We know that,
Total number of page hits = Total number
of reference strings - Total number of page
faults
otal number of page hits = 16 - 12 = 4
Page fault probability = Total number of
page faults / Total number of reference
strings
Page fault probability = 12/16 = 0.75 52
Page fault percentage = Total number of
page faults / Total number of reference
DTEL 52
Differences between the LRU and LFU Page Replacement
Algorithm
Sr.no. LRU LFU
1 LRU stands for the Least Recently LFU stands for the Least
Used page replacement algorithm. Frequently Used page
replacement algorithm.
2 It removes the page that has not It replaces the least frequently
been utilized in the memory for the used pages.
longest period of time.

3 It keeps track of page usage in the In the LFU page replacement


memory over a short time period. algorithm, the page with the least
visits in a given period of time is
removed.

53

DTEL 53
LIFO

Last In First Out (LIFO) Page Replacement Algorithm


This is the Last in First Out algorithm and works
on LIFO principles.
In this algorithm, the newest page is replaced by the
requested page.
Usually, this is done through a stack, where we
maintain a stack of pages currently in the memory with
the newest page being at the top.
Whenever a page fault occurs, the page at the top of
the stack is replaced.

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

This algorithm, chooses any random page in the


memory to be replaced by the requested page. This
algorithm can behave like any of the algorithms based
on the random page chosen to be replaced.

Example: Suppose we choose to replace the middle


frame every time a page fault occurs.
Letʼs see how our series of 3, 1, 2, 1, 6, 5, 1, 3 with 3-page
frames perform with this algorithm:

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:

2) Consider the following page reference string


A,B,C,D,B,A,E,F,A,B,C,G,F,C,F. How many page faults and page hit would
occur for the following page replacement algorithm assuming three and four
frames? Remember all frames are initially empty:
i) FIFO
ii) Optimal
iii) LRU
iv)MRU

60

DTEL 60
Thrashing
A process is said to be thrashing if the CPU spends more
time serving page faults than executing the pages,

Thrashing occurs when there are too many pages


in memory, and each page refers to another page.
The real memory shortens in capacity to have all
the pages in it, so it uses 'virtual memory'.

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.

•That means, The high degree of multiprogramming, lack


of frames, and page replacement policy are the main causes
of thrashing. 64

DTEL 64
•With local replacement, if one process starts
thrashing, it cannot steal frames from another
process and cause the latter to thrash also.

•Pages are replaced with regard to the process of


which they are a part. However, if processes are
thrashing, they will be in the queue for the paging
device most of the time.

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

•Working Set = An approximation of the program's locality.


• The set of pages in the most recent Δ page references.
• Δ: working-set window
•As a process executes, it moves from locality to locality.
•Example : Let, delta= 10 (Δ = 10):
•t1 → WS = {1,2,5,6,7}
•t2 → WS = {3,4}
•If allocated frames do not accommodate current locality, the 69
process will thrash.
DTEL 69
• Δ ≡ working-set window ≡ a fixed number of page
references
•If a page is in active use, it will be in the working set
•If it is not in the use, it will be drooped from the
working set
•Working set = sets of pages in the working set window.
•Working-set is the approximation of the program’s locality.
•The accuracy of the working set depends on the
selection of the Δ.
•Important property of the working set is the size
•Compute working set size for each process in
the system WSS
70

DTEL 70
•If D is the total demand for frames and WSS_i
is the working set size for process i,

• D = Σ WSSi,

•Now, if ‘m’ is the number of frames available in the


memory, there are 2 possibilities:

•(i) D > m i.e. total demand exceeds the number of


frames, then thrashing will occur as some processes
would not get enough frames.
•(ii) D <= m, then there would be no thrashing.
71

DTEL 71
The operating system monitors the working-set of
each process and allocates them enough frames

•Working-set strategy prevents thrashing.


•Degree of multiprogramming will be high.

Disadvantage
•Keeping track of working set.
•Working set is a moving window.

72

DTEL 72
THANK YOU

73

DTEL 73

You might also like