You are on page 1of 24

discover yourself

inspire posterity

CHAPTER 4
Memory Management Issues
Lecture 23

22/10/2019 1
discover yourself
inspire posterity

Optimal Page Replacement Algorithm


1. The optimal page replacement algorithm is an algorithm
when a page needs to be swapped in. The Operating
System swaps out the page whose next use will occur
farthest in the future.
2. Replace page that will not be used for longest period of
time
3. For example, a page that is not going to be used for the
next 6 seconds will be swapped out over a page that is
going to be used within the next 0.4 seconds.
discover yourself
inspire posterity

EXAMPLE-1
OPTIMAL ALGORITHM
discover yourself
inspire posterity

The first three references cause faults that fill the three empty frames.
The reference to page 2 replaces page 7, because 7 will not be used until
reference 18, whereas page 0 will be used at 5, and page 1 at 14.
The reference to page 3 replaces page 1, as page 1 will be the last of the
three pages in memory to be referenced again.

With only nine page faults, optimal replacement is much better than a FIFO
algorithm, which resulted in fifteen faults.
discover yourself
inspire posterity

EXAMPLE-2
OPTIMAL ALGORITHM
discover yourself
inspire posterity

EXAMPLE-3
Apply OPTIMAL ALGORITHM
No. of Frames = 3

0 2 4 6 2 0 9 5 3 8 1 7 1 7
discover yourself
inspire posterity

EXAMPLE-3 SOLUTION
Apply OPTIMAL ALGO
No. of Frames = 3

0 2 4 6 2 0 9 5 3 8 1 7 1 7
0 0 0 0 0 0 9 9 9 8 8 8 8 8
2 2 2 2 2 2 5 5 5 1 1 1 1
4 6 6 6 6 6 3 3 3 7 7 7
discover yourself
inspire posterity

EXERCISE-1
Apply OPTIMAL Algorithm
discover yourself
inspire posterity

• Unfortunately, the optimal page-replacement is difficult


to implement, because it requires future knowledge of
the reference string
discover yourself
inspire posterity

Least-Recently-used (LRU) algorithm

• LRU replacement associates with each page the time


of that page’s last use
• When a page must be replaced, LRU chooses the
page that has not been used for the longest period
of time
discover yourself
inspire posterity

Least-Recently-used (LRU) algorithm


EXAMPLE-1
discover yourself
inspire posterity

EXPLANATION
Notice that the first 5 faults are the same as those for optimal
replacement.
When the reference to page 4 occurs, however, LRU replacement sees
that, of the three frames in memory, page 2 was used least recently.
Thus, the LRU algorithm replaces page 2, not knowing that page 2 is
about to be used.
When it then faults for page 2, the LRU algorithm replaces page 3, since it
is now the least recently used of the three pages in memory.
Despite these problems, LRU replacement with 12 faults is much better than
FIFO replacement with 15.
discover yourself
inspire posterity

Least-Recently-used (LRU) algorithm


EXAMPLE-2
discover yourself
inspire posterity

SOLUTION of Example-2
discover yourself
inspire posterity

Comparison of OPT with LRU


• Example: A process of 5 pages
discover yourself
inspire posterity
Comparison of FIFO with LRU

• LRU recognizes that pages 2 and 5 are referenced


more frequently than others but FIFO does not.

Frank - P. Weisberg
discover yourself
inspire posterity

Not/Least Frequently Used


The not frequently used (NFU) page replacement algorithm
requires a counter, and every page has one counter of its own
which is initially set to 0. At each clock interval, all pages that
have been referenced within that interval will have their
counter incremented by 1. In effect, the counters keep track of
how frequently a page has been used. Thus, the page with the
lowest counter can be swapped out when necessary.

10/22/201
9 17
discover yourself
inspire posterity

Not/Least Frequently Used


EXAMPLE-1

3 7 6 4 6 2 1 9 2 8

10/22/201
9 18
discover yourself
inspire posterity

SOLUTION OF EXAMPLE-1

10/22/201
9 19
discover yourself
inspire posterity

Not/Least Frequently Used


EXAMPLE-2

701203042303212

10/22/201
9 20
discover yourself
inspire posterity

SOLUTION OF EXAMPLE-2

10/22/201
9 21
discover yourself
inspire posterity

Segmentation (HOME READING)


A process is divided into number of segments that need not
be of equal size. When a process is brought in, all of its
segments are loaded into available regions of memory and
a segment table is set up.

10/22/2019 22
discover yourself
inspire posterity

PAGING Segmentation
1. Main memory is divided into small fixed- 1. Main memory not partitioned
size chunks called frames. 2. Program is broken down into segments
2. Program is broken down into small specified by programmer to the
chunks called pages, handled by memory compiler.
management or compiler. 3. external fragmentation.
3. no external fragmentation. 4. OS must maintain a segment table for
4. OS must maintain a page table for each each process showing which frame each
process showing which frame each process occupy.
process occupy. 5. OS must maintain a free frame list.
5. OS must maintain a free frame list. 6. All the pages of a process must be in
6. All the pages of a process must be in main memory for process to run.
main memory for process to run.

10/22/2019 23
discover yourself
inspire posterity

End of Chapter-4

You might also like