0% found this document useful (0 votes)
25 views30 pages

Ch9 Virtual Memory

Uploaded by

khan.pg4100224
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views30 pages

Ch9 Virtual Memory

Uploaded by

khan.pg4100224
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Virtual Memory

Background
• Virtual memory – program uses virtual memory which can be partially
loaded into physical memory

• Benefits:
– Only part of the program needs to be in memory for execution
• more concurrent programs
– Logical address space can therefore be much larger than physical
address space
• execute programs larger than RAM size
– Easy sharing of address spaces by several processes
• Library or a memory segment can be shared
– Allows for more efficient process creation

2
Virtual Memory That is Larger Than Physical
Memory
Page 0
0
Page 1 1
2 Page
3 Page22 Page 0 Page 1
Page 2
4 unavail
unavail
Page 3 Page 2 Page 3
Page
Page00 move
Page 4 pages
… unavail
unavail Page 4

… Page
Page33
n-2 Page
n-1 Page11 page n-2 Page n-1
page table Physical memory
page n-2
page n-1 all pages of program sitting on physical Disk
Virtual memory
3
A typical virtual-address space layout of a
process
function parameters;
local variables;
return addresses

unused address space

will be used whenever


malloc() allocates needed
space from here
(dynamic memory
allocation)

global data (variables)

4
Shared Library Using Virtual Memory
Virtual memory of process A Virtual memory of process B

only one copy of


a page
needs to
be in memory

5
Implementing Virtual Memory
• Virtual memory can be implemented via:

– Demand paging
• Bring pages into memory when they are used, i.e. allocate memory for
pages when they are used

– Demand segmentation
• Bring segments into memory when they are used, i.e. allocate memory for
segments when they are used.

6
Policies for Paging/ Segmentation
• Fetch Strategies
– When should a page or segment be brought into primary memory from
secondary (disk) storage?
• Demand Fetch
• Anticipatory Fetch
• Placement Strategies
– where is it to be put?
• Paging - trivial
• Segmentation - significant problem
• Replacement Strategies
– Which page/segment should be replaced if there is not enough room for a
required page/segment?
Demand Paging
• Bring a page into memory only when it is needed
– Less I/O needed
– Less memory needed
– Faster response
– More users

• Page is needed  reference to it


– invalid reference (page is not in used portion of address space)  abort
– not-in-memory  bring to memory

• Pager never brings a page into memory unless page will be needed

8
Valid-Invalid Bit
• With each page table entry a valid–invalid bit is associated
(v  in-memory, i  not-in-memory)
• Initially valid–invalid bit is set to i on all entries
• Example of a page table snapshot: Frame # valid-invalid bit
v
v
v
v
i
….

i
i
page table
• During address translation, if valid–invalid bit in page table entry
is i  page fault

9
Page Table When Some Pages Are Not in Main Memory

10
Page Fault
• When CPU makes a memory reference (i.e. page reference), HW consults the page
table. If entry is invalid, then exception occurs and kernel gets executed.
Kernel handling such as case:
1. Kernel looks at another table to decide:
– Invalid reference (page is in unused portion of address space)  Abort
– Just not in memory (page is in used portion, but not in RAM)  Page Fault
2. Get empty frame
(we may need to remove a page; if removed page is modified, we need disk I/O to
swap it out)
3. Swap page into frame
(we need disk I/O)
4. Reset tables (install mapping into page table)
5. Set validation bit = v
6. Restart the instruction that caused the page fault

11
Page Fault (Cont.)
• If page fault occurs when trying to fetch an instruction, fetch the instruction
again after bringing the page in.
• If page fault occurs while we are executing an instruction: Restart the
instruction after bringing the page in.

12
Steps in Handling a Page Fault

swap
space

13
What happens if there is no free frame?
• Page replacement – find some page in memory, but not really in use, swap it out

– Algorithm ? Which page should be remove?

– performance – want an algorithm which will result in minimum number of


page faults

• With page replacement, same page may be brought into memory several times

14
Need For Page Replacement

While executing “load M”


we will have a page
fault and we need
page replacement.
15
Basic Page Replacement

Steps performed by OS while replacing a page upon a page fault:

1. Find the location of the desired page on disk

2. Find a free frame:


- If there is a free frame, use it
- If there is no free frame, use a page replacement algorithm to select a victim
frame; if the victim page is modified, write it back to disk.

3. Bring the desired page into the (new) free frame; update the page and frame
tables

4. Restart the process

16
Page Replacement

17
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 is

1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

18
Driving reference string
• Assume process makes the following memory references (in decimal) in a system
with 100 bytes per page:

• 0100 0432 0101 0612 0102 0103 0104 0101 0611 0102 0103 0104 0101
0610 0102 0103 0104 0609 0102 0105
• Example: Bytes (addresses) 0…99 will be in page 0
• Pages referenced with each memory reference
– 1, 4, 1, 6, 1, 1, 1, 1, 6, 1, 1, 1, 1, 6, 1, 1, 6, 1, 1

• Corresponding page reference string


– 1, 4, 1, 6, 1, 6, 1, 6, 1, 6, 1

19
First-In-First-Out (FIFO) Algorithm
• 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)

1 1 4 5
2 2 1 3 9 page faults

• 4 frames 3 3 2 4

1 1 5 4
2 2 1 5 10 page faults
3 3 2

4 4 3
• Belady’s Anomaly: more frames  more page faults
20
FIFO Page Replacement

21
Optimal Algorithm
• Replace page that will not be used for longest period of time
• 4 frames example
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

1 4
2 6 page faults
3

4 5

• How do you know this?


• Used for measuring how well your algorithm performs

22
Optimal Page Replacement

23
Least Recently Used (LRU) Algorithm
• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

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

8 page faults

24
LRU Page Replacement

25
LRU Algorithm Implementation
• Counter implementation

– Every page entry has a counter field; every time page is referenced through
this entry, copy the clock into the counter field

– When a page needs to be replaced, look at the counters to determine which


one to replace
• The one with the smallest counter value will be replaced

26
LRU Algorithm Implementation
• Stack implementation – keep a stack of page numbers
– Page referenced:
• move it to the top
– No search for replacement (replacement fast)

27
Buddy System Allocator

28
Example
• Object A needs memory 45 KB in size
• Object B needs memory 70 KB in size
• Object C needs memory 50 KB in size
• Object D needs memory 90 KB in size

• Object C removed
• Object A removed
• Object B removed
• Object D removed

29
Example
512 KB of Memory (physically contiguous area)

A C B D

Alloc A 45 KB
512
Alloc B 70 KB
Alloc C 50 KB
256 256 Alloc D 90 KB
Free C
Free A
128 128(B)
128 128
128(D) 128 Free B
Free D

64(A)
64 64(C)
64

30

You might also like