View
224
Download
2
Tags:
Embed Size (px)
Citation preview
Chapter 9 Virtual Memory
Produced by Lemlem Kebede
Monday, July 16, 2001
What is Virtual Memory Swaps data in and out of physical
memory. Make physical memory appear to
the CPU that there is more physical memory than is actually present.
The three main reasons for using Virtual Memory1. To free programmers from the need to carry out storage
allocation and to permit efficient sharing of memory space among different users.
2. To make programs independent of the configuration and capacity of the memory systems used during their execution. 3. To achieve the high access rates and low cost Per bit that is possible with a memory hierarchy.
What does Virtual Memory really do? Virtual memory places open
applications that are not active and portions of active applications on your hard drive and only keeps what is necessary for functionality in real RAM.
Memory Management Unit (MMU)
Moves data between physical memory and some slower storage device, usually a disk.
It translates the CPU address (logical address) to physical memory address.
It moves data from the swap disk to physical memory and from physical memory back to the swap.
How the MMU convert logical address output from the CPU to a physical address
CPU MMUPhysicalMemory
Swap Disk
Controlsignal
Logical
address
Physical
address
Two primary methods to implement virtual memory
Paging Segmentation
Paging and Frames In paging, the entire range of logical
addresses is divided into contiguous blocks called pages.
Each pages is the same size and each logical address is resides in exactly one page.
Physical memory is divided into non-overlapping frames
The size of each frame is the same as the size of one page
paging Moves pages from the swap disk to
frames of the physical memory so data can be accessed by the processor. Any page can occupy any frame. This leads to several issues must be resolved by a paging system.
Paging issues that are handled
by the MMU
1. When should a page be moved into physical memory?
2. How does the CPU find data in physical memory?
3. What happens when all the frames have page and the CPU needs to access data from a page not currently stored in physical memory?
Paging Example: Simple CPU
Physical memory space –16K Logical address space –64k Page size –4k 4 frames in physical memory 4k
Page F
Page 0
Page 1
Page 2
FFFFH F000H
2FFFH 2000H 1FFFH 1000H 0FFFH 0000H
Frame 3
Frame 2
Frame 1
Frame 0
3FFFH 3000H 2FFFH 2000H
1FFFH 1000H 0FFFH 0000H
Logical memory Address space
Physical memory...
Page Table The MMU uses a page table to
keep track of which pages are stored in which frames.
The MMU uses a page table to calculate the required physical addresses.
Example of Page table and corresponding Physical Memory
0 1 1 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 0 1 1
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 1 0 0 0
Frame Valid Count Dirty
0
1
2
3
45
6
7
8
9A
B
CD
E
F
Page 4
Unused
Page 0
Page F
3FFFH3000H
2FFFH2000H
1FFFH1000H
0FFFH0000H
Physical Memory
Page Table
Page Fault The page is not located in physical
memory. The Memory Management unit
would cause the page to be copied from the swap disk to a frame in physical memory.
Demand Paging Memory Management unit would
clear a page out of physical memory and move the newly required page into the vacated frame if all frames in physical memory already contained pages.
BeginProcess fetched
word
Compute Physical address X of nextWord required Fetch required word
From M
Is X in M?
Is there a freePage frame in
M?
Select page frame F in M to replace
Has F been altered?
Transfer pageIn F to Secondary
memoryTransfer page containing XForm secondary memoryTo free page frame in M
Update page Address tables
YesNo
SuspendNormalexecution
Resume normalexecution
Yes
No
Yes
No
OPERATION
OF
A
DEMAND
PAGINGSystem
Translation Lookaside Buffer (TLB) Because of a time delay associated
with reading the frame number from the page table, and the system may read the same value hundreds or thousands of times consecutively as it fetches contiguous instructions, MMU usually contain a TLB.
Conversion of Logical Address to Physical Address Using the TLB
Offset
Logical address
Page
Page Frame Valid
0000 01 10100 11 1
1111 00 1
0101 10 1
OffsetFrame
Physical address:
To physical memory
Segmentation Another method of allocating
memory that can be used instead of or in conjunction with page.
Program is divided into several segments.
Segments can vary in size. Segmentation have a segmented
MMU.
Segments compared to paging Segments has advantage (internal
fragmentation), and disadvantage (external fragmentation)
Internal fragmentation A program of size 4k + 1 would
require the MMU to allocate two pages of memory, even though the second page would use one of its 4K locations.
External Fragmentation There are three segments resident
in memory and 8K of free space.
However, the free spaces is partitioned
such that no segment greater than 3K
can be loaded into memory with out
moving or removing one of the currently
loaded segments.
Unused
Segment 3
Unused
Segment 2
Unused
Segment 1
16K –1 13K13K –1 9k
9k –1 6k 6k –1 5k
5k –1 3k 3k –1 0
External fragmentation in physical memory caused by segmentation
Memory Protection
Why need Memory Protection?1. A multitasking operating system may have several different program, or parts
of programs, resident in memory simultaneously.
2. These program components may belong to one user or to different users.
3. The operating system may also have components.
Advantages of Virtual Memory You can run more
applications at once. You can run larger applications with less
real RAM. Applications may launch
faster because of File Mapping. You don't have to buy
more memory.
Disadvantages of Virtual Memory Applications run slower . It takes more time to switch
between applications. Less hard drive space for
your use. Reduced system stability.
Thanks
The End
Do you have any Question ?