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

Slide 8 OS Virtual Memory 2025

The document discusses virtual memory and its advantages over traditional memory management, such as allowing larger programs to run on limited RAM and improving multitasking. It explains demand paging, which loads only necessary pages into RAM, and details the handling of page faults and various page replacement algorithms, including FIFO and Optimal. The document highlights the performance implications of these techniques, including page fault rates and the concept of Belady's Anomaly.

Uploaded by

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

Slide 8 OS Virtual Memory 2025

The document discusses virtual memory and its advantages over traditional memory management, such as allowing larger programs to run on limited RAM and improving multitasking. It explains demand paging, which loads only necessary pages into RAM, and details the handling of page faults and various page replacement algorithms, including FIFO and Optimal. The document highlights the performance implications of these techniques, including page fault rates and the concept of Belady's Anomaly.

Uploaded by

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

Operating Systems

Virtual Memory

Alok Kumar Jagadev


Background

Before virtual memory, early computers used contiguous memory


allocation, meaning:
• A program had to fit entirely into RAM before execution.
• Memory fragmentation was common due to variable-sized allocations.
• Multiprogramming was inefficient because of fixed memory partitions.

Challenges of Traditional Memory Management (Without Virtual


Memory):
• Limited Physical RAM: Programs couldn’t run if they required more
memory than available RAM.
• Inefficient CPU Utilization: If a process was waiting for memory, the
CPU remained idle.
• Fragmentation Issues: Fixed partitions and dynamic allocation led to
2 external and internal fragmentation.
Motivation
The primary reasons for using virtual memory include:
• Allows Running Large Programs on Limited RAM
• Programs larger than available RAM can still execute since only necessary
parts (pages) are loaded.
• Efficient Multitasking & Process Isolation
• Virtual memory allows multiple programs to run simultaneously by allocating
separate virtual address spaces to each process.
• Prevents one process from accessing another’s memory, improving security
and stability.
• Reduces Fragmentation (Paging & Segmentation)
• Paging eliminates external fragmentation since memory is divided into fixed-
size pages.
• Segmentation improves logical memory management by dividing it into
variable-sized logical segments.
3
Motivation
• Provides Memory Protection & Security
• Processes cannot directly access another process's memory, reducing crashes
and security vulnerabilities.
• Implements Demand Paging for Better Performance
• The OS loads pages into memory only when needed, reducing unnecessary
memory usage.

4
Benefits
Ability to execute a program, partially in memory has many :
• Less number of I/O needed to load or swap program into memory.
• A program no longer be constrained by amount of MM available.
• Each program take less physical memory
– more programs run at same time,
– increase in CPU utilization and throughput.

5
Virtual Memory
Example: Assume
• OS requires 300 MB of memory to store all the running programs.
• However, only 50 MB of physical memory is currently available.

Explanation:
• OS will set up 250 MB of virtual memory
• Use Virtual Memory Manager (VMM) to manage that 250 MB.
• VMM will create a required 250 MB extra memory on hard disk.
• OS will address memory as 300 MB of physical memory, even if only 50
MB is available.
• The job of the VMM to manage 300 MB memory even if just 50 MB of
physical memory space available.

6
Demand Paging
• Demand paging is a memory management technique where only required
pages of a process are loaded into RAM, while the rest remain on disk
(swap space) until needed.
• Key Idea: Instead of loading the entire process into memory at once, the
OS loads pages on demand (when the process requests them).
• Working of Demand Paging:
– When a process starts, only the necessary pages (like main program
instructions) are loaded into RAM.
– If a process tries to access a page not in RAM, a page fault occurs.
– The OS loads the missing page from secondary storage (disk) into RAM.
– The page table is updated to reflect the new mapping of virtual pages to
physical frames.
– The process resumes execution after the page is available in RAM.
7
Demand Paging
• Example: Let’s assume a program has 8 pages, but only 4 frames are
available in RAM. Initially, only a few pages are loaded:
Virtual Page Physical Frame
0 5
1 3
2 - (Page Fault)
3 9
• When the CPU tries to access page 2, a page fault occurs, and the OS
loads page 2 from disk into RAM.

