OS Virtual Memory
OS Virtual Memory
Topperworld.in
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.
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.
©Topperworl d
Operating System
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
▪ 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
©Topperworl d