Post on 24-Dec-2015
CSS430 Virtual Memory 1
CSS430 Virtual MemoryCSS430 Virtual MemoryTextbook Ch9Textbook Ch9
These slides were compiled from the OSC textbook slides (Silberschatz, Galvin, and Gagne) and the instructor’s class materials.
CSS430 Virtual Memory 2
Background Why dynamic loading and overlays are not enough
Users must handle unusual error conditions Users need to keep track of which portion of data are loaded. While programs load data portion by portion, even such a loaded
portion is never referred. Virtual memory – separation of user memory from physical memory.
Only part of the program needs to be in memory for execution. Logical space can be much larger than physical address space. Need to allow pages to be swapped in and out.
Benefits No more constraints by physical memory Less physical memory needed, thus increasing multiprogramming
degree Less I/O needed, thus improving performance
CSS430 Virtual Memory 3
Virtual Memory Page is referenced Check memory map If the corresponding
entry points a physical memory frame,
Reference it. Otherwise, the page
does not exit in physical memory
Bring it from to disk to memory
If physical memory is full,
Find a victim page Swap it out to disk
Page 0
Page 1
Page 2
Page n
Page 5
Page 3
Page 4
Page 7
Page 8
Virtual memory
Physical memory
Memory mapPage table
CSS430 Virtual Memory 4
Swapping v.s. Demand Paging
Swapping All pages composing
a program are brought into memory and taken out to disk.
Demand Paging Only pages currently
referenced by a program are brought into memory and taken out to disk.
210 3
654 7
1098 11
141312 15
181716 19
222120 23
Physical memory
Program A
Program B
Swap out
Swap in
CSS430 Virtual Memory 5
Page Table Managing VM If a frame bit is valid,
the frame is in memory
Otherwise, a page fault occurs.
The corresponding page is loaded from disk
A new memory space is allocated.
C
D
E
F
B
A0
1
2
3
4
5
6
7
C
F
A
0
1
2
3
4
5
6
7
8
9
10
11
6
9
4
viivii
iv
frame01234567
Page table
Logical memory
Physical memory
A B
C D E
F
?
CSS430 Virtual Memory 6
Page Fault
load Mi
OperatingSystem
free frame
Physical memory
Disk
Page table1. Reference to M(Does M’s page exists?)
2. Trap on page fault
3. OS looks at another table to decide:Invalid reference abort.Just not in memory.
4. Bring in missing page
5. Reset tables, validation bit = 1
6. Restart instruction
CSS430 Virtual Memory 7
Page Replacement No more free frame Find a victim page
Paging out M cause another page fault Paging out H may be okay. Algorithm to minimize page faults
H
Load M
J
M
A
B
D
E
OS
OS
D
H
J
A
E
3 vi
5 vi
6 vi
2 v7 v
Process 1’s logical memory
Process 2’s logical memory
Proc1’s page table
Proc2’s page table
0
1
2
1
0
1
2
3
0
1
2
3
4
5
6
7
Physical memory
PC
PC
Load M
M
4 v
B?
CSS430 Virtual Memory 8
Page Replacement Mechanism
f v d
frame
valid/invalid
dirty/clean
Page table
1. If it’s dirty, swap out a victim page
3. Overwrite this frame with a desired page
0 i c
2. Clear all bits of theswapped frame entry
0 i c 4. Reset a frame entryfor this new page
f v c
CSS430 Virtual Memory 9
Page Replacement Algorithms
Want lowest page-fault rate. Evaluate algorithm by running it on a particular
string of memory references (reference string) and computing the number of page faults on that string.
In all our examples, the reference string is7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7.
Algorithms FIFO (The oldest page may be no longer used.) OPT (All page references are known a priori.) LRU (The least recently used page may be no
longer used.) Second Chance (A simplest form of LRU)
CSS430 Virtual Memory 10
Discussions 1 Consider data structures and
algorithms that fits LRU better than FIFO. Repetitive binary tree operations Quick sort over a large array Sieve of Eratosthenes using a large array Are there any examples?
In terms of the worst-case analysis, which is better?
CSS430 Virtual Memory 11
First-In-First-Out (FIFO) Algorithm Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7. 3 frames (3 pages can be in memory at a time per process)
4 frames
more frames less page faults
7
0
2
3
7
0
0
2
3
7
0
1
2
0
1
2
0
1
2
3
1
2
3
0
4
3
0
4
2
0
4
2
3
0
2
3
0
1
3
0
1
2
0
1
2
0
1
2
7
1
2Pagefault
Pagehit
Pagehit
Pagehit
Pagehit
Pagehit
7
3
4
0
7
0
3
4
0
7
0
1
7
0
1
7
0
1
3
0
1
3
0
1
3
4
1
3
4
1
3
4
1
3
4
0
3
4
0
2
4
0
2
4
0
2
4
0
2
7
0
2 2 2 2 2 2 2 2 2 2 1 1 1 1 1Pagehit
Pagehit
Pagehit
Pagehit
Pagehit
Pagehit
Pagehit
Pagehit
CSS430 Virtual Memory 12
How about this example? Reference string: 1,2,3,4,1,2,5,1,2,3,4,5. 3 frames (3 pages can be in memory at a time per process)
4 frames
If more frames more page faults Belady’s anomaly
1
5
3
4
1
2
5
3
4
1
2
3
4
2
3
4
1
3
4
1
2
5
1
2
5
1
2
5
1
2
5
3
2Pagefault
Pagehit
Pagehit
1
4
5
2
1
2
4
1
2
1
2
3
1
2
3
1
2
3
1
2
3
5
2
3
5
1
3
5
1
2
5
1
2
3 3 4 4 4 4 4 4 3Pagehit
Pagehit
Pagehit
CSS430 Virtual Memory 13
Optimal (OPT) Algorithm Replace page that will not be used for longest period of
time. 3 frames example 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7.
How do you know this? Used for measuring how well your algorithm performs.
7
2
0
3
7
0
2
0
3
7
0
1
2
0
1
2
0
1
2
0
3
2
0
3
2
4
3
2
4
3
2
4
3
2
0
3
2
0
1
2
0
1
2
0
1
2
0
1
7
0
1Pagefault
Pagehit
Pagehit
Pagehit
Pagehit
Pagehit
Pagehit
Pagehit
Pagehit
Pagehit
CSS430 Virtual Memory 14
Least Recently Used (LRU) Algorithm
Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7.
Counter implementation Every page entry has a counter; every time page is referenced through this
entry, copy the clock into the counter. When a page needs to be changed, look at the counters to determine which
are to change. Stack implementation – keep a stack of page numbers in a double link form:
Page referenced, move it to the top No search for replacement
7
0
3
2
7
0
0
3
2
7
0
1
2
0
1
2
0
1
2
0
3
2
0
3
4
0
3
4
0
2
4
3
2
0
3
2
1
3
2
1
3
2
1
0
2
1
0
2
1
0
7Pagefault
Pagehit
Pagehit
Pagehit
Pagehit
Pagehit
Pagehit
CSS430 Virtual Memory 15
Second-Chance Algorithm Reference bit
Each page has a reference bit, initially = 0 When page is referenced, bit set to 1.
Algorithm All pages are maintained in a circular list. If page to be replaced,
1. If the next victim pointer points to a page whose reference bit = 0
1. Replace it with a new brought-in page
2. Otherwise, (i.e.,if the reference bit = 1), reset the bit to 0.
3. Advance the next victim pointer.4. Go to 1.
0
0
0
1
1
0
1
Nextvictim
Find it!
0
0
CSS430 Virtual Memory 16
Discussions 2 Consider the matrix multiplication The first algorithm is an ordinary
multiplication, (i.e., row * column)
The second multiplication is to flip the matrix B in diagonal and perform the multiplication in row basis, (i.e., row * row).
Which performs better? Why?
// Matrix Multiplication 1 for ( int i = 0; i < SIZE; i++ ) for ( int j = 0; j < SIZE; j++ ) for ( int k = 0; k < SIZE; k++ )
c[i][j] += a[i][k] * b[k][j];
// Matrix Multiplication 2 for ( int i = 0; i < SIZE; i++ ) for ( int j = 0; j < SIZE; j++ ) for ( int k = 0; k < SIZE; k++ )
c[i][j] += a[i][k] * b[j][k];
X
Flip diagonally
X
CSS430 Virtual Memory 17
Page Allocation Algorithm #frames = m, #processes = n Equal allocation
m/n frames are allocated to each process. Proportional allocation
(Pi’s vm size / pi’s vm size) * m frame are allocated to each process.
This allocation is achieved regardless of process priority Priority allocation
A process with a higher priority receives more frames. Global versus local allocation
Global: can steal frames from another process. Local: must find out a victim page among pages
allocated to this process.
CSS430 Virtual Memory 18
Thrashing If a process does not have “enough” pages, the page-fault
rate goes very high. This leads to: low CPU utilization. operating system thinks that it needs to increase the
degree of multiprogramming. another process added to the system. The new process requesting pages Thrashing increased
CSS430 Virtual Memory 19
Preventing Thrashing Local (or priority) replacement algorithm
Effect: A thrashing process cannot steal frames from another process and thus trashing will not be disseminated.
Problem: Thrashing processes causes frequent page faults and thus degrade the average service time.
Locality model Process migrates from one locality to
another. When size of locality > total memory size, a
thrashing occurs. Suspend some processes
CSS430 Virtual Memory 20
Working-Set Model working-set window, a fixed number of page
references
Page reference sequence…2 6 1 5 7 7 7 7 5 1 6 2 3 4 1 2 3 4 4 4 3 4 3 4 4 4 1 3 2 3 4 44 4 3 4 4 4 …
t1 t2WS(t1) = { 1, 2, 5, 6, 7} WS(t2) = { 3, 4 }
CSS430 Virtual Memory 21
Controlling Multiprogramming Level by Working-Set Model WSSi (working set size of Process Pi) =
total number of pages referenced in the most recent if too small will not encompass entire locality. if too large will encompass several localities. if = will encompass entire program.
D = WSSi total frames demanded by all processes if D > m Thrashing Policy if D > m, then suspend one of the processes.
P0: 1, 2, 3, 2, 3, 3, 2, 2, 1, 2, 3, 2, 1, 2, 3, 1WSS0 1, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, 3, 2, 3, 3
P1: 5, 5, 5, 6, 7, 5, 7, 7, 7, 6, 6, 7, 5, 7, 6, 7WSS1 1, 1, 1, 2, 3, 3, 2, 2, 1, 2, 2, 2, 3, 2, 3, 2
P2: 8, 9, 9, 8, 4, 8, 8, 8, 4, 9, 8, 4, 9, 8, 4, 9WSS2 1, 2, 2, 2, 3, 2, 2, 1, 2, 3, 3, 3, 3, 3, 3, 3
WSSi 3, 5, 6, 6, 8, 7, 6, 5, 4, 7, 8, 7, 9, 7, 9, 8
When = 3, m = 8
Thrashing
CSS430 Virtual Memory 22
Page-Fault Frequency Scheme
Establish “acceptable” page-fault rate. If actual rate too low,
Process may have too many frames, and thus it loses frames.
If actual rate too high, Process needs more frames, and thus it gains frames
CSS430 Virtual Memory 23
Exercises Programming Assignment 4:
Check the syllabus for its due date. No turn-in problems:
1. Assuming a physical memory of four page frames, give the number of page faults for the reference string 1,2,6,1,4,5,1,2,1,4,5,6,4,5 for each of the following replacement algorithms. (Initially, all frames are empty.)
1. OPT2. FIFO3. Second-chance4. LRU5. Working Set with =3 (A page not reference within
will be paged out.)2. Solve Exercise 9.7 of your textbook.3. Solve Exercise 9.28 of your textbook.