0% found this document useful (0 votes)
4 views

Os - Lecture - 07 - Memory MNGNT 2

The number of page faults with 3 frames is 9 and the fault rate is 9/12 = 0.75 The number of page faults with 4 frames is 10 and the fault rate is 10/12 = 0.83 This demonstrates Belady's Anomaly - increasing the number of frames resulted in more page faults.

Uploaded by

Kutemwa Mithi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Os - Lecture - 07 - Memory MNGNT 2

The number of page faults with 3 frames is 9 and the fault rate is 9/12 = 0.75 The number of page faults with 4 frames is 10 and the fault rate is 10/12 = 0.83 This demonstrates Belady's Anomaly - increasing the number of frames resulted in more page faults.

Uploaded by

Kutemwa Mithi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Operating Systems

Lecture 7 - Memory Management /2

Ralph Tambala

MUST . CSIT

Lecture 7 1 / 19
Outline

1 Overview

2 Virtual Memory Management

3 Page Replacement Algorithm


FIFO
FIFO
LRU
OPT

4 Belady’s Anomaly

5 Additional Info

Lecture 7 2 / 19
Overview

Overview

In this last part of memory management, we will discuss


1 Virtual memory management
2 Page replacement algorithms

Lecture 7 3 / 19
Virtual Memory Management

Virtual Memory Management (1)

The code and data required to run programs should ideally be kept in
main memory, where it can be executed fast by the CPU. However,
while running large programs or multiple programs at once, the
system’s RAM may become full. In practice, entire program rarely
used. Some of the code rarely used include error handling code,
unusual routines, and large data structures.
Loading the program partially in memory can be beneficial in several
ways:
◦ Program no longer constrained by limits of physical memory.
◦ Each program takes less memory while running resulting to more
programs running at the same time resulting to increased CPU
utilization and throughput.
◦ Less I/O needed to load or swap programs into memory resulting to
each user program running faster.

Lecture 7 4 / 19
Virtual Memory Management

Virtual Memory Management (2)

To get around this issue, some data stored in memory that isn’t being
actively used can be temporarily moved to virtual memory. Virtual
memory is a section of volatile memory created temporarily on the
secondary storage.
This frees up RAM space, which can then be used to store data that
the system will need to access soon. Virtual memory offers user an
illusion of having a very big main memory.
One of the most used technique in memory management is using
virtual memory with paging;
◦ virtual space of process divided into fixed-size pages
◦ virtual address composed of page number and page offset
◦ physical memory divided into fixed-size frames
◦ page in virtual space fits into frame in physical memory

Lecture 7 5 / 19
Virtual Memory Management

Virtual Memory Management (3)


Steps in handling a page fault

A page hit is when a program attempts to access a block of memory


that is stored in the physical memory.
In contrast, a page fault occurs when a program attempts to access a
block of memory that is not stored in the physical memory. A page
fault is also known as page miss.
Steps in handling a page fault:
1 If there is ever a reference to a page not in memory, first reference will
cause page fault.
2 Page fault is handled by the appropriate OS service routines.
3 Locate needed page on disk (in file or in backing store).
4 Swap page into free frame (assuming available).
5 Reset page tables.
6 Restart the instruction that caused the page fault.

Lecture 7 6 / 19
Virtual Memory Management

Virtual Memory Management (4)


Steps in handling a page fault

Lecture 7 7 / 19
Virtual Memory Management

Virtual Memory Management (5)


Pros and cons of virtual memory

Advantages of virtual memory include:


Allows more applications to be run at the same time.
Allows larger applications to run in systems that do not have enough
physical RAM alone to run them.
Provides a way to increase memory which is less costly than buying
more RAM.
Provides a way to increase memory in a system which has the
maximum amount of RAM that its hardware and operating system
can support.

The following are the disadvantages of virtual memory:


Does not offer the same performance as RAM.
Can negatively affect the overall performance of a system.
Takes up storage space which could otherwise be used for long term
data storage.
Lecture 7 8 / 19
Page Replacement Algorithm

Page Replacement Algorithm (1)


