Virtual Memory Management G. Anuradha Ref:- Galvin.

35
Virtual Memory Management G. Anuradha Ref:- Galvin

Transcript of Virtual Memory Management G. Anuradha Ref:- Galvin.

Page 1: Virtual Memory Management G. Anuradha Ref:- Galvin.

Virtual Memory Management

G. AnuradhaRef:- Galvin

Page 2: Virtual Memory Management G. Anuradha Ref:- Galvin.

Background• Virtual memory – 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– Allows address spaces to be shared by several processes– Allows for more efficient process creation

• Virtual memory can be implemented via:– Demand paging – Demand segmentation

Page 3: Virtual Memory Management G. Anuradha Ref:- Galvin.
Page 4: Virtual Memory Management G. Anuradha Ref:- Galvin.
Page 5: Virtual Memory Management G. Anuradha Ref:- Galvin.
Page 6: Virtual Memory Management G. Anuradha Ref:- Galvin.
Page 7: Virtual Memory Management G. Anuradha Ref:- Galvin.

Demand Paging• Bring a page into memory only when it is needed

– Less I/O needed– Less memory needed – Faster response– More users

• Page is needed reference to it– invalid reference abort– not-in-memory bring to memory

• Lazy swapper – never swaps a page into memory unless page will be needed– Swapper that deals with pages is a pager

Page 8: Virtual Memory Management G. Anuradha Ref:- Galvin.

Transfer of a Paged Memory to Contiguous Disk Space

Page 9: Virtual Memory Management G. Anuradha Ref:- Galvin.

Valid-Invalid Bit• With each page table entry a valid–invalid bit is associated

(v in-memory, i not-in-memory)• Initially valid–invalid bit is set to i on all entries• Example of a page table snapshot:

• During address translation, if valid–invalid bit in page table entry is I page fault

vvv

v

i

ii

….

Frame # valid-invalid bit

page table

Page 10: Virtual Memory Management G. Anuradha Ref:- Galvin.

Page Table When Some Pages Are Not in Main Memory

Page 11: Virtual Memory Management G. Anuradha Ref:- Galvin.

Page Fault• If there is a reference to a page, first reference to that page will

trap to operating system: page fault1. Operating system looks at another table to decide:

– Invalid reference abort– Just not in memory

2. Get empty frame3. Swap page into frame4. Reset tables5. Set validation bit = v6. Restart the instruction that caused the page fault

Page 12: Virtual Memory Management G. Anuradha Ref:- Galvin.

Steps in Handling a Page Fault

Page 13: Virtual Memory Management G. Anuradha Ref:- Galvin.

Demand Paging

• Pure Demand Paging:- – Begin a process with no pages in the memory– Bring the pages only as and when required

• Hardware support for demand paging– Page table– Secondary memory

Page 14: Virtual Memory Management G. Anuradha Ref:- Galvin.

Performance of Demand Paging• Page Fault Rate 0 p 1.0

– if p = 0 no page faults – if p = 1, every reference is a fault

• Effective Access Time (EAT)EAT = (1 – p) x memory access

+ p (page fault overhead )

Page 15: Virtual Memory Management G. Anuradha Ref:- Galvin.

Sequence of events when a page fault occurs

1. Trap to OS2. Save the user registers and process states3. Determine the interrupt that causes a page fault4. Check that the page referred was legal and

determine the location of the page on the disk5. Issue a read from the disk to free frame

1. Wait in a queue until serviced2. Wait for device seek/latency time3. Begin the transfer of page to a free frame

Page 16: Virtual Memory Management G. Anuradha Ref:- Galvin.

6. While waiting, allocate the CPU to some other user7. Receive an interrupt from the disk I/O subsystem 8. Save the registers and process state for the other user9. Determine that the interrupt was from disk10. Correct page table entries11. Wait for CPU to be allocated to the process again12. Restore the user registers, process state and then resume

the interrupted instruction

Sequence of events when a page fault occurs

Page 17: Virtual Memory Management G. Anuradha Ref:- Galvin.

Effective Access Time (EAT)EAT = (1 – p) x memory access

+ p (page fault overhead + swap page out + swap page in + restart overhead )

Page 18: Virtual Memory Management G. Anuradha Ref:- Galvin.

Demand Paging Example• Memory access time = 200 nanoseconds

• Average page-fault service time = 8 milliseconds

• Effective Access Time (EAT) = (1 – p) x 200 + p (8 milliseconds) = (1 – p) x 200 + p x 8,000,000

= 200 + p x 7,999,800

• If one access out of 1,000 causes a page fault, then EAT = 8.2 microseconds. This is a slowdown by a factor of 40!!(PAGE FAULT SHOULD BE KEPT AT HE MINIMUM POSSIBLE VALUE IN ORDER TO

IMPROVE THE ACCESS TIME)

Page 19: Virtual Memory Management G. Anuradha Ref:- Galvin.

Copy-on-write

• Process creation can be initiated by demand paging

• However fork command bypasses the need of demand paging

• Fork() command created a copy of the parent’s address space for the child

• If many child process uses the exec() system call a copy-on-write method is used

Page 20: Virtual Memory Management G. Anuradha Ref:- Galvin.

Before Process 1 Modifies Page C

Page 21: Virtual Memory Management G. Anuradha Ref:- Galvin.

After Process 1 Modifies Page C

• A copy of page C is created• Child process will modify its copied page and

not the page belonging to the parent process• If pages are not modified then its shared by

parent and child processes.

Page 22: Virtual Memory Management G. Anuradha Ref:- Galvin.

Page Replacement

Page 23: Virtual Memory Management G. Anuradha Ref:- Galvin.

Basic Page Replacement1. 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

3. Bring the desired page into the (newly) free frame; update the page and frame tables

4. Restart the process

Page 24: Virtual Memory Management G. Anuradha Ref:- Galvin.

Page Replacement

Use modify (dirty) bit to reduce overhead of page transfers – only modified pages are written to disk

Page 25: Virtual Memory Management G. Anuradha Ref:- Galvin.

Page Replacement contd…

• Two major problems must be solved to implement demand paging– Frame allocation algorithm– Page-replacement algorithm

• Want lowest page-fault rateEvaluate algorithm by running it on a particular

string of memory references (reference string) and computing the number of page faults on that string

Page 26: Virtual Memory Management G. Anuradha Ref:- Galvin.

First-In-First-Out (FIFO) Algorithm• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5• 3 frames (3 pages can be in memory at a time per process)

• 4 frames

• Belady’s Anomaly: more frames more page faults

1

2

3

1

2

3

4

1

2

5

3

4

9 page faults

1

2

3

1

2

3

5

1

2

4

5 10 page faults

44 3

Page 27: Virtual Memory Management G. Anuradha Ref:- Galvin.

FIFO Page Replacement

Page 28: Virtual Memory Management G. Anuradha Ref:- Galvin.

FIFO Illustrating Belady’s Anomaly

Page 29: Virtual Memory Management G. Anuradha Ref:- Galvin.

Optimal Algorithm• Replace page that will not be used for longest period of time• 4 frames example

1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

• How do you know this?• Used for measuring how well your algorithm performs

1

2

3

4

6 page faults

4 5

Page 30: Virtual Memory Management G. Anuradha Ref:- Galvin.

Optimal Page Replacement

Difficult to implement becos it requires future knowledge of reference string

Page 31: Virtual Memory Management G. Anuradha Ref:- Galvin.

Least Recently Used (LRU) Algorithm

• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

• 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

determine which are to change

5

2

4

3

1

2

3

4

1

2

5

4

1

2

5

3

1

2

4

3

Page 32: Virtual Memory Management G. Anuradha Ref:- Galvin.

LRU Page Replacement

Page 33: Virtual Memory Management G. Anuradha Ref:- Galvin.

LRU Algorithm (Cont.)• Stack implementation – keep a stack of page numbers in a double link form:

– Page referenced:• move it to the top• Most recently used page is always at the top of the stack and least

recently used page is always at the bottom

– Can be implemented by a double linked list with a head pointer and a tail pointer

– Both LRU and ORU comes under the class of algos called as stack algorithm

– Does not suffer from Belady’s Anamoly

Page 34: Virtual Memory Management G. Anuradha Ref:- Galvin.

Important questions1. What is paging? Explain the structure of page table2. What is belady’s algorithm? Explain LRU, FIFO, OPR

algos. Which algorithm suffers from Belady’s anomaly?3. Short note on page fault handling4. Explain virtual memory and demand paging5. Draw and explain paging hardware with TLB6. Explain paging in detail. Describe how logical address

converted to physical address 7. Explain how memory management takes place in Linux

Page 35: Virtual Memory Management G. Anuradha Ref:- Galvin.

Important questions