Skip to content

Latest commit

 

History

History
89 lines (58 loc) · 6.53 KB

9_Virtual Memory Pages.md

File metadata and controls

89 lines (58 loc) · 6.53 KB

Virtual Memory Pages 📘

Virtual Memory and Virtual Pages

  • Virtual Memory: A technique that provides an "idealized abstraction of the storage resources that are actually available on a given machine" which

    • "creates the illusion to users of a very large (main) memory."
  • Virtual Pages:

    • These are fragments of the virtual memory, each having a specific size, often 4096 bytes or 4 KB.
    • Virtual memory of a process is
    • divided into blocks or pages of equal size, and every page is designated by a unique number (e.g., P0, P1, P2, etc.)
    • If the virtual memory size is 2^32 bytes and the size of each page is 4096 bytes, then the total number of virtual pages is 2^20.

Addressing Virtual Pages and Bytes Within Them

  • Addressing a Virtual Page:

    • To uniquely identify a page within a process's virtual address space, a specific number of bits is required.
    • For 2^20 pages, 20 bits are needed.
  • Addressing Bytes Within a Page:

    • Since each page is 4096(4kB) bytes, and each byte needs a unique address, 12 bits are needed to uniquely address a byte within a page.
  • Using a combination of

  • 20 bits (for page identification)

  • and 12 bits (for byte identification within the page), any byte within the virtual memory can be uniquely addressed.

🧐 Curiosity

Technical Interview Questions

  1. How does the operating system manage virtual memory and physical memory using virtual pages and physical frames?

    • Answer: The OS uses a page table to keep track of the mapping between virtual pages and physical frames. When a process requests access to data, the OS looks up the corresponding physical frame in the page table, retrieves the data, and presents it as though it were coming from a contiguous memory space.
  2. What is the role of the Memory Management Unit (MMU) in the context of virtual memory?

    • Answer: The MMU translates virtual addresses to physical addresses. When a program accesses data, it uses a virtual address, which the MMU translates into a physical address in RAM, using information from the page table. This abstraction allows programs to function as if they have access to a larger contiguous block of memory, even though physical memory might be fragmented.
  3. Why is the size of a virtual page typically set to 4 KB, and what are the implications of having larger or smaller page sizes?

    • Answer: A 4 KB page size is a trade-off between internal and external fragmentation and the size of the page table. Larger pages could mean less external fragmentation but potentially more internal fragmentation, whereas smaller pages might reduce internal fragmentation but increase the page table size and possibly increase the rate of page swaps.
  4. Explain the concept of paging and how it's beneficial in memory management?

    • Answer: Paging is a memory management scheme that eliminates the need for contiguous allocation of physical memory and thus eliminates the problems of fitting varying sized memory chunks onto the backing store. It allows the physical address space of a process to be noncontiguous, which makes swapping process in/out easier and allows physical memory to be utilized more effectively, as free memory can be used to satisfy requests without waiting for larger chunks of memory to become available.
  5. How does swapping and paging relate, and how do they contribute to efficient memory management?

    • Answer: Swapping and paging are both memory management strategies. Swapping involves moving entire processes between main memory and a backing store, usually a hard disk. Paging, on the other hand, involves moving smaller fixed-size portions (pages) of a process to and from main memory. Together, they enable more efficient use of physical memory, allowing processes to execute even if their entire memory footprint isn’t loaded in RAM, and enabling multi-programming and multitasking by using disk storage to extend RAM virtually.

🎯 Concepts in Simple Words

  • Virtual Memory: Imagine your computer can borrow space from your hard drive and pretend it's part of the super-fast RAM! That borrowed part is what we call virtual memory.

  • Virtual Pages: Think of virtual memory as a book and the data as the text. But the book is too big to read in one go. So, we divide it into smaller pages, which are easier to read and manage. Each small page is known as a virtual page.

  • Addressing in Virtual Memory: Now, to find information quickly in this big book, every page gets a unique number and every word (byte of data) in the page also gets a specific position number. This way, by using the page number and the position number, we can quickly find any word we want in the whole book, super efficiently! This is similar to how the computer uses bits to find and manage data in virtual memory.

+--------------------------------------------------+
|                  Virtual Memory                   |
| +-------+ +-------+ +-------+ +-------+ +-------+ |
| | Page  | | Page  | | Page  | | Page  | | Page  | |
| |  P0   | |  P1   | |  P2   | |  P3   | |  P4   | |
| |       | |       | |       | |       | |       | |
| +-------+ +-------+ +-------+ +-------+ +-------+ |
+--------------------------------------------------+
  • The "Virtual Memory" is the entire block showing a section of the process’s memory space.

  • Each "Page Px" represents a block of virtual memory, where x is the page number (e.g., P0, P1, P2, etc.).

Each of these pages is of the same size (often 4KB) and serves as a chunk that the operating system (OS) manages independently.

  • The OS maps these virtual pages to physical memory frames in potentially non-contiguous manner, which provides flexibility and improves memory utilization.
+------------------+---------------------+
| Page Number Bits | Byte Offset in Page |
|      (20 bits)   |      (12 bits)      |
+------------------+---------------------+
  • Page Number Bits: This 20-bit section is utilized to uniquely identify the virtual page in memory.
  • Byte Offset in Page: These 12 bits are used to pinpoint the exact byte within the identified page, ensuring every byte within the virtual memory space can be accurately and uniquely addressed.

The addressing mechanism utilizes these two segments of bits to determine precisely where data is located within the virtual memory.

  • This concept enables the OS to manipulate and interact with memory efficiently, providing the illusion of a larger, continuous memory space to processes, while in reality, the physical memory may be fragmented and limited.