Brendan Ang

Search

Search IconIcon to open search

Virtual Memory

Last updated Nov 8, 2022 Edit Source

# Virtual Memory

In memory organisation, we assumed that for each program, its entirety has to be loaded into the memory. This means that the overall program size must be restricted to the size of physical memory.

[!Aim: size of virtual memory limited by the address scheme of the computer and not the actual size of physical memory]

# Swapping

A process can be swapped temporarily out of memory into a backing store.

# Demand Paging

To support implementation of virtual memory, demand paging is used. Each process is divided into pages and each page can be loaded when it is in demand. Initially, all pages are not in memory.

# Page Fault

# Thrashing

Consider what occurs if a process does not have “enough” frames—that is, it does not have the minimum number of frames it needs to support pages in the working set. The process will quickly page-fault. At this point, it must replace some page. However, since all its pages are in active use, it must replace a page that will be needed again right away. Consequently, it quickly faults again, and again, and again, replacing pages that it must bring back in immediately. This high paging activity is thrashing. To prevent thrashing, we must provide a process with as many frames as it needs.

# Working Set Model

Based on the concept of locality of reference: processes tend to refer to pages in a localised manner Temporal locality: locations referred to recently are likely to be referenced again Spatial locality: code and data are usually clustered physically Implementation: Additional metrics:

# Page Fault Frequency

# Page Replacement

If there are no empty frames, OS needs to locate a victim to evict:

# Belady’s Anomaly

500

[!Inclusion Property] Pages loaded in n frames is always a subset of pages in n+1 frames

An algorithm does not suffer from Belady’s anomaly if it satisfies the inclusion property. This is because such an algorithm will only increase its total coverage of available frames and does not replace any frame that was previously loaded.

An example with FIFO:

400

# Policies

Page Replacement Policies

# Allocation of Frames

# Fixed allocation

Give a process a fixed number of frames in memory for execution.

# Variable allocation

Allow the number of frames allocated to the process to vary across its execution lifetime.

# Scope of Replacement

# Global

Process selects a replacement frame from the set of all frames; one process can take a frame from another process. Implication: performance of the process depends on external processes.

# Local

Each process selects only from its own set of allocated frame. Implication: may hinder other processes by not making available its less used pages/frames

# Other Considerations

# Program Structure

Specific choice in data structures used and program structure can affect the performance of demand paging.

# Practice Problems

a. FIFO will replace the earliest loaded page. Page 2 b. Replace the first page with R=0. Page 0. c. Replace the oldest accessed page. Page 1.

TickPage RefFIFOClockLRULoadedRAccessed
10000101
210,10,10,11,20,01,2
360,1,60,1,60,1,61,2,30,0,01,2,3
400,1,60,1,60,1,61,2,31,0,04,2,3
530,1,6,30,1,6,30,1,6,31,2,3,51,0,0,04,2,3,5
644,1,6,3 F0,4,6,3 F0,4,6,3 F6,2,3,50,0,0,04,6,3,5
704,0,6,3 F0,4,6,30,4,6,36,7,3,51,0,0,07,6,3,5
814,0,1,3 F0,4,1,3 F0,4,1,3 F6,7,8,50,0,0,07,6,8,5
904,0,1,30,4,1,30,4,1,36,7,8,51,0,0,09,6,8,5
1034,0,1,30,4,1,30,4,1,36,7,8,51,0,0,19,6,8,10
1144,0,1,30,4,1,30,4,1,36,7,8,50,0,0,19,11,8,10
1264,0,1,6 F0,4,6,3 F0,4,6,3 F6,7,8,120,0,1,19,11,12,10
1333,0,1,6 F0,4,6,30,4,6,313,7,8,120,0,1,19,11,12,13
1443,4,1,6 F0,4,6,30,4,6,313,14,8,120,1,1,19,14,12,13
The first 4 page loads are always page faults.
a. $4+6=10$
b. $4+3=7$
c. $4+3=7$
a.
If N <= M:
No more page faults will occur after all distinct pages are loaded into memory.
Lower bound = N, Upper bound = N
If N > M:
Lower bound occurs on the minimum number of page faults. Every unique page will result in a page fault = N
Upper bound occurs when every page reference is a page fault = L
b. No. LRU does not guarantee optimality.
a.
Page RefLRUAccessedFault
——–——-——————–
-1,0,3,2161,160,162,163N
41,4,3,2161,164,162,163Y
00,4,3,2165,164,162,163Y
00,4,3,2166,164,162,163N
00,4,3,2167,164,162,163N
20,4,3,2166,164,162,167N
40,4,3,2166,168,162,167N
20,4,3,2166,168,162,169N
10,4,1,2166,168,170,169Y
00,4,1,2171,168,170,169N
30,3,1,2171,172,170,169Y
20,3,1,2171,172,170,173N
4 Page Faults.
b.
Page RefWorking SetFault
——–———–—–
-0,1,3,2-
41,3,2,4Y
03,2,4,0Y
02,4,0N
04,0N
20,2Y
40,2,4Y
20,4,2N
14,2,1Y
04,2,1,0Y
32,1,0,3Y
21,0,3,2N
7 page faults.
Illustrates a thrashing situation.
a. F. CPU is already under utilised
b. T. Increase available memory for pages to be loaded for processes False. A larger paging disk does not allow for more page frames in memory.
c. F. Already not enough memory for existing programs
d. T. Swap out some programs to the backing store to allow for more pages for existing programs
e. T. More pages can be stored in memory
f. T. Less time spent in I/O
g. F.