Virtual Memory
Virtual Memory
Demand Paging
• Demand paging is a technique used in virtual memory systems where the pages are
brought in the main memory only when required or demanded by the CPU.
• It is also called lazy swapper because the swapping of pages is done only when the
CPU requires it.
• In demand paging, the pager brings only those necessary pages into the memory
instead of swapping in a whole process.
• Demand paging avoids reading into memory pages that will not be used anyways,
decreasing the swap time and the amount of physical memory needed.
Swap out: Moving the pages of a program from main memory to secondary memor
Swap in: Moving the pages of a program from secondary memory to main memory
Page table when some pages are not in main memory
Page Fault
A page fault is an exception that occurs when a program accesses a memory page that is not
currently present in physical memory (RAM).
In virtual memory management, memory is divided into fixed-size pages, and only the pages
that are actively being used by a process are kept in physical memory. When a program
attempts to access a page that is not in RAM, a page fault occurs, triggering the operating
system to load the required page from secondary storage (such as a hard disk or SSD) into
physical memory.
Steps in handling a page fault
1. **Memory Access Attempt**: The program attempts to access a memory address that should be in RAM,
but the system discovers that the data is not there.
2. **Trap to the Kernel**: The CPU recognizes that the required page is not in RAM and issues a trap to the
operating system’s kernel, signaling a page fault.
3. **Interrupt Handling & Locate the Required Page**: The operating system pauses the program, and the
kernel’s interrupt handler takes over to process the page fault.
The OS identifies where the required page is stored (typically on the disk, in a swap space, or a memory-
mapped file).
A free frame is an available slot in
physical memory (RAM) that is not
currently being used by any process.
When a page fault occurs, the
operating system (OS) needs a free
frame to load the required page from
disk into memory.
4. **Allocate a Free Frame**: The kernel retrieves the needed page from the disk and places it into a free
frame in physical memory.
5. **Update the Page Table**: The OS updates the page table with the new page frame mapping. The valid-
bit is set to indicate that the page is now in memory.
6. **Restart the Faulting Process**: The CPU instruction that caused the page fault is restarted. The process
resumes execution as if no fault had occurred.
Page Replacement
One of the techniques which are used for memory management is paging. In paging, processes are
divided into pages and main memory is divided into frames. Pages of a process are loaded into
frames of main memory when required.
Page Replacement Algorithm is used when a page fault occurs. Page Fault means the page
referenced by the CPU is not present in the main memory.
When the CPU generates the reference of a page, if there is any vacant frame available in the main
memory then the page is loaded in that vacant frame. In another case, if there is no vacant frame
available in the main memory, it is required to replace one of the pages in the main memory with the
page referenced by the CPU.
Page Replacement Algorithm is used to decide which page will be replaced to allocate memory to
the current referenced page.
Different Page Replacement Algorithms suggest different ways to decide which page is to be
replaced. The main objective of these algorithms is to reduce the number of page faults.
Common Page Replacement Techniques
• First In First Out (FIFO)
• Optimal Page replacement
• Least Recently Used (LRU)
• Most Recently Used (MRU)
1. Find the location of the desired page on the disk.
2. Find a free frame:
a. If there is a free frame, use it.
b. If there is no free frame, use a page-replacement
algorithm to select a victim frame.
c. Write the victim frame to the disk; change the
page and frame tables accordingly.
3. Read the desired page into the newly freed frame;
change the page and frame tables.
4. Continue the user process from where the page fault
occurred.
Page
s 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
F3 1 1 1 1 0 0 0 3 3 3 3 3 2 2 2 2 2 1
F2 0 0 0 0 3 3 3 2 2 2 2 2 1 1 1 1 1 0 0
F1 7 7 7 2 2 2 2 4 4 4 0 0 0 0 0 0 0 7 7 7
Miss Miss Miss Miss Hit Miss Miss Miss Miss Miss Miss Hit Hit Miss Miss Hit Hit Miss Miss Miss
While using the First In First Out algorithm, the number of page faults increases by increasing the number of
frames. This phenomenon is called Belady's Anomaly.
Example: Consider the Pages referenced by the CPU in the order are 6, 7, 8, 9, 6, 7, 1, 6, 7, 8, 9, 1
Optimal Page Replacement - In this algorithm, the page which would be used after the longest interval is
replaced. In other words, the page which is farthest to come in the upcoming sequence is replaced.
Example: Consider the Pages referenced by the CPU in the order are 6, 7, 8, 9, 6, 7, 1, 6, 7, 8, 9, 1, 7, 9, 6
Least Recently Used - This algorithm works on previous data. The page which is used the earliest is replaced or
which appears the earliest in the sequence is replaced.
Example: Consider the Pages referenced by the CPU in the order are 6, 7, 8, 9, 6, 7, 1, 6, 7, 8, 9, 1, 7, 9, 6
Pages 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
F3 1 1 1 3 3 3 2 2 2 2 2 2 2 2 2 7 7 7
F2 0 0 0 0 0 0 0 0 3 3 3 3 3 3 0 0 0 0 0
F1 7 7 7 2 2 2 2 4 4 4 0 0 0 1 1 1 1 1 1 1
Miss Miss Miss Miss Hit Miss Hit Miss Miss Miss Miss Hit Hit Miss Hit Miss Hit Miss Hit Hit
Consider the page reference string as 7,1,0,2,0,3,0,4,2,1,0 with 3-page frames.