Operating Systems, 112 Practical Session 10, Memory Management continues 1.

Post on 19-Dec-2015

234 views 7 download

Transcript of Operating Systems, 112 Practical Session 10, Memory Management continues 1.

Operating Systems, 112

Practical Session 10, Memory Management continues

1

PAGE REPLACEMENT ALGORITHMSQuick recap

2

Optimal

• Assumes the memory manager knows the “future” sequence of page references

• The optimal algorithm: page out the page that will be used latest

• Problem: the manager doesn’t know the future sequence of requests!

FIFO/FIFO Second-chance• FIFO

– First page In will be the First page taken Out– Problem: we may be removing a page that will be constantly in use:

• Assume a capacity of 2 pages, and take the following sequence of page requests: 1,2,3,1,2,3,1,2,3,1…

• FIFO second-chance:– Add a reference bit which will be turned on whenever the page is

accessed – When a “swap out” is needed: go over the pages from the oldest to

newest and if the page’s reference bit is on, clear it; otherwise remove the page.

• Both FIFO and FIFO second-chance can be implemented as a circular queue: the “clock algorithm”

2nd chance FIFO (clock)

5

Least Recently Used (LRU)

• If we need to remove a page, then the Least Recently Used page will be chosen throw out the page that has been unused for longest time

period

• Problem: have to keep “history” and remember when a page was referenced Counter for each page, updated on every memory reference!

• LRU can be approximated:– Shift counter

o updated every page reference can be too often! => shift only every clock tick (NFU)

– Use n2 bit matrix

Not Frequently used

7

0

1

2

3

4

5

10000000

00000000

10000000

00000000

10000000

10000000

11000000

10000000

01000000

00000000

11000000

01000000

11100000

11000000

00100000

10000000

01100000

10100000

11110000

01100000

00010000

01000000

10110000

01010000

01111000

10110000

10001000

00100000

01011000

00101000

1 0 1 0 1 1 1 1 0 0 1 0 1 1 0 1 0 1 1 0 0 0 1 0 0 1 1 0 0 0

Clock tick 0 Clock tick 1 Clock tick 2 Clock tick 3 Clock tick 4

LRU with bit tables

0 1 2 30 0 1 1 11 0 0 0 02 0 0 0 03 0 0 0 0

8

0 1 2 30 0 1 11 0 1 10 0 0 00 0 0 0

0 1 2 30 0 0 01 0 0 01 1 0 01 1 1 0

0 1 2 30 0 0 01 0 0 01 1 0 11 1 0 0

0 1 2 30 0 0 11 0 0 11 1 0 10 0 0 0

0 1 2 30 0 0 01 0 1 11 0 0 11 0 0 0

0 1 2 30 1 1 10 0 1 10 0 0 10 0 0 0

0 1 2 30 1 0 00 0 0 01 1 0 11 1 0 0

0 1 2 30 1 0 00 0 0 01 1 0 01 1 1 0

0 1 2 30 1 1 00 0 1 00 0 0 01 1 1 0

Reference string is: 0,1,2,3,2,1,0,3,2,3

Quick recap: global vs. local

• The scope of the page replacement policy can be:– Local: choose a page to remove only among the

pages of the process that caused the page fault– Global: choose a page to remove from all pages in

main memory, independent of the process• Global policies are more efficient• Local policies may have variable allocation of

pages per process (“working set”)

Local vs. global algorithms• Adding page A6:

10

A0 10 A0 A0A1 7 A1 A1A2 5 A2 A2A3 4 A3 A3A4 6 A4 A4A5 3 A6 A5

B0 9 B0 B0B1 4 B1 B1B2 6 B2 B2B3 2 B3 A6B4 5 B4 B4B5 6 B5 B5

B6 12 B6 B6C1 3 C1 C1C2 5 C2 C2C3 6 C3 C3

Age

Local policy

Globalpolicy

Quick recap: WS-Clock

• Modified version of the “clock algorithm” that takes into consideration the current working set of a process

• WS-Clock have a parameter τ • Each page in memory has a reference bit and a field

containing the last time it was used measured by the process’s internal clock

