0% found this document useful (0 votes)
16 views5 pages

Understanding Virtual Memory in OS

Uploaded by

Sourav Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Understanding Virtual Memory in OS

Uploaded by

Sourav Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Operating System

[Link]

Virtual memory in os

Virtual Memory
✓ Virtual memory is a common technique used in a computer's operating
system (OS).
✓ Virtual memory uses both hardware and software to enable a computer
to compensate for physical memory shortages, temporarily transferring
data from Random Access Memory (RAM) to disk storage.
✓ Virtual Memory is implemented using Demand Paging.
✓ It can also be implemented using Demand Segmentation.

Here are the key concepts associated with virtual memory:

Address Spaces:
• Physical Address Space:
✓ The actual hardware memory (RAM) available in the system.
• Virtual Address Space:
✓ The logical address space visible to processes.
✓ It can be larger than the physical address space.
Pages:
• Page Size:
✓ Virtual memory is divided into fixed-size blocks called pages.
✓ Similarly, physical memory is divided into page frames of the same
size.
• Page Table:
✓ A data structure that maps virtual pages to physical page frames.
✓ The operating system uses the page table to translate virtual
addresses to physical addresses.

©Topperworl d
Operating System

Page Faults:
• Page Fault:
✓ Occurs when a program accesses a page that is not currently in
physical memory.
• Page Replacement:
✓ If physical memory is full, the operating system must decide which
pages to keep in RAM and which to move to disk.
Demand Paging:
• Lazy Loading:
✓ Only loads pages into physical memory when they are needed,
reducing the initial loading time of programs.
• Copy-On-Write:
✓ Allows multiple processes to share the same physical pages until one
of them modifies the content, triggering a copy of the page to a new
location.
Swapping:
• Swapping Out:
✓ Moving an entire process or part of it from RAM to disk to free up
space.
• Swapping In:
✓ Bringing a process or part of it back into RAM when it is needed.
Memory-Mapped Files:
• File Mapping:
✓ Allows a part of a file to be directly mapped into the virtual memory,
enabling I/O operations to be treated as ordinary memory access.

Benefits of Virtual Memory:


➢ Increased Address Space: Programs can use more memory than
physically available.
➢ Isolation: Processes are isolated from each other and from the
operating system.
➢ Ease of Management: Simplifies memory management and allows for
more efficient use of resources.
➢ Combination Approach: Some systems use a combination of paging
and segmentation to manage virtual memory efficiently.

©Topperworl d
Operating System

➢ Thrashing: Excessive paging activity that occurs when the system is


spending more time swapping pages than executing actual instructions.
➢ LRU (Least Recently Used): Replaces the page that has not been used
for the longest time.
➢ FIFO (First-In-First-Out): Replaces the oldest page in memory.
➢ Optimal: Replaces the page that will not be used for the longest time in
the future (theoretical optimum).

Advantages
✓ Virtual Memory allows you to run more applications at a time.
✓ With the help of virtual memory, you can easily fit many large
programs into smaller programs.
✓ With the help of Virtual memory, a multiprogramming environment
can be easily implemented.
✓ Data should be read from disk at the time when required.
✓ Common data can be shared easily between memory.
✓ The process may even become larger than all of the physical
memory.
Disadvantages
✓ Virtual memory reduces the stability of the system.
✓ The performance of Virtual memory is not as good as that of RAM.
✓ If a system is using virtual memory then applications may run slower.
✓ Virtual memory negatively affects the overall performance of a
system.
Demand Paging
• CPU generates a logical address which is converted to a physical address
by MMU (Memory Management Unit).
• When a page referenced by CPU is not found in the main memory, this
is called Page Fault.
• Demand Paging is a process of copying a page from the disk into the
main memory whenever required or when a Page Fault occurs.
This is done by the Operating System through the following steps:

©Topperworl d
Operating System

• When a CPU references to a Page that is not present in the main


memory, a Page Fault occurs.
• To execute the process, the Operating System needs to bring the
reference page in the main memory.
• The Operating System then searches for the required page in the Disk
Storage. It is a very slow process.
• Then it stores the page in one of the vacant frames of the main memory.
• Then Page table is updated according to that frame number for future
references.
• Then the CPU executes the program again.
Working :

▪ Let’s say the CPU wants to access a page for a specific process.
▪ The first step is to look for the required page in the page table.
▪ Suppose we find the required page in the page table.
▪ Therefore, the CPU access the page and forwards it to the process.
▪ However, if we can’t find the target page, the page table generates a
trap or page fault signal.
▪ Furthermore, the page table sends the signal to the operating system.
▪ This’s where we apply the demand paging technique.
▪ The demand paging technique involves six steps:

