UNIT IV
Chapter 1
VIRTUAL MEMORY
Virtual memory is a technique that allows the execution of processes that are not completely
in memory. One major advantage of this scheme is that programs can be larger than physical
memory. Further, virtual memory abstracts main memory into an extremely large, uniform array
of storage, separating logical memory as viewed by the programmer from physical memory. This
technique frees programmers from the concerns of memory-storage limitations. Virtual memory
also allows processes to share files and libraries, and to implement shared memory. In addition, it
provides an efficient mechanism for process creation.
Virtual memory involves the separation of user logical memory from physical memory. This
separation allows an extremely large virtual memory to be provided for programmers when only
a smaller physical memory is available.Virtual memory makes the task of programming much
easier, because the programmer no longer needs to worry about the amount of physical memory
available. Only part of the program needs to be in memory for execution. Logical address space
can therefore be much larger than physical address space. Allows address spaces to be shared by
several processes. Allows for more efficient process creation. More programs running
concurrently. Less I/O needed to load or swap processes.
Virtual Memory That is Larger Than Physical Memory
Virtual address space of a process refers to the logical view of how process is stored in
memory. Usually start at address 0, and exists in contiguous addresses until end of space.
Meanwhile, physical memory organized in page frames and that the physical page frames
assigned to a process may not be contiguous. MMU must map logical to physical page frames in
memory
Virtual memory can be implemented via:
Demand paging
Demand segmentation
DEMAND PAGING
An alternative strategy is to load pages only as they are needed. This technique is known as
demand paging and is commonly used in virtual memory systems. With demand-paged virtual
memory, pages are loaded only when they are demanded during program execution. Pages that
1
are never accessed are thus never loaded into physical memory. A demand-paging system is
similar to a paging system with swapping where processes reside in secondary memory. Demand
paging explains one of the primary benefits of virtual memory—by loading only the portions of
programs that are needed, memory is used more efficiently
Basic concepts
The general concept behind demand paging, as mentioned, is to load a page in memory only
when it is needed. As a result, while a process is executing, some pages will be in memory, and
some will be in secondary storage. Thus, we need some form of hardware support to distinguish
between the two. The valid–invalid bit scheme can be used for this purpose. when the bit is set to
“valid,” the associated page is both legal and in memory. If the bit is set to “invalid,” the page
either is not valid (that is, not in the logical address space of the process) or is valid but is
currently in secondary storage. The page-table entry for a page that is brought into memory is set
as usual, but the page-table entry for a page that is not currently in memory is simply marked
invalid.
Page Table When Some Pages Are Not in Main Memory
2
Page Fault
Page faults dominate more like an error. A page fault will happen if a program tries to access
a piece of memory that does not exist in physical memory (main memory). A page fault trap
occurs if the requested page is not loaded into memory. The page fault primarily causes an
exception, which is used to notify the operating system to retrieve the "pages" from virtual
memory to continue operation.
Steps in Handling Page Fault
A Page Fault happens when you access a page that has been marked as invalid.
1. We check an internal table for this process to determine whether the reference was a
valid or an invalid memory access.
2. If the reference was invalid, we terminate the process. If it was valid but we have not
yet brought in that page, we now page it in.
3. We find a free frame.
4. We schedule a secondary storage operation to read the desired page into the newly
allocated frame.
5. When the storage read is complete, we modify the internal table kept with the process
and the page table to indicate that the page is now in memory.
6. We restart the instruction that was interrupted by the trap. The process can now access
the page as though it had always been in memory.
Steps in Handling a Page Fault
Aspects of Demand Paging
In Extreme case – start process with no pages in memory. OS sets instruction pointer to first
instruction of process, which is non-memory-resident -> page fault
Pure demand paging : Never bring a page into memory until it is required.Actually, a given
instruction could access multiple pages -> multiple page faults.
Locality of reference refers to a phenomenon in which a computer program tends to access
same set of memory locations for a particular time period. Hardware support needed for demand
paging is the same as the hardware for paging and swapping. Page table with valid / invalid bit.
Secondary memory (swap device with swap space).
3
The hardware to support demand paging is the same as the hardware for paging and swapping:
• Page table. This table has the ability to mark an entry invalid through a valid –invalid bit or a
special value of protection bits.
• Secondary memory. This memory holds those pages that are not present in main memory. The
secondary memory is usually a high-speed disk or NVM device. It is known as the swap device,
and the section of storage used for this purpose is known as swap space.
Free-Frame List
When a page fault occurs, the operating system must bring the desired page from secondary
storage into main memory. Most operating systems maintain a free-frame list a pool of free
frames for satisfying such requests. Operating system typically allocate free frames using a
technique known as zero-fill-on-demand the content of the frames zeroed-out before being
allocated. When a system starts up, all available memory is placed on the free-frame list.
PAGE REPLACEMENT
In an operating system, page replacement is referred to a scenario in which a page from the main
memory should be replaced by a page from secondary memory. Page replacement occurs due to
page faults.If we increase our degree of multiprogramming, we are over-allocating memory. If
we run six processes, each of which is ten pages in size but actually uses only five pages. Prevent
over-allocation of memory by modifying page-fault service routine to include page replacement
Need For Page Replacement
4
Basic Page Replacement
1. Find the location of the desired page on disk
2. Find a free frame:
- If there is a free frame, use it
- If there is no free frame, use a page replacement algorithm to select a victim frame
- Write the victim frame to secondary storage (if necessary); change the page and
frame tables accordingly.
3. Bring the desired page into the (newly) free frame; update the page and frame tables
4. Continue the process by restarting the instruction that caused the trap.
5. Notice that, if no frames are free, two page transfers (one for the page-out and one for
the page-in) are required. This situation effectively doubles the page-fault service
time and increases the effective access time accordingly.
6. If no frame is free, we find one that is not currently being used and free it. We can
free a frame by writing its contents to swap space and changing the page table to
indicate that the page is no longer in memory. We can now use the freed frame to hold
the page for which the process faulted. We modify the page-fault service routine to
include page replacement.
Page Replacement
Page and Frame Replacement Algorithms
Frame-allocation algorithm determines:
How many frames to allocate to each process.
Which frames to replace
Page-replacement algorithm
Want lowest page-fault rate on both first access and re-access. Evaluate algorithm by running it
on a particular string of memory references (reference string) and computing the number of page
faults on that string. String is just page numbers, not full addresses. Repeated access to the same
page does not cause a page fault. Results depend on number of frames available.
Page replacement is basic to demand paging. It completes the separation between logical
memory and physical memory. With this mechanism, an enormous virtual memory can be
provided for programmers on a smaller physical memory. With no demand paging, logical
addresses are mapped into physical addresses, and the two sets of addresses can be different. All
5
the pages of a process still must be in physical memory, however. With demand paging, the size
of the logical address space is no longer constrained by physical memory. If we have a process of
twenty pages, we can execute it in ten frames simply by using demand paging and using a
replacement algorithm to find a free frame whenever necessary. If a page that has been modified
is to be replaced, its contents are copied to secondary storage. A later reference to that page will
cause a page fault. At that time, the page will be brought back into memory, perhaps replacing
some other page in the process.
Graph of Page Faults Versus the Number of Frames
We evaluate an algorithm by running it on a particular string of memory references and
computing the number of page faults. The string of memory references is called a reference
string. We can generate reference strings artificially (by using a random-number generator, for
example), or we can trace a given system and record the address of each memory reference. The
latter choice produces a large number of data (on the order of 1 million addresses per second). To
reduce the number of data, we use two facts. First, for a given page size (and the page size is
generally fixed by the hardware or system), we need to consider only the page number, rather
than the entire address. Second, if we have a reference to a page p, then any references to page p
that immediately follow will never cause a page fault. Page p will be in memory after the first
reference, so the immediately following references will not fault.
First-In-First-Out (FIFO) Algorithm
The simplest page-replacement algorithm is a first-in, first-out (FIFO) algorithm.
A FIFO replacement algorithm associates with each page the time when that page was brought
into memory. When a page must be replaced, the oldest page is chosen. We can create a FIFO
queue to hold all pages in memory. We replace the page at the head of the queue. When a page is
brought into memory, we insert it at the tail of the queue.
Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
3 frames (3 pages can be in memory at a time per process)
15 page faults
6
For our example reference string, our three frames are initially empty. The first three
references (7, 0, 1) cause page faults and are brought into these empty frames. The next reference
(2) replaces page 7, because page 7 was brought in first. Since 0 is the next reference and 0 is
already in memory, we have no fault for this reference. The first reference to 3 results in
replacement of page 0, since it is now first in line. Because of this replacement, the next
reference, to 0, will fault. Page 1 is then replaced by page 0. This process continues as shown in
Figure 10.12. Every time a fault occurs, we show which pages are in our three frames. There are
fifteen faults altogether.
Belady’s anomaly is the name given to the phenomenon where increasing the number of page
frames results in an increase in the number of page faults for a given memory access pattern.
The FIFO page-replacement algorithm is easy to understand and program. However, its
performance is not always good. On the one hand, the page replaced may be an initialization
module that was used a long time ago and is no longer needed. On the other hand, it could
contain a heavily used variable that was initialized early and is in constant use.
Optimal Algorithm
Replace page that will not be used for longest period of time.
For example, on our sample reference string, the optimal page-replacement algorithm would
yield nine page faults, as shown in Figure .
Replace the page that will not be used for the longest period of time. Use of this page-
replacement algorithm guarantees the lowest possible pagefault rate for a fixed number of
frames. For example, on our sample reference string, the optimal page-replacement algorithm
would yield nine page faults, as shown in Figure 10.14. The first three references cause faults
that fill the three empty frames. The reference to page 2 replaces page 7, because page 7 will not
be used until reference 18, whereas page 0 will be used at 5, and page 1 at 14. The reference to
page 3 replaces page 1, as page 1 will be the last of the three pages in memory to be referenced
again. With only nine page faults, optimal replacement is much better than a FIFO algorithm,
which results in fifteen faults. In fact, no replacement algorithm can process this reference
string in three frames with fewer than nine faults. The optimal page-replacement algorithm is
difficult to implement, because it requires future knowledge of the reference string.
Least Recently Used (LRU) Algorithm
Use past knowledge rather than future. Replace page that has not been used in the most
amount of time. Associate time of last use with each page.
If the optimal algorithm is not feasible, perhaps an approximation of the optimal
algorithm is possible. The key distinction between the FIFO and OPT algorithms (other than
7
looking backward versus forward in time) is that the FIFO algorithm uses the time when a page
was brought into memory, whereas the OPT algorithm uses the time when a page is to be used. If
we use the recent past as an approximation of the near future, then we can replace the page that
has not been used for the longest period of time. This approach is the least recently used (LRU)
algorithm.
12 faults in LRU
It is better than FIFO but worse than OPT(optimal).Generally good algorithm and frequently
used.
Counter implementation
Every page entry has a time-of-use field counter, every time page is referenced through this
entry. Whenever CPU is referring a page ,counter will be incremented and copy the counter is
added into the time-of-use field. When a page needs to be replaced, look at the counters to find
smallest time-of-use value. Search through entire page table to find the LRU page and a write
to memory for each memory access.
Stack implementation
Keep a stack of page numbers in a double linked list with head and tail pointer:
Page referenced:
move it to the top
Each update operation is more expensive
No search for replacement
Use Of A Stack to Record Most Recent Page References
Another approach to implementing LRU replacement is to keep a stack of page numbers.
Whenever a page is referenced, it is removed from the stack and put on the top. In this way, the
most recently used page is always at the top of the stack, and the least recently used page is
always at the bottom (Figure 10.16). Because entries must be removed from the middle of the
stack, it is best to implement this approach by using a doubly linked list with a head pointer and a
tail pointer. Removing a page and putting it on the top of the stack then requires changing six
pointers at worst. Each update is a little more expensive, but there is no search for a replacement;
the tail pointer points to the bottom of the stack, which is the LRU page. This approach is
particularly appropriate for software or microcode implementations of LRU replacement.
8
ALLOCATION OF FRAMES
The main memory in the operating system is divided into frames. These frames store the
process and once the process is stored as a frame the CPU can execute the process. Therefore the
operating system has to allocate a sufficient number of frames for each corresponding process.
There are many variations on this simple strategy. We can require that the operating
system allocate all its buffer and table space from the free-frame list. When this space is not in
use by the operating system, it can be used to support user paging. We can try to keep three free
frames reserved on the free-frame list at all times. Thus, when a page fault occurs, there is a free
frame available to page into. While the page swap is taking place, a replacement can be selected,
which is then written to the storage device as the user process continues to execute. Other
variants are also possible, but the basic strategy is clear: the user process is allocated any free
frame.
Minimum Number of Frame
Our strategies for the allocation of frames are constrained in various ways. We must also allocate
at least a minimum number of frames. One reason for allocating at least a minimum number of
frames involves performance. Obviously, as the number of frames allocated to each process
decreases, the page-fault rate increases, slowing process execution.
One reason for allocating at least a minimum number of frames involves performance.
Obviously, as the number of frames allocated to each process decreases, the page-fault rate
increases, slowing process execution. In addition, remember that, when a page fault occurs
before an executing instruction is complete, the instruction must be restarted. Consequently, we
must have enough frames to hold all the different pages that any single instruction can reference
Allocation Algorithm
For instance, if there are 93 frames and 5 processes, each process will get 18 frames. The 3
leftover frames can be used as a free-frame buffer pool. This scheme is called equal allocation.
Proportional allocation – Allocate available memory to each process according to its size
si size of process pi m 62
S si s1 10
m total number of frames s2 127
si 10
ai allocation for pi m a1 62 4
S 137
127
a2 62 57
137
9
Global vs. Local Allocation
Another important factor in the way frames are allocated to the various processes is page
replacement. With multiple processes competing for frames, we can classify page-replacement
algorithms into two broad categories: global replacement and local replacement.
Global replacement – process selects a replacement frame from the set of all frames, even if
that frame is currently allocated to some other process; one process can take a frame from
another
Local replacement – each process selects from only its own set of allocated frames
THRASHING
If a process does not have “enough” pages- that is, it does not have the minimum number of
frames it needs to support pages in the working set, the page-fault rate is very high. Thrashing is
when the page fault and swapping happens very frequently at a higher rate, and then the
operating system has to spend more time swapping these pages. This state in the operating
system is known as thrashing
Cause of thrashing
Low CPU utilization
Operating system thinking that it needs to increase the degree of multiprogramming by
introducing a new process to the system
This phenomenon is illustrated in Figure in which CPU utilization is plotted against the degree of
multiprogramming.
As the degree of multiprogramming increases, CPU utilization also increases, although more
slowly, until a maximum is reached
If the degree of multiprogramming is increased further, thrashing sets in, and CPU utilization
drops sharply
At this point, to increase CPU utilization and stop thrashing, we must decrease the degree of
multiprogramming
10
Solution for Thrashing
We can limit the effects of thrashing by using a local replacement algorithm
Local replacement requires that each process select from only its own set of allocated frames.
Thus, if one process starts thrashing, it cannot steal frames from another process.
To prevent thrashing, we use one strategy starts by looking at how many frames a process
is actually using. This approach defines the locality model of process execution.
Locality model
As a process executes, it moves from one locality to another.
Localities may overlap
Why does thrashing occur?
size of locality > total memory size
Limit effects by using local or priority page replacement
Working-Set Model
The working-set model is based on the assumption of locality.
This model uses a parameter to define the working set window.
The idea is to examine the most recent Δ page references.
If a page is in active use, it will be in the working set. If it is no longer being used, it will
drop from the working set Δ time units after its last reference
Given the sequence of memory references shown in Figure , if Δ = 10 memory references,
then the working set at time t1 is {1, 2, 5, 6, 7}. By time t2, the working set has changed to
{3, 4}.
23
The accuracy of the working set depends on the selection of Δ.
if too small ,It will not encompass entire locality
if too large,It may overlap several localities.
if is infinite, the working set is the set of pages touched during the process
execution
The most important property of the working set, then, is its size.
If we compute the working-set size, WSSi , for each process in the system, we can then
consider that
D = WSSi Where D is total demand for frames
11