VIRTUAL MEMORY MANAGEMENT

Post on 04-Jan-2016

29 views 2 download

Tags:

description

VIRTUAL MEMORY MANAGEMENT. DEMAND PAGING. Pages loaded on demand is known as demand paging Never bring a page into memory until it is required VM handler loads only one page of a program to start with. Three concepts are important in understanding operation of demand paging Page fault - PowerPoint PPT Presentation

Transcript of VIRTUAL MEMORY MANAGEMENT

VIRTUAL MEMORY MANAGEMENT

DEMAND PAGING• Pages loaded on demand is known as demand paging

– Never bring a page into memory until it is required• VM handler loads only one page of a program to start with.• Three concepts are important in understanding operation of demand paging

– Page fault– Page in and page out– Page replacement

Page fault

• When a program tries to access locations that are not in memory, the hardware traps to operating system (page fault).

• The operating system reads desired page into memory and restarts the process as though the page had always been in memory

Steps handling page fault1. Check the page table to determine whether the reference is valid or invalid memory

access2. If the reference was invalid, terminate the process. If it was valid, but have not yet

brought in that page, page in the latter.3. Find a free frame(from the free frame list)4. schedule a disk operation to read the desired page into the newly allocated frame5. When the disk read is complete, modify the internal table kept with the process and the

page table to indicate that the page is now in memory.6. Restart the instruction that was interrupted by the illegal address trap. The process can

now access the page as though it had always been in memory

Page In & Page Out• While initiating execution of a program, an area is allocated on the paging device for

its legal address space and its code and data are copied into the space. This space is known as swap space of a program.

• When a page fault occurs during reference to a page say Pi, the VM handler finds a free page frame in memory and loads Pi in it. This is known as Page In operation for Pi.

• If no free frame exist in memory some page Pk existing in memory is written out into the swap space to free its page frame. This is a Page Out operation for Pk.

• Page I/O.• The term Page Traffic is used to describe movements of pages in and out of memory.

Page I/O

Page outoperation

Page inoperation

Swap space of the program

PkPage beingWritten out

PiPage beingloadedin

•A program which encounters a page fault becomes blocked tillThe required page is loaded in memory. Hence the execution Performance of the program suffers

Transfer of a Paged Memory to Contiguous Disk Space

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 Table When Some Pages Are Not in Main Memory

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

Steps in Handling a Page Fault

Page Replacement• Prevent over-allocation of memory by modifying page-fault service routine to include

page replacement

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

• Page replacement completes separation between logical memory and physical memory – large virtual memory can be provided on a smaller physical memory

Need For Page Replacement

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 Replacement

Page Replacement Algorithms

• Want lowest page-fault rate

• Evaluate algorithm by running it on a particular string of memory references (reference string) and computing the number of page faults on that string

• In all our examples, the reference string is

0100, 0432, 0101,0612,0102,0103,0104,0101,0611,

0102,0103,0104,0101,0610,0103,0104,0101,0609

100 bytes per page is reduced to following reference string

1,4,1,6,1,6,1,6,1,6

Belady’s anomaly

• It is found that under FIFO page replacement, certain page reference pattern cause more page faults when the number of page frames allocated to a process is increased

• By Belady, Shedler and Nelson• This phenomenon is known as FIFO anomaly or Belady’s anomaly

• Try doing the following eg:

• Reference string

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

Page fault for 4 frames and 3 frames

Graph of Page Faults Versus The Number of Frames

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

FIFO Page Replacement

Optimal Algorithm• The principle of optimality states that to obtain optimum performance, replace the page that will

not be used for a 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

Optimal Page Replacement

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

LRU Page Replacement

LRU Algorithm- using stack

• 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

Use Of A Stack to Record The Most Recent Page References