A page replacement policy/algorithm determines how a victim (a page to
be replaced) is selected when a page fault occurs.
Why do we care about replacement policy? We want to achieve a low
page fault rate
insure that heavily used pages stay in memory
the replaced page should not be needed for some time
Secondary objective is to reduce latency of a page fault
efficient code
replace pages that do not need to be written out
In this course, we will only focus on these 3 page replacement algorithms:
1 FIFO – First In, First Out
2 LRU – Least Recently Used
3 OPT – Optimal
Lecture 7 9 / 19
Page Replacement Algorithm

Page Replacement Algorithm (2)


Reference string

The efficiency of a page replacement algorithm is evaluated by


running it on a particular string of memory references and computing
the page fault rate.
Reference string is the sequence of pages being referenced. For
example, if user has the following sequence of addresses
123, 215, 600, 1234, 76, 96
If the page size is 100, then the reference string is
1, 2, 6, 12, 0, 0

Lecture 7 10 / 19
Page Replacement Algorithm FIFO

FIFO
The oldest page in physical memory is the one selected for
replacement
Very simple to implement
◦ keep a list
◦ victims are chosen from the tail
◦ new pages in are placed at the head
Example: Assume a system has 4 frames and no pages loaded yet in
memory. (* = page fault)

Fault rate = no. of faults/ref string length = 9/12 = 0.75


Lecture 7 11 / 19
Page Replacement Algorithm FIFO

FIFO
Issues

Evicts the oldest page in the system


◦ usually a heavily used variable should be around for a long time
◦ FIFO replaces the oldest page - perhaps the one with the heavily used
variable
FIFO does not consider page usage

Lecture 7 12 / 19
Page Replacement Algorithm LRU

LRU

LRU’s basic idea is to replace the page in memory that has not been
accessed for the longest time
Example: Example: Assume a system has 4 frames and no pages
loaded yet in memory. (* = page fault)

Fault rate = no. of faults/ref string length = 8/12 = 0.67

Lecture 7 13 / 19
Page Replacement Algorithm LRU

LRU
Issues

How to keep track of last page access?


— requires special hardware support
2 major solutions
◦ counters
– hardware clock “ticks” on every memory reference
– the page referenced is marked with this “time”
– the page with the smallest “time” value is replaced
◦ stack
– keep a stack of references
– on every reference to a page, move it to top of stack
– page at bottom of stack is next one to be replaced

Lecture 7 14 / 19
Page Replacement Algorithm OPT

OPT

OPT has the lowest page-fault rate of all algorithms. OPT is also
known as Belady’s MIN.
OPT replaces the page that will not be used for the longest period of
time.
Example: Assume a system has 4 frames and no pages loaded yet in
memory. (* = page fault)

Fault rate = no. of faults/ref string length = 6/12 = 0.50


Lecture 7 15 / 19
Page Replacement Algorithm OPT

OPT
Issues

Impossible to implement. It is requires future knowledge. We only use


OPT for comparison purposes with other page replacement
algorithms.

Lecture 7 16 / 19
Belady’s Anomaly

Belady’s Anomaly (1)

Ideally, on increasing the number of frames to a process‘ virtual memory,


its execution becomes faster as less number of page faults occur.

However, the unexpected happens sometimes. Increasing the number of


page frames may result in an increase in the number of page faults for a
given memory access pattern. This phenomenon is termed Belady’s
Anomaly.

FIFO is an example of page replacemnet algorithm that commonly


experiences Belady’s Anomaly. Other examples include second chance
algorithm and random page replacement algorithm, which are out of scope
of this course.

Lecture 7 17 / 19
Belady’s Anomaly

Belady’s Anomaly (2)


Exercise

Assuming a system that has no pages loaded in the memory and uses the
FIFO page replacement algorithm. Consider the following reference string:
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1 If the system has 3 frames using FIFO page replacement algorithm,
how many page faults will it yield? What will be the fault rate?
2 If the system has 4 frames using FIFO page replacement algorithm,
how many page faults will it yield? What will be the fault rate?

OPT and LRU do not suffer Belady’s Anomaly because these algorithms
assign a priority to a page (for replacement) that is independent of the
number of page frames.

Lecture 7 18 / 19
Additional Info

Additional Tasks

Consider the reference string given on slide 18. Do (1) and (2) this
time using LRU. What do you observe?
Prescribed textbooks are more detailed... Read them.

Lecture 7 19 / 19

You might also like