©Topperworl d
Operating System

• The first step is to handle the page faults.


• A page fault is generated when a process attempts to access a memory
address or a page that isn’t currently available on the main memory.
• Hence, the operating system must handle this page fault and load the
required page from secondary storage into memory.
Advantages

✓ Large virtual memory.


✓ More efficient use of memory.
✓ There is no limit on degree of multiprogramming.
Disadvantages

✓ Number of tables and the amount of processor overhead for


handling page interrupts are greater than in the case of the simple
paged management techniques.

©Topperworl d

Common questions

Powered by AI

The main challenges associated with virtual memory include a reduction in system stability and performance issues, as applications may run slower due to the system's reliance on disk storage instead of faster RAM. This can lead to thrashing, where excessive paging activity causes more time to be spent swapping pages than executing instructions, negatively affecting overall performance . Moreover, the processor overhead required to handle page interrupts and manage the increased number of tables complicates system operations .

Page faults occur when a program tries to access a page not currently in physical memory, which can significantly degrade system performance due to the time-consuming process of retrieving data from disk. Frequent page faults can lead to thrashing, where excessive paging disrupts program execution . To mitigate their effects, operating systems implement strategies like demand paging, where pages are loaded only when needed, and employ efficient page replacement algorithms such as LRU and Optimal to minimize page replacement time and maximize effective memory use . These mechanisms help optimize the balance between memory availability and system responsiveness.

Virtual memory provides isolation by ensuring that processes are separated from each other and the operating system, which enhances security and stability. This isolation prevents processes from directly accessing the memory space of another process . It also simplifies memory management by allowing the operating system to decide which pages to move in and out of physical memory, enabling a more efficient use of resources . These features collectively allow for easier implementation of a multiprogramming environment, increasing the overall system efficiency .

Virtual memory supports a multiprogramming environment by allowing programs to operate as if they have access to a large, contiguous block of memory, even when the total available physical memory is limited. This capability is significant because it enables several programs to execute simultaneously without requiring the entire application to be in RAM at once. It effectively balances CPU time between processes by using paging, swapping, and segmentation to manage memory allocation, resulting in improved system utilization, responsiveness, and throughput .

Demand paging is a method where pages are loaded into physical memory only when they are required rather than at the start of a program. This approach minimizes the initial load time and optimizes memory usage by only having the essential pages in RAM. When a page fault occurs, indicating a page is not in memory, the operating system retrieves the page from disk and updates the page table accordingly . This lazy loading mechanism reduces unnecessary memory occupancy and speeds up program start times by delaying loading and thus improves memory efficiency .

The LRU (Least Recently Used) strategy replaces the page that has not been used for the longest time, making it suitable in environments where access patterns are known and predictable . FIFO (First-In-First-Out) replaces the oldest page in memory, useful in simple systems with minimal overhead, albeit less efficient under varying access patterns leading to frequent thrashing . The Optimal strategy, theoretically replacing the page that will not be used for the longest time in the future, requires precise future knowledge, making it ideal for comparison or systems using sophisticated prediction algorithms, but impractical for real-time execution .

Virtual memory enhances the ability of a computer system to run multiple applications simultaneously by creating a larger virtual address space than the physical memory, thus allowing more processes to execute concurrently. Techniques such as paging and demand segmentation are effectively utilized to manage memory allocation. Paging divides memory into fixed-size blocks, with a page table mapping virtual pages to physical page frames . Demand paging only loads pages into physical memory when they are necessary, which helps in reducing initial program loading time and efficiently using resources .

Swapping contributes to virtual memory functioning by moving entire processes or parts of them between RAM and disk to free up space. When the system needs more memory, it swaps out less frequently used or idle processes to disk and swaps them back into RAM when they are needed again. The process involves identifying a process or part to be swapped, transferring it to disk storage, and then updating the memory tables to reflect these movements. Swapping is crucial for maintaining optimal RAM usage and implementing process isolation .

Memory-mapped files allow sections of a file to be directly mapped into the virtual memory, which enables file input/output operations to be treated as ordinary memory access. This can enhance performance by reducing the number of I/O calls needed and simplifying access to file data. However, it can increase complexity and memory usage if not managed properly, leading to issues such as insufficient system resources for other processes or increased page faults . The direct mapping requires careful handling to ensure efficient synchronization between disk and memory-state and to prevent data corruption or loss.

A page table plays a crucial role in virtual memory systems by acting as a data structure that maps virtual pages to physical page frames. It facilitates address translation by the operating system, converting virtual addresses generated by processes into physical addresses used by the hardware. This translation is essential for accessing the correct data stored in RAM, ensuring that each process can access its memory space correctly .

You might also like