8
Demand Paging
• Advantages:
– Reduces memory usage by loading only required pages.
– Enables execution of large programs on limited RAM.
– Improves CPU utilization by allowing more processes to run simultaneously.

• Disadvantages:
– Page faults increase latency since loading from disk is slow.
– Thrashing can occur if too many page faults happen frequently.

9
Demand Paging
• When a process will run, bring only a few necessary pages into memory.
• During running the process, bring a page into memory only when it is
needed (lazy swapping)
– Less I/O needed
– Less memory needed
– Faster response
– Support more processes/users

• Page is needed
– If memory resident  use the reference to page from page table
– If not in memory  Page fault trap

10
Demand Paging
• Hardware support is required to distinguish between
– Pages are in memory and
– Pages are on the disk
• valid-invalid bit scheme: Scheme to implement
– valid and invalid pages can be checked by checking the bit.
• Marking a page will have no effect if the process never attempts to access
the page.
• Process execution proceeds normally when access to a page is in
memory.

11
Valid-Invalid Bit
• Page table entry is associated with a Frame # valid-
valid–invalid bit invalid bit
– v  in-memory i.e. memory resident, v
v
– i  not-in-memory v
• Initially valid–invalid bit is set to i on all entries v
i
….
• Example of a page table snapshot: i
i
• During address translation, if valid–invalid bit in
Page Table
page table entry is i  page fault

12
Use of Page Table

13
Page Fault

• A page fault occurs when a process tries to access a page that is not
currently in RAM.
– first reference to each page always a page fault
• The OS then fetch the required page from disk and load it into RAM
before execution continues.
• OS decides:
– Invalid reference  abort.
– i.e. just not in memory.

14
Handling a Page Fault

• The CPU generates a page fault exception.


• The OS checks if the requested page is valid (or illegal).
• If valid but not in RAM, the OS finds a free frame (or replaces an old
page).
• The missing page is loaded from disk into RAM.
• The page table is updated, and the process resumes execution.

• Example of Page Fault Handling:


– Process needs Page 5, but it's not in RAM → Page fault occurs.
– OS loads Page 5 from disk into a free frame in RAM.
– Page table is updated, and the process resumes execution.

15
Steps in Handling a Page Fault

16
Performance of Demand Paging
• Page Fault Rate 0  p  1.0
– if p = 0 no page faults
– if p = 1, every reference is a fault

• Effective Access Time (EAT)


EAT = (1 – p) × memory access
+ p × ( page fault overhead
+ swap page out
+ swap page in
+ restart overhead )

17
Demand Paging: Example
Given:
• Memory access time = 200 nanoseconds
• Average page-fault service time = 8 milliseconds = 8,000,000 ns
• One access out of 1,000 causes a page fault
– page fault rate (p) = 10-3 = 0.001

• EAT = (1 – p) × 200 + p × (8 milliseconds)


= 200 – p × 200 + p × 8,000,000
= 200 + p × 7,999,800
= 200 + 0.001 × 7,999,800

• EAT = 8.2 microseconds.

This is a slowdown by a factor of 40!!


18
What happens if there is no free frame?
• Page replacement – find some page in memory to swap it out
– Algorithm: How to choose a victim page (i.e. swaped out page).
– performance: Algorithm which will result in minimum number of
page faults

• Side effect: The same page may be brought in and out of memory
several times.

19
Page Replacement
• Use modify (dirty) bit to reduce overhead of page transfers
– only modified pages are written to disk

• Page replacement completes separation between logical memory and


physical memory
– large virtual memory can be provided on a smaller physical memory

20
Page Replacement
• Basic idea:
– if there is a free frame in memory, use it
– if not, select a victim frame
– write the victim out to disk
– read the desired page into the now free frame
– update page tables
– restart the process

• A page replacement algorithm is needed to decide which page needs to be


