virtual memory - part1

Post on 10-Aug-2015

166 Views

Category:

Software

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Virtual Memory (Part I)

Amir H. Payberahamir@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

I A program needs to be in memory to execute.

I But, entire program rarely used.• Error code, unusual routines, large data structures

I Entire program code not needed at same time.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 3 / 66

Motivation

I Consider ability to execute partially-loaded program.

I Program no longer constrained by limits of physical memory.

I Each program takes less memory while running: more programs runat the same time.

• Increased CPU utilization and throughput with no increase inresponse time or turnaround time.

I Less I/O needed to load or swap programs into memory: each userprogram runs faster.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 4 / 66

Motivation

I Consider ability to execute partially-loaded program.

I Program no longer constrained by limits of physical memory.

I Each program takes less memory while running: more programs runat the same time.

• Increased CPU utilization and throughput with no increase inresponse time or turnaround time.

I Less I/O needed to load or swap programs into memory: each userprogram runs faster.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 4 / 66

Motivation

I Consider ability to execute partially-loaded program.

I Program no longer constrained by limits of physical memory.

I Each program takes less memory while running: more programs runat the same time.

• Increased CPU utilization and throughput with no increase inresponse time or turnaround time.

I Less I/O needed to load or swap programs into memory: each userprogram runs faster.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 4 / 66

Motivation

I Consider ability to execute partially-loaded program.

I Program no longer constrained by limits of physical memory.

I Each program takes less memory while running: more programs runat the same time.

• Increased CPU utilization and throughput with no increase inresponse time or turnaround time.

I Less I/O needed to load or swap programs into memory: each userprogram 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)

I Separation of user logical memory from physical memory.

I Only part of the program needs to be in memory for execution.

I Logical address space can therefore be much larger than physicaladdress space.

I More programs running concurrently.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 6 / 66

Virtual Memory (1/2)

I Separation of user logical memory from physical memory.

I Only part of the program needs to be in memory for execution.

I Logical address space can therefore be much larger than physicaladdress space.

I More programs running concurrently.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 6 / 66

Virtual Memory (1/2)

I Separation of user logical memory from physical memory.

I Only part of the program needs to be in memory for execution.

I Logical address space can therefore be much larger than physicaladdress space.

I More programs running concurrently.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 6 / 66

Virtual Memory (1/2)

I Separation of user logical memory from physical memory.

I Only part of the program needs to be in memory for execution.

I Logical address space can therefore be much larger than physicaladdress space.

I 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)

I Virtual address space: logical view of how process is stored in mem-ory.

I Usually starts at address 0, contiguous addresses until end of space.

I Meanwhile, physical memory organized in page frames.

I MMU must map logical to physical.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 8 / 66

Virtual Address Space (1/3)

I Virtual address space: logical view of how process is stored in mem-ory.

I Usually starts at address 0, contiguous addresses until end of space.

I Meanwhile, physical memory organized in page frames.

I MMU must map logical to physical.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 8 / 66

Virtual Address Space (1/3)

I Virtual address space: logical view of how process is stored in mem-ory.

I Usually starts at address 0, contiguous addresses until end of space.

I Meanwhile, physical memory organized in page frames.

I MMU must map logical to physical.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 8 / 66

Virtual Address Space (1/3)

I Virtual address space: logical view of how process is stored in mem-ory.

I Usually starts at address 0, contiguous addresses until end of space.

I Meanwhile, physical memory organized in page frames.

I 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)

I The heap grows upward in memory, and the stack grows downward.

I The hole between the heap and the stack is part of the virtualaddress space, but will require actual physical pages only if the heapor stack grows.

I Virtual address spaces that include holes are known as sparse addressspaces.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 10 / 66

Virtual Address Space (3/3)

I The heap grows upward in memory, and the stack grows downward.

I The hole between the heap and the stack is part of the virtualaddress space, but will require actual physical pages only if the heapor stack grows.

I Virtual address spaces that include holes are known as sparse addressspaces.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 10 / 66

Virtual Address Space (3/3)

I The heap grows upward in memory, and the stack grows downward.