• A page will be chosen for removal if both conditions apply:– Its reference bit is 0– The time elapsed since the last reference to the page is

bigger than τ (takes into account each process virtual time for computation)

Dynamic set - WSClock Example 3 processes p0, p1 and p2

current (virtual) times of the 3 processes areTp0 = 50; Tp1 = 70; Tp2 = 90

WSClock: replace when Tp - ref(frame) > the minimal distance (“window size”) is = 20 The clock hand is currently pointing to page frame 4

page-frames: 0 1 2 3 4 5 6 7 8 9 10

ref. bit: 0 0 1 1 1 0 1 0 0 1 0process ID: 0 1 0 1 2 1 0 0 1 2 2last ref: 10 30 42 65 81 57 31 37 31 47 55

13 13 39

>20

12

Question 1: 2007aהבאה:( inverted page tableנתונה טבלת הדפים ההפוכה )

Virtual time Ref-bit Process /page

20 0 P0 / 3

60 0 P2 / 0

30 1 P0 / 5

100 1 P1 / 3

10 1 P0 / 6

70 0 P2 / 5

20 1 P1 / 7

20 1 P1 / 2

Question 1: 2007a

process-virtualמרחב הזיכרון הלוגי של כל תהליך )address space ) במערכת הוא בגודל שמונה דפים וגם

גודל הזיכרון הפיזי הינו שמונה דפים.

דרכה ניגשים לטבלה היא: hashנתון כי פונקצית ה-א.

(process-number + page-number) mod 8

שרטטו במחברת את הטבלאות וההצבעות הרלוונטיות . (anchor tableלמציאת הדפים בטבלה )כלומר את ה-

Question 1: 2007a

Question 1: 2007aב. שרטטו את טבלאות הדפים הסטנדרטיות של שלושת

התהליכים ומלאו את המשבצות עבורן ניתנה אינפורמציה בטבלה שלמעלה.

Question 1: 2007a

ג. נניח כי אלגוריתם החלפת הדפים במערכת הינו המחוג של Clock (. (FIFO second chanceאלגוריתם

האלגוריתם מטייל על דפי הזכרון של הטבלה שלעיל מלמעלה למטה, מתחיל בדף הראשון מלמעלה, יורד למטה, וחוזר חלילה. נניח עוד כי המחוג מצביע כעת על הדף השלישי מלמעלה. שרטטו את מצב הטבלה

)כולל התוספות שהוספתם בסעיף א'( לאחר שתהליך P0 שלו ואלגוריתם ה-4 ביקש לקרוא בייט מדף Clock

הופעל.

Question 1: 2007a

Question 1: 2007a

-WSד. כעת נניח כי המערכת משתמשת באלגוריתם clock וכי הזמנים הוירטואליים של הדפים בזיכרון הפיסי

הם כפי שמופיע בעמודה הימנית בטבלה שלמעלה. נניח מצביע כעת על WS-clockגם כי המחוג של אלגוריתם . נניח גם כי ערכי τ = 30הדף השלישי מלמעלה וכי

השעונים הוירטואליים של התהליכים הינם:Tp0 = 90, Tp1 = 150, Tp2 = 80 שרטטו את מצב הטבלה .

)כולל התוספות שהוספתם בסעיף א'( לאחר שתהליך P0 'והמערכת הפעילה את 4 ביקש לקרוא בייט מדף מס

.WS-clockאלגוריתם

Question 1: 2007a

Question 2

Assume that the array a is stored consecutively: a[0,0], a[0,1] ... and also assume that the size of each entry is one word. The virtual memory has a page size of 200 words. The program code is in address 0-199 in the virtual memory. a[0][0] is in virtual address 200. We run both programs in a machine with physical memory of 3 pages. Where the code of the program is in the 1'st page and the other two are empty. If the page replacement algorithm is LRU, how many page faults there will be in each of the programs? Explain.

