Memory Management: Virtual Memory
Memory Management: Virtual Memory
Memory Management:
Virtual Memory
Objectives
You will be able to describe: (General)
■ The basic functionality of the memory allocation
2
Objectives (continued)
You will be able to describe: (General)
■ The mechanics of paging and how a memory allocation
multiprogramming
■ Cache memory and its role in improving system
response time
3
What is virtual memory?
■ It is a memory created by using the hard disk to
simulate additional random-access memory
4
What are the reasons of
developing the virtual memory?
■ Disadvantages of early schemes:
■ Required storing entire program in memory
■ Fragmentation
contiguously
■ Eliminate the need for entire program to reside in
5
Memory Allocation
■ Page Memory Allocation
■ Demand Page Memory Allocation
■ Segmented Memory Allocation
■ Segmented/Demand Page Memory Allocation
6
Objectives:
You will be able to describe: (1)
■ Define page memory allocation
7
Paged Memory Allocation
■ Divides each incoming job into pages of equal size
■ Works well if page size, memory block size (page
frames), and size of disk section (sector, block) are
all equal
■ Before executing a program, Memory Manager:
■ Determines number of pages in program
memory
■ Loads all of the program’s pages into them
8
Paged Memory Allocation
(continued)
10
Paged Memory Allocation
(continued)
11
Paged Memory Allocation
(continued)
12
Paged Memory Allocation
(continued)
■ Displacement (offset) of a line: Determines how
far away a line is from the beginning of its page
■ Used to locate that line within its page frame
number division
13
Paged Memory Allocation
(continued)
■ Steps to determine exact location of a line in
memory:
■ Determine page number and displacement of a line
14
Example
■ Determine the exact location of the Line 215 of Job 1 if
the memory size is 100 bytes and 1 bytes is equal to 1
line of code. Assuming the PMT has the following
information as shown in the table below:
15
Chapter 3
Memory Management:
Virtual Memory
18
What is Demand Paging?
■ It is a memory allocation schemes that load the pages
into memory only as they are needed, allowing jobs to
be run with less main memory
■ Uses the virtual memory (hard disk) as a temporary
storage of the jobs to be processed
19
What makes it possible to use load
only the demand page of the
program into the memory?
■ Programs are written sequentially so not all pages are
necessary at once. For example:
■ User-written error handling modules are processed
20
Mutually exclusive modules
■ void rectangleType::GetInput()
■ {
■ cout<<"Enter length:";
■ cin>>length;
■ cout<<"Enter width:";
■ cin>>width;
■ }
■ double rectangleType::ComputeResult()
■ {
■ return length*width;
■ }
■ double rectangleType::DisplayResult() const
■ {
■ cout<<"The area of the rectangle is " << ComputeResult();
■ }
21
Architecture of Demand Page
■ Requires use of a high-speed direct access storage
device that can work directly with CPU
■ A space in the hard disk used for virtual memory(2GB)
■ Swapping algorithms (predefined policies)
■ Table for monitoring the process
22
Demand Paging (continued)
■ The OS depends on following tables:
■ Job Table
23
Demand Paging (continued)
24
Demand Paging (continued)
■ Swapping Process:
■ To move in a new page, a resident page must be
25
Page Replacement Policies
and Concepts
■ Policy that selects the page to be removed; crucial
to system efficiency. Types include:
■ First-in first-out (FIFO) policy: Removes
26
Demand Paging (continued)
■ Page fault handler: The section of the operating
system that determines
■ Whether there are empty page frames in memory
27
Demand Paging (continued)
■ Thrashing : An excessive amount of page swapping
between main memory and secondary storage
■ Operation becomes inefficient
28
Demand Paging (continued)
■ Advantages:
■ Job no longer constrained by the size of physical
schemes
■ Disadvantages:
■ Increased overhead caused by the tables and the
page interrupts
29
Page Replacement Policies and
Concepts (continued)
30
Page Replacement Policies and
Concepts (continued)
33
FIFO Example
Page Requested: B A C A B D B A C D
Page Frame 1:
Page Frame 2:
Interrupt:
Time Snapshot:
Success Ratio: =
Failure Ratio: =
34
FIFO Example
Page Requested: A B A C A B D B A C D
Page Frame 1:
Page Frame 2:
Interrupt:
Time Snapshot: 1 2 3 4 5 6 7 8 9 10 11
Success Ratio: =
Failure Ratio: =
35
LRU Example
Page Requested: A B A C A B D B A C D
Page Frame 1:
Page Frame 2:
Interrupt:
Time Snapshot: 1 2 3 4 5 6 7 8 9 10 11
Success Ratio: =
Failure Ratio: =
36
LRU Exercises
Page Requested: A B A C A B D E A C A B D
Page Frame 1:
Page Frame 2:
Page Frame 3:
Interrupt:
Time Snapshot: 1 2 3 4 5 6 7 8 9 10 11 12 13
Success Ratio: =
Failure Ratio: =
37
Page Replacement Policies and
Concepts (continued)
• Initially, leftmost bit of its reference byte is set to 1, all bits
to the right are set to zero
• Each time a page is referenced, the leftmost bit is set to 1
• Reference bit for each page is updated with every time tick
swapped out
■ Modified bit: Indicates if page contents have been
altered
■ Used to determine if page must be rewritten to
39
The Mechanics of Paging
(continued)
Table 3.3: Page Map Table for Job 1 shown in Figure 3.5.
40
The Mechanics of Paging
(continued)
42
The Working Set (continued)
43
Chapter 3
Memory Management:
Virtual Memory
Topics:
1. Segmented Memory Allocation
2. Segmented/Demand Page Memory Allocation
Objectives:
■ After the discussion, the students should be able to:
■ Define segmented and segmented/page memory
allocation
■ Familiarize the architecture of segmented and
45
Segmented Memory Allocation
■ What is the problem of the demand page
memory allocation that gives birth the concept
of segmented memory allocation?
■ Thrashing
46
Segmented Memory Allocation
(cont.)
M=a*j;
printf(“\n%d %d %d”, j,k,m)
} Page 1
printf(“\n”);
An example of demand paging that causes a page swap each time the
loop is executed and results in thrashing. If only a single page frame
is available, this program will have one page fault each time the loop
is executed.
47
Segmented Memory Allocation
(cont.)
■ What is segmented memory allocation?
■ Each job is divided into several segments of
different sizes, one for each module that contains
pieces to perform related functions
■ Main memory is no longer divided into page
frames, rather allocated in a dynamic manner
■ Segments are set up according to the program’s
structural modules when a program is compiled or
assembled
48
#include <stdio.h>
main(){
double[] list={10,5,20,6,11,25};
double smallest=SmallestNumber(list);
double biggest=BiggestNumber(list);
DisplayResult(smallest,biggest);
}
double SmallestNumber(double[] l){
double smallest=l[0];
for(int x=0;x<l.length;x++)
if(smallest>l[x]) smallest=l[x];
return smallest;
}
double BiggestNumber(double[] l){
double biggest=l[0];
for(int x=0;x<l.length;x++)
if(biggest<l[x]) biggest=l[x];
return biggest;
}
void DisplayResult(double a,double b){
cout<<“The smallest is ” <<a <<“ and biggest number is ”<<b;
}
49
Segmented Memory Allocation
(cont.)
52
Segmented Memory Allocation
(continued)
■ Advantages:
■ Internal fragmentation is removed
■ Disadvantages:
■ Difficulty managing variable-length segments in
secondary storage
■ External fragmentation
53
Segmented Memory Allocation
(continued)
■ How does the computer knows the exact address of
the line of code in the main memory?
■ exact address = #line of code + starting address
54
Sample Problem
■ What is the exact address of the line 340 of segment
#0?
55
Segmented Memory Allocation
(continued)
■ Segment Memory allocation allows the sharing of
code.
■ A segment of code called copy of Microsoft Word can
be used also in Microsoft Power Point
56
Sample Problem
■ Given the following Segment Map Tables for two jobs:
SMT for Job 1 SMT for Job 2
Segment Number Memory Location Segment Number Memory Location
0 4096 0 2048
1 6144 1 6144
2 9216 2 9216
3 2048
4 7168
59
Segmented/Demand Paged
Memory Allocation (continued)
■ This scheme requires following four tables:
■ Job Table lists every job in process (one for the
whole system)
■ Segment Map Table lists details about each
60
Segmented/Demand Paged
Memory Allocation (continued)
■ Disadvantages:
■ Table handling overhead
62
Segmented/Demand Paged
Memory Allocation (continued)
■ How does the computer determine the exact
address of the line of code in the memory?
■ Compute the page and displacement of the line of
code
■ Determine what segments the line belong
63
Sample Problem
Topics:
1. Virtual and Cache Memory
Objectives
■ After the discussion, the students should be able to:
■ Define virtual memory and cache memory
cache memory
66
What is virtual memory
■ It is a technique that allows programs to be executed
even though they are not stored entirely in memory.
■ Requires cooperation between the Memory Manager
and the processor hardware
67
Example: Word processor
Main Memory Table Sub-Program Picture Sub-Program
Virtual memory
Main Memory
Main memory
68
Virtual Memory (continued)
■ Advantages of virtual memory management:
■ Job size is not restricted to the size of main memory
■ Memory is used more efficiently
■ Allows an unlimited amount of multiprogramming
■ Eliminates external fragmentation and minimizes internal
fragmentation
■ Allows the sharing of code and data
■ Facilitates dynamic linking of program segments
■ Disadvantages:
■ Increased processor hardware costs
■ Increased overhead for handling paging interrupts
■ Increased software complexity to prevent thrashing
69
Virtual Memory (continued)
Needed Entire
Program Program
70
Cache Memory
■ A small high-speed memory unit that a processor can
access more rapidly than main memory
■ Used to store frequently used data, or instructions
■ Movement of data, or instructions, from main
memory to cache memory uses a method similar to
that used in paging algorithms
71
Cache Memory (continued)
73
Cache Memory (continued)
■ Efficiency Calculation
■ Hit Ratio
74
Summary
■ Paged memory allocations allow efficient use of
memory by allocating jobs in noncontiguous memory
locations
■ Increased overhead and internal fragmentation are
problems in paged memory allocations
■ Job no longer constrained by the size of physical
memory in demand paging scheme
■ LRU scheme results in slightly better efficiency as
compared to FIFO scheme
■ Segmented memory allocation scheme solves internal
fragmentation problem
75
Summary (continued)
■ Segmented/demand paged memory allocation removes
the problems of compaction, external fragmentation,
and secondary storage handling
■ Associative memory can be used to speed up the
process
■ Virtual memory allows programs to be executed even
though they are not stored entirely in memory
■ Job’s size is no longer restricted to the size of main
memory by using the concept of virtual memory
■ CPU can execute instruction faster with the use of
cache memory
76