replaced when new page comes in.

21
Page Replacement

22
Page Replacement Algorithm: Basics

• Main objective of a good replacement algorithm is to achieve a low


page fault rate
– insure that heavily used pages stay in memory and
– 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

23
Reference String
• Reference string is the sequence of pages being referenced.
• 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

24
Page and Frame Replacement Algorithms

• Frame-allocation algorithm determines


– How many frames to be allocated to each process
– Which frames to replace
• Page-replacement algorithm
– want lowest page-fault rate on both first access and re-access.

• Evaluate algorithm by running it on


– a particular string of memory references (reference string) and
– computing the number of page faults on that string
• String is just page numbers, not full addresses
• Repeated access to the same page does not cause a page fault

• The reference string is


25 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1
Graph of Page Faults Versus
The Number of Frames

26
First-In First-Out (FIFO)
• The First-In-First-Out (FIFO) page replacement algorithm removes the
oldest page from memory when a new page needs to be loaded and no
free frames are available.

• Concept: The earliest loaded page (first-in) is the first to be removed


(first-out).
• Implementation: Uses a queue (FIFO structure) to track pages in
memory.
– victims are chosen from the tail (front)
– new pages referenced are placed at the head (rear)

27
First-In First-Out (FIFO) Algorithm

Problem: A system uses 3 page frames for storing process pages in main
memory. It uses the First in First out (FIFO) page replacement policy.
Assume that all the page frames are initially empty.
What is the total number of page faults that will occur while processing the
page reference string given below:
– 4, 7, 6, 1, 7, 6, 1, 2, 7, 2
– Also calculate the hit ratio and miss ratio.

28
First-In First-Out (FIFO) Algorithm

• Total number of references = 10


4, 7, 6, 1, 7, 6, 1, 2, 7, 2

• Total number of page faults occurred = 6


29
First-In First-Out (FIFO) Algorithm

Calculating Hit ratio

Total number of page hits


= Total number of references – Total number of page misses or page faults
= 10 – 6
=4

Thus, Hit ratio


= Total number of page hits / Total number of references
= 4 / 10
= 0.4 or 40%

30
First-In First-Out (FIFO) Algorithm

Calculating Miss ratio

Total number of page misses or page faults = 6


Thus, Miss ratio
= Total number of page misses / Total number of references
= 6 / 10
= 0.6 or 60%

Alternatively,
Miss ratio
= 1 – Hit ratio
= 1 – 0.4
= 0.6 or 60%

31
First-In First-Out (FIFO) Algorithm

• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 1 1 4 5
• 3 frames (3 pages can be in memory 2 2 1 3 9 page
at a time per process): faults
3 3 2 4

1 1 5 4
• 4 frames: 2 2 1 5 10 page
faults
3 3 2

4 4 3
• FIFO Replacement manifests Belady’s Anomaly:
– occurs when increasing the number of frames results in more page
faults instead of reducing them
– i.e. more frames  less page faults
32
FIFO Illustrating Belady’s Anomaly

33
FIFO Information
Time & Space Complexity:
• Time Complexity = O(1) (Enqueue and Dequeue operations are constant
time).
• Space Complexity = O(n) (Stores page references in memory).

Advantages:
• Simple and easy to implement.
• Fast execution (Queue-based).

Disadvantages:
• Belady’s Anomaly (May increase page faults with more frames).
• Not optimal (May evict important pages).
• Ignores page usage frequency (LRU performs better).

34
Optimal Page Replacement

• The Optimal Page Replacement (OPT) Algorithm is a theoretical, ideal


page replacement strategy that minimizes page faults.
• It replaces the page that will not be used for the longest time in the
future.
• Concept: Look ahead into the future and evict the page that will be
needed the farthest in the future.
• Implementation: Requires knowledge of future page references (which
is impossible in real-world systems).

35
Optimal Algorithm

• An optimal page-replacement algorithm has


– the lowest page-fault rate of all algorithms.
– called OPT or MIN.
• Replace the page that will not be used for the longest period of time.

36
Optimal Algorithm

• A system uses 3 page frames for storing process pages in main memory.
• It uses the Optimal page replacement policy.
• Assume that all the page frames are initially empty.
• What is the total number of page faults that will occur while processing
the page reference string given below:
– 4, 7, 6, 1, 7, 6, 1, 2, 7, 2
• Also calculate the hit ratio and miss ratio.

37
Optimal Algorithm

• Total number of references = 10


• 4 , 7, 6, 1, 7, 6, 1, 2, 7, 2

• Total number of page faults occurred = 5


• Hit ratio = 0.5 or 50%
• Miss ratio = 0.5 or 50%
38
Optimal Algorithm

Advantages:
• Guaranteed minimum page faults.
• No Belady’s Anomaly.
• Best for theoretical comparison of page replacement algorithms.

Disadvantages:
• Not practical (requires future knowledge).
• Can’t be implemented in real OS.
• Only used for performance benchmarking.

39
Least Recently Used (LRU)
• The Least Recently Used (LRU) algorithm replaces the page that has not
been used for the longest time when a new page needs to be loaded into
memory.
• Concept: Pages that haven't been used for the longest time are the least
likely to be needed soon.
• Implementation: Requires tracking of past page accesses.

40
Least Recently Used (LRU)
Algorithm
• Use past knowledge rather than future
• Replace page that has not been used in the most amount of time
• Associate time of last use with each page

• 12 faults – better than FIFO but worse than OPT


• Generally good algorithm and frequently used
• But how to implement?
41
Least Recently Used (LRU)
Algorithm
• A system uses 3 page frames for storing process pages in main memory.
• It uses the Least Recently Used (LRU) page replacement policy.
• Assume that all the page frames are initially empty.
• What is the total number of page faults that will occur while processing
the page reference string given below:
– 4 , 7, 6, 1, 7, 6, 1, 2, 7, 2

• Also calculate the hit ratio and miss ratio.

42
Least Recently Used (LRU)
Algorithm
• Total number of references = 10
• Reference string: 4, 7, 6, 1, 7, 6, 1, 2, 7, 2

• Total number of page faults occurred = 6


• Hit ratio = 0.4 or 40%
• Miss ratio = 0.6 or 60%

43
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

44
LRU Issues
Advantages:
• Better than FIFO (avoids Belady’s Anomaly).
• Close to OPT in real-world performance.
• Widely used in real operating systems.

Disadvantages:
• Extra overhead (tracking order of page accesses).
• Expensive to implement efficiently in hardware.

45
Use Of A Stack to Record The
Most Recent Page References

46
Thrashing
§ Thrashing occurs when a system spends more time swapping pages in and
out of memory (paging) than executing actual processes.
§ This leads to severe performance degradation and
§ can cause the system to become nearly unresponsive.
§ Cause: Excessive page faults due to insufficient memory allocation.
§ Effect: CPU utilization drops significantly as it spends most of its time
handling page faults.

47
Thrashing
Working Set Model (WSM) and Thrashing
§ A process needs a certain number of pages (its working set) to execute
efficiently.
§ If it does not get enough frames, it will constantly trigger page faults,
leading to frequent swapping (thrashing).
§ When total memory demand > available memory ⟶Thrashing occurs.
§ Page fault rate becomes very high, leading to excessive disk I/O.
§ CPU spends more time handling page faults instead of running processes.

Thrashing Scenario
§ The system allocates fewer frames per process than required.
§ Processes keep requesting pages that are not in memory.
§ Frequent page replacements cause pages to be swapped in and out
continuously.
§ CPU utilization drops drastically ⟶ OS may think it needs to load more
48 processes, making the situation worse.
Thrashing
Symptoms of Thrashing
• High page fault rate (sudden spike in page faults per second).
• Slow system performance (even though CPU is active).
• Excessive disk activity (frequent paging/swapping).
• Low CPU utilization (OS spends time handling page faults instead of
running processes).

Thrashing Detection Methods


• Monitor page fault rate ⟶ If page faults increase sharply while CPU
utilization drops, thrashing is likely.
• Observe disk activity ⟶ If the system is accessing disk frequently but CPU
usage remains low, thrashing is occurring.

49
Thrashing
How to Prevent or Control Thrashing?
1. Working Set Model (WSM)
• The idea: Keep the entire working set of pages in memory to minimize
page faults.
– Each process is allocated enough frames to hold its working set.
– If the working set changes dynamically, the OS adjusts frame allocation
accordingly.

2. Local Page Replacement Instead of Global


• Global Replacement ⟶ A process can steal frames from another process,
leading to instability.
• Local Replacement ⟶ A process only replaces its own pages, preventing
one process from causing thrashing for others.

50
Thrashing
How to Prevent or Control Thrashing?
3. Page-Fault Frequency (PFF) Algorithm
• The OS dynamically adjusts frame allocation based on the observed page
fault rate.
• If page faults increase, allocate more frames.
• If page faults decrease, deallocate extra frames to free memory.

4. Reduce Degree of Multiprogramming


• Too many processes in memory ⟶ Insufficient frames per process ⟶
Thrashing.
• Solution: Temporarily suspend some processes to free up memory.
• Allows remaining processes to get enough frames to avoid excessive page
faults.
51
Thrashing
How to Prevent or Control Thrashing?
5. Use Demand Paging with Proper Page Replacement
• Algorithms like LRU, Clock, or Working Set Model help manage memory
efficiently.
• Ensure frequently used pages remain in memory.
• Avoid FIFO, which may remove critical pages, leading to more faults.

6. Increase RAM (Hardware Solution)


• If the system has very little RAM, thrashing is unavoidable.
• Adding more RAM allows more processes to run smoothly without excessive
swapping.

52
Thrashing: Example
Assume that 3 processes (P1, P2, P3) and each requires 5 frames to run efficiently.
Case 1: Enough Frames (No Thrashing)
• Available Frames = 15
• Each process gets 5 frames.
• Page faults occur occasionally, but processes run smoothly.
Case 2: Memory Overload (Thrashing)
• Available Frames = 9 (insufficient)
• Each process gets only 3 frames instead of 5.
• Frequent page faults occur, and processes keep swapping pages.
• CPU spends most of its time handling page faults instead of executing
processes.
Solution?
• Reduce the number of processes.
• Allocate enough frames per process.
53 • Use page-fault frequency (PFF) to monitor memory demand.
Thrashing
Thrashing results in severe performance problems:
• Low CPU utilization increases the degree of multiprogramming by
introducing a new process to the system.
• CPU utilization is plotted against the degree of multiprogramming.
• As the degree of multiprogramming increases, CPU utilization also increases.
• If the degree of multiprogramming is increased further, thrashing sets in and
.
• At this point, to increase CPU utilization and to stop thrashing, decrease
the degree of multiprogramming.

54
Solution to Thrashing
• How do we fix thrashing?
– Working Set Model (Based on Locality Model)
– Page Fault Frequency

55
Locality Model
• As a process executes, it moves from locality to locality
• locality: a set of pages actively used together
Understanding the Locality Model
• Locality of Reference states that a process tends to access a small set of
pages repeatedly for a certain period before moving to another set.
• Two types of locality:
– Temporal Locality: Recently accessed pages are likely to be accessed again
soon.
– Spatial Locality: Nearby memory locations are likely to be accessed together.

56
Locality Model

Definition of the Working Set Model


• The Working Set of a process at time t, denoted as WS(t, Δ), is the set of
pages the process has used in the last Δ (Delta) time units.
• Δ is called the working set window, representing the recent history of
memory references.
– If Δ is too small, the system might miss some pages needed for locality,
leading to excessive page faults.
– If Δ is too large, it may include unnecessary pages, leading to wasted
memory.

