SlideShare a Scribd company logo
1 of 130
Download to read offline
Virtual Memory (Part I)
Amir H. Payberah
amir@sics.se
Amirkabir University of Technology
(Tehran Polytechnic)
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 1 / 66
Motivation and Background
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 2 / 66
Motivation
A program needs to be in memory to execute.
But, entire program rarely used.
• Error code, unusual routines, large data structures
Entire program code not needed at same time.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 3 / 66
Motivation
Consider ability to execute partially-loaded program.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 4 / 66
Motivation
Consider ability to execute partially-loaded program.
Program no longer constrained by limits of physical memory.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 4 / 66
Motivation
Consider ability to execute partially-loaded program.
Program no longer constrained by limits of physical memory.
Each program takes less memory while running: more programs run
at the same time.
• Increased CPU utilization and throughput with no increase in
response time or turnaround time.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 4 / 66
Motivation
Consider ability to execute partially-loaded program.
Program no longer constrained by limits of physical memory.
Each program takes less memory while running: more programs run
at the same time.
• Increased CPU utilization and throughput with no increase in
response time or turnaround time.
Less I/O needed to load or swap programs into memory: each user
program runs faster.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 4 / 66
Virtual Memory
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 5 / 66
Virtual Memory (1/2)
Separation of user logical memory from physical memory.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 6 / 66
Virtual Memory (1/2)
Separation of user logical memory from physical memory.
Only part of the program needs to be in memory for execution.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 6 / 66
Virtual Memory (1/2)
Separation of user logical memory from physical memory.
Only part of the program needs to be in memory for execution.
Logical address space can therefore be much larger than physical
address space.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 6 / 66
Virtual Memory (1/2)
Separation of user logical memory from physical memory.
Only part of the program needs to be in memory for execution.
Logical address space can therefore be much larger than physical
address space.
More programs running concurrently.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 6 / 66
Virtual Memory (2/2)
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 7 / 66
Virtual Address Space (1/3)
Virtual address space: logical view of how process is stored in mem-
ory.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 8 / 66
Virtual Address Space (1/3)
Virtual address space: logical view of how process is stored in mem-
ory.
Usually starts at address 0, contiguous addresses until end of space.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 8 / 66
Virtual Address Space (1/3)
Virtual address space: logical view of how process is stored in mem-
ory.
Usually starts at address 0, contiguous addresses until end of space.
Meanwhile, physical memory organized in page frames.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 8 / 66
Virtual Address Space (1/3)
Virtual address space: logical view of how process is stored in mem-
ory.
Usually starts at address 0, contiguous addresses until end of space.
Meanwhile, physical memory organized in page frames.
MMU must map logical to physical.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 8 / 66
Virtual Address Space (2/3)
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 9 / 66
Virtual Address Space (3/3)
The heap grows upward in memory, and the stack grows downward.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 10 / 66
Virtual Address Space (3/3)
The heap grows upward in memory, and the stack grows downward.
The hole between the heap and the stack is part of the virtual
address space, but will require actual physical pages only if the heap
or stack grows.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 10 / 66
Virtual Address Space (3/3)
The heap grows upward in memory, and the stack grows downward.
The hole between the heap and the stack is part of the virtual
address space, but will require actual physical pages only if the heap
or stack grows.
Virtual address spaces that include holes are known as sparse address
spaces.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 10 / 66
Heap vs. Stack
Stack
• Stores local variables created by functions.
• New variables are pushed onto the stack.
• When a function exits, all of the its pushed variables
are freed.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 11 / 66
Heap vs. Stack
Stack
• Stores local variables created by functions.
• New variables are pushed onto the stack.
• When a function exits, all of the its pushed variables
are freed.
Heap
• Used for dynamic allocation.
• Use malloc() or calloc() to allocate memory
on the heap.
• Use free() to deallocate the memory.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 11 / 66
Virtual Memory Benefits
Separating logical memory from physical memory.
Page sharing.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 12 / 66
Demand Paging
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 13 / 66
Demand Paging (1/2)
Could bring entire process into memory at load time, or bring a page
into memory only when it is needed.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 14 / 66
Demand Paging (1/2)
Could bring entire process into memory at load time, or bring a page
into memory only when it is needed.
A demand-paging system is similar to a paging system with swap-
ping.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 14 / 66
Demand Paging (2/2)
Rather than swapping the entire process into memory, we use a lazy
swapper.
A lazy swapper never swaps a page into memory unless that page
will be needed.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 15 / 66
Demand Paging (2/2)
Rather than swapping the entire process into memory, we use a lazy
swapper.
A lazy swapper never swaps a page into memory unless that page
will be needed.
A swapper that deals with pages
is a pager.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 15 / 66
Basic Concepts
The pager guesses which pages will be used before swapping out
again.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 16 / 66
Basic Concepts
The pager guesses which pages will be used before swapping out
again.
The pager brings only the needed pages into memory.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 16 / 66
Basic Concepts
The pager guesses which pages will be used before swapping out
again.
The pager brings only the needed pages into memory.
Uses valid-invalid bit to distinguish between the
pages that are in memory and
the pages that are on the disk?
v: memory resident
i: not in memory
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 16 / 66
Page Fault
Access to a page marked invalid causes a page fault.
Causing a trap to the OS: brings the desired page into memory.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 17 / 66
Handling Page Fault (1/6)
Check an internal table for the process to determine whether the
reference was a valid or an invalid memory access.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 18 / 66
Handling Page Fault (2/6)
If the reference was invalid, we terminate the process.
If it was valid but we have not yet brought in that page, we now
page it in.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 19 / 66
Handling Page Fault (3/6)
We find a free frame.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 20 / 66
Handling Page Fault (4/6)
We schedule a disk operation to read the desired page into the newly
allocated frame.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 21 / 66
Handling Page Fault (5/6)
When the disk read is complete, we modify the internal table kept
with the process and the page table to indicate that the page is now
in memory.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 22 / 66
Handling Page Fault (6/6)
We restart the instruction that was interrupted by the trap.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 23 / 66
Aspects of Demand Paging (1/2)
Extreme case: start process with no pages in memory.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 24 / 66
Aspects of Demand Paging (1/2)
Extreme case: start process with no pages in memory.
OS sets instruction pointer to first instruction of process, non-
memory-resident: page fault
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 24 / 66
Aspects of Demand Paging (1/2)
Extreme case: start process with no pages in memory.
OS sets instruction pointer to first instruction of process, non-
memory-resident: page fault
And for every other process pages on first access.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 24 / 66
Aspects of Demand Paging (1/2)
Extreme case: start process with no pages in memory.
OS sets instruction pointer to first instruction of process, non-
memory-resident: page fault
And for every other process pages on first access.
Pure demand paging
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 24 / 66
Aspects of Demand Paging (2/2)
A program could access several new pages of memory with each
instruction execution: multiple page faults per instruction.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 25 / 66
Aspects of Demand Paging (2/2)
A program could access several new pages of memory with each
instruction execution: multiple page faults per instruction.
Unacceptable system performance
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 25 / 66
Aspects of Demand Paging (2/2)
A program could access several new pages of memory with each
instruction execution: multiple page faults per instruction.
Unacceptable system performance
Locality of reference results in reasonable performance from demand
paging.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 25 / 66
Demand Paging Hardware
The hardware to support demand paging is the same as the hardware
for paging and swapping:
• Page table with valid-invalid bit
• Secondary memory with swap space
The ability to restart any instruction after a page fault.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 26 / 66
Stages in Demand Paging (1/3)
1. Trap to the OS.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 27 / 66
Stages in Demand Paging (1/3)
1. Trap to the OS.
2. Save the user registers and process state.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 27 / 66
Stages in Demand Paging (1/3)
1. Trap to the OS.
2. Save the user registers and process state.
3. Determine that the interrupt was a page fault.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 27 / 66
Stages in Demand Paging (1/3)
1. Trap to the OS.
2. Save the user registers and process state.
3. Determine that the interrupt was a page fault.
4. Check that the page reference was legal and determine the loca-
tion of the page on the disk.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 27 / 66
Stages in Demand Paging (2/3)
5. Issue a read from the disk to a free frame:
• Wait in a queue for this device until the read request is serviced.
• Wait for the device seek and/or latency time.
• Begin the transfer of the page to a free frame.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 28 / 66
Stages in Demand Paging (2/3)
5. Issue a read from the disk to a free frame:
• Wait in a queue for this device until the read request is serviced.
• Wait for the device seek and/or latency time.
• Begin the transfer of the page to a free frame.
6. While waiting, allocate the CPU to some other user.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 28 / 66
Stages in Demand Paging (2/3)
5. Issue a read from the disk to a free frame:
• Wait in a queue for this device until the read request is serviced.
• Wait for the device seek and/or latency time.
• Begin the transfer of the page to a free frame.
6. While waiting, allocate the CPU to some other user.
7. Receive an interrupt from the disk I/O subsystem (I/O com-
pleted).
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 28 / 66
Stages in Demand Paging (3/3)
8. Save the registers and process state for the other user.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 29 / 66
Stages in Demand Paging (3/3)
8. Save the registers and process state for the other user.
9. Determine that the interrupt was from the disk.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 29 / 66
Stages in Demand Paging (3/3)
8. Save the registers and process state for the other user.
9. Determine that the interrupt was from the disk.
10. Correct the page table and other tables to show page is now in
memory.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 29 / 66
Stages in Demand Paging (3/3)
8. Save the registers and process state for the other user.
9. Determine that the interrupt was from the disk.
10. Correct the page table and other tables to show page is now in
memory.
11. Wait for the CPU to be allocated to this process again.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 29 / 66
Stages in Demand Paging (3/3)
8. Save the registers and process state for the other user.
9. Determine that the interrupt was from the disk.
10. Correct the page table and other tables to show page is now in
memory.
11. Wait for the CPU to be allocated to this process again.
12. Restore the user registers, process state, and new page table,
and then resume the interrupted instruction.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 29 / 66
Performance of Demand Paging
Probability of a page fault: 0 ≤ p ≤ 1
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 30 / 66
Performance of Demand Paging
Probability of a page fault: 0 ≤ p ≤ 1
If p = 0: no page faults
If p = 1: every reference is a fault
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 30 / 66
Performance of Demand Paging
Probability of a page fault: 0 ≤ p ≤ 1
If p = 0: no page faults
If p = 1: every reference is a fault
Effective Access Time (EAT)
EAT = (1 − p) × memory access + p × page fault time
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 30 / 66
Demand Paging Example
Memory access time = 200 nanoseconds
Average page-fault service time = 8 milliseconds
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 31 / 66
Demand Paging Example
Memory access time = 200 nanoseconds
Average page-fault service time = 8 milliseconds
EAT = (1 − p) × 200 + p × 8, 000, 000 = 200 + p × 7, 999, 800
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 31 / 66
Demand Paging Example
Memory access time = 200 nanoseconds
Average page-fault service time = 8 milliseconds
EAT = (1 − p) × 200 + p × 8, 000, 000 = 200 + p × 7, 999, 800
If p = 0.001, then EAT = 8.2microseconds: this is a slowdown by
a factor of 40.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 31 / 66
Demand Paging Example
Memory access time = 200 nanoseconds
Average page-fault service time = 8 milliseconds
EAT = (1 − p) × 200 + p × 8, 000, 000 = 200 + p × 7, 999, 800
If p = 0.001, then EAT = 8.2microseconds: this is a slowdown by
a factor of 40.
If want performance degradation < 10 percent:
• 220 > 200 + p × 7, 999, 800
• 20 > p × 7, 999, 800
• p < .0000025: less than one page fault in every 400,000 memory
accesses
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 31 / 66
Demand Paging Optimization
Swap space I/O faster than file system I/O even if on the same
device.
• Swap allocated in larger chunks, less management needed than file
system.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 32 / 66
Demand Paging Optimization
Swap space I/O faster than file system I/O even if on the same
device.
• Swap allocated in larger chunks, less management needed than file
system.
Copy entire process image to swap space at process load time.
• Then page in and out of swap space.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 32 / 66
Demand Paging Optimization
Swap space I/O faster than file system I/O even if on the same
device.
• Swap allocated in larger chunks, less management needed than file
system.
Copy entire process image to swap space at process load time.
• Then page in and out of swap space.
Demand page in from program binary on disk, but discard rather
than paging out when freeing frame.
• Pages not associated with a file, i.e., heap and stack (anonymous
memory) till need to write to swap space.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 32 / 66
Copy-on-Write
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 33 / 66
Copy-on-Write
Copy-on-Write allows both parent and child processes to initially
share the same pages in memory.
If either process modifies a shared page, only then is the page copied.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 34 / 66
Copy-on-Write Example
Before modification
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 35 / 66
Copy-on-Write Example
Before modification
After modification
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 35 / 66
More About Copy-on-Write
Copy-on-Write allows more efficient process creation as only modi-
fied pages are copied.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 36 / 66
More About Copy-on-Write
Copy-on-Write allows more efficient process creation as only modi-
fied pages are copied.
Free pages are allocated from a pool of zero-fill-on-demand pages.
• Pool should always have free frames for fast demand page execution.
• Zero-fill-on-demand pages have been zeroed-out before being
allocated, thus erasing the previous contents.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 36 / 66
More About Copy-on-Write
Copy-on-Write allows more efficient process creation as only modi-
fied pages are copied.
Free pages are allocated from a pool of zero-fill-on-demand pages.
• Pool should always have free frames for fast demand page execution.
• Zero-fill-on-demand pages have been zeroed-out before being
allocated, thus erasing the previous contents.
vfork() variation on fork() does not use Copy-on-Write: with
vfork(), the parent process is suspended, and the child process
uses the address space of the parent.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 36 / 66
Page Replacement
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 37 / 66
What Happens if There is no Free Frame?
Assume, we had forty frames in physical memory.
And, we run six processes, each of which is ten pages in size, but
actually uses only five pages.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 38 / 66
What Happens if There is no Free Frame?
Assume, we had forty frames in physical memory.
And, we run six processes, each of which is ten pages in size, but
actually uses only five pages.
It is possible that each of these processes may suddenly try to use
all ten of its pages: resulting in a need for sixty frames when only
forty are available.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 38 / 66
What Happens if There is no Free Frame?
Assume, we had forty frames in physical memory.
And, we run six processes, each of which is ten pages in size, but
actually uses only five pages.
It is possible that each of these processes may suddenly try to use
all ten of its pages: resulting in a need for sixty frames when only
forty are available.
Increasing the degree of multiprogramming: over-allocating memory
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 38 / 66
Over-Allocation of Memory
While a user process is executing, a page fault occurs.
The OS determines where the desired page is residing on the disk.
But, it finds that there are no free frames on the free-frame list.
Need for page replacement
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 39 / 66
Need For Page Replacement
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 40 / 66
Basic Page Replacement
1 Find the location of the desired page on disk.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 41 / 66
Basic Page Replacement
1 Find the location of the desired page on disk.
2 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.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 41 / 66
Basic Page Replacement
1 Find the location of the desired page on disk.
2 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.
3 Bring the desired page into the (newly) free frame; update the page
and frame tables
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 41 / 66
Basic Page Replacement
1 Find the location of the desired page on disk.
2 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.
3 Bring the desired page into the (newly) free frame; update the page
and frame tables
4 Continue the process by restarting the instruction that caused the
trap.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 41 / 66
Page Replacement
Use modify (dirty) bit to reduce overhead of page transfers - only
modified pages are written to disk.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 42 / 66
Page Faults vs. The Number of Frames
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 43 / 66
Evaluate Page Replacement Algorithms
Evaluate algorithm by running it on a particular string of memory
references (reference string) and computing the number of page
faults on that string.
String is just page numbers, not full addresses.
For example, a reference string could be
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 44 / 66
Page Replacement Algorithms
First-In-First-Out (FIFO) page replacement
Optimal page replacement
Least Recently Used (LRU) page replacement
LRU-Approximation page replacement
Counting-Based page replacement
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 45 / 66
FIFO Page Replacement
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 46 / 66
FIFO Page Replacement
Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
3 frames (3 pages can be in memory at a time per process)
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 47 / 66
FIFO Page Replacement
Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
3 frames (3 pages can be in memory at a time per process)
15 page faults
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 47 / 66
FIFO Belady’s Anomaly
Adding more frames can cause more page faults: Belady’s Anomaly
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 48 / 66
Optimal Page Replacement
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 49 / 66
Optimal Page Replacement
Replace page that will not be used for longest period of time: 9
page fault is optimal for the example.
How do you know this? Can’t read the future
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 50 / 66
Optimal Page Replacement
Replace page that will not be used for longest period of time: 9
page fault is optimal for the example.
How do you know this? Can’t read the future
Used for measuring how well your algorithm performs.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 50 / 66
LRU Page Replacement
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 51 / 66
LRU Page Replacement
Use past knowledge rather than the future.
Replace page that has not been used in the most amount of time
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 52 / 66
LRU Page Replacement
Use past knowledge rather than the future.
Replace page that has not been used in the most amount of time
12 faults: better than FIFO but worse than OPT
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 52 / 66
LRU Page Replacement
Use past knowledge rather than the future.
Replace page that has not been used in the most amount of time
12 faults: better than FIFO but worse than OPT
Generally good algorithm and frequently used
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 52 / 66
LRU Implementation (1/2)
Counter implementation
Every page entry has a counter; every time page is referenced
through this entry, copy the clock into the counter.
When a page needs to be changed, look at the counters to find
smallest value.
Search through table needed.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 53 / 66
LRU Implementation (2/2)
Stack implementation
Keep a stack of page numbers in a double link form.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 54 / 66
LRU Implementation (2/2)
Stack implementation
Keep a stack of page numbers in a double link form.
Page referenced:
• Move it to the top
• Requires 6 pointers to be changed
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 54 / 66
LRU Implementation (2/2)
Stack implementation
Keep a stack of page numbers in a double link form.
Page referenced:
• Move it to the top
• Requires 6 pointers to be changed
No search for replacement.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 54 / 66
LRU Implementation (2/2)
Stack implementation
Keep a stack of page numbers in a double link form.
Page referenced:
• Move it to the top
• Requires 6 pointers to be changed
No search for replacement.
LRU and OPT are cases of stack algorithms with no Belady’s
Anomaly.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 54 / 66
Stack Implementation
Use of a stack to record most recent page references.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 55 / 66
LRU-Approximation
Page Replacement
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 56 / 66
LRU-Approximation Page Replacement
LRU needs special hardware and still slow
Improvements: LRU-Approximation
• Reference bit
• Second-chance algorithm
• Enhanced second-chance algorithm
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 57 / 66
Reference Bit
With each page associate a bit, initially = 0
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 58 / 66
Reference Bit
With each page associate a bit, initially = 0
When page is referenced, bit set to 1
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 58 / 66
Reference Bit
With each page associate a bit, initially = 0
When page is referenced, bit set to 1
Replace any with reference bit = 0 (if one exists)
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 58 / 66
Reference Bit
With each page associate a bit, initially = 0
When page is referenced, bit set to 1
Replace any with reference bit = 0 (if one exists)
We do not know the order
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 58 / 66
Second-Chance Algorithm (1/2)
It is also called clock algorithm.
Generally FIFO, plus hardware-provided reference bit
If page to be replaced has
• Reference bit = 0 → replace it
• Reference bit = 1 then, set reference bit 0, leave page in memory,
and replace next page, subject to same rules.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 59 / 66
Second-Chance Algorithm (2/2)
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 60 / 66
Enhanced Second-Chance Algorithm
Improve algorithm by using reference bit and modify bit
(reference, modify)
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 61 / 66
Enhanced Second-Chance Algorithm
Improve algorithm by using reference bit and modify bit
(reference, modify)
Take ordered pair:
1 (0, 0) neither recently used not modified: best page to replace
2 (0, 1) not recently used but modified: not quite as good, must write
out before replacement
3 (1, 0) recently used but clean: probably will be used again soon
4 (1, 1) recently used and modified: probably will be used again soon
and need to write out before replacement
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 61 / 66
Enhanced Second-Chance Algorithm
Improve algorithm by using reference bit and modify bit
(reference, modify)
Take ordered pair:
1 (0, 0) neither recently used not modified: best page to replace
2 (0, 1) not recently used but modified: not quite as good, must write
out before replacement
3 (1, 0) recently used but clean: probably will be used again soon
4 (1, 1) recently used and modified: probably will be used again soon
and need to write out before replacement
When page replacement called for, use the clock scheme but use
the four classes replace page in lowest non-empty class
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 61 / 66
Enhanced Second-Chance Algorithm
Improve algorithm by using reference bit and modify bit
(reference, modify)
Take ordered pair:
1 (0, 0) neither recently used not modified: best page to replace
2 (0, 1) not recently used but modified: not quite as good, must write
out before replacement
3 (1, 0) recently used but clean: probably will be used again soon
4 (1, 1) recently used and modified: probably will be used again soon
and need to write out before replacement
When page replacement called for, use the clock scheme but use
the four classes replace page in lowest non-empty class
Might need to search circular queue several times.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 61 / 66
Counting Page Replacement
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 62 / 66
Counting Page Replacement
Keep a counter of the number of references that have been made
to each page.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 63 / 66
Counting Page Replacement
Keep a counter of the number of references that have been made
to each page.
Lease Frequently Used (LFU) algorithm: replaces page with smallest
count.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 63 / 66
Counting Page Replacement
Keep a counter of the number of references that have been made
to each page.
Lease Frequently Used (LFU) algorithm: replaces page with smallest
count.
Most Frequently Used (MFU) algorithm: based on the argument
that the page with the smallest count was probably just brought in
and has yet to be used.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 63 / 66
Summary
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 64 / 66
Summary
Partially-loaded programs
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 65 / 66
Summary
Partially-loaded programs
Virtual memory: much larger than physical memory
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 65 / 66
Summary
Partially-loaded programs
Virtual memory: much larger than physical memory
Demand paging similar to paging + swapping
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 65 / 66
Summary
Partially-loaded programs
Virtual memory: much larger than physical memory
Demand paging similar to paging + swapping
Page fault
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 65 / 66
Summary
Partially-loaded programs
Virtual memory: much larger than physical memory
Demand paging similar to paging + swapping
Page fault
Page replacement algorithms:
• FIFO, optimal, LRU, LRU-approximate, counting-based
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 65 / 66
Questions?
Acknowledgements
Some slides were derived from Avi Silberschatz slides.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 66 / 66

