Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Operating Systems, 112 Practical Session 10, Memory Management continues 1."— Presentation transcript:

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

2 PAGE REPLACEMENT ALGORITHMS Quick recap 2

3 Optimal Assumes the memory manager knows the “future” sequence of page references optimal The optimal algorithm: page out the page that will be used latest Problem Problem: the manager doesn’t know the future sequence of requests!

4 FIFO/FIFO Second-chance FIFO – First page In will be the First page taken Out – Problem – 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: reference bit – Add a reference bit which will be turned on whenever the page is accessed reference bit – 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”

5 2 nd chance FIFO (clock) 5

6 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 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 NFU o updated every page reference can be too often! => shift only every clock tick (NFU) – Use n 2 bit matrix

7 Not Frequently used 7 0 1 2 3 4 5 10000000 00000000 10000000 00000000 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 101011110010110101100010011000 Clock tick 0Clock tick 1 Clock tick 2 Clock tick 3Clock tick 4

8 LRU with bit tables 0123 00111 10000 20000 30000 8 0123 0011 1011 0000 0000 0123 0000 1000 1100 1110 0123 0000 1000 1101 1100 0123 0001 1001 1101 0000 0123 0000 1011 1001 1000 0123 0111 0011 0001 0000 0123 0100 0000 1101 1100 0123 0100 0000 1100 1110 0123 0110 0010 0000 1110 Reference string is: 0,1,2,3,2,1,0,3,2,3

9 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”)

10 Local vs. global algorithms Adding page A6 Adding page A6: 10 Age Local policy Global policy

11 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 τ process’s internal clock 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)

12 Dynamic set - WSClock Example 3 processes p 0, p 1 and p 2 current (virtual) times of the 3 processes are Tp 0 = 50; Tp 1 = 70; Tp 2 = 90 WSClock: replace when T p - 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 0 process ID:0 1 0 1 2 1 0 0 1 2 2 last ref:10 30 42 65 81 57 31 37 31 47 55 13 13 39 >20 12

13 Question 1: 2007a נתונה טבלת הדפים ההפוכה (inverted page table) הבאה : Virtual timeRef-bitProcess /page 200P 0 / 3 600P 2 / 0 301P 0 / 5 1001P 1 / 3 101P 0 / 6 700P 2 / 5 201P 1 / 7 201P 1 / 2

14 Question 1: 2007a מרחב הזיכרון הלוגי של כל תהליך (process-virtual address space) במערכת הוא בגודל שמונה דפים וגם גודל הזיכרון הפיזי הינו שמונה דפים. א. נתון כי פונקצית ה -hash דרכה ניגשים לטבלה היא : (process-number + page-number) mod 8 שרטטו במחברת את הטבלאות וההצבעות הרלוונטיות למציאת הדפים בטבלה ( כלומר את ה - anchor table).

15 Question 1: 2007a

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

17 Question 1: 2007a ג. נניח כי אלגוריתם החלפת הדפים במערכת הינו אלגוריתם Clock (. (FIFO second chance המחוג של האלגוריתם מטייל על דפי הזכרון של הטבלה שלעיל מלמעלה למטה, מתחיל בדף הראשון מלמעלה, יורד למטה, וחוזר חלילה. נניח עוד כי המחוג מצביע כעת על הדף השלישי מלמעלה. שרטטו את מצב הטבלה ( כולל התוספות שהוספתם בסעיף א ') לאחר שתהליך P 0 ביקש לקרוא בייט מדף 4 שלו ואלגוריתם ה -Clock הופעל.

18 Question 1: 2007a

19 ד. כעת נניח כי המערכת משתמשת באלגוריתם WS-clock וכי הזמנים הוירטואליים של הדפים בזיכרון הפיסי הם כפי שמופיע בעמודה הימנית בטבלה שלמעלה. נניח גם כי המחוג של אלגוריתם WS-clock מצביע כעת על הדף השלישי מלמעלה וכי 30 = τ. נניח גם כי ערכי השעונים הוירטואליים של התהליכים הינם : Tp 0 = 90, Tp 1 = 150, Tp 2 = 80. שרטטו את מצב הטבלה ( כולל התוספות שהוספתם בסעיף א ') לאחר שתהליך P 0 ביקש לקרוא בייט מדף מס ' 4 והמערכת הפעילה את אלגוריתם WS-clock.

20 Question 1: 2007a

21 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; }

22 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.

23 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.

24 Question 3: FIFO 70120304230321201701 7772 224440 00 777 000 333222 11 100 11 100033 32 221 15 page faults

25 Question 3: LRU 12 page faults 70120304230321201701 7772 2 4440 1 1 1 000 0 0033 3 0 0 11 3 3222 2 2 2

26 Question 3: Optimal 9 page faults 70120304230321201701 7772 2 2 2 2 7 000 0 4 0 0 0 11 3 3 3 1 1

27 Question 4 – 2001 a נתונה סדרת דרישות הדפים הבאה : 1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2 1. אם משתמשים ב -LRU, כתוב את ה -distance string עבור הסדרה הנתונה. חשב מתוך ה - distance string כמה page-faults יהיו עבור זיכרון פיזי בן 4 דפים. האם כדאי להגדיל את הזיכרון הפיזי ל -5 דפים במקרה זה ? 2. עבור אלג ' FIFO וזיכרון פיסי בן 4 דפים, חשב מספר ה page faults.

28 Question 4 – 2001 a 1234215621237632 123421562123763 12342156612376 1134215561227 3444456111 333345555 4444 Page faultppppppppp distance∞∞∞∞34∞∞4426∞534 1. 1234215621237632

29 Question 4 – 2001 a בשביל לחשב את מספר ה -page-faults כשמשתמשים בזיכרון פיזי בן 5 דפים, נצטרך לסכום על כל המרחקים הגדולים מ -5: 8. מנענו page-fault אחד.

30 Question 4 – 2001 a 1234445621137662 123334562213776 12223456621337 1112345562113 1234456221 113345555 4444 Page faultpppppppppppp 2. 1234215621237632

31 Question 5 – 2005 a במערכת קיים זיכרון פיזי בגודל 5 דפים. נתון כי אלגוריתם ההחלפה בו משתמשים הוא FIFO-second- chance. תארי את הבעייתיות הנוצרת עבור כל אחד מן ה -reference strings הבאים והציעי פתרון. הניחי כי לא ניתן להגדיל את הזיכרון הנתון והסבירי את הפתרון הנתון.

32 Question 5 – 2005 a bbbbbbbbbbbbaaaaaacceeeefffffgbaaaaa aaaaaaaaaaacccccceeffffgggggbaddddd cccccceeeeeeffggggbbbbbadeeeee eeffffffggbbbbaaaaadefffff fggggggbbaaaadddddefhhhhh Page fault pppppppppppp baabaacbabefgefefebbabafdbaabefhfefe Where: P 1 1 = a; P 1 2 = b; P 1 3 = c; P 1 4 = d; P 2 1 = e; P 2 2 = f; P 2 3 = g; P 2 4 = h; Bold is used to represent pages which have their ref bit on.

33 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.

34 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).

35 Question 6 Consider the following virtual page reference string: 0, 1, 2, 3, 0, 0, 1, 2, 3 Which 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).

36 Question 6 *000*3333*22 *111*0 00*3 *2222*111 Page faultpppppppp 012300123 Where: * represents the placement of the clock’s “hand” before the request p represents a page

37 Question 7 – 2006 a א. נתון כי בזיכרון הפיזי נמצאים דפים של שלושה תהליכים :.P 1,P 2,P 3 המערכת משתמשת באלגוריתם החלפת דפים WS-clock. ערכם הנוכחי של שעוני התהליכים הם : T 1 = 60, T2 = 130, T 3 = 240. עוד נתון כי τ=30. ידית השעון מצביעה על מסגרת מספר 1. תארו את פעולת האלגוריתם במקרה של page-fault על פי הנתונים הבאים :

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

39 Question 7 – 2006 a ב. הגרף הבא מתאר את גודל ה - Working Set של תהליך מסוים בנקודת זמן t כתלות בגודל החלון k: הסבירו כיצד ניתן לקבוע את ה - k הרצוי על פי הגרף הנ " ל. W(k,t) k

40 Question 7 – 2006 a K צריך להיות מספיק גדול על מנת לכלול את כל ה working set הראשון אבל קטן מספיק כדי לא לכלול דפים מה working set השני כל מדרגה בגרף מייצגת working set אחר. K צריך להיות מספיק גדול על מנת לכלול את כל ה working set הראשון אבל קטן מספיק כדי לא לכלול דפים מה working set השני. לכן נקבע את גודלו להיות הנקודה k' בציור. W(k,t) k 'k'k

41 Question 7 – 2006 a הסבירו את הקשר בין high level scheduling לבין הגרף הבא : CPU utilization degree of multiprogramming

42 Question 7 – 2006 a תפקידו של ה - high level scheduler הוא לקבוע את מס ' התהליכים בזיכרון. כשה - CPU utilization נמוך, ה high level scheduler צריך לקבוע האם הסיבה לירידה ביעילות היא : – מיעוט תהליכים בזיכרון ( הוא בצד שמאל של העקומה ) ורצוי שיוסיף תהליך לקבוצת התהליכים בזיכרון. – thrashing ( הוא בצד הימני של העקומה ), ועליו להוריד תהליך מקבוצת התהליכים בזיכרון.


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

Similar presentations


Ads by Google