57
Locality Model
Working Set and Thrashing Prevention
• Thrashing occurs when a system does not allocate enough frames to a
process, causing frequent page faults and excessive swapping.
• The Working Set Model prevents thrashing by ensuring that a process is
allocated enough frames to hold its working set.
• If total available frames < total working set pages of all active processes,
the system reduces the degree of multiprogramming by suspending some
processes.

58
Locality Model
Working Set Algorithm
• Monitor each process's working set to track which pages it needs.
• Allocate enough frames to each process to store its working set.
• If the total working set size exceeds available memory, suspend some
processes.
• Resume suspended processes when enough memory is available.

59
Locality Model
Example
• Suppose P1 needs {A, B, C}, P2 needs {D, E, F, G}, and P3 needs {H, I, J}.
• If total available frames = 6 and total working set pages = 10, the system
will suspend P2 to reduce memory demand.
• When memory is available again, P2 is resumed.

60
Working-Set Model
Advantages
• Reduces page faults by ensuring essential pages remain in memory.
• Prevents thrashing by dynamically adjusting the number of active
processes.
• Improves CPU utilization by keeping only necessary processes active.

Disadvantages
• Overhead in tracking the working set for each process.
• Requires tuning of Δ to balance accuracy and efficiency.
• Process suspension delays execution but helps prevent system slowdown

61
Page Fault Frequency

Page Fault Frequency (PFF) in Thrashing


• Page Fault Frequency (PFF) method is a memory management technique
used to prevent thrashing by dynamically adjusting the allocation of
frames to processes based on their page fault rate.
• Instead of relying on fixed working set sizes, the system monitors how
frequently a process generates page faults and adjusts its memory
allocation accordingly.

62
Page Fault Frequency

Understanding Page Fault Frequency (PFF)


• Page Fault Frequency (PFF) is the rate at which a process experiences
page faults over time.
• If the PFF is high, the process does not have enough frames, causing
excessive page faults (thrashing).
• If the PFF is low, the process has more frames than needed, leading to
inefficient memory usage.
• The system dynamically adjusts the number of allocated frames based on
the observed page fault rate.

63
Page Fault Frequency

How PFF Prevents Thrashing


• A threshold value is set to determine acceptable page fault frequency:
– Upper Threshold (High PFF) → Process needs more frames.
– Lower Threshold (Low PFF) → Process has excess frames.
• If a process’s PFF exceeds the upper threshold, it is given more frames.
• If a process’s PFF drops below the lower threshold, it is given fewer
frames.
• If there are not enough free frames, the system may suspend some
processes to free up memory.

64
Page Fault Frequency

Steps in PFF-Based Frame Allocation


• Monitor the page fault rate of each process.
• Compare the PFF with predefined threshold values.
• Adjust the allocated frames:
– Increase frames if PFF is too high.
– Reduce frames if PFF is too low.
• If total available frames are insufficient, suspend a process to free up
memory.
• Resume suspended processes when enough frames become available.

65
Page Fault Frequency

Example Scenario
• Assume a system has 4 processes (P1, P2, P3, P4) and 20 available
frames.
• Each process initially gets 5 frames.
• The system observes the PFF values after execution:
– P1 → High PFF → Needs more frames.
– P2 → Low PFF → Can release some frames.
– P3 → High PFF → Needs more frames.
– P4 → Low PFF → Can release some frames.
• The system reallocates frames accordingly to optimize performance and
prevent thrashing.

66
Page Fault Frequency

Advantages of PFF Method


• Dynamically adjusts memory allocation based on actual process needs.
• Prevents thrashing by ensuring processes have sufficient frames.
• Maximizes system efficiency by balancing memory allocation.
• No need to predefine a working set window (Δ) like in the Working Set
Model.

67
Page Fault Frequency

Disadvantages of PFF Method


• Overhead in monitoring page faults for each process.
• Sudden changes in process behavior may cause frequent adjustments.
• If too many processes have high PFF, the system may still face memory
shortages.

68

You might also like