I The hole between the heap and the stack is part of the virtualaddress space, but will require actual physical pages only if the heapor stack grows.

I Virtual address spaces that include holes are known as sparse addressspaces.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 10 / 66

Heap vs. Stack

I 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.

I 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

Heap vs. Stack

I 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.

I 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

I Separating logical memory from physical memory.

I 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)

I Could bring entire process into memory at load time, or bring a pageinto memory only when it is needed.

I 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 (1/2)

I Could bring entire process into memory at load time, or bring a pageinto memory only when it is needed.

I 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)

I Rather than swapping the entire process into memory, we use a lazyswapper.

I A lazy swapper never swaps a page into memory unless that pagewill be needed.

I A swapper that deals with pagesis a pager.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 15 / 66

Demand Paging (2/2)

I Rather than swapping the entire process into memory, we use a lazyswapper.

I A lazy swapper never swaps a page into memory unless that pagewill be needed.

I A swapper that deals with pagesis a pager.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 15 / 66

Basic Concepts

I The pager guesses which pages will be used before swapping outagain.

I The pager brings only the needed pages into memory.

I Uses valid-invalid bit to distinguish between thepages that are in memory andthe pages that are on the disk?v: memory residenti: not in memory

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 16 / 66

Basic Concepts

I The pager guesses which pages will be used before swapping outagain.

I The pager brings only the needed pages into memory.

I Uses valid-invalid bit to distinguish between thepages that are in memory andthe pages that are on the disk?v: memory residenti: not in memory

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 16 / 66

Basic Concepts

I The pager guesses which pages will be used before swapping outagain.

I The pager brings only the needed pages into memory.

I Uses valid-invalid bit to distinguish between thepages that are in memory andthe pages that are on the disk?v: memory residenti: not in memory

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 16 / 66

Page Fault

I Access to a page marked invalid causes a page fault.

I 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)

I Check an internal table for the process to determine whether thereference 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)

I If the reference was invalid, we terminate the process.

I If it was valid but we have not yet brought in that page, we nowpage it in.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 19 / 66

Handling Page Fault (3/6)

I We find a free frame.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 20 / 66

Handling Page Fault (4/6)

I We schedule a disk operation to read the desired page into the newlyallocated frame.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 21 / 66

Handling Page Fault (5/6)

I When the disk read is complete, we modify the internal table keptwith the process and the page table to indicate that the page is nowin memory.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 22 / 66

Handling Page Fault (6/6)

I 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)

I Extreme case: start process with no pages in memory.

I OS sets instruction pointer to first instruction of process, non-memory-resident: page fault

I And for every other process pages on first access.

I Pure demand paging

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 24 / 66

Aspects of Demand Paging (1/2)

I Extreme case: start process with no pages in memory.

I OS sets instruction pointer to first instruction of process, non-memory-resident: page fault

I And for every other process pages on first access.

I Pure demand paging

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 24 / 66

Aspects of Demand Paging (1/2)

I Extreme case: start process with no pages in memory.

I OS sets instruction pointer to first instruction of process, non-memory-resident: page fault

I And for every other process pages on first access.

I Pure demand paging

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 24 / 66

Aspects of Demand Paging (1/2)

I Extreme case: start process with no pages in memory.

I OS sets instruction pointer to first instruction of process, non-memory-resident: page fault

I And for every other process pages on first access.

I Pure demand paging

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 24 / 66

Aspects of Demand Paging (2/2)

I A program could access several new pages of memory with eachinstruction execution: multiple page faults per instruction.

I Unacceptable system performance

I Locality of reference results in reasonable performance from demandpaging.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 25 / 66

Aspects of Demand Paging (2/2)

I A program could access several new pages of memory with eachinstruction execution: multiple page faults per instruction.

I Unacceptable system performance

I Locality of reference results in reasonable performance from demandpaging.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 25 / 66

Aspects of Demand Paging (2/2)

I A program could access several new pages of memory with eachinstruction execution: multiple page faults per instruction.

I Unacceptable system performance

I Locality of reference results in reasonable performance from demandpaging.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 25 / 66

Demand Paging Hardware

