You are on page 1of 4

James Luis Partosa

BSCS - Binan

Virtual memory is a crucial memory management concept in modern computer


systems. It enables a computer to use more memory than is physically available, and
it provides memory protection and efficient memory allocation. Here's a detailed
explanation of how virtual memory works:

1. Virtual Address Space

● Each process running on a computer has its own virtual address space,
which is the range of memory addresses that the process can use.
● The virtual address space is divided into pages, which are typically 4KB
in size. These pages are the basic units of memory management.

2. Physical Memory (RAM)

● The computer's physical memory, also known as RAM (Random


Access Memory), is the actual hardware memory available for storing
data and program code.

3. Virtual-to-Physical Mapping

● Virtual memory is managed by a component called the Memory


Management Unit (MMU), which is a part of the computer's CPU.
● The MMU maps virtual addresses to physical addresses. Each process
has its own page table that the MMU uses to perform this mapping.

4. Page Tables

● Each process has its own page table, which is a data structure used to
store the mapping information for every page in the process's virtual
address space.
● A page table entry typically contains:
● Virtual Page Number (VPN): The page number in the process's
virtual address space.
● Physical Page Number (PPN): The corresponding page number
in physical memory (RAM).
● Valid/Invalid Bit: Indicates whether the page is currently in
physical memory or not.
● Permissions Bits: Information regarding whether the page
read-only or read-write, and other access permissions.
● Additional control bits: Some page table entries may have additional
bits for various purposes, such as dirty bit (indicating whether the page
has been modified), accessed bit, etc.

5. Address Translation

● When a process accesses a virtual memory address, the MMU


translates the virtual address into a physical address using the
process's page table.
● If the page is in physical memory (valid bit is set), the MMU directly
accesses the physical memory location.
● If the page is not in physical memory (invalid bit is set), a page fault
occurs. The operating system intervenes to load the required page
from secondary storage (e.g., a hard disk) into physical memory.

6. Page Fault

● A page fault is an exception that occurs when a process attempts to


access a page that is not in physical memory. The operating system
handles page faults by swapping pages between physical memory and
secondary storage.

7. Swap Space

● The operating system maintains a portion of secondary storage as


swap space. This space is used to store pages that have been
swapped out of physical memory.
● When a page is swapped in or out, the operating system manages the
transfer between physical memory and swap space.

8. Page Replacement:

● If physical memory becomes full, the operating system may need to


swap out pages to make room for new pages. Various page
replacement algorithms (e.g., LRU, FIFO, etc.) are used to determine
which pages to evict.
9. Memory Protection:

● Virtual memory provides memory protection by isolating processes


from each other. A process cannot access the memory of another
process unless explicitly allowed.

10. Efficient Memory Allocation:

● Virtual memory allows the operating system to allocate memory to


processes more efficiently. It can use a technique called demand paging,
where pages are loaded into memory only when they are accessed, reducing
the initial memory overhead.

Virtual memory is a powerful and essential concept in modern computer systems. It


allows for efficient memory management, multitasking, and the isolation of
processes, all of which contribute to the stability and security of computer systems.

2. How pages works in MMU?

In computer systems, a Memory Management Unit (MMU) is a hardware component that


plays a crucial role in virtual memory management. The concept of "pages" in the
context of an MMU is integral to how modern operating systems and processors manage
memory. Here's an overview of how pages work in an MMU:

1. Page Size:
Pages are fixed-size blocks of memory typically ranging from a few kilobytes to a few
megabytes. The most common page size is 4KB. The choice of page size is a trade-off
between reducing internal fragmentation (wasted memory within a page) and reducing
the overhead of managing a large number of pages.

2. Virtual Memory:
An MMU enables the use of virtual memory. Virtual memory allows a computer to use
more memory than is physically available. Each process running on the system has its
own virtual address space, and the MMU maps these virtual addresses to physical
addresses in RAM.
3. Page Tables:
To manage this mapping between virtual and physical memory, an MMU uses a data
structure known as a page table. Each process has its own page table. The page table is
used to store the mapping information for every page in the process's virtual address
space.

4. Page Table Entries:


Each entry in a page table typically contains the following information:

- Virtual Page Number (VPN): The page number in the process's virtual address space.
Physical Page Number (PPN): The corresponding page number in physical memory
(RAM).

- Valid/Invalid Bit: Indicates whether the page is currently in physical memory or not.
Permissions Bits: Information regarding whether the page is read-only or read-write, and
other access permissions.

- Additional control bits: Some page table entries may have additional bits for various
purposes, such as dirty bit (indicating whether the page has been modified), accessed
bit, etc.

5. Page Faults:
When a program accesses a virtual address, the MMU consults the page table to find
the corresponding physical address. If the page is not in physical memory, a page fault
occurs. In response to a page fault, the operating system loads the required page from
secondary storage (e.g., a hard disk) into physical memory.

6. TLB (Translation Lookaside Buffer):


To speed up the address translation process, MMUs often include a cache called the
Translation Lookaside Buffer (TLB). The TLB stores a subset of page table entries that
have been recently used. This helps reduce the overhead of accessing the full page
table for every memory access.

7. Page Replacement:
If physical memory becomes full, the operating system may need to swap out pages to
secondary storage to make room for new pages. Various page replacement algorithms
(e.g., LRU, FIFO, etc.) are used to determine which pages to evict.

Pages and the MMU are fundamental concepts in modern computer systems, enabling
efficient use of physical memory and providing memory protection between processes.
They are critical for the stability, security, and performance of operating systems and
applications.

You might also like