Upload
ali-gentry
View
21
Download
2
Tags:
Embed Size (px)
DESCRIPTION
Operating Systems, 132. Practical Session 10, Memory Management continues. Quick recap. Page replacement algorithms. Optimal. Assumes the memory manager knows the “future” sequence of page references The optimal algorithm: page out the page that will be used latest - PowerPoint PPT Presentation
Citation preview
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!
3
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 memory size of 2 frames, 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 that 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”.
4
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 Updating every page reference can be too often! => shift only every clock tick (modified version of NFU, also known as aging)
– Use n2 bit matrix o Hardware LRU algorithm, where n is the number of page frames
6
Modified NFU (Aging)
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
Page references
Pag
es
Hardware LRU algorithm (bit tables)
0 1 2 30 0 1 1 11 0 0 0 02 0 0 0 03 0 0 0 0
8
Reference string is: 0,1,2,3,2,1,0,3,2,3
0 1 2 3
0 0 1 1
1 0 1 1
0 0 0 0
0 0 0 0
1
0 1 2 3
0 0 0 1
1 0 0 1
1 1 0 1
0 0 0 0
2
0 1 2 3
0 0 0 0
1 0 0 0
1 1 0 0
1 1 1 03
0 1 2 3
0 0 0 0
1 0 0 0
1 1 0 1
1 1 0 0
2
0 1 2 3
0 0 0 0
1 0 1 1
1 0 0 1
1 0 0 0
1
0 1 2 3
0 1 1 1
0 0 1 1
0 0 0 1
0 0 0 0
0
0 1 2 3
0 1 1 0
0 0 1 0
0 0 0 0
1 1 1 03
0 1 2 3
0 1 0 0
0 0 0 0
1 1 0 1
1 1 0 0
2
0 1 2 3
0 1 0 0
0 0 0 0
1 1 0 0
1 1 1 03
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
– Dynamically allocate page frames among the runnable processes. This is useful when the size of a WS is dynamically changing.
• Local policies may have variable allocation of pages per process (“working set”)
9
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
Last reference time
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)
11
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
12
Frames 0 1 2 3 4 5 6 7 8 9 10
reference 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 reference 10 30 42 65 81 57 31 37 31 47 55
Tp - ref(frame) 13 13 39
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
13
Question 1: 2007a
process-virtualמרחב הזיכרון הלוגי של כל תהליך )address space ) במערכת הוא בגודל שמונה דפים וגם
גודל הזיכרון הפיזי הינו שמונה דפים.
היא: דרכה ניגשים לטבלה hashנתון כי פונקצית ה-א.
(process-number + page-number) mod 8
שרטטו במחברת את הטבלאות וההצבעות הרלוונטיות . (anchor tableלמציאת הדפים בטבלה )כלומר את ה-
14
Question 1: 2007aב. שרטטו את טבלאות הדפים הסטנדרטיות של שלושת
התהליכים ומלאו את המשבצות עבורן ניתנה אינפורמציה בטבלה שלמעלה.
16
Question 1: 2007a
ג. נניח כי אלגוריתם החלפת הדפים במערכת הינו . המחוג של FIFO 2nd chance (clock)אלגוריתם
האלגוריתם מטייל על דפי הזיכרון של הטבלה שלעיל מלמעלה למטה, מתחיל בדף הראשון מלמעלה, יורד למטה, וחוזר חלילה. נניח עוד כי המחוג מצביע כעת על הדף השלישי מלמעלה. שרטטו את מצב הטבלה
)כולל התוספות שהוספתם בסעיף א'( לאחר שתהליך P0 שלו ואלגוריתם ה-4 ביקש לקרוא בייט מדף Clock
הופעל.
17
Question 1: 2007a
-WSד. כעת נניח כי המערכת משתמשת באלגוריתם clock וכי הזמנים הוירטואליים של הדפים בזיכרון
הפיסי הם כפי שמופיע בעמודה הימנית בטבלה WS-clockשלמעלה. נניח גם כי המחוג של אלגוריתם . τ = 30מצביע כעת על הדף השלישי מלמעלה וכי
נניח גם כי ערכי השעונים הוירטואליים של התהליכים הינם:
Tp0 = 90, Tp1 = 150, Tp2 = 80 שרטטו את מצב הטבלה .)כולל התוספות שהוספתם בסעיף א'( לאחר
והמערכת 4 ביקש לקרוא בייט מדף מס' P0שתהליך .WS-clockהפעילה את אלגוריתם
19
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 at virtual address 200. We run both programs on a machine with physical memory of 3 frames. Where the code of the program is in the 1'st frame and the other two are empty. If the page replacement algorithm is LRU, how many page faults will there 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; }}
21
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.
22
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 size is 3 frames, how many page faults would occur for the following algorithms:
1. FIFO2. LRU 3. Optimal
Note: Remember that all frames are initially empty, so your first unique pages will all cost one fault each.
23
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
24
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
25
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
26
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מספר ה
27
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
28
Question 4 – 2001 a
page-faultsבשביל לחשב את מספר ה-• דפים, נצטרך 5כשמשתמשים בזיכרון פיזי בן
8: ישנם 5לסכום על כל המרחקים הגדולים מ-כאלו. אחד. page-faultמנענו
29
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
30
Question 5 – 2005 a
דפים. נתון כי 5במערכת קיים זיכרון פיזי בגודל •-FIFOאלגוריתם ההחלפה בו משתמשים הוא
second-chance תארו את הבעייתיות הנוצרת עבור . הבאים והציעו reference stringsכל אחד מן ה-
פתרון. הניחו כי לא ניתן להגדיל את הזיכרון.
31
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.
32
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 process) are 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.
33
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 ran 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 by swapping out one of the processes to the backing store (disk).
34
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).
35
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 pf pf pf pf pf pf pf pf
0 1 2 3 0 0 1 2 3
Where:* represents the placement of the clock’s “hand” before the request pf represents a page fault
36
Question 7 – 2006 a
של תהליך מסוים Working Setהגרף הבא מתאר את גודל ה- ב. :k כתלות בגודל החלון tבנקודת זמן
הרצוי על פי הגרף הנ"ל.kהסבירו כיצד ניתן לקבוע את ה-
W)k,t(
k
37
Question 7 – 2006 a
צריך K אחר. working setכל מדרגה בגרף מייצגת workingלהיות מספיק גדול על מנת לכלול את כל ה
set הראשון אבל קטן מספיק כדי לא לכלול דפים מה working setלכן נקבע את גודלו להיות הנקודה השני .
k.בציור '
W)k,t(
k'k 38
Question 7 – 2006 a
לבין high level schedulingהסבירו את הקשר בין הגרף הבא:
CP
U u
tiliz
atio
n
degree of multiprogramming
39
Question 7 – 2006 a
הוא לקבוע את high level scheduler תפקידו של ה-מס' התהליכים בזיכרון.
high level scheduler נמוך, ה CPU utilization כשה- צריך לקבוע האם הסיבה לירידה ביעילות היא:
מיעוט תהליכים בזיכרון )הצד השמאלי של העקומה( –ורצוי שיוסיף תהליך לקבוצת התהליכים בזיכרון.
–thrashing הצד הימני של העקומה(, ועליו להוריד( תהליך מקבוצת התהליכים בזיכרון.
40