I The hardware to support demand paging is the same as the hardwarefor paging and swapping:

• Page table with valid-invalid bit• Secondary memory with swap space

I 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)

I 1. Trap to the OS.

I 2. Save the user registers and process state.

I 3. Determine that the interrupt was a page fault.

I 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 (1/3)

I 1. Trap to the OS.

I 2. Save the user registers and process state.

I 3. Determine that the interrupt was a page fault.

I 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 (1/3)

I 1. Trap to the OS.

I 2. Save the user registers and process state.

I 3. Determine that the interrupt was a page fault.

I 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 (1/3)

I 1. Trap to the OS.

I 2. Save the user registers and process state.

I 3. Determine that the interrupt was a page fault.

I 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)

I 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.

I 6. While waiting, allocate the CPU to some other user.

I 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 (2/3)

I 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.

I 6. While waiting, allocate the CPU to some other user.

I 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 (2/3)

I 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.

I 6. While waiting, allocate the CPU to some other user.

I 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)

I 8. Save the registers and process state for the other user.

I 9. Determine that the interrupt was from the disk.

I 10. Correct the page table and other tables to show page is now inmemory.

I 11. Wait for the CPU to be allocated to this process again.

I 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

Stages in Demand Paging (3/3)

I 8. Save the registers and process state for the other user.

I 9. Determine that the interrupt was from the disk.

I 10. Correct the page table and other tables to show page is now inmemory.

I 11. Wait for the CPU to be allocated to this process again.

I 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

Stages in Demand Paging (3/3)

I 8. Save the registers and process state for the other user.

I 9. Determine that the interrupt was from the disk.

I 10. Correct the page table and other tables to show page is now inmemory.

I 11. Wait for the CPU to be allocated to this process again.

I 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

Stages in Demand Paging (3/3)

I 8. Save the registers and process state for the other user.

I 9. Determine that the interrupt was from the disk.

I 10. Correct the page table and other tables to show page is now inmemory.

I 11. Wait for the CPU to be allocated to this process again.

I 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

Stages in Demand Paging (3/3)

I 8. Save the registers and process state for the other user.

I 9. Determine that the interrupt was from the disk.

I 10. Correct the page table and other tables to show page is now inmemory.

I 11. Wait for the CPU to be allocated to this process again.

I 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

I Probability of a page fault: 0 ≤ p ≤ 1

I If p = 0: no page faults

I If p = 1: every reference is a fault

I Effective Access Time (EAT)

I EAT = (1− p)×memory access + p × page fault time

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 30 / 66

Performance of Demand Paging

I Probability of a page fault: 0 ≤ p ≤ 1

I If p = 0: no page faults

I If p = 1: every reference is a fault

I Effective Access Time (EAT)

I EAT = (1− p)×memory access + p × page fault time

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 30 / 66

Performance of Demand Paging

I Probability of a page fault: 0 ≤ p ≤ 1

I If p = 0: no page faults

I If p = 1: every reference is a fault

I Effective Access Time (EAT)

I EAT = (1− p)×memory access + p × page fault time

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 30 / 66

Demand Paging Example

I Memory access time = 200 nanoseconds

I Average page-fault service time = 8 milliseconds

I EAT = (1− p)× 200 + p × 8, 000, 000 = 200 + p × 7, 999, 800

I If p = 0.001, then EAT = 8.2microseconds: this is a slowdown bya factor of 40.

I 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 Example

I Memory access time = 200 nanoseconds

I Average page-fault service time = 8 milliseconds

I EAT = (1− p)× 200 + p × 8, 000, 000 = 200 + p × 7, 999, 800

I If p = 0.001, then EAT = 8.2microseconds: this is a slowdown bya factor of 40.

I 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 Example

I Memory access time = 200 nanoseconds

I Average page-fault service time = 8 milliseconds

I EAT = (1− p)× 200 + p × 8, 000, 000 = 200 + p × 7, 999, 800

I If p = 0.001, then EAT = 8.2microseconds: this is a slowdown bya factor of 40.

I 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 Example

I Memory access time = 200 nanoseconds

I Average page-fault service time = 8 milliseconds

I EAT = (1− p)× 200 + p × 8, 000, 000 = 200 + p × 7, 999, 800

I If p = 0.001, then EAT = 8.2microseconds: this is a slowdown bya factor of 40.

I 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

I Swap space I/O faster than file system I/O even if on the samedevice.

• Swap allocated in larger chunks, less management needed than filesystem.

I Copy entire process image to swap space at process load time.• Then page in and out of swap space.

I Demand page in from program binary on disk, but discard ratherthan paging out when freeing frame.

• Pages not associated with a file, i.e., heap and stack (anonymousmemory) till need to write to swap space.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 32 / 66

Demand Paging Optimization

I Swap space I/O faster than file system I/O even if on the samedevice.

• Swap allocated in larger chunks, less management needed than filesystem.

I Copy entire process image to swap space at process load time.• Then page in and out of swap space.

I Demand page in from program binary on disk, but discard ratherthan paging out when freeing frame.

• Pages not associated with a file, i.e., heap and stack (anonymousmemory) till need to write to swap space.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 32 / 66

Demand Paging Optimization

I Swap space I/O faster than file system I/O even if on the samedevice.

• Swap allocated in larger chunks, less management needed than filesystem.

I Copy entire process image to swap space at process load time.• Then page in and out of swap space.

I Demand page in from program binary on disk, but discard ratherthan paging out when freeing frame.

• Pages not associated with a file, i.e., heap and stack (anonymousmemory) 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

I Copy-on-Write allows both parent and child processes to initiallyshare the same pages in memory.

I 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

After 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

I Copy-on-Write allows more efficient process creation as only modi-fied pages are copied.

I 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.

I vfork() variation on fork() does not use Copy-on-Write: withvfork(), the parent process is suspended, and the child processuses the address space of the parent.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 36 / 66

More About Copy-on-Write

I Copy-on-Write allows more efficient process creation as only modi-fied pages are copied.

I 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.

I vfork() variation on fork() does not use Copy-on-Write: withvfork(), the parent process is suspended, and the child processuses the address space of the parent.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 36 / 66

More About Copy-on-Write

I Copy-on-Write allows more efficient process creation as only modi-fied pages are copied.

I 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.

I vfork() variation on fork() does not use Copy-on-Write: withvfork(), the parent process is suspended, and the child processuses 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?

I Assume, we had forty frames in physical memory.

I And, we run six processes, each of which is ten pages in size, butactually uses only five pages.

I It is possible that each of these processes may suddenly try to useall ten of its pages: resulting in a need for sixty frames when onlyforty are available.

I Increasing the degree of multiprogramming: over-allocating memory

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 38 / 66

What Happens if There is no Free Frame?

I Assume, we had forty frames in physical memory.

I And, we run six processes, each of which is ten pages in size, butactually uses only five pages.

I It is possible that each of these processes may suddenly try to useall ten of its pages: resulting in a need for sixty frames when onlyforty are available.

I Increasing the degree of multiprogramming: over-allocating memory

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 38 / 66

What Happens if There is no Free Frame?

I Assume, we had forty frames in physical memory.

I And, we run six processes, each of which is ten pages in size, butactually uses only five pages.

I It is possible that each of these processes may suddenly try to useall ten of its pages: resulting in a need for sixty frames when onlyforty are available.

I Increasing the degree of multiprogramming: over-allocating memory

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 38 / 66

Over-Allocation of Memory

I While a user process is executing, a page fault occurs.

I The OS determines where the desired page is residing on the disk.

I But, it finds that there are no free frames on the free-frame list.

I 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.

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 pageand frame tables

4 Continue the process by restarting the instruction that caused thetrap.

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 pageand frame tables

4 Continue the process by restarting the instruction that caused thetrap.

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 pageand frame tables

4 Continue the process by restarting the instruction that caused thetrap.

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 pageand frame tables

4 Continue the process by restarting the instruction that caused thetrap.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 41 / 66

Page Replacement

I Use modify (dirty) bit to reduce overhead of page transfers - onlymodified 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

I Evaluate algorithm by running it on a particular string of memoryreferences (reference string) and computing the number of pagefaults on that string.

I String is just page numbers, not full addresses.

I For example, a reference string could be7, 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

I First-In-First-Out (FIFO) page replacement

I Optimal page replacement

I Least Recently Used (LRU) page replacement

I LRU-Approximation page replacement

I 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

I Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1

I 3 frames (3 pages can be in memory at a time per process)

I 15 page faults

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 47 / 66

FIFO Page Replacement

I Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1

I 3 frames (3 pages can be in memory at a time per process)

I 15 page faults

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 47 / 66

FIFO Belady’s Anomaly

I 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

I Replace page that will not be used for longest period of time: 9page fault is optimal for the example.

I How do you know this? Can’t read the future

I Used for measuring how well your algorithm performs.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 50 / 66

Optimal Page Replacement

I Replace page that will not be used for longest period of time: 9page fault is optimal for the example.

I How do you know this? Can’t read the future

I 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

I Use past knowledge rather than the future.

I Replace page that has not been used in the most amount of time

I 12 faults: better than FIFO but worse than OPT

I Generally good algorithm and frequently used

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 52 / 66

LRU Page Replacement

I Use past knowledge rather than the future.

I Replace page that has not been used in the most amount of time

I 12 faults: better than FIFO but worse than OPT

I Generally good algorithm and frequently used

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 52 / 66

LRU Page Replacement

I Use past knowledge rather than the future.

I Replace page that has not been used in the most amount of time

I 12 faults: better than FIFO but worse than OPT

I Generally good algorithm and frequently used

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 52 / 66

LRU Implementation (1/2)

I Counter implementation

I Every page entry has a counter; every time page is referencedthrough this entry, copy the clock into the counter.

I When a page needs to be changed, look at the counters to findsmallest value.

I Search through table needed.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 53 / 66

LRU Implementation (2/2)

I Stack implementation

I Keep a stack of page numbers in a double link form.

I Page referenced:• Move it to the top• Requires 6 pointers to be changed

I No search for replacement.

I LRU and OPT are cases of stack algorithms with no Belady’sAnomaly.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 54 / 66

LRU Implementation (2/2)

I Stack implementation

I Keep a stack of page numbers in a double link form.

I Page referenced:• Move it to the top• Requires 6 pointers to be changed

I No search for replacement.

I LRU and OPT are cases of stack algorithms with no Belady’sAnomaly.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 54 / 66

LRU Implementation (2/2)

I Stack implementation

I Keep a stack of page numbers in a double link form.

I Page referenced:• Move it to the top• Requires 6 pointers to be changed

I No search for replacement.

I LRU and OPT are cases of stack algorithms with no Belady’sAnomaly.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 54 / 66

LRU Implementation (2/2)

I Stack implementation

I Keep a stack of page numbers in a double link form.

I Page referenced:• Move it to the top• Requires 6 pointers to be changed

I No search for replacement.

I LRU and OPT are cases of stack algorithms with no Belady’sAnomaly.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 54 / 66

Stack Implementation

I Use of a stack to record most recent page references.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 55 / 66

LRU-ApproximationPage Replacement

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 56 / 66

LRU-Approximation Page Replacement

I LRU needs special hardware and still slow

I 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

I With each page associate a bit, initially = 0

I When page is referenced, bit set to 1

I Replace any with reference bit = 0 (if one exists)

I We do not know the order

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 58 / 66

Reference Bit

I With each page associate a bit, initially = 0

I When page is referenced, bit set to 1

I Replace any with reference bit = 0 (if one exists)

I We do not know the order

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 58 / 66

Reference Bit

I With each page associate a bit, initially = 0

I When page is referenced, bit set to 1

I Replace any with reference bit = 0 (if one exists)

I We do not know the order

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 58 / 66

Reference Bit

I With each page associate a bit, initially = 0

I When page is referenced, bit set to 1

I Replace any with reference bit = 0 (if one exists)

I We do not know the order

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 58 / 66

Second-Chance Algorithm (1/2)

I It is also called clock algorithm.

I Generally FIFO, plus hardware-provided reference bit

I 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

I Improve algorithm by using reference bit and modify bit(reference, modify)

I Take ordered pair:1 (0, 0) neither recently used not modified: best page to replace2 (0, 1) not recently used but modified: not quite as good, must write

out before replacement3 (1, 0) recently used but clean: probably will be used again soon4 (1, 1) recently used and modified: probably will be used again soon

and need to write out before replacement

I When page replacement called for, use the clock scheme but usethe four classes replace page in lowest non-empty class

I Might need to search circular queue several times.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 61 / 66

Enhanced Second-Chance Algorithm

I Improve algorithm by using reference bit and modify bit(reference, modify)

I Take ordered pair:1 (0, 0) neither recently used not modified: best page to replace2 (0, 1) not recently used but modified: not quite as good, must write

out before replacement3 (1, 0) recently used but clean: probably will be used again soon4 (1, 1) recently used and modified: probably will be used again soon

and need to write out before replacement

I When page replacement called for, use the clock scheme but usethe four classes replace page in lowest non-empty class

I Might need to search circular queue several times.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 61 / 66

Enhanced Second-Chance Algorithm

I Improve algorithm by using reference bit and modify bit(reference, modify)

I Take ordered pair:1 (0, 0) neither recently used not modified: best page to replace2 (0, 1) not recently used but modified: not quite as good, must write

out before replacement3 (1, 0) recently used but clean: probably will be used again soon4 (1, 1) recently used and modified: probably will be used again soon

and need to write out before replacement

I When page replacement called for, use the clock scheme but usethe four classes replace page in lowest non-empty class

I Might need to search circular queue several times.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 61 / 66

Enhanced Second-Chance Algorithm

I Improve algorithm by using reference bit and modify bit(reference, modify)

I Take ordered pair:1 (0, 0) neither recently used not modified: best page to replace2 (0, 1) not recently used but modified: not quite as good, must write

out before replacement3 (1, 0) recently used but clean: probably will be used again soon4 (1, 1) recently used and modified: probably will be used again soon

and need to write out before replacement

I When page replacement called for, use the clock scheme but usethe four classes replace page in lowest non-empty class

I 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

I Keep a counter of the number of references that have been madeto each page.

I Lease Frequently Used (LFU) algorithm: replaces page with smallestcount.

I Most Frequently Used (MFU) algorithm: based on the argumentthat the page with the smallest count was probably just brought inand has yet to be used.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 63 / 66

Counting Page Replacement

I Keep a counter of the number of references that have been madeto each page.

I Lease Frequently Used (LFU) algorithm: replaces page with smallestcount.

I Most Frequently Used (MFU) algorithm: based on the argumentthat the page with the smallest count was probably just brought inand has yet to be used.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 63 / 66

Counting Page Replacement

I Keep a counter of the number of references that have been madeto each page.

I Lease Frequently Used (LFU) algorithm: replaces page with smallestcount.

I Most Frequently Used (MFU) algorithm: based on the argumentthat the page with the smallest count was probably just brought inand 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

I Partially-loaded programs

I Virtual memory: much larger than physical memory

I Demand paging similar to paging + swapping

I Page fault

I Page replacement algorithms:• FIFO, optimal, LRU, LRU-approximate, counting-based

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 65 / 66

Summary

I Partially-loaded programs

I Virtual memory: much larger than physical memory

I Demand paging similar to paging + swapping

I Page fault

I Page replacement algorithms:• FIFO, optimal, LRU, LRU-approximate, counting-based

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 65 / 66

Summary

I Partially-loaded programs

I Virtual memory: much larger than physical memory

I Demand paging similar to paging + swapping

I Page fault

I Page replacement algorithms:• FIFO, optimal, LRU, LRU-approximate, counting-based

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 65 / 66

Summary

I Partially-loaded programs

I Virtual memory: much larger than physical memory

I Demand paging similar to paging + swapping

I Page fault

I Page replacement algorithms:• FIFO, optimal, LRU, LRU-approximate, counting-based

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/19 65 / 66

Summary

I Partially-loaded programs

I Virtual memory: much larger than physical memory

I Demand paging similar to paging + swapping

I Page fault

I 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

top related