Virtual Memory 2.0

  • Upload
    mmmaya

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

  • 8/10/2019 Virtual Memory 2.0

    1/52

    Course 10: Virtual Memory

  • 8/10/2019 Virtual Memory 2.0

    2/52

    9.2 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Course 9: Review

    Memory management concept: a logicaladdress space is bound to a separatephysical address space

    Logical address generated by theCPU; also referred to as virtualaddress

    Physical address address seen bythe memory unit

    Logical and physical addresses are thesame in compile-time and load-timeaddress-binding schemes; logical (virtual)

    and physical addresses differ in execution-time address-binding scheme

  • 8/10/2019 Virtual Memory 2.0

    3/52

    9.3 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Course 9:

    Review

    -

    MMU/ classic model

    MMU - Hardware devicethat maps virtual tophysical address

    In classic MMU scheme,the value in the relocationregister is added to everyaddress generated by auser process at the time itis sent to memory

    The user program dealswith logicaladdresses; it

    never sees the realphysical addresses

  • 8/10/2019 Virtual Memory 2.0

    4/52

    9.4 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Course 9:

    Review

    -

    Paging

    Page table is kept in mainmemory

    In this scheme everydata/instruction accessrequires two memoryaccesses. One for thepage table and one forthe data/instruction.

    The two memory accessproblem can be solved bythe use of a special fast-

    lookup hardware cachecalled associativememory or translationlook-aside buffers(TLBs)

  • 8/10/2019 Virtual Memory 2.0

    5/52

    9.5 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Course 9:

    Review

    -

    Paging

  • 8/10/2019 Virtual Memory 2.0

    6/52

    9.6 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Course 9:

    Review

    -

    Segmentation

    Logical address consistsof a two tuple: ,

    Segment table mapstwo-dimensional physicaladdresses; each tableentry has:

    base contains thestarting physicaladdress where thesegments reside in

    memory limit specifies the

    length of the segment

  • 8/10/2019 Virtual Memory 2.0

    7/529.7 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Course 9: Review - Segmentation

    A reference to byte 53 ofsegment 2 is mapped ontolocation 4300 + 53 = 4353

    A reference to segment 3rdbyte 852, is mapped to 3200(the base of segment 3) +852 = 4052

    A reference to byte 1222 ofsegment 0 would result in atrap to the operating system,as this segment is only 1,000bytes long.

  • 8/10/2019 Virtual Memory 2.0

    8/529.8 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Chapter 10: Virtual Memory

    Background

    Demand Paging

    Copy-on-Write

    Page Replacement

    Allocation of Frames

  • 8/10/2019 Virtual Memory 2.0

    9/529.9 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Objectives

    To describe the benefits of a virtual memory system

    To explain the concepts of demand paging, page-replacementalgorithms, and allocation of page frames

  • 8/10/2019 Virtual Memory 2.0

    10/529.10 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Background

  • 8/10/2019 Virtual Memory 2.0

    11/529.11 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Background

    Virtual memory separation of user logical memory from physicalmemory.

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

    Logical address space can therefore be much larger thanphysical 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

  • 8/10/2019 Virtual Memory 2.0

    12/529.12 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Virtual Memory That is Larger Than Physical Memory

  • 8/10/2019 Virtual Memory 2.0

    13/529.13 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Shared Library Using Virtual Memory

  • 8/10/2019 Virtual Memory 2.0

    14/529.14 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Demand paging

  • 8/10/2019 Virtual Memory 2.0

    15/529.15 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    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 willbe needed

    Swapper that deals with pages is a pager

  • 8/10/2019 Virtual Memory 2.0

    16/529.16 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Transfer of a Paged Memory to Contiguous Disk Space

  • 8/10/2019 Virtual Memory 2.0

    17/529.17 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Valid-Invalid Bit

    With each page table entry a validinvalid bit is associated

    (v in-memory, i not-in-memory) Initially validinvalid bit is set to i on all entries

    Example of a page table snapshot:

    During address translation, if validinvalid bit in page table entry

    is I page fault

    v

    vv

    v

    i

    i

    i

    .

    Frame # valid-invalid bit

    page table

  • 8/10/2019 Virtual Memory 2.0

    18/529.18 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Page Table When Some Pages Are Not in Main Memory

  • 8/10/2019 Virtual Memory 2.0

    19/529.19 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Page Fault

    If there is a reference to a page, first reference to thatpage will trap to operating system:

    page fault

    1. Operating system looks at another table to decide:

    Invalid reference abort

    Just not in memory2. Get empty frame

    3. Swap page into frame

    4. Reset tables

    5. Set validation bit = v

    6. Restart the instruction that caused the page fault

  • 8/10/2019 Virtual Memory 2.0

    20/529.20 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Steps in Handling a Page Fault

  • 8/10/2019 Virtual Memory 2.0

    21/529.21 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    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 = (1p) x memory access+ p (page fault overhead

    + swap page out

    + swap page in

    + restart overhead

    )

  • 8/10/2019 Virtual Memory 2.0

    22/529.22 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    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,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!!

  • 8/10/2019 Virtual Memory 2.0

    23/52

    9.23 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Process creation copy on write

  • 8/10/2019 Virtual Memory 2.0

    24/52

    9.24 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Copy-on-Write

    Copy-on-Write (COW) allows both parent and child processes toinitially share the same pages in memory

    If either process modifies a shared page, only then is the pagecopied

    COW allows more efficient process creation as only modifiedpages are copied

    Free pages are allocated from a pool of zeroed-out pages

  • 8/10/2019 Virtual Memory 2.0

    25/52

    9.25 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Before Process 1 Modifies Page C

  • 8/10/2019 Virtual Memory 2.0

    26/52

    9.26 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    After Process 1 Modifies Page C

  • 8/10/2019 Virtual Memory 2.0

    27/52

    9.27 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Page Replacement

  • 8/10/2019 Virtual Memory 2.0

    28/52

    9.28 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    What happens if there is no free frame?

    Page replacement find some page in memory, but notreally in use, swap it out

    algorithm

    performance want an algorithm which will result inminimum number of page faults

    Same page may be brought into memory several times

  • 8/10/2019 Virtual Memory 2.0

    29/52

    9.29 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    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 onlymodified pages are written to disk

    Page replacement completes separation between logical memoryand physical memory large virtual memory can be provided on asmaller physical memory

  • 8/10/2019 Virtual Memory 2.0

    30/52

    9.30 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Need For Page Replacement

  • 8/10/2019 Virtual Memory 2.0

    31/52

    9.31 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    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

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

    4. Restart the process

  • 8/10/2019 Virtual Memory 2.0

    32/52

    9.32 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Page Replacement

  • 8/10/2019 Virtual Memory 2.0

    33/52

    9.33 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Page Replacement Algorithms

    Want lowest page-fault rate

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

  • 8/10/2019 Virtual Memory 2.0

    34/52

    9.34 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Graph of Page Faults Versus The Number of Frames

  • 8/10/2019 Virtual Memory 2.0

    35/52

    9.35 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    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

    Beladys 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

  • 8/10/2019 Virtual Memory 2.0

    36/52

    9.36 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    FIFO Page Replacement

  • 8/10/2019 Virtual Memory 2.0

    37/52

    9.37 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    FIFO Illustrating Beladys Anomaly

  • 8/10/2019 Virtual Memory 2.0

    38/52

    9.38 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    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

  • 8/10/2019 Virtual Memory 2.0

    39/52

    9.39 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Optimal Page Replacement

  • 8/10/2019 Virtual Memory 2.0

    40/52

    9.40 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    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 referencedthrough this entry, copy the clock into the counter

    When a page needs to be changed, look at the counters todetermine which are to change

    5

    2

    4

    3

    1

    2

    3

    4

    1

    2

    5

    4

    1

    2

    5

    3

    1

    2

    4

    3

  • 8/10/2019 Virtual Memory 2.0

    41/52

  • 8/10/2019 Virtual Memory 2.0

    42/52

    9.42 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    LRU Algorithm (Cont.)

    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

  • 8/10/2019 Virtual Memory 2.0

    43/52

    9.43 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Use Of A Stack to Record The Most Recent Page References

  • 8/10/2019 Virtual Memory 2.0

    44/52

    9.44 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    LRU Approximation Algorithms

    Reference bit

    With each page associate a bit, initially = 0

    When page is referenced bit set to 1

    Replace the one which is 0 (if one exists)

    We do not know the order, however

    Second chance

    Need reference bit

    Clock replacement

    If page to be replaced (in clock order) has reference bit = 1then:

    set reference bit 0

    leave page in memory

    replace next page (in clock order), subject to same rules

  • 8/10/2019 Virtual Memory 2.0

    45/52

    9.45 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Second-Chance (clock) Page-Replacement Algorithm

  • 8/10/2019 Virtual Memory 2.0

    46/52

    9.46 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Counting Algorithms

    Keep a counter of the number of references that have been

    made to each page

    LFU Algorithm: replaces page with smallest count

    MFU Algorithm: based on the argument that the page with

    the smallest count was probably just brought in and has yetto be used

  • 8/10/2019 Virtual Memory 2.0

    47/52

    9.47 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Allocation of frames

  • 8/10/2019 Virtual Memory 2.0

    48/52

    9.48 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Allocation of Frames

    Each process needs minimum number of pages Example: IBM 370 6 pages to handle SS MOVE instruction:

    instruction is 6 bytes, might span 2 pages

    2 pages to handle from

    2 pages to handle to

    Two major allocation schemes

    fixed allocation

    priority allocation

    i i

  • 8/10/2019 Virtual Memory 2.0

    49/52

    9.49 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Fixed Allocation

    Equal allocation For example, if there are 100 frames and 5

    processes, give each process 20 frames.

    Proportional allocation Allocate according to the size of process

    mS

    spa

    m

    sS

    ps

    iii

    i

    ii

    forallocation

    framesofnumbertotal

    processofsize

    5964137

    127

    564137

    10

    127

    10

    64

    2

    1

    2

    a

    a

    s

    s

    m

    i

    P i i All i

  • 8/10/2019 Virtual Memory 2.0

    50/52

    9.50 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Priority Allocation

    Use a proportional allocation scheme using priorities ratherthan size

    If process Pigenerates a page fault,

    select for replacement one of its frames

    select for replacement a frame from a process withlower priority number

    Gl b l L l All i

  • 8/10/2019 Virtual Memory 2.0

    51/52

    9.51 Silberschatz, Galvin and Gagne 2008Operating System Concepts 8th Edition

    Global vs. Local Allocation

    Global replacement process selects a replacementframe from the set of all frames; one process can take aframe from another

    Local replacement each process selects from only itsown set of allocated frames

  • 8/10/2019 Virtual Memory 2.0

    52/52

    End of Course 10