Document

advertisement
Operating Systems, 142
Practical Session 10,
Memory Management continues
Quick recap
PAGE REPLACEMENT ALGORITHMS
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!
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
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 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)
Page references
Clock tick 1
Clock tick 2
Clock tick 3
Clock tick 4
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
0
10000000
11000000
11100000
11110000
01111000
1
00000000
10000000
11000000
01100000
10110000
2
10000000
01000000
00100000
00010000
10001000
3
00000000
00000000
10000000
01000000
00100000
4
10000000
11000000
01100000
10110000
01011000
5
10000000
01000000
10100000
01010000
00101000
Pages
Clock tick 0
7
Hardware LRU algorithm (bit tables)
0
1
2
3
0
1
2
3
0
1
2
3
0
1
2
3
0
1
2
3
0 0
1
1
1
0
0
1
1
0
0
0
1
0
0
0
0
0
0
0
0
1 0
0
0
0 1 1
0
1
1
1
0
0
1
1
0
0
0
1
0
0
0
2 0
0
0
0
0
0
0
0 2 1
1
0
1
1
1
0
0 2 1
1
0
1
3 0
0
0
0
0
0
0
0
0
0
0
0 3 1
1
1
0
1
1
0
0
0
1
2
3
0
1
2
3
0
1
2
3
0
1
2
3
0
1
2
3
0
0
0
0 0 0
1
1
1
0
1
1
0
0
1
0
0
0
1
0
0
1
0
1
1
0
0
1
1
0
0
1
0
0
0
0
0
0
0
0
0
1
0
0
1
0
0
0
1
0
0
0
0 2 1
1
0
1
1
1
0
0
1
0
0
0
0
0
0
0 3 1
1
1
0
1
0
0 3 1
1
1
0
1
1
Reference string is: 0,1,2,3,2,1,0,3,2,3
8
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:
A0
A1
A2
A3
A4
A5
10
7
5
4
6
3
B0
B1
B2
B3
B4
B5
B6
C1
C2
C3
Last
reference
time
A0
A1
A2
A3
A4
A6
A0
A1
A2
A3
A4
A5
9
4
6
2
5
6
B0
B1
B2
B3
B4
B5
B0
B1
B2
A6
B4
B5
12
3
5
6
B6
C1
C2
C3
B6
C1
C2
C3
Local
policy
Global
policy
10
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 are
Tp0 = 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
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
13
39
Tp - ref(frame)
13
12
Question 1: 2007a
:‫) הבאה‬inverted page table( ‫נתונה טבלת הדפים ההפוכה‬
Process / page
Ref-bit
Virtual time
P0 / 3
0
20
P2 / 0
0
60
P0 / 5
1
30
P1 / 3
1
100
P0 / 6
1
10
P2 / 5
0
70
P1 / 7
1
20
P1 / 2
1
20
13
‫‪Question 1: 2007a‬‬
‫מרחב הזיכרון הלוגי של כל תהליך ( ‪process-virtual‬‬
‫‪ )address space‬במערכת הוא בגודל שמונה דפים וגם‬
‫גודל הזיכרון הפיזי הינו שמונה דפים‪.‬‬
‫א‪.‬‬
‫נתון כי פונקצית ה‪ hash-‬דרכה ניגשים לטבלה‬
‫היא‪:‬‬
‫‪(process-number + page-number) mod 8‬‬
‫שרטטו במחברת את הטבלאות וההצבעות הרלוונטיות‬
‫למציאת הדפים בטבלה (כלומר את ה‪.)anchor table -‬‬
‫‪14‬‬
Question 1: 2007a
0
1
2
3
4
5
6
7
15
‫‪Question 1: 2007a‬‬
‫ב‪ .‬שרטטו את טבלאות הדפים הסטנדרטיות של שלושת‬
‫התהליכים ומלאו את המשבצות עבורן ניתנה‬
‫אינפורמציה בטבלה שלמעלה‪.‬‬
‫‪16‬‬
‫‪Question 1: 2007a‬‬
‫ג‪ .‬נניח כי אלגוריתם החלפת הדפים במערכת הינו אלגוריתם‬
‫)‪ .FIFO 2nd chance (clock‬המחוג של האלגוריתם מטייל‬
‫על דפי הזיכרון של הטבלה שלעיל מלמעלה למטה‪,‬‬
‫מתחיל בדף הראשון מלמעלה‪ ,‬יורד למטה‪ ,‬וחוזר חלילה‪.‬‬
‫נניח עוד כי המחוג מצביע כעת על הדף השלישי‬
‫מלמעלה‪ .‬שרטטו את מצב הטבלה (כולל התוספות‬
‫שהוספתם בסעיף א') לאחר שתהליך ‪ P0‬ביקש לקרוא‬
‫בייט מדף ‪ 4‬שלו ואלגוריתם ה‪ Clock-‬הופעל‪.‬‬
‫‪17‬‬
Question 1: 2007a
0
0
0
18
‫‪Question 1: 2007a‬‬
‫ד‪ .‬כעת נניח כי המערכת משתמשת באלגוריתם ‪WS-clock‬‬
‫וכי הזמנים הוירטואליים של הדפים בזיכרון הפיסי הם כפי‬
‫שמופיע בעמודה הימנית בטבלה שלמעלה‪ .‬נניח גם כי‬
‫המחוג של אלגוריתם ‪ WS-clock‬מצביע כעת על הדף‬
‫השלישי מלמעלה וכי ‪ .τ = 30‬נניח גם כי ערכי השעונים‬
‫הוירטואליים של התהליכים הינם‪:‬‬
‫‪ .Tp0 = 90, Tp1 = 150, Tp2 = 80‬שרטטו את מצב הטבלה‬
‫(כולל התוספות שהוספתם בסעיף א') לאחר שתהליך ‪P0‬‬
‫ביקש לקרוא בייט מדף מס' ‪ 4‬והמערכת הפעילה את‬
‫אלגוריתם ‪.WS-clock‬‬
‫‪19‬‬
Question 1: 2007a
Tp0 = 90, Tp1 = 150, Tp2 = 80
𝜏 = 30
20
Question 2
Program A:
Program B:
int i, j, a[100][100];
int i, j, a[100][100];
for (i = 0; i < 100; i++) {
for (j = 0; j < 100; j++) {
a[i][j] = 0;
}
}
for (j = 0; j < 100; j++) {
for (i = 0; i < 100; i++) {
a[i][j] = 0;
}
}
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.
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.
2.
3.
FIFO
LRU
Optimal
Note: Remember that all frames are initially empty, so your
first unique pages will all cost one fault each.
23
Question 3: FIFO
15 page faults
7
0
1
2
7
7
7
0
0
3
0
4
2
3
0
2
2
2
4
4
4
0
0
3
3
3
2
1
1
1
0
0
0
3
2
1
2
0
0
2
2
3
3
0
1
7
0
1
0
7
7
7
1
1
1
0
0
3
2
2
2
1
24
Question 3: LRU
12 page faults
7
0
1
2
7
7
7
2
0
0
1
0
3
0
4
2
3
0
3
2
1
2
0
1
7
2
4
4
4
0
1
1
1
0
0
0
0
3
3
3
0
0
1
3
3
2
2
2
2
2
2
0
1
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
7
7
7
2
2
2
2
2
7
0
0
0
0
4
0
0
0
1
1
3
3
3
1
1
0
1
26
‫‪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‬‬
‫‪27‬‬
Question 4 – 2001 a
.1
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
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
p
p
p
6
∞
5
3
4
Page fault
p
p
p
p
distance
∞
∞
∞
∞
3
4
p
p
∞
∞
4
4
2
28
‫‪Question 4 – 2001 a‬‬
‫• בשביל לחשב את מספר ה‪page-faults-‬‬
‫כשמשתמשים בזיכרון פיזי בן ‪ 5‬דפים‪ ,‬נצטרך‬
‫לסכום על כל המרחקים הגדולים מ‪ :5-‬ישנם ‪8‬‬
‫כאלו‪.‬‬
‫מנענו ‪ page-fault‬אחד‪.‬‬
‫‪29‬‬
Question 4 – 2001 a
.2
Page fault
1
2
3
4
2
1
5
6
2
1
2
3
7
6
3
2
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
p
p
p
p
p
p
p
p
p
p
p
p
30
‫‪Question 5 – 2005 a‬‬
‫• במערכת קיים זיכרון פיזי בגודל ‪ 5‬דפים‪ .‬נתון כי‬
‫אלגוריתם ההחלפה בו משתמשים הוא ‪FIFO-second-‬‬
‫‪ .chance‬תארו את הבעייתיות הנוצרת עבור כל אחד‬
‫מן ה‪ reference strings-‬הבאים והציעו פתרון‪ .‬הניחו‬
‫כי לא ניתן להגדיל את הזיכרון‪.‬‬
‫‪31‬‬
Question 5 – 2005 a
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
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
p p
fault
p
p p p
p
p
p
p p p
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, 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).
35
Question 6
Page fault
0
1
2
3
0
0
1
2
3
*0
0
0
*3
3
3
3
*2
2
*1
1
1
*0
*0
0
0
*3
*2
2
2
2
*1
1
1
pf
pf
pf
pf
pf
pf
pf
pf
Where:
* represents the placement of the clock’s “hand” before the request
pf represents a page fault
36
‫‪Question 7 – 2006 a‬‬
‫ב‪ .‬הגרף הבא מתאר את גודל ה‪ Working Set -‬של תהליך מסוים‬
‫בנקודת זמן ‪ t‬כתלות בגודל החלון ‪:k‬‬
‫)‪W(k,t‬‬
‫‪k‬‬
‫הסבירו כיצד ניתן לקבוע את ה‪ k -‬הרצוי על פי הגרף הנ"ל‪.‬‬
‫‪37‬‬
‫‪Question 7 – 2006 a‬‬
‫כל מדרגה בגרף מייצגת ‪ working set‬אחר‪ K .‬צריך‬
‫להיות מספיק גדול על מנת לכלול את כל ה ‪working set‬‬
‫הראשון אבל קטן מספיק כדי לא לכלול דפים מה‬
‫‪ working set‬השני‪ .‬לכן נקבע את גודלו להיות הנקודה‬
‫'‪ k‬בציור‪.‬‬
‫)‪W(k,t‬‬
‫‪38‬‬
‫‪k‬‬
‫'‪k‬‬
Question 7 – 2006 a
‫ לבין‬high level scheduling ‫הסבירו את הקשר בין‬
:‫הגרף הבא‬
degree of multiprogramming
39
‫‪Question 7 – 2006 a‬‬
‫תפקידו של ה‪ high level scheduler-‬הוא לקבוע את‬
‫מס' התהליכים בזיכרון‪.‬‬
‫כשה‪ CPU utilization -‬נמוך‪ ,‬ה ‪high level‬‬
‫‪ scheduler‬צריך לקבוע האם הסיבה לירידה ביעילות‬
‫היא‪:‬‬
‫– מיעוט תהליכים בזיכרון (הצד השמאלי של העקומה)‬
‫ורצוי שיוסיף תהליך לקבוצת התהליכים בזיכרון‪.‬‬
‫– ‪( thrashing‬הצד הימני של העקומה)‪ ,‬ועליו להוריד‬
‫תהליך מקבוצת התהליכים בזיכרון‪.‬‬
‫‪40‬‬
Download