More Related Content

What's hot

Memory management
Memory managementMemory management
Memory managementcpjcollege
 
Operating System (Scheduling, Input and Output Management, Memory Management,...
Operating System (Scheduling, Input and Output Management, Memory Management,...Operating System (Scheduling, Input and Output Management, Memory Management,...
Operating System (Scheduling, Input and Output Management, Memory Management,...Project Student
 
CPU Scheduling - Part2
CPU Scheduling - Part2CPU Scheduling - Part2
CPU Scheduling - Part2Amir Payberah
 
Operating Systems - memory management
Operating Systems - memory managementOperating Systems - memory management
Operating Systems - memory managementMukesh Chinta
 
Operating System-Memory Management
Operating System-Memory ManagementOperating System-Memory Management
Operating System-Memory ManagementAkmal Cikmat
 
File System Implementation - Part2
File System Implementation - Part2File System Implementation - Part2
File System Implementation - Part2Amir Payberah
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OSvampugani
 
Operating Systems 1 (9/12) - Memory Management Concepts
Operating Systems 1 (9/12) - Memory Management ConceptsOperating Systems 1 (9/12) - Memory Management Concepts
Operating Systems 1 (9/12) - Memory Management ConceptsPeter Tröger
 
Deterministic Memory Abstraction and Supporting Multicore System Architecture
Deterministic Memory Abstraction and Supporting Multicore System ArchitectureDeterministic Memory Abstraction and Supporting Multicore System Architecture
Deterministic Memory Abstraction and Supporting Multicore System ArchitectureHeechul Yun
 
Memory management ppt
Memory management pptMemory management ppt
Memory management pptManishaJha43
 

What's hot (20)

Virtual memory
Virtual memoryVirtual memory
Virtual memory
 
Memory management
Memory managementMemory management
Memory management
 
Memory management
Memory managementMemory management
Memory management
 
Operating System (Scheduling, Input and Output Management, Memory Management,...
Operating System (Scheduling, Input and Output Management, Memory Management,...Operating System (Scheduling, Input and Output Management, Memory Management,...
Operating System (Scheduling, Input and Output Management, Memory Management,...
 
Linux Memory
Linux MemoryLinux Memory
Linux Memory
 
CPU Scheduling - Part2
CPU Scheduling - Part2CPU Scheduling - Part2
CPU Scheduling - Part2
 
Operating Systems - memory management
Operating Systems - memory managementOperating Systems - memory management
Operating Systems - memory management
 
Chapter 8 - Main Memory
Chapter 8 - Main MemoryChapter 8 - Main Memory
Chapter 8 - Main Memory
 
Main Memory - Part1
Main Memory - Part1Main Memory - Part1
Main Memory - Part1
 
Memory managment
Memory managmentMemory managment
Memory managment
 
Memory Management
Memory ManagementMemory Management
Memory Management
 
Operating System-Memory Management
Operating System-Memory ManagementOperating System-Memory Management
Operating System-Memory Management
 
Storage
StorageStorage
Storage
 
File System Implementation - Part2
File System Implementation - Part2File System Implementation - Part2
File System Implementation - Part2
 
VIRTUAL MEMORY
VIRTUAL MEMORYVIRTUAL MEMORY
VIRTUAL MEMORY
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OS
 
cache
cachecache
cache
 
Operating Systems 1 (9/12) - Memory Management Concepts
Operating Systems 1 (9/12) - Memory Management ConceptsOperating Systems 1 (9/12) - Memory Management Concepts
Operating Systems 1 (9/12) - Memory Management Concepts
 
Deterministic Memory Abstraction and Supporting Multicore System Architecture
Deterministic Memory Abstraction and Supporting Multicore System ArchitectureDeterministic Memory Abstraction and Supporting Multicore System Architecture
Deterministic Memory Abstraction and Supporting Multicore System Architecture
 
Memory management ppt
Memory management pptMemory management ppt
Memory management ppt
 

Viewers also liked

Virtual Memory - Part2
Virtual Memory - Part2Virtual Memory - Part2
Virtual Memory - Part2Amir Payberah
 
Introduction to Operating Systems - Part2
Introduction to Operating Systems - Part2Introduction to Operating Systems - Part2
Introduction to Operating Systems - Part2Amir Payberah
 
Process Management - Part2
Process Management - Part2Process Management - Part2
Process Management - Part2Amir Payberah
 
The Stratosphere Big Data Analytics Platform
The Stratosphere Big Data Analytics PlatformThe Stratosphere Big Data Analytics Platform
The Stratosphere Big Data Analytics PlatformAmir Payberah
 
Data Intensive Computing Frameworks
Data Intensive Computing FrameworksData Intensive Computing Frameworks
Data Intensive Computing FrameworksAmir Payberah
 
File System Interface
File System InterfaceFile System Interface
File System InterfaceAmir Payberah
 
CPU Scheduling - Part1
CPU Scheduling - Part1CPU Scheduling - Part1
CPU Scheduling - Part1Amir Payberah
 
P2P Content Distribution Network
P2P Content Distribution NetworkP2P Content Distribution Network
P2P Content Distribution NetworkAmir Payberah
 
File System Implementation - Part1
File System Implementation - Part1File System Implementation - Part1
File System Implementation - Part1Amir Payberah
 
Virtual Memory and Paging
Virtual Memory and PagingVirtual Memory and Paging
Virtual Memory and PagingEmery Berger
 
Virtual memory
Virtual memoryVirtual memory
Virtual memoryAnuj Modi
 
Process Management - Part3
Process Management - Part3Process Management - Part3
Process Management - Part3Amir Payberah
 
Process Management - Part1
Process Management - Part1Process Management - Part1
Process Management - Part1Amir Payberah
 

Viewers also liked (20)

Virtual Memory - Part2
Virtual Memory - Part2Virtual Memory - Part2
Virtual Memory - Part2
 
Virtual memory ppt
Virtual memory pptVirtual memory ppt
Virtual memory ppt
 
Introduction to Operating Systems - Part2
Introduction to Operating Systems - Part2Introduction to Operating Systems - Part2
Introduction to Operating Systems - Part2
 
Process Management - Part2
Process Management - Part2Process Management - Part2
Process Management - Part2
 
Cloud Computing
Cloud ComputingCloud Computing
Cloud Computing
 
IO Systems
IO SystemsIO Systems
IO Systems
 
Protection
ProtectionProtection
Protection
 
Security
SecuritySecurity
Security
 
The Stratosphere Big Data Analytics Platform
The Stratosphere Big Data Analytics PlatformThe Stratosphere Big Data Analytics Platform
The Stratosphere Big Data Analytics Platform
 
Data Intensive Computing Frameworks
Data Intensive Computing FrameworksData Intensive Computing Frameworks
Data Intensive Computing Frameworks
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
 
File System Interface
File System InterfaceFile System Interface
File System Interface
 
CPU Scheduling - Part1
CPU Scheduling - Part1CPU Scheduling - Part1
CPU Scheduling - Part1
 
P2P Content Distribution Network
P2P Content Distribution NetworkP2P Content Distribution Network
P2P Content Distribution Network
 
File System Implementation - Part1
File System Implementation - Part1File System Implementation - Part1
File System Implementation - Part1
 
Virtual Memory and Paging
Virtual Memory and PagingVirtual Memory and Paging
Virtual Memory and Paging
 
Virtual memory
Virtual memoryVirtual memory
Virtual memory
 
Process Management - Part3
Process Management - Part3Process Management - Part3
Process Management - Part3
 
Process Management - Part1
Process Management - Part1Process Management - Part1
Process Management - Part1
 
Threads
ThreadsThreads
Threads
 

Similar to Virtual Memory - Part1

virtual-memory-130126022841-phpapp01.pdf
virtual-memory-130126022841-phpapp01.pdfvirtual-memory-130126022841-phpapp01.pdf
virtual-memory-130126022841-phpapp01.pdfry54321288
 
Virtual Memory
Virtual MemoryVirtual Memory
Virtual Memoryvampugani
 
Virtual Memory in Operating System
Virtual Memory in Operating SystemVirtual Memory in Operating System
Virtual Memory in Operating SystemMeghaj Mallick
 
Introduction to Operating Systems - Part1
Introduction to Operating Systems - Part1Introduction to Operating Systems - Part1
Introduction to Operating Systems - Part1Amir Payberah
 
Unit 2chapter 2 memory mgmt complete
Unit 2chapter 2  memory mgmt completeUnit 2chapter 2  memory mgmt complete
Unit 2chapter 2 memory mgmt completeKalai Selvi
 
CSI-503 - 9. Virtual Memory
CSI-503 - 9. Virtual MemoryCSI-503 - 9. Virtual Memory
CSI-503 - 9. Virtual Memoryghayour abbas
 
virtualmemory-111228090333-phpapp02 (1).pdf
virtualmemory-111228090333-phpapp02 (1).pdfvirtualmemory-111228090333-phpapp02 (1).pdf
virtualmemory-111228090333-phpapp02 (1).pdfArchanaPandiyan
 
Lecture 8- Virtual Memory Final.pptx
Lecture 8- Virtual Memory Final.pptxLecture 8- Virtual Memory Final.pptx
Lecture 8- Virtual Memory Final.pptxAmanuelmergia
 
Paging +Algorithem+Segmentation+memory management
Paging +Algorithem+Segmentation+memory managementPaging +Algorithem+Segmentation+memory management
Paging +Algorithem+Segmentation+memory managementkazim Hussain
 
MegaStore and Spanner
MegaStore and SpannerMegaStore and Spanner
MegaStore and SpannerAmir Payberah
 
CS304PC:Computer Organization and Architecture Session 29 Memory organization...
CS304PC:Computer Organization and Architecture Session 29 Memory organization...CS304PC:Computer Organization and Architecture Session 29 Memory organization...
CS304PC:Computer Organization and Architecture Session 29 Memory organization...Asst.prof M.Gokilavani
 
Mca ii os u-4 memory management
Mca  ii  os u-4 memory managementMca  ii  os u-4 memory management
Mca ii os u-4 memory managementRai University
 

Similar to Virtual Memory - Part1 (20)

virtual-memory-130126022841-phpapp01.pdf
virtual-memory-130126022841-phpapp01.pdfvirtual-memory-130126022841-phpapp01.pdf
virtual-memory-130126022841-phpapp01.pdf
 
Chubby
ChubbyChubby
Chubby
 
Lecture 43
Lecture 43Lecture 43
Lecture 43
 
Virtual Memory
Virtual MemoryVirtual Memory
Virtual Memory
 
Bigtable
BigtableBigtable
Bigtable
 
Google File System
Google File SystemGoogle File System
Google File System
 
virtual memory
virtual memoryvirtual memory
virtual memory
 
Virtual Memory in Operating System
Virtual Memory in Operating SystemVirtual Memory in Operating System
Virtual Memory in Operating System
 
Introduction to Operating Systems - Part1
Introduction to Operating Systems - Part1Introduction to Operating Systems - Part1
Introduction to Operating Systems - Part1
 
Unit 2chapter 2 memory mgmt complete
Unit 2chapter 2  memory mgmt completeUnit 2chapter 2  memory mgmt complete
Unit 2chapter 2 memory mgmt complete
 
CSI-503 - 9. Virtual Memory
CSI-503 - 9. Virtual MemoryCSI-503 - 9. Virtual Memory
CSI-503 - 9. Virtual Memory
 
virtualmemory-111228090333-phpapp02 (1).pdf
virtualmemory-111228090333-phpapp02 (1).pdfvirtualmemory-111228090333-phpapp02 (1).pdf
virtualmemory-111228090333-phpapp02 (1).pdf
 
Lecture 8- Virtual Memory Final.pptx
Lecture 8- Virtual Memory Final.pptxLecture 8- Virtual Memory Final.pptx
Lecture 8- Virtual Memory Final.pptx
 
CS6401 OPERATING SYSTEMS Unit 3
CS6401 OPERATING SYSTEMS Unit 3CS6401 OPERATING SYSTEMS Unit 3
CS6401 OPERATING SYSTEMS Unit 3
 
virtual memory
virtual memoryvirtual memory
virtual memory
 
Paging +Algorithem+Segmentation+memory management
Paging +Algorithem+Segmentation+memory managementPaging +Algorithem+Segmentation+memory management
Paging +Algorithem+Segmentation+memory management
 
MegaStore and Spanner
MegaStore and SpannerMegaStore and Spanner
MegaStore and Spanner
 
Os unit 2
Os unit 2Os unit 2
Os unit 2
 
CS304PC:Computer Organization and Architecture Session 29 Memory organization...
CS304PC:Computer Organization and Architecture Session 29 Memory organization...CS304PC:Computer Organization and Architecture Session 29 Memory organization...
CS304PC:Computer Organization and Architecture Session 29 Memory organization...
 
Mca ii os u-4 memory management
Mca  ii  os u-4 memory managementMca  ii  os u-4 memory management
Mca ii os u-4 memory management
 

More from Amir Payberah

The Spark Big Data Analytics Platform
The Spark Big Data Analytics PlatformThe Spark Big Data Analytics Platform
The Spark Big Data Analytics PlatformAmir Payberah
 
Linux Module Programming
Linux Module ProgrammingLinux Module Programming
Linux Module ProgrammingAmir Payberah
 
Process Synchronization - Part2
Process Synchronization -  Part2Process Synchronization -  Part2
Process Synchronization - Part2Amir Payberah
 
Process Synchronization - Part1
Process Synchronization -  Part1Process Synchronization -  Part1
Process Synchronization - Part1Amir Payberah
 
Introduction to Operating Systems - Part3
Introduction to Operating Systems - Part3Introduction to Operating Systems - Part3
Introduction to Operating Systems - Part3Amir Payberah
 
Graph processing - Powergraph and GraphX
Graph processing - Powergraph and GraphXGraph processing - Powergraph and GraphX
Graph processing - Powergraph and GraphXAmir Payberah
 

More from Amir Payberah (7)

The Spark Big Data Analytics Platform
The Spark Big Data Analytics PlatformThe Spark Big Data Analytics Platform
The Spark Big Data Analytics Platform
 
Linux Module Programming
Linux Module ProgrammingLinux Module Programming
Linux Module Programming
 
Process Synchronization - Part2
Process Synchronization -  Part2Process Synchronization -  Part2
Process Synchronization - Part2
 
Process Synchronization - Part1
Process Synchronization -  Part1Process Synchronization -  Part1
Process Synchronization - Part1
 
Introduction to Operating Systems - Part3
Introduction to Operating Systems - Part3Introduction to Operating Systems - Part3
Introduction to Operating Systems - Part3
 
Mesos and YARN
Mesos and YARNMesos and YARN
Mesos and YARN
 
Graph processing - Powergraph and GraphX
Graph processing - Powergraph and GraphXGraph processing - Powergraph and GraphX
Graph processing - Powergraph and GraphX
 

Recently uploaded

(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 

Recently uploaded (20)

(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 

Virtual Memory - Part1

  • 1. Virtual Memory (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 1 / 66
  • 2. Motivation and Background Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 2 / 66
  • 3. Motivation A program needs to be in memory to execute. But, entire program rarely used. • Error code, unusual routines, large data structures Entire program code not needed at same time. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 3 / 66
  • 4. Motivation Consider ability to execute partially-loaded program. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 4 / 66
  • 5. Motivation Consider ability to execute partially-loaded program. Program no longer constrained by limits of physical memory. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 4 / 66
  • 6. Motivation Consider ability to execute partially-loaded program. Program no longer constrained by limits of physical memory. Each program takes less memory while running: more programs run at the same time. • Increased CPU utilization and throughput with no increase in response time or turnaround time. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 4 / 66
  • 7. Motivation Consider ability to execute partially-loaded program. Program no longer constrained by limits of physical memory. Each program takes less memory while running: more programs run at the same time. • Increased CPU utilization and throughput with no increase in response time or turnaround time. Less I/O needed to load or swap programs into memory: each user program runs faster. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 4 / 66
  • 8. Virtual Memory Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 5 / 66
  • 9. Virtual Memory (1/2) Separation of user logical memory from physical memory. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 6 / 66
  • 10. Virtual Memory (1/2) Separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 6 / 66
  • 11. Virtual Memory (1/2) Separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution. Logical address space can therefore be much larger than physical address space. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 6 / 66
  • 12. Virtual Memory (1/2) Separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution. Logical address space can therefore be much larger than physical address space. More programs running concurrently. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 6 / 66
  • 13. Virtual Memory (2/2) Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 7 / 66
  • 14. Virtual Address Space (1/3) Virtual address space: logical view of how process is stored in mem- ory. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 8 / 66
  • 15. Virtual Address Space (1/3) Virtual address space: logical view of how process is stored in mem- ory. Usually starts at address 0, contiguous addresses until end of space. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 8 / 66
  • 16. Virtual Address Space (1/3) Virtual address space: logical view of how process is stored in mem- ory. Usually starts at address 0, contiguous addresses until end of space. Meanwhile, physical memory organized in page frames. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 8 / 66
  • 17. Virtual Address Space (1/3) Virtual address space: logical view of how process is stored in mem- ory. Usually starts at address 0, contiguous addresses until end of space. Meanwhile, physical memory organized in page frames. MMU must map logical to physical. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 8 / 66
  • 18. Virtual Address Space (2/3) Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 9 / 66
  • 19. Virtual Address Space (3/3) The heap grows upward in memory, and the stack grows downward. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 10 / 66
  • 20. Virtual Address Space (3/3) The heap grows upward in memory, and the stack grows downward. The hole between the heap and the stack is part of the virtual address space, but will require actual physical pages only if the heap or stack grows. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 10 / 66
  • 21. Virtual Address Space (3/3) The heap grows upward in memory, and the stack grows downward. The hole between the heap and the stack is part of the virtual address space, but will require actual physical pages only if the heap or stack grows. Virtual address spaces that include holes are known as sparse address spaces. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 10 / 66
  • 22. Heap vs. Stack Stack • Stores local variables created by functions. • New variables are pushed onto the stack. • When a function exits, all of the its pushed variables are freed. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 11 / 66
  • 23. Heap vs. Stack Stack • Stores local variables created by functions. • New variables are pushed onto the stack. • When a function exits, all of the its pushed variables are freed. Heap • Used for dynamic allocation. • Use malloc() or calloc() to allocate memory on the heap. • Use free() to deallocate the memory. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 11 / 66
  • 24. Virtual Memory Benefits Separating logical memory from physical memory. Page sharing. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 12 / 66
  • 25. Demand Paging Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 13 / 66
  • 26. Demand Paging (1/2) Could bring entire process into memory at load time, or bring a page into memory only when it is needed. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 14 / 66
  • 27. Demand Paging (1/2) Could bring entire process into memory at load time, or bring a page into memory only when it is needed. A demand-paging system is similar to a paging system with swap- ping. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 14 / 66
  • 28. Demand Paging (2/2) Rather than swapping the entire process into memory, we use a lazy swapper. A lazy swapper never swaps a page into memory unless that page will be needed. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 15 / 66
  • 29. Demand Paging (2/2) Rather than swapping the entire process into memory, we use a lazy swapper. A lazy swapper never swaps a page into memory unless that page will be needed. A swapper that deals with pages is a pager. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 15 / 66
  • 30. Basic Concepts The pager guesses which pages will be used before swapping out again. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 16 / 66
  • 31. Basic Concepts The pager guesses which pages will be used before swapping out again. The pager brings only the needed pages into memory. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 16 / 66
  • 32. Basic Concepts The pager guesses which pages will be used before swapping out again. The pager brings only the needed pages into memory. Uses valid-invalid bit to distinguish between the pages that are in memory and the pages that are on the disk? v: memory resident i: not in memory Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 16 / 66
  • 33. Page Fault Access to a page marked invalid causes a page fault. Causing a trap to the OS: brings the desired page into memory. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 17 / 66
  • 34. Handling Page Fault (1/6) Check an internal table for the process to determine whether the reference was a valid or an invalid memory access. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 18 / 66
  • 35. Handling Page Fault (2/6) If the reference was invalid, we terminate the process. If it was valid but we have not yet brought in that page, we now page it in. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 19 / 66
  • 36. Handling Page Fault (3/6) We find a free frame. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 20 / 66
  • 37. Handling Page Fault (4/6) We schedule a disk operation to read the desired page into the newly allocated frame. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 21 / 66
  • 38. Handling Page Fault (5/6) When the disk read is complete, we modify the internal table kept with the process and the page table to indicate that the page is now in memory. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 22 / 66
  • 39. Handling Page Fault (6/6) We restart the instruction that was interrupted by the trap. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 23 / 66
  • 40. Aspects of Demand Paging (1/2) Extreme case: start process with no pages in memory. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 24 / 66
  • 41. Aspects of Demand Paging (1/2) Extreme case: start process with no pages in memory. OS sets instruction pointer to first instruction of process, non- memory-resident: page fault Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 24 / 66
  • 42. Aspects of Demand Paging (1/2) Extreme case: start process with no pages in memory. OS sets instruction pointer to first instruction of process, non- memory-resident: page fault And for every other process pages on first access. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 24 / 66
  • 43. Aspects of Demand Paging (1/2) Extreme case: start process with no pages in memory. OS sets instruction pointer to first instruction of process, non- memory-resident: page fault And for every other process pages on first access. Pure demand paging Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 24 / 66
  • 44. Aspects of Demand Paging (2/2) A program could access several new pages of memory with each instruction execution: multiple page faults per instruction. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 25 / 66
  • 45. Aspects of Demand Paging (2/2) A program could access several new pages of memory with each instruction execution: multiple page faults per instruction. Unacceptable system performance Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 25 / 66
  • 46. Aspects of Demand Paging (2/2) A program could access several new pages of memory with each instruction execution: multiple page faults per instruction. Unacceptable system performance Locality of reference results in reasonable performance from demand paging. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 25 / 66
  • 47. Demand Paging Hardware The hardware to support demand paging is the same as the hardware for paging and swapping: • Page table with valid-invalid bit • Secondary memory with swap space The ability to restart any instruction after a page fault. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 26 / 66
  • 48. Stages in Demand Paging (1/3) 1. Trap to the OS. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 27 / 66
  • 49. Stages in Demand Paging (1/3) 1. Trap to the OS. 2. Save the user registers and process state. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 27 / 66
  • 50. Stages in Demand Paging (1/3) 1. Trap to the OS. 2. Save the user registers and process state. 3. Determine that the interrupt was a page fault. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 27 / 66
  • 51. Stages in Demand Paging (1/3) 1. Trap to the OS. 2. Save the user registers and process state. 3. Determine that the interrupt was a page fault. 4. Check that the page reference was legal and determine the loca- tion of the page on the disk. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 27 / 66
  • 52. Stages in Demand Paging (2/3) 5. Issue a read from the disk to a free frame: • Wait in a queue for this device until the read request is serviced. • Wait for the device seek and/or latency time. • Begin the transfer of the page to a free frame. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 28 / 66
  • 53. Stages in Demand Paging (2/3) 5. Issue a read from the disk to a free frame: • Wait in a queue for this device until the read request is serviced. • Wait for the device seek and/or latency time. • Begin the transfer of the page to a free frame. 6. While waiting, allocate the CPU to some other user. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 28 / 66
  • 54. Stages in Demand Paging (2/3) 5. Issue a read from the disk to a free frame: • Wait in a queue for this device until the read request is serviced. • Wait for the device seek and/or latency time. • Begin the transfer of the page to a free frame. 6. While waiting, allocate the CPU to some other user. 7. Receive an interrupt from the disk I/O subsystem (I/O com- pleted). Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 28 / 66
  • 55. Stages in Demand Paging (3/3) 8. Save the registers and process state for the other user. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 29 / 66
  • 56. Stages in Demand Paging (3/3) 8. Save the registers and process state for the other user. 9. Determine that the interrupt was from the disk. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 29 / 66
  • 57. Stages in Demand Paging (3/3) 8. Save the registers and process state for the other user. 9. Determine that the interrupt was from the disk. 10. Correct the page table and other tables to show page is now in memory. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 29 / 66
  • 58. Stages in Demand Paging (3/3) 8. Save the registers and process state for the other user. 9. Determine that the interrupt was from the disk. 10. Correct the page table and other tables to show page is now in memory. 11. Wait for the CPU to be allocated to this process again. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 29 / 66
  • 59. Stages in Demand Paging (3/3) 8. Save the registers and process state for the other user. 9. Determine that the interrupt was from the disk. 10. Correct the page table and other tables to show page is now in memory. 11. Wait for the CPU to be allocated to this process again. 12. Restore the user registers, process state, and new page table, and then resume the interrupted instruction. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 29 / 66
  • 60. Performance of Demand Paging Probability of a page fault: 0 ≤ p ≤ 1 Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 30 / 66
  • 61. Performance of Demand Paging Probability of a page fault: 0 ≤ p ≤ 1 If p = 0: no page faults If p = 1: every reference is a fault Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 30 / 66
  • 62. Performance of Demand Paging Probability of a page fault: 0 ≤ p ≤ 1 If p = 0: no page faults If p = 1: every reference is a fault Effective Access Time (EAT) EAT = (1 − p) × memory access + p × page fault time Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 30 / 66
  • 63. Demand Paging Example Memory access time = 200 nanoseconds Average page-fault service time = 8 milliseconds Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 31 / 66
  • 64. Demand Paging Example Memory access time = 200 nanoseconds Average page-fault service time = 8 milliseconds EAT = (1 − p) × 200 + p × 8, 000, 000 = 200 + p × 7, 999, 800 Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 31 / 66
  • 65. Demand Paging Example Memory access time = 200 nanoseconds Average page-fault service time = 8 milliseconds EAT = (1 − p) × 200 + p × 8, 000, 000 = 200 + p × 7, 999, 800 If p = 0.001, then EAT = 8.2microseconds: this is a slowdown by a factor of 40. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 31 / 66
  • 66. Demand Paging Example Memory access time = 200 nanoseconds Average page-fault service time = 8 milliseconds EAT = (1 − p) × 200 + p × 8, 000, 000 = 200 + p × 7, 999, 800 If p = 0.001, then EAT = 8.2microseconds: this is a slowdown by a factor of 40. If want performance degradation < 10 percent: • 220 > 200 + p × 7, 999, 800 • 20 > p × 7, 999, 800 • p < .0000025: less than one page fault in every 400,000 memory accesses Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 31 / 66
  • 67. Demand Paging Optimization Swap space I/O faster than file system I/O even if on the same device. • Swap allocated in larger chunks, less management needed than file system. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 32 / 66
  • 68. Demand Paging Optimization Swap space I/O faster than file system I/O even if on the same device. • Swap allocated in larger chunks, less management needed than file system. Copy entire process image to swap space at process load time. • Then page in and out of swap space. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 32 / 66
  • 69. Demand Paging Optimization Swap space I/O faster than file system I/O even if on the same device. • Swap allocated in larger chunks, less management needed than file system. Copy entire process image to swap space at process load time. • Then page in and out of swap space. Demand page in from program binary on disk, but discard rather than paging out when freeing frame. • Pages not associated with a file, i.e., heap and stack (anonymous memory) till need to write to swap space. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 32 / 66
  • 70. Copy-on-Write Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 33 / 66
  • 71. Copy-on-Write Copy-on-Write allows both parent and child processes to initially share the same pages in memory. If either process modifies a shared page, only then is the page copied. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 34 / 66
  • 72. Copy-on-Write Example Before modification Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 35 / 66
  • 73. Copy-on-Write Example Before modification After modification Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 35 / 66
  • 74. More About Copy-on-Write Copy-on-Write allows more efficient process creation as only modi- fied pages are copied. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 36 / 66
  • 75. More About Copy-on-Write Copy-on-Write allows more efficient process creation as only modi- fied pages are copied. Free pages are allocated from a pool of zero-fill-on-demand pages. • Pool should always have free frames for fast demand page execution. • Zero-fill-on-demand pages have been zeroed-out before being allocated, thus erasing the previous contents. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 36 / 66
  • 76. More About Copy-on-Write Copy-on-Write allows more efficient process creation as only modi- fied pages are copied. Free pages are allocated from a pool of zero-fill-on-demand pages. • Pool should always have free frames for fast demand page execution. • Zero-fill-on-demand pages have been zeroed-out before being allocated, thus erasing the previous contents. vfork() variation on fork() does not use Copy-on-Write: with vfork(), the parent process is suspended, and the child process uses the address space of the parent. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 36 / 66
  • 77. Page Replacement Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 37 / 66
  • 78. What Happens if There is no Free Frame? Assume, we had forty frames in physical memory. And, we run six processes, each of which is ten pages in size, but actually uses only five pages. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 38 / 66
  • 79. What Happens if There is no Free Frame? Assume, we had forty frames in physical memory. And, we run six processes, each of which is ten pages in size, but actually uses only five pages. It is possible that each of these processes may suddenly try to use all ten of its pages: resulting in a need for sixty frames when only forty are available. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 38 / 66
  • 80. What Happens if There is no Free Frame? Assume, we had forty frames in physical memory. And, we run six processes, each of which is ten pages in size, but actually uses only five pages. It is possible that each of these processes may suddenly try to use all ten of its pages: resulting in a need for sixty frames when only forty are available. Increasing the degree of multiprogramming: over-allocating memory Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 38 / 66
  • 81. Over-Allocation of Memory While a user process is executing, a page fault occurs. The OS determines where the desired page is residing on the disk. But, it finds that there are no free frames on the free-frame list. Need for page replacement Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 39 / 66
  • 82. Need For Page Replacement Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 40 / 66
  • 83. Basic Page Replacement 1 Find the location of the desired page on disk. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 41 / 66
  • 84. Basic Page Replacement 1 Find the location of the desired page on disk. 2 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. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 41 / 66
  • 85. Basic Page Replacement 1 Find the location of the desired page on disk. 2 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. 3 Bring the desired page into the (newly) free frame; update the page and frame tables Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 41 / 66
  • 86. Basic Page Replacement 1 Find the location of the desired page on disk. 2 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. 3 Bring the desired page into the (newly) free frame; update the page and frame tables 4 Continue the process by restarting the instruction that caused the trap. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 41 / 66
  • 87. Page Replacement Use modify (dirty) bit to reduce overhead of page transfers - only modified pages are written to disk. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 42 / 66
  • 88. Page Faults vs. The Number of Frames Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 43 / 66
  • 89. Evaluate Page Replacement Algorithms Evaluate algorithm by running it on a particular string of memory references (reference string) and computing the number of page faults on that string. String is just page numbers, not full addresses. For example, a reference string could be 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 44 / 66
  • 90. Page Replacement Algorithms First-In-First-Out (FIFO) page replacement Optimal page replacement Least Recently Used (LRU) page replacement LRU-Approximation page replacement Counting-Based page replacement Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 45 / 66
  • 91. FIFO Page Replacement Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 46 / 66
  • 92. FIFO Page Replacement Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1 3 frames (3 pages can be in memory at a time per process) Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 47 / 66
  • 93. FIFO Page Replacement Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1 3 frames (3 pages can be in memory at a time per process) 15 page faults Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 47 / 66
  • 94. FIFO Belady’s Anomaly Adding more frames can cause more page faults: Belady’s Anomaly Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 48 / 66
  • 95. Optimal Page Replacement Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 49 / 66
  • 96. Optimal Page Replacement Replace page that will not be used for longest period of time: 9 page fault is optimal for the example. How do you know this? Can’t read the future Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 50 / 66
  • 97. Optimal Page Replacement Replace page that will not be used for longest period of time: 9 page fault is optimal for the example. How do you know this? Can’t read the future Used for measuring how well your algorithm performs. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 50 / 66
  • 98. LRU Page Replacement Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 51 / 66
  • 99. LRU Page Replacement Use past knowledge rather than the future. Replace page that has not been used in the most amount of time Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 52 / 66
  • 100. LRU Page Replacement Use past knowledge rather than the future. Replace page that has not been used in the most amount of time 12 faults: better than FIFO but worse than OPT Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 52 / 66
  • 101. LRU Page Replacement Use past knowledge rather than the future. Replace page that has not been used in the most amount of time 12 faults: better than FIFO but worse than OPT Generally good algorithm and frequently used Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 52 / 66
  • 102. LRU Implementation (1/2) Counter implementation Every page entry has a counter; every time page is referenced through this entry, copy the clock into the counter. When a page needs to be changed, look at the counters to find smallest value. Search through table needed. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 53 / 66
  • 103. LRU Implementation (2/2) Stack implementation Keep a stack of page numbers in a double link form. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 54 / 66
  • 104. LRU Implementation (2/2) Stack implementation Keep a stack of page numbers in a double link form. Page referenced: • Move it to the top • Requires 6 pointers to be changed Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 54 / 66
  • 105. LRU Implementation (2/2) Stack implementation Keep a stack of page numbers in a double link form. Page referenced: • Move it to the top • Requires 6 pointers to be changed No search for replacement. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 54 / 66
  • 106. LRU Implementation (2/2) Stack implementation Keep a stack of page numbers in a double link form. Page referenced: • Move it to the top • Requires 6 pointers to be changed No search for replacement. LRU and OPT are cases of stack algorithms with no Belady’s Anomaly. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 54 / 66
  • 107. Stack Implementation Use of a stack to record most recent page references. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 55 / 66
  • 108. LRU-Approximation Page Replacement Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 56 / 66
  • 109. LRU-Approximation Page Replacement LRU needs special hardware and still slow Improvements: LRU-Approximation • Reference bit • Second-chance algorithm • Enhanced second-chance algorithm Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 57 / 66
  • 110. Reference Bit With each page associate a bit, initially = 0 Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 58 / 66
  • 111. Reference Bit With each page associate a bit, initially = 0 When page is referenced, bit set to 1 Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 58 / 66
  • 112. Reference Bit With each page associate a bit, initially = 0 When page is referenced, bit set to 1 Replace any with reference bit = 0 (if one exists) Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 58 / 66
  • 113. Reference Bit With each page associate a bit, initially = 0 When page is referenced, bit set to 1 Replace any with reference bit = 0 (if one exists) We do not know the order Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 58 / 66
  • 114. Second-Chance Algorithm (1/2) It is also called clock algorithm. Generally FIFO, plus hardware-provided reference bit If page to be replaced has • Reference bit = 0 → replace it • Reference bit = 1 then, set reference bit 0, leave page in memory, and replace next page, subject to same rules. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 59 / 66
  • 115. Second-Chance Algorithm (2/2) Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 60 / 66
  • 116. Enhanced Second-Chance Algorithm Improve algorithm by using reference bit and modify bit (reference, modify) Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 61 / 66
  • 117. Enhanced Second-Chance Algorithm Improve algorithm by using reference bit and modify bit (reference, modify) Take ordered pair: 1 (0, 0) neither recently used not modified: best page to replace 2 (0, 1) not recently used but modified: not quite as good, must write out before replacement 3 (1, 0) recently used but clean: probably will be used again soon 4 (1, 1) recently used and modified: probably will be used again soon and need to write out before replacement Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 61 / 66
  • 118. Enhanced Second-Chance Algorithm Improve algorithm by using reference bit and modify bit (reference, modify) Take ordered pair: 1 (0, 0) neither recently used not modified: best page to replace 2 (0, 1) not recently used but modified: not quite as good, must write out before replacement 3 (1, 0) recently used but clean: probably will be used again soon 4 (1, 1) recently used and modified: probably will be used again soon and need to write out before replacement When page replacement called for, use the clock scheme but use the four classes replace page in lowest non-empty class Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 61 / 66
  • 119. Enhanced Second-Chance Algorithm Improve algorithm by using reference bit and modify bit (reference, modify) Take ordered pair: 1 (0, 0) neither recently used not modified: best page to replace 2 (0, 1) not recently used but modified: not quite as good, must write out before replacement 3 (1, 0) recently used but clean: probably will be used again soon 4 (1, 1) recently used and modified: probably will be used again soon and need to write out before replacement When page replacement called for, use the clock scheme but use the four classes replace page in lowest non-empty class Might need to search circular queue several times. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 61 / 66
  • 120. Counting Page Replacement Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 62 / 66
  • 121. Counting Page Replacement Keep a counter of the number of references that have been made to each page. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 63 / 66
  • 122. Counting Page Replacement Keep a counter of the number of references that have been made to each page. Lease Frequently Used (LFU) algorithm: replaces page with smallest count. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 63 / 66
  • 123. Counting Page Replacement Keep a counter of the number of references that have been made to each page. Lease Frequently Used (LFU) algorithm: replaces page with smallest count. Most Frequently Used (MFU) algorithm: based on the argument that the page with the smallest count was probably just brought in and has yet to be used. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 63 / 66
  • 124. Summary Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 64 / 66
  • 125. Summary Partially-loaded programs Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 65 / 66
  • 126. Summary Partially-loaded programs Virtual memory: much larger than physical memory Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 65 / 66
  • 127. Summary Partially-loaded programs Virtual memory: much larger than physical memory Demand paging similar to paging + swapping Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 65 / 66
  • 128. Summary Partially-loaded programs Virtual memory: much larger than physical memory Demand paging similar to paging + swapping Page fault Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 65 / 66
  • 129. Summary Partially-loaded programs Virtual memory: much larger than physical memory Demand paging similar to paging + swapping Page fault Page replacement algorithms: • FIFO, optimal, LRU, LRU-approximate, counting-based Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 65 / 66
  • 130. Questions? Acknowledgements Some slides were derived from Avi Silberschatz slides. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 66 / 66