Virtual Memory Flashcards
Virtual Memory
A memory management method where computers use secondary memory to compensate for the scarcity of physical memory
Methods of handling Virtual Memory
- Paging
- Segmentation
Paging
1- Divide physical memory into fixed-sized blocks called frames where Size
is power of 2, between 512 bytes and 16 Mbytes
2- Divide logical memory into blocks of same size called pages
3- To run a program of size N pages, need to find N free frames and load program
4- Set up a page table to translate logical to physical addresses
5- Still have Internal fragmentation
Address generated by CPU is divided into?
- Page number (p) – used as an index into a page table which contains base address of each page in physical memory
- Page offset (d) – combined with base address to define the physical memory address that is sent to the memory unit
page number page offset
—————
|p | d|
—————
m -n n
For given logical address space 2^m and page size 2^n
Suppose that VA i = 3257, and Page size c is 100, find:
-Page number
-Line # (Offset)
- Page number:
└ i/c (p) ┘-> └ 3257/100 ┘= 32 - Line # (Offset): i/c = 3257%100 = 57
map 32 to the frame # and add 57 to get physical address
Estimate Internal Fragmentation if:
- Page size = 2,048 bytes
- Process size = 72,766 bytes
- 35 pages + 1,086 bytes
- Internal fragmentation of 2,048 - 1,086 = 962 bytes
What is the worst case fragmentation
1 frame – 1 byte
On average fragmentation = XX of frame size?
1 / 2
If (only) 32 bits are used with 4KB pages, a page table may have?
2^20 entries
Page Fault
If we try to address a virtual address that is not in the RAM
Page fault handling process
- Page Fault Trap: The CPU detects a page fault while trying to access a memory page that isn’t currently in RAM. This triggers a page fault exception, which transfers control to the operating system.
- Page Table Lookup: The operating system looks up the page table to determine the location of the required page in secondary storage, such as a disk.
-Fetch Page from Secondary Storage: The OS initiates a process to fetch the required page from secondary storage into an available page frame in RAM. This involves reading the page data from disk into a free page frame.
-Update Page Table: Once the page is successfully brought into RAM, the operating system updates the page table to reflect the new location of the page in physical memory. This includes updating the page table entry with the physical address of the page frame where the page now resides.
-Resume Process Execution: Finally, the operating system restarts the interrupted memory access instruction that caused the page fault. With the required page now available in RAM, the process can proceed as expected, accessing the data it needs from memory.
Demand Paging
A process in which data is moved from secondary memory to RAM on a demand basis
Page Replacement
Prevent over-allocation of memory by modifying page-fault service routine to include page replacement
How does page replacement reduce soverhead of page transfers
Use modify (dirty) bit to reduce overhead of page transfers – only modified pages are written to disk
Basic Page Replacement steps
- Find the location of the desired page on disk.
- Find a free frame:
- If there is a free frame, use it
- If there is no free frame, use a page replacement algorithm to select a victim frame
- Write victim frame to disk if dirty - Bring the desired page into the (newly) free frame; update the page and frame tables
- Continue the process by restarting the instruction that caused the trap
Frame-allocation algorithm determines :
- How many frames to give each process
- Which frames to replace