Program A: int i, j, a[100][100]; for )i = 0; i < 100; i++( {        for )j = 0; j < 100; j++( {                a[i][j] = 0;        }}

Program B:int i, j, a[100][100]; for )j = 0; j < 100; j++( {        for )i = 0; i < 100; i++( {                a[i][j] = 0;        }}

Question 2

Array a is stored in a[0][0],a[0][1] ... in virtual pages 1..50 The reference string (specifying only possible page faults) of program A will be: 0,1,0,2,0,3...50 We'll get a total of 50 page faults.

The reference string of B will be: 0,1,0,2...,0,50,0,1,0,2....0,50,.. Leading to a total of 5000 page faults.

Note that due to the use of the LRU algorithm, page 0 will be in memory at all times.

Question 3

Consider the following page reference string:

7,0,1,2,0,3, 0,4,2,3,0,3,2,1,2,0,1,7,0,1

Assuming that the memory’s size is 3 frames, how many page faults would occur for the following algorithms:

1. LRU 2. FIFO 3. Optimal

Note: Remember that all frames are initially empty, so your first unique pages will all cost one fault each.

Question 3: FIFO

7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1

7 7 7 2   2 2 4 4 4 0     0 0     7 7 7

  0 0 0   3 3 3 2 2 2     1 1     1 0 0

    1 1   1 0 0 0 3 3     3 2     2 2 1

15 page faults

Question 3: LRU

12 page faults

7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1

7 7 7 2   2   4 4 4 0     1   1   1    

  0 0 0   0   0 0 3 3     3   0   0    

    1 1   3   3 2 2 2     2   2   2    

Question 3: Optimal

9 page faults

7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1

7 7 7 2   2   2     2     2       7    

  0 0 0   0   4     0     0       0    

    1 1   3   3     3     1       1    

Question 4 – 2001 a

נתונה סדרת דרישות הדפים הבאה:

1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2

distance, כתוב את ה-LRUאם משתמשים ב-1.string עבור הסדרה הנתונה. חשב מתוך

יהיו עבור page-faultsכמה distance stringה- דפים. האם כדאי להגדיל את 4זיכרון פיזי בן

דפים במקרה זה?5הזיכרון הפיזי ל-

דפים , חשב 4 וזיכרון פיסי בן FIFOעבור אלג' 2..page faultsמספר ה

Question 4 – 2001 a

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3

1 2 3 4 2 1 5 6 6 1 2 3 7 6

1 1 3 4 2 1 5 5 6 1 2 2 7

3 4 4 4 4 5 6 1 1 1

3 3 3 3 4 5 5 5 5

4 4 4 4

Page fault p p p p p p p p p

distance ∞ ∞ ∞ ∞ 3 4 ∞ ∞ 4 4 2 6 ∞ 5 3 4

1.1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2

Question 4 – 2001 a

page-faultsבשביל לחשב את מספר ה-• דפים, נצטרך 5כשמשתמשים בזיכרון פיזי בן

. 8: 5לסכום על כל המרחקים הגדולים מ- אחד. page-faultמנענו

Question 4 – 2001 a

1 2 3 4 4 4 5 6 2 1 1 3 7 6 6 2

1 2 3 3 3 4 5 6 2 2 1 3 7 7 6

1 2 2 2 3 4 5 6 6 2 1 3 3 7

1 1 1 2 3 4 5 5 6 2 1 1 3

1 2 3 4 4 5 6 2 2 1

1 1 3 3 4 5 5 5 5

4 4 4 4

Page fault p p p p p p p p p p p p

2.

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2

Question 5 – 2005 a

דפים. נתון כי 5במערכת קיים זיכרון פיזי בגודל •-FIFO-secondאלגוריתם ההחלפה בו משתמשים הוא

chance תארי את הבעייתיות הנוצרת עבור כל אחד מן . הבאים והציעי פתרון. הניחי כי לא reference stringsה-

ניתן להגדיל את הזיכרון הנתון והסבירי את הפתרון הנתון.

Question 5 – 2005 a

b b b b b b b b b b b b a a a a a a c c e e e e f f f f f g b a a a a a

a a a a a a a a a a a c c c c c c e e f f f f g g g g g b a d d d d d

c c c c c c e e e e e e f f g g g g b b b b b a d e e e e e

e e f f f f f f g g b b b b a a a a a d e f f f f f

f g g g g g g b b a a a a d d d d d e f h h h h h

Page fault

p p p p p p p p p p p p

b a a b a a c b a b e f g e f e f e b b a b a f d b a a b e f h f e f e

Where:P11 = a; P12 = b; P13 = c; P14 = d; P21 = e; P22 = f; P23 = g; P24 = h;Bold is used to represent pages which have their ref bit on.

Question 5 – 2005 a

• We can see that both processes usually refer to pages 1 and 2 often (a, b and e, f) and rarely to other pages.

• Since FIFO-second-chance is a global algorithm, we get for the above reference string, that pages from the working set of a process are sometimes paged out.

• Whenever page 3 or 4 (for either processes) is referenced, the last two pages used by each process are 1 and 2. So with a time window of size 2, whenever pages 3 or 4 are requested, pages 1 and 2 of both processes will be in the working set of the specific process and will not be paged out by WSClock.

Question 5 – 2005 a

• If you look at the string closely, you could see that the working set of each process is at least of size 2. The total memory size is 5.

• The processes are running in sequence, first process 1, then 2, then 3, then again 1,2,3…

• When some process x continues its run, we know that there were two other processes which run before him and needed at least 2 pages. This means that the memory contains at least 4 pages not belonging to process x (saved from its last run).

• The remaining memory is not enough for x’s current run and it will have to page out other page(s). This will result in not having the pages of the next process in memory which will repeat the process.

• The total sum of the working set is bigger than the size of the memory. This situation is called thrashing and is solved by the OS through swapping out one of the processes to the backing store (disk).

Question 6

Consider the following virtual page reference string:

0, 1, 2, 3, 0, 0, 1, 2, 3Which page references will cause a page fault when a basic clock replacement algorithm is used? Assume that there are 3 page frames and that the memory is initially empty. Show all page faults (including frame loading).

Question 6

*0 0 0 *3 3 3 3 *2 2

*1 1 1 *0 *0 0 0 *3

*2 2 2 2 *1 1 1

Page fault p p p p p p p p

0 1 2 3 0 0 1 2 3

Where:* represents the placement of the clock’s “hand” before the

request p represents a page

Question 7 – 2006 a

P1,P2,P3 .נתון כי בזיכרון הפיזי נמצאים דפים של שלושה תהליכים: א.

. WS-clockהמערכת משתמשת באלגוריתם החלפת דפים

ערכם הנוכחי של שעוני התהליכים הם:

T1 = 60, T2 = 130, T3 = 240 .

.1. ידית השעון מצביעה על מסגרת מספר τ=30עוד נתון כי

על פי הנתונים page-faultתארו את פעולת האלגוריתם במקרה של הבאים:

Question 7 – 2006 a

last-ref ואת ה- ref-bitsהאלגוריתם יעדכן את ה- .5 ויחליף את הדף במסגרת 3 ו 2של מסגרות

Question 7 – 2006 a

של Working Setהגרף הבא מתאר את גודל ה- ב. :k כתלות בגודל החלון tתהליך מסוים בנקודת זמן

הרצוי על פי kהסבירו כיצד ניתן לקבוע את ה- הגרף הנ"ל.

W)k,t(

k

Question 7 – 2006 a

צריך K אחר. working setכל מדרגה בגרף מייצגת workingלהיות מספיק גדול על מנת לכלול את כל ה

set הראשון אבל קטן מספיק כדי לא לכלול דפים מה working setלכן נקבע את גודלו להיות הנקודה השני .

k.בציור '

W)k,t(

k'k

Question 7 – 2006 a

לבין high level schedulingהסבירו את הקשר בין הגרף הבא:

CP

U u

tiliz

atio

n

degree of multiprogramming

Question 7 – 2006 a

הוא לקבוע את high level scheduler תפקידו של ה-מס' התהליכים בזיכרון.

high level scheduler נמוך, ה CPU utilization כשה- צריך לקבוע האם הסיבה לירידה ביעילות היא:

מיעוט תהליכים בזיכרון )הוא בצד שמאל של –העקומה( ורצוי שיוסיף תהליך לקבוצת התהליכים

בזיכרון.–thrashing הוא בצד הימני של העקומה(, ועליו(

להוריד תהליך מקבוצת התהליכים בזיכרון.