0% found this document useful (0 votes)
9 views42 pages

os9-p3c9-virtualmemory

The document discusses virtual memory, including its role, demand paging, page replacement algorithms, and frame allocation. It outlines the importance of managing memory efficiently to enhance multiprogramming and reduce page faults, with various strategies like FIFO and LRU for page replacement. Additionally, it addresses issues like thrashing and performance considerations in virtual memory management.

Uploaded by

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

os9-p3c9-virtualmemory

The document discusses virtual memory, including its role, demand paging, page replacement algorithms, and frame allocation. It outlines the importance of managing memory efficiently to enhance multiprogramming and reduce page faults, with various strategies like FIFO and LRU for page replacement. Additionally, it addresses issues like thrashing and performance considerations in virtual memory management.

Uploaded by

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

Virtual memory

Tran, Van Hoai

Faculty of Computer Science & Engineering


HCMC University of Technology

E-mail: hoai@hcmut.edu.vn
(partly based on slides of Le Thanh Van)

1 / 42
Outline

1 Background

2 Operations on virtual memory

3 Page-replacement algorithms

4 Frame allocation

5 Thrashing

6 Other consideration

2 / 42
Outline

1 Background

2 Operations on virtual memory

3 Page-replacement algorithms

4 Frame allocation

5 Thrashing

6 Other consideration

3 / 42
Role of virtual memory

There needs a mechanism to load partially a program into


physical memory for execution
Why do we need to load those into memory ? Partially loaded programs may provide some advantages
Code to handle unusual error Programmers can write program for extremely large virtual
conditions space
Array allocated largely, but seldomly User program takes less physical memory ⇒
used fully multiprogramming increases without any sacrifice of
response/turnaround time
Less I/O needed to load/swap programs into memory

4 / 42
Role of virtual memory

There needs a mechanism to load partially a program into


physical memory for execution
Why do we need to load those into memory ? Partially loaded programs may provide some advantages
Code to handle unusual error Programmers can write program for extremely large virtual
conditions space
Array allocated largely, but seldomly User program takes less physical memory ⇒
used fully multiprogramming increases without any sacrifice of
response/turnaround time
Less I/O needed to load/swap programs into memory

Virtual memory involves the


separation of logical memory as
perceived by users from
physical memory

5 / 42
Demand paging

Idea
Demand paging is a strategy to load pages only as they are
needed (pager is not swapper)

Valid-invalid bit is
used to check
memory resident of a
page

6 / 42
Steps in demand paging
Pure demand paging

Pure demand paging


= only bringing pages
into memory as
required

7 / 42
Hardware support

Page table: marking valid-invalid of a page


Secondary memory (disk): known as swap device or swap
space
An instruction may have page fault anywhere, restarting the
whole instruction is needed. Consider three-address instruction
ADD A, B, C.

1 Fetch and decode the instruction (ADD)


2 Fetch A
3 Fetch B
4 Add A and B
5 Store sum to C

8 / 42
Hardware support

Page table: marking valid-invalid of a page


Secondary memory (disk): known as swap device or swap
space
An instruction may have page fault anywhere, restarting the
whole instruction is needed. Consider three-address instruction
ADD A, B, C.

1 Fetch and decode the instruction (ADD) What happens if A, B, C


2 Fetch A cannot be in main
3 Fetch B memory at the time of
4 Add A and B
instruction ADD
5 Store sum to C
executed ?

9 / 42
Performance of demand paging
Denote p be probability of a page fault
Effective access time = (1 − p) × memory access time
+p × page fault time
Page fault time generally consists of
1 Service the page-fault interrupt
2 Read in the page
3 Restart the process

10 / 42
Performance of demand paging
Denote p be probability of a page fault
Effective access time = (1 − p) × memory access time
+p × page fault time
Page fault time generally consists of
1 Service the page-fault interrupt
2 Read in the page
3 Restart the process

If page fault time = 8ms and memory access time = 200ns,


then

Effective access time (in ns) = 200 + 7, 999, 800 × p

If page-fault rate p = 1/1000,


then effective access time = 8.2 microseconds (≈ 40 ×
200ns). 11 / 42
Outline

1 Background

2 Operations on virtual memory

3 Page-replacement algorithms

4 Frame allocation

5 Thrashing

6 Other consideration

12 / 42
Copy-on-write

Copy-on-write
Rapid process creation
Minimizing the number of new pages allocated to newly
created processes
Before

Applied for fork()


vfork() has no
copy-on-write (more
After process 1 modifies page C efficient), and
parent process is
suspended

13 / 42
Page replacement
Some processes can utilize less pages than demanding
⇒ In order to increase multiprogramming degree, OS could
over-allocating memory (when increasing multiprogramming
degree)

14 / 42
Page replacement
Some processes can utilize less pages than demanding
⇒ In order to increase multiprogramming degree, OS could
over-allocating memory (when increasing multiprogramming
degree)

No free frames. Options:


- kill the process
- swap out other process
- page replacement

15 / 42
Basic page replacement

1 Find location of desired page on disk


2 Find a free frame
a. if there is a free frame, use it
b. if there is no free frame, call
page-replacement algorithm
to select a victim frame
c. Write victim to disk and
change status of related data
structures
3 Read desired page into frame,
change data structures
4 Continue the process where page
fault occurred

16 / 42
Basic page replacement

1 Find location of desired page on disk


2 Find a free frame
a. if there is a free frame, use it
b. if there is no free frame, call
page-replacement algorithm
to select a victim frame
c. Write victim to disk and
change status of related data
structures
3 Read desired page into frame,
change data structures
4 Continue the process where page
fault occurred

Minor tunnings
Many stuffs in above scheme can be modified to get better
performance, such as
modify bit (no need to write pages without any modification).
Read-only pages can be discarded when desired.

17 / 42
Outline

1 Background

2 Operations on virtual memory

3 Page-replacement algorithms

4 Frame allocation

5 Thrashing

6 Other consideration

18 / 42
Page replacement algorithm

Questions
How many frames allocated for a process ?
Which frame to be replaced ?
Different programs have different memory references
(namely reference string)

Recording an address sequence as follows


0100, 0432, 0101, 0612, 0102, 0103, 0104, 0101, 0611, 0102, 0103, 0104,
0101, 0610, 0102, 0103, 0104, 0609, 0102, 0105

100 bytes per page, then address sequence converted to


reference string
1, 4, 1, 6, 1 , 6, 1, 6, 1, 6, 1

19 / 42
FIFO page replacement

Let’s try the following reference string for three-frame memory


7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1

20 / 42
FIFO page replacement

Let’s try the following reference string for three-frame memory


7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1

FIFO page replacement


Choose next page to be replaced in FIFO scheme

15 page faults

21 / 42
Optimal replacement algorithm

Bélády’s anomaly for FIFO


Number of available frames
increases, but page fault does
increase (proved to be
unbounded (2010))

22 / 42
Optimal replacement algorithm

Bélády’s anomaly for FIFO


Number of available frames
increases, but page fault does
increase (proved to be
unbounded (2010))

Optimal (MIN/OPT) page replacement


Replace the page that will not be used for the longest period
of time

23 / 42
Optimal replacement algorithm

Bélády’s anomaly for FIFO


Number of available frames
increases, but page fault does
increase (proved to be
unbounded (2010))

Optimal (MIN/OPT) page replacement


Replace the page that will not be used for the longest period
of time

9 page faults

24 / 42
Optimal replacement algorithm

Bélády’s anomaly for FIFO


Number of available frames
increases, but page fault does
increase (proved to be
unbounded (2010))

Optimal (MIN/OPT) page replacement


Replace the page that will not be used for the longest period
of time

9 page faults

There is no information of the future 25 / 42


LRU replacement algorithm

LRU page replacement


LRU (Least recently used) chooses the page that has not been
used for the longest period of time

26 / 42
LRU replacement algorithm

LRU page replacement


LRU (Least recently used) chooses the page that has not been
used for the longest period of time

27 / 42
LRU replacement algorithm

LRU page replacement


LRU (Least recently used) chooses the page that has not been
used for the longest period of time

We need hardware support to implement LRU algorithm.


Counters: an additional field in each page-table entry to store time-of-use
(clock or counter). Referencing a page → copying storing clock register to
time-of-use field
Stack: using the idea of a stack, “Last In First Out”

28 / 42
Other algorithms

LRU-approximation page replacement


Additional-reference-bits algorithm
Second-chance algorithm
Enhanced second-chance algorithm
Counting-based page replacement
Least frequently used (LFU)
Most frequently used (MFU)
Page-buffering algorithm

29 / 42
Outline

1 Background

2 Operations on virtual memory

3 Page-replacement algorithms

4 Frame allocation

5 Thrashing

6 Other consideration

30 / 42
Minimum number of frames

Minimum number of frames is defined by computer


architecture
IBM MVC instruction needs
a) 6 bytes (can straddle 2 pages)
b) source location (2 pages)
c) destination location (2 pages)

→ 6 pages totally

Number levels of indirection should be limited

31 / 42
Allocation algorithms
There are m frames
Equal allocation: n processes → b mn c for each process
Proportional allocation: virtual memory size of pi is si
X
a i = m × si / si

ai possibly depends on a combination of size and priority


of the process

32 / 42
Allocation algorithms
There are m frames
Equal allocation: n processes → b mn c for each process
Proportional allocation: virtual memory size of pi is si
X
a i = m × si / si

ai possibly depends on a combination of size and priority


of the process
Performance issues
Global vs. local allocation
Global replacement: replacement frame is any in the set of all
frames
Local replacement: replacement frame is in its own set of
allocated frames
Non-uniform memory access (NUMA)
33 / 42
Outline

1 Background

2 Operations on virtual memory

3 Page-replacement algorithms

4 Frame allocation

5 Thrashing

6 Other consideration

34 / 42
Thrashing
A process has no enough frames. Page faults occur frequently.
Time for paging is more than that for execution.

35 / 42
Thrashing
A process has no enough frames. Page faults occur frequently.
Time for paging is more than that for execution.
Thrashing
High paging activity is called thrashing

36 / 42
Working with thrashing

We can limit effects of thrashing by


Using local replacement algorithm, not steal frames from another
process
Working-set strategy: using a locality model (locality = set of
pages that are actively used together)
A function has its own
locality which consists of
its local variables
subset of global
variables

37 / 42
Working-set model

∆: working-set window
Working set: set of pages in the most recent ∆ pages
references
∆ = 10

Denote WSSi be working-set size of process i. Total demand is


X
D= WSSi

We can monitor D and respond accordingly when D > m


38 / 42
Page-fault frequency (PFF)

39 / 42
Outline

1 Background

2 Operations on virtual memory

3 Page-replacement algorithms

4 Frame allocation

5 Thrashing

6 Other consideration

40 / 42
Prepaging: prevent large number of page faults that
occur when a process is started
Page size: choice of page sige is often performed in OS
design
TLB reach
Inverted page tables
Program structure
int i, j; int i, j;
int data[128][128]; int data[128][128];

for( j = 0; j < 128; j++ ) for( i = 0; i < 128; i++ )


for( i = 0; i < 128; i++ ) for( j = 0; j < 128; j++ )
data[i][j] = 0; data[i][j] = 0;

41 / 42
Prepaging: prevent large number of page faults that
occur when a process is started
Page size: choice of page sige is often performed in OS
design
TLB reach
Inverted page tables
Program structure
int i, j; int i, j;
int data[128][128]; int data[128][128];

for( j = 0; j < 128; j++ ) for( i = 0; i < 128; i++ )


for( i = 0; i < 128; i++ ) for( j = 0; j < 128; j++ )
data[i][j] = 0; data[i][j] = 0;

If OS allocates fewer than 128 frames of 128 words for entire


program, the left code has 128 × 128 = 16, 384 page faults.
The right code possibly has only 128 page faults.
42 / 42

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy