ch_5 vertual memoryt (1).docx
ch_5 vertual memoryt (1).docx
VIRTUAL MEMORY
Virtual Memory
In this you will learn about virtual memory, its management within the operating system,
and how it functions. Virtual memory is a technical concept that enables the execution of
1
different processes that cannot fit entirely in memory. One significant advantage of this
method is that programs can exceed the physical memory capacity.
Virtual memory abstracts primary memory into a broad, consistent array of storage,
separating logical memory as viewed by the user from that of physical memory. This
technique relieves programmers of the stress of memory storage limitations.
2
VIRTUAL MEMORY
WHY VIRTUAL MEMORY?
We've previously required the entire logical space of the process to be in memory
before the process could run. We will now look at alternatives to this.
Most code/data isn't needed at any instant, or even within a finite time - we can bring it
in only as needed.
VIRTUES
The program size isn't constrained (thus the term 'virtual memory'). Virtual memory
allows very large logical address spaces.
3
VIRTUAL MEMORY Definitions
Virtual memory
The conceptual separation
of user logical memory from
physical memory. Thus we
can have large virtual
memory on a small physical
memory.
3
VIRTUAL MEMORY Definitions
Demand paging When a page is touched, bring it from secondary to main memory.
Overlays Laying of code data on the same logical addresses - this is the
reuse of logical memory. Useful when the program is in phases or
when logical address space is small.
4
VIRTUAL MEMORY Demand Paging
When a page is referenced, either as code execution or data access, and that
page isn’t in memory, then get the page from disk and re-execute the statement.
5
VIRTUAL MEMORY Demand Paging
One instruction may require several pages. For
example, a block move of data. Frame # valid-invalid bit
1
May page fault part way through an operation - 1
may have to undo what was done.
Example: an instruction crosses a page
1
boundary. 1
0page table
Time to service page faults demands that they
happen only infrequently.
0
Note here that the page table requires a "resident" valid-
bit showing that page is/isn't in memory. Resid
invalid
(Book uses "valid" bit to indicate Frame # ent
bit
residency. An "invalid" page is that way
because a legal page isn't resident or
because the address is illegal.
1 1
It makes more sense to have two bits - one 0 0
indicating that the page is legal (valid) and
a second to show that the page is in
memory.
6
VIRTUAL MEMORY Demand Paging
STEPS IN HANDLING A PAGE FAULT
2. Check an internal table for the target process to determine if the reference was
valid (do this in hardware.)
3. If page valid, but page not resident, try to get it from secondary storage.
4. Find a free frame; a page of physical memory not currently in use. (May need
to free up a page.)
5. Schedule a disk operation to read the desired page into the newly allocated
frame.
6. When memory is filled, modify the page table to show the page is now resident.
Do these steps using the figure you can see on the next page.
7
VIRTUAL MEMORY Demand Paging
8
VIRTUAL MEMORY Demand Paging
Architectural rules concerning restarting of instructions. (For instance, block moves across
faulted pages.)
9
VIRTUAL MEMORY Demand Paging
PERFORMANCE OF DEMAND PAGING
We are interested in the effective access time: a combination of "normal" and "paged"
accesses.
It’s important to keep fraction of faults to a minimum. If fault ratio is "p", then
effective_access_time = ( 1 - p ) * memory_access_time
+ p * page_fault_time.
11
VIRTUAL MEMORY Page Replacement
When we over-allocate memory, we need to push out something already in memory.
Over-allocation may occur when programs need to fault in more pages than there
are physical frames to handle.
Approach: If no physical frame is free, find one not currently being touched and free
it. Steps to follow are:
When memory is overallocated, we can either swap out some process, or overwrite some
pages. Which pages should we replace?? <--- here the goal is to minimize the number of
faults.
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
FIFO 1 1 5 4
Conceptually easy to implement; either use 2 21 5 10 page faults
a time-stamp on pages, or organize on a
queue. (The queue is by far the easier of 3 32
the two methods.)
4 4 3
13
VIRTUAL MEMORY Page Replacement
OPTIMAL REPLACEMENT
• This is the replacement policy that results in the lowest page fault rate.
• Algorithm: Replace that page which will not be next used for the longest period of
time.
• Impossible to achieve in practice; requires crystal ball.
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
4 1
2 6 page faults
3
4 5
14
VIRTUAL MEMORY Page Replacement
LEAST RECENTLY USED ( LRU )
• Replace that page which has not been used for the longest period of time.
• Results of this method considered favorable. The difficulty comes in making it work.
• Implementation possibilities:
Both methods need hardware assist since the update must be done on every instruction. So
in practice this is rarely done.
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1 5
2 8 page faults
3 5 4
4 3
15
VIRTUAL MEMORY Page Replacement
PAGE REPLACEMENT ALGORITHMS :
Using another string:
FIFO
OPTIMAL
LRU
9: Virtual Memory 16
VIRTUAL MEMORY Page Replacement
LRU APPROXIMATION
These methods are frequently used over and above the standard methods
given above.
Occasional writes of dirties - make clean and then we can use them quickly
when needed.
Write out and free a page but remember a page id in case the page is needed
again - even though a page is in the free pool, it can be recaptured by a
process (a soft page fault.)
18
VIRTUAL MEMORY
ALLOCATION OF FRAMES:
Page Allocation
What happens when several processes contend for memory? What algorithm
determines which process gets memory - is page management a global or local
decision?
A good rule is to ensure that a process has at least a minimum number of pages.
This minimum ensures it can go about its business without constantly thrashing.
ALLOCATION ALGORITHMS
Local replacement -- the process needing a new page can only steal from itself.
(Doesn't take advantage of entire picture.)
Global replacement - sees the whole picture, but a memory hog steals from
everyone else
Can divide memory equally, or can give more to a needier process. Should high
priority processes get more memory?
19
VIRTUAL MEMORY Thrashing
Suppose there are too few physical pages (less than the logical pages being actively
used). This reduces CPU utilization, and may cause increase in multiprogramming
needs defined by locality.
A program will thrash if all pages of its locality aren’t present in the working set.
Two programs thrash if they fight each other too violently for memory.
9: Virtual Memory 20
VIRTUAL MEMORY Locality of Reference
21
VIRTUAL MEMORY Working Set Model
WORKING SET MODEL
The pages used by a process within a window of time are called its working set.
Changes continuously - hard to maintain an accurate number. How can the system use this
number to give optimum memory to the process?
22
VIRTUAL MEMORY Working Set Model
PAGE FAULT FREQUENCY
23
VIRTUAL MEMORY Other Issues
PREPAGING
• Bring lots of pages into memory at one time, either when the program is
initializing, or when a fault occurs.
• Uses the principle that a program often uses the page right after the one
previously accessed (locality of reference.)
PAGE SIZE
• If too big, there's considerable fragmentation and unused portions of the page.
24
VIRTUAL MEMORY Other Issues
Memory Mapped IO
• A file is initially read using demand paging. Multiple page-sized portions of the
file are read from the file system into physical pages. Subsequent
reads/writes to/from the file are treated as ordinary memory accesses.
• Simplifies file access by treating file I/O through memory rather than read()
write() system calls
• Also allows several processes to map the same file allowing the pages
in memory to be shared
25
VIRTUAL MEMORY Other Issues
Memory Mapped IO
26
VIRTUAL MEMORY Other Issues
Program structure
– int data [128,128];
– Each row is stored in one page Which method should you
– Program 1 program??
for (j = 0; j <128; j++)
for (i = 0; i < 128; i++)
data[i,j] = 0;
– Program 2
for (i = 0; i < 128; i++)
for (j = 0; j < 128; j++)
data[i,j] = 0;
27
VIRTUAL MEMORY
Wrap up
But to bring pages into memory, means kicking other pages out, so we
need to worry about paging algorithms.
28