page replacement algorithms important donot delete

Upload: sabirsvcm

Post on 06-Apr-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Page Replacement Algorithms Important Donot Delete

    1/10

    CSC468/2204 TUTORIAL WEEK 8

    by Tristan Miller -- Updated 7 November 2000

    OVERVIEW OF TODAYS TUTORIAL

    announcements regarding A2Q1

    page replacement

    overview of A2Q2

    page replacement algorithms

    questions & answers

    ANNOUNCEMENTS REGARDING A2Q1

    clarifications regarding previous tutorial (briefly):

    o for FCFS tie-breaking rule, select the process that has been in

    the system the longest

    o job service and interarrival times are figured as real numbers,

    not integers after you generate the random numbers, do not

    round off to the nearest integer

    o except for round-robin, preemptions should be performed only

    when new processes arrive

    o the HRRN algorithm presented in tutorial was a preemptive

    version; the assignment calls for a non-preemptive

    implementation

    all the above clarifications are explained in detail on the newsgroup

  • 8/3/2019 Page Replacement Algorithms Important Donot Delete

    2/10

    tutorial notes are now available on the web at

    http://www.cs.utoronto.ca/~psy/teaching/2000/468f/

    suggested reading list for discrete event simulation is available on the

    newsgroup

    diagram of discrete event simulation:

    0 tt1+t0

    Generate service time,sn,for jobjn. Generateinterarrival time,tn+1, forobjn+1.

    =

    n

    iit

    0

    Generate servictime,s0, for jobj0.Generateinterarrival timt1, for jobj1.

    t0

    Generate servictime,s1, for jobj1.Generateinterarrival timt2, for jobj2.

    Generateinterarrival timt0, for jobj0

    non-preemptive schedulers need to recompute priorities only when a

    process finishes executing

    except for round-robin, preemptive schedulers need to recompute

    priorities only a process arrives or finishes

    http://www.cs.utoronto.ca/~psy/teaching/2000/468f/http://www.cs.utoronto.ca/~psy/teaching/2000/468f/
  • 8/3/2019 Page Replacement Algorithms Important Donot Delete

    3/10

    for round-robin, the scheduler needs to recompute priorities when a

    process finishes or one quantum after a process begins executing

    (whichever is sooner)

    your simulation can conveniently ignore the time between events

    just advance the clock to the next event (i.e. a process arrival, a

    process completion, or the end of a round-robin quantum) instead of

    waiting

    remember, no scheduling algorithm, including round robin, will allow

    the CPU to remain idle while there are waiting jobs

    PAGE REPLACEMENT

    each process is allocated frames (memory) which hold the processs

    pages (data)

    frames are filled with pages as needed this is called demand paging

    over-allocation of memory is prevented by modifying the page-fault

    service routine to replace pages

    the job of the page replacement algorithm is to decide which page gets

    victimized to make room for a new page

    page replacement completes separation of logical and physical

    memory

    OVERVIEW OF A2Q2

    given a certain memory size and a sequence of page numbers (the

    reference string), use each of three different page replacement

    algorithms to determine the performance

  • 8/3/2019 Page Replacement Algorithms Important Donot Delete

    4/10

    calculate, tabulate, and graph page faults vs. number of frames

    the performance calculations should be done with as few passes of the

    reference string as possible i.e. even though there are nine different

    test cases (3 algorithms 3 parameters each), you may not have to

    run a simulation nine times

    PAGE REPLACEMENT ALGORITHMS

    Optimal algorithm

    ideally we want to select an algorithm with the lowest page-fault rate

    such an algorithm exists, and is called (unsurprisingly) the optimal

    algorithm:

    procedure: replace the page that will not be used for the longest time

    (or at all) i.e. replace the page with the greatest forward distance in

    the reference string

    example using 4 frames:

    analysis: 12 page references, 6 page faults, 2 page replacements.

    Page faults per number of frames = 6/4 = 1.5

    unfortunately, the optimal algorithm requires special hardware (crystal

    ball, magic mirror, etc.) not typically found on todays computers

    Reference #

    1

    2

    3

    4

    5

    6

    7

    8

    9

    1

    0

    1

    1

    12

    Page referenced 1 2 3 4 1 2 5 1 2 3 4 5

    Frames

    _ = faulting

    page

    1 1 1 1 1 1 1 1 1 1 4 4

    2 2 2 2 2 2 2 2 2 2 2

    3 3 3 3 3 3 3 3 3 3

    4 4 4 5 5 5 5 5 5

  • 8/3/2019 Page Replacement Algorithms Important Donot Delete

    5/10

    optimal algorithm is still used as a metric for judging other page

    replacement algorithms

    FIFO algorithm

    replaces pages based on their order of arrival: oldest page is replaced

    example using 4 frames:

    analysis: 12 page references, 10 page faults, 6 page replacements.

    Page faults per number of frames = 10/4 = 2.5

    LFU algorithm (page-based)

    procedure: replace the page which has been referenced least often

    for each page in the reference string, we need to keep a reference

    count. All reference counts start at 0 and are incremented every time

    a page is referenced.

    example using 4 frames:

    Reference #

    1

    2

    3

    4

    5

    6

    7

    8

    9

    1

    0

    1

    1

    12

    Page referenced 1 2 3 4 1 2 5 1 2 3 4 5

    Frames_ = faulting

    page

    1 1 1 1 1 1 5 5 5 5 4 42 2 2 2 2 2 1 1 1 1 5

    3 3 3 3 3 3 2 2 2 2

    4 4 4 4 4 4 3 3 3

    Reference # 1 2 3 4 5 6 7 8 9 1

    0

    1

    1

    12

    Page referenced 1 2 3 4 1 2 5 1 2 3 4 5

    Frames

    _ = faulting page

    n = reference

    count

    11 11 11 11 21 21 21 31 31 31 31 3112 12 12 12 22 22 22 32 32 32 32

    13 13 13 13 15 15 15 23 23 2514 14 14 14 14 14 14 24 24

  • 8/3/2019 Page Replacement Algorithms Important Donot Delete

    6/10

    at the 7th page in the reference string, we need to select a page to be

    victimized. Either 3 or 4 will do since they have the same reference

    count (1). Lets pick 3.

    likewise at the 10th page reference; pages 4 and 5 have been

    referenced once each. Lets pick page 4 to victimize. Page 3 is

    brought in, and its reference count (which was 1 before we paged it

    out a while ago) is updated to 2.

    analysis: 12 page references, 7 page faults, 3 page replacements.

    Page faults per number of frames = 7/4 = 1.75

    LFU algorithm (frame-based)

    procedure: replace the page in the frame which has been referenced

    least often

    need to keep a reference countfor each frame which is initialized to 1

    when the page is paged in, incremented every time the page in the

    frame is referenced, and reset every time the page in the frame is

    replaced

    example using 4 frames:

    Reference # 1 2 3 4 5 6 7 8 9 1

    0

    1

    1

    12

    Page referenced 1 2 3 4 1 2 5 1 2 3 4 5

    Frames

    _ = faulting page

    n = reference

    count

    11 11 11 11 21 21 21 31 31 31 31 3112 12 12 12 22 22 22 32 32 32 32

    13 13 13 13 15 15 15 13 13 1514 14 14 14 14 14 14 24 24

  • 8/3/2019 Page Replacement Algorithms Important Donot Delete

    7/10

    at the 7th reference, we victimize the page in the frame which has

    been referenced least often -- in this case, pages 3 and 4 (contained

    within frames 3 and 4) are candidates, each with a reference count of

    1. Lets pick the page in frame 3. Page 5 is paged in and frame 3s

    reference count is reset to 1.

    at the 10th reference, we again have a page fault. Pages 5 and 4

    (contained within frames 3 and 4) are candidates, each with a count of

    1. Lets pick page 4. Page 3 is paged into frame 3, and frame 3s

    reference count is reset to 1.

    analysis: 12 page references, 7 page faults, 3 page replacements.

    Page faults per number of frames = 7/4 = 1.75

    LRU algorithm

    replaces pages based on their most recent reference replace the page

    with the greatest backward distance in the reference string

    example using 4 frames:

    analysis: 12 page references, 8 page faults, 4 page replacements.

    Page faults per number of frames = 8/4 = 2

    Reference #

    1

    2

    3

    4

    5

    6

    7

    8

    9

    1

    0

    1

    1

    12

    Page referenced 1 2 3 4 1 2 5 1 2 3 4 5

    Frames

    _ = faulting

    page

    1 1 1 1 1 1 1 1 1 1 1 5

    2 2 2 2 2 2 2 2 2 2 2

    3 3 3 3 5 5 5 5 4 44 4 4 4 4 4 3 3 3

  • 8/3/2019 Page Replacement Algorithms Important Donot Delete

    8/10

    one possible implementation (not necessarily the best):

    o every frame has a time field; every time a page is referenced,

    copy the current time into its frames time field

    o when a page needs to be replaced, look at the time stamps to

    find the oldest

    PFF algorithm

    thus far, all the algorithms presented assume each process is granted a

    fixed amount of memory (i.e. number of frames)

    in the real world, different processes have different memory

    requirements

    allocating too few frames to a process may result in that process

    thrashing (rapid concentrated page replacement)

    allocating too many frames to a process may result in other processes

    thrashing

    PFF aims to prevent thrashing by allocating or deallocating frames as

    required

    procedure:

    o every frame has a reference bit (initially 0)

    o whenever a page is referenced, set its frames reference bit

    o when a page fault occurs, compare the IFT (inter-fault time)

    with a certain threshold

    o if IFT < threshold, allocate a new frame to the process and reset

    all reference bits

  • 8/3/2019 Page Replacement Algorithms Important Donot Delete

    9/10

    o if IFT threshold, deallocate all frames whose reference bit is

    not set, allocate a new frame for the faulting page, and reset all

    reference bits

    example with threshold = 3:

    since the number of frames allocated to a process is dynamic with this

    algorithm, when performing the analysis, use the mean number of

    frames in use per reference

    Reference #

    1

    2

    3

    4

    5 6

    7

    8 9 1

    0

    1

    1

    12

    Page referenced 1 2 3 4 1 2 5 1 2 3 4 5

    Frames

    * = reference bit

    set

    _ = faulting page

    1 1 1 1 *

    1

    *

    1

    1 *

    1

    *

    1

    1 1 1

    2 2 2 2 *

    2

    2 2 *

    2

    2 2 2

    3 3 3 3

    4 4 4

    5 5 5

    3 3 3

    4 4

    5

  • 8/3/2019 Page Replacement Algorithms Important Donot Delete

    10/10

    in the above example, there were 39 frames in use over 12 page

    references, so the mean number of frames is 39/12 = 3.25

    analysis for the above example: 12 page references, 8 page faults.

    Page faults per number of frames = 8/3.25 2.4615