Virtual Memory: Understanding Page Faults and Replacement Algorithms, Study notes of Computer Science

An in-depth look into virtual memory, focusing on page faults and various page replacement algorithms. It covers the concept of virtual memory, the need for page replacement, and different algorithms such as fifo, nru, lru, and opt. The document also discusses belady's anomaly and its impact on paging behavior.

Typology: Study notes

Pre 2010

Uploaded on 09/24/2009

koofers-user-dcx
koofers-user-dcx 🇺🇸

10 documents

1 / 39

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Operating Systems
Operating Systems
CMPSC 473
CMPSC 473
Virtual Memory
Virtual Memory
March 18, 2008 - Lecture
March 18, 2008 - Lecture
16
16
Instructor: Trent Jaeger
Instructor: Trent Jaeger
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27

Partial preview of the text

Download Virtual Memory: Understanding Page Faults and Replacement Algorithms and more Study notes Computer Science in PDF only on Docsity!

Operating SystemsOperating Systems

CMPSC 473 CMPSC 473

Virtual Memory Virtual Memory

March 18, 2008 - Lecture March 18, 2008 - Lecture 1616

Instructor: Trent Jaeger Instructor: Trent Jaeger

  • Last class:
    • Paging
  • Today:
    • Virtual Memory

Example

Physical

Memory

(16 KB)

Virtual Address Space-1 (1 MB) Virtual Address Space-2 (1 MB) Virtual Address Space-n (1 MB)

Page Faults

• If a Page-table mapping indicates an absence of the

page in physical memory, hardware raises a “Page-

Fault”.

• OS traps this fault and the interrupt handler services

the fault by initiating a disk-read request.

• Once page is brought in from disk to main memory,

page-table entry is updated and the process which

faulted is restarted.

  • May involve replacing another page and invalidating the

corresponding page-table entry.

Page Fault

  • If there is a reference to a page, first reference to that page will trap to operating system: - page fault
  • Operating system looks at another table to decide:
    • Invalid reference -- abort
    • Just not in memory
  • Get empty frame
  • Swap page into frame
  • Reset tables
  • Set validation bit = v
  • Restart the instruction that caused the page fault

Steps in Handling a Page Fault

Demand Paging Example

  • Memory access time = 200 nanoseconds
  • Average page-fault service time = 8 milliseconds
  • EAT = (1 – p) x 200 + p (8 milliseconds) = (1 – p x 200 + p x 8,000, = 200 + p x 7,999,
  • 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!!

Putting it all together!

int A[2K];

int B[2k];

main() {

int i, j, p;

p = malloc(16K);

VAS before execution

0x0… 0 0xf…f Page Table

PC

Heap Pointer Stack Pointer All Point To Null (i.e. fault the first time)

4K page size

No Page table Entries here

Page Replacement

• When bringing in a page, something has to

be evicted.

• What should we evict? – page replacement

algorithm.

Optimal Page Replacement Algorithm

  • Why optimal?
    • No other algorithm can have # of page faults lower than this, for a given page reference stream.
  • Algorithm:
    • At any point, amongst the given pages in memory, evict the one whose first reference from now is the furthest.

Problem with OPT

  • Not implementable!
  • Requires us to know the future.
  • But it has the best page fault behavior
  • How do we approach OPT?

1. First-in First-out

• Maintain a linked list of pages in the order they

were brought into PM.

• On a page fault, evict the one at the head.

• Put the newly brought in page (from disk) at tail of

this list.

• Problems:

  • Reference String: 1,2,3,4,1,1,5,1,1,…
  • Page fault at (5) would replace (1)!
  • Need to know what is in recent use!
  1. Second Chance Replacement or Clock Algorithm
  • Same as FIFO, except you skip over the pages whose reference bit is set, resetting this bit, and moving those pages to end of list.
  • Implementation: 1

Evict

4. Least Recently Used

  • Order the list of physical memory pages in decreasing order

of recency of usage.

  • Replace the page at the tail.
  • Problem:
    • This list will need to be updated on each memory reference.
    • Asking the h/w to do this is ridiculous!
  • Solution: Approximate LRU