0% found this document useful (0 votes)
20 views

Virtual Memory Notes

Virtual Memory is a technique that allows large programs to execute by storing them in pages and loading only the necessary pages into physical memory, thereby providing an illusion of more memory than physically available. It utilizes methods like Demand Paging and Demand Segmentation to manage memory efficiently, allowing for faster process execution and better resource utilization. However, challenges such as page faults, thrashing, and the need for effective page replacement algorithms must be addressed to optimize performance.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Virtual Memory Notes

Virtual Memory is a technique that allows large programs to execute by storing them in pages and loading only the necessary pages into physical memory, thereby providing an illusion of more memory than physically available. It utilizes methods like Demand Paging and Demand Segmentation to manage memory efficiently, allowing for faster process execution and better resource utilization. However, challenges such as page faults, thrashing, and the need for effective page replacement algorithms must be addressed to optimize performance.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Virtual Memory

Virtual Memory is a space where large programs can store themselves in form of pages while
their execution and only the required pages or portions of processes are loaded into the main
memory. This technique is useful as a large virtual memory is provided for user programs
when a very small physical memory is there. Thus Virtual memory is a technique that allows
the execution of processes that are not in the physical memory completely.
Virtual Memory mainly gives the illusion of more physical memory than there really is with
the help of Demand Paging.

In real scenarios, most processes never need all their pages at once, for the following
reasons :

 Error handling code is not needed unless that specific error occurs, some of which are
quite rare.

 Arrays are often over-sized for worst-case scenarios, and only a small fraction of the
arrays are actually used in practice.

 Certain features of certain programs are rarely used.

In an Operating system, the memory is usually stored in the form of units that are known
as pages. Basically, these are atomic units used to store large programs.

Virtual memory can be implemented with the help of:-


1. Demand Paging

2. Demand Segmentation

Need of Virtual Memory

Following are the reasons due to which there is a need for Virtual Memory:

 In case, if a computer running the Windows operating system needs more memory or
RAM than the memory installed in the system then it uses a small portion of the hard
drive for this purpose.

 Suppose there is a situation when your computer does not have space in the physical
memory, then it writes things that it needs to remember into the hard disk in a swap
file and that as virtual memory.

Benefits of having Virtual Memory

1. Large programs can be written, as the virtual space available is huge compared to
physical memory.

2. Less I/O required leads to faster and easy swapping of processes.


3. More physical memory available, as programs are stored on virtual memory, so they
occupy very less space on actual physical memory.

4. Therefore, the Logical address space can be much more larger than that of physical
address space.

5. Virtual memory allows address spaces to be shared by several processes.

6. During the process creation, virtual memory allows: copy-on-write and Memory-
mapped files

Demand paging:
 To be simple in demand paging the entire process should be in the disk in the form
of pages
 In this technique a page is brought into memory for its execution only when it
is demanded
 It is a combination of paging and swapping

Fig1: Paging with Swapping

Here in the above diagram all the pages are loaded into backing store (hard disk).By the
mechanism of swapping when the main memory requests the page only then it is loaded from
hard disk. As main memory is small in size and cannot handle large programs only few pages
are loaded into main memory after completing its execution it is swapped out simply and new
process is then swapped in.
Basic Concepts :

When a process is to be swapped in, the pager guesses which pages will be used before the
process is swapped out again. Instead of swapping in a whole process, the pager brings only
those pages into memory. Thus, it avoids reading into memory pages that will not be used
anyway, decreasing the swap time and the amount of physical memory needed. With this
scheme, we need some form of hardware support to distinguish between the pages that are in
memory and the pages that are on the disk. The valid–invalid bit scheme can be used for this
purpose. This time, however, when this 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 on the disk. 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 either simply marked invalid or contains the address
of the page on disk.

Marking a page invalid will have no effect if the process never attempts to access that page.

Figure2: Demand Paging

Figure3: Steps in handling Page Fault


But what happens if the process tries to access a page that was not brought into memory?
Access to a page marked invalid causes a page fault. The paging hardware, in translating the
address through the page table, will notice that the invalid bit is set, causing a trap to the
operating system. This trap is the result of the operating system’s failure to bring the desired
page into memory. The procedure for handling this page fault is straightforward.

1. We check an internal table (usually kept with the process control block) 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 (by taking one from the free-frame list, for example).
4. We schedule a disk operation to read the desired page into the newly allocated frame.

5. When the disk 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.

In the extreme case, we can start executing a process with no pages in memory. When the
operating system sets the instruction pointer to the first instruction of the process, which is on
a non-memory-resident page, the process immediately faults for the page. After this page is
brought into memory, the process continues to execute, faulting as necessary until every page
that it needs is in memory. At that point, it can execute with no more faults. This scheme is
pure demand paging: never bring a page into memory until it is required.

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. It is known as the swap device, and the
section of disk used for this purpose is known as swap space.

A crucial requirement for demand paging is the ability to restart any instruction after a page
fault. Because we save the state (registers, condition code, instruction counter) of the
interrupted process when the page fault occurs, we must be able to restart the process in
exactly the same place and state, except that the desired page is now in memory and is
accessible. A page fault may occur at any memory reference. If the page fault occurs on
the instruction fetch, we can restart by fetching the instruction again. If a page fault occurs
while we are fetching an operand, we must fetch and decode the instruction again and then
fetch the operand.

Advantages :
1.Reduces memory requirement
2.Swap time is also reduced.
3. Increases the degree of multiprogramming (cpu utilization time increases)

Disadvantages:
->Page fault rate increases for bigger programs.
-> If the size of swap file is big it is difficult for main memory
Page Replacement

Page replacement is needed in the operating systems that use virtual memory using Demand
Paging. As we know that in Demand paging, only a set of pages of a process is loaded into
the memory. This is done so that we can have more processes in the memory at the same
time.

When a page that is residing in virtual memory is requested by a process for its execution, the
Operating System needs to decide which page will be replaced by this requested page. This
process is known as page replacement and is a vital component in virtual memory
management.

Why Need Page Replacement Algorithms?

To understand why we need page replacement algorithms, we first need to know about page
faults. Let’s see what is a page fault.

Page Fault: A Page Fault occurs when a program running in CPU tries to access a page that
is in the address space of that program, but the requested page is currently not loaded into the
main physical memory, the RAM of the system.

Since the actual RAM is much less than the virtual memory the page faults occur. So
whenever a page fault occurs, the Operating system has to replace an existing page
in RAM with the newly requested page. In this scenario, page replacement algorithms help
the Operating System in deciding which page to replace. The primary objective of all the
page replacement algorithms is to minimize the number of page faults.

Page Replacement Algorithms:

1. First In First Out (FIFO): This is the simplest page replacement algorithm. In this
algorithm, the operating system keeps track of all pages in the memory in a queue, the
oldest page is in the front of the queue. When a page needs to be replaced page in the front
of the queue is selected for removal.
Example 1: Consider page reference string 1, 3, 0, 3, 5, 6, 3 with 3 page frames. Find the
number of page faults.
Initially, all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots —
> 3 Page Faults.
when 3 comes, it is already in memory so —> 0 Page Faults. Then 5 comes, it is not
available in memory so it replaces the oldest page slot i.e 1. —>1 Page Fault. 6 comes, it is
also not available in memory so it replaces the oldest page slot i.e 3 —>1 Page
Fault. Finally, when 3 come it is not available so it replaces 0 1 page fault.
Belady’s anomaly proves that it is possible to have more page faults when increasing the
number of page frames while using the First in First Out (FIFO) page replacement
algorithm. For example, if we consider reference strings 3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4 and
3 slots, we get 9 total page faults, but if we increase slots to 4, we get 10-page faults.
2. Optimal Page replacement: In this algorithm, pages are replaced which would not be
used for the longest duration of time in the future.
Example-2: Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4 page
frame. Find number of page fault.

Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page
faults
0 is already there so —> 0 Page fault. when 3 came it will take the place of 7 because it is
not used for the longest duration of time in the future.—>1 Page fault. 0 is already there so
—> 0 Page fault. 4 will takes place of 1 —> 1 Page Fault.
Now for the further page reference string —> 0 Page fault because they are already
available in the memory.
Optimal page replacement is perfect, but not possible in practice as the operating system
cannot know future requests. The use of Optimal Page replacement is to set up a benchmark
so that other replacement algorithms can be analyzed against it.

3. Least Recently Used: In this algorithm, page will be replaced which is least recently
used.
Example-3: Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4
page frames. Find number of page faults.
Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page
faults
0 is already their so —> 0 Page fault. when 3 came it will take the place of 7 because it is
least recently used —>1 Page fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault
Now for the further page reference string —> 0 Page fault because they are already
available in the memory.

4. Most Recently Used (MRU): In this algorithm, page will be replaced which has been
used recently. Belady’s anomaly can occur in this algorithm.

Difference between Demand Paging and Segmentation


Demand Paging: Demand paging is identical to the paging system with swapping. In
demand paging, a page is delivered into the memory on demand i.e., only when a reference
is made to a location on that page. Demand paging combines the feature of simple paging
and implement virtual memory as it has a large virtual memory. Lazy swapper concept is
implemented in demand paging in which a page is not swapped into the memory unless it is
required. Segmentation: Segmentation is the arrangement of memory management.
According to the segmentation the logical address space is a collection of segments. Each
segment has a name and length. Each logical address have two quantities segment name
and the segment offset, for simplicity we use the segment number in place of segment
name. The difference between Demand Paging and Segmentation are as follows:

S.No. Demand Paging Segmentation

In demand paging, the pages are While in segmentation, segments can be of


1.
of equal size. different size.

Page size is fixed in the demand Segment size may vary in segmentation as it
2.
paging. grants dynamic increase of segments.

3. It does not allows sharing of the While segments can be shared in


S.No. Demand Paging Segmentation

pages. segmentation.

In demand paging, on demand In segmentation, during compilation


4.
pages are loaded in the memory. segments are allocated to the program.

Page map table in demand Segment map table in segmentation


5. paging manages record of pages demonstrates every segment address in the
in memory. memory.

It provides large virtual memory It provides virtual memory and maximum


6. and have more efficient use of size of segment is defined by the size of
memory. memory.

Virtual memory is divided into


7. Virtual memory is divided into segments.
pages.

Size of Unit of demand Paging


Size of Unit of segmentation Segments are
8. Pages can be of equal or variable
variable in size.
size.

Memory allocation of Pages are Memory allocation of Segments are allocated


9.
allocated dynamically. at the process start.

Memory access is Page-level


10. Memory access is segment-level protection.
protection.

Memory wastage is Internal


11. Memory wastage is external fragmentation.
fragmentation.

Paging algorithm is Demand Paging algorithm is non- demand


12.
Paging. Paging(Fixed Allocation).

Thrashing
Look at any process that does not have “enough” frames. If the process does not have the
number of frames it needs to support pages in active use, it 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 called thrashing. A process is thrashing if it is spending more time paging
than executing.
Cause of Thrashing :
Thrashing results in severe performance problems.
The operating system monitors CPU utilization. If CPU utilization is too low, we increase
the degree of multiprogramming by introducing a new process to the system. A global page-
replacement algorithm is used; it replaces pages without regard to the process to which they
belong. Now suppose that a process enters a new phase in its execution and needs more
frames. It starts faulting and taking frames away from other processes. These processes need
those pages, however, and so they also fault, taking frames from other processes. These
faulting processes must use the paging device to swap pages in and out. As they queue up
for the paging device, the ready queue empties. As processes wait for the paging device,
CPU utilization decreases. The CPU scheduler sees the decreasing CPU utilization and
increases the degree of multiprogramming as a result. The new process tries to get started by
taking frames from running processes, causing more page faults and a longer queue for the
paging device. As a result, CPU utilization drops even further, and the CPU scheduler tries
to increase the degree of multiprogramming even more. Thrashing has occurred, and system
throughput plunges. The page fault rate increases tremendously. As a result, the effective
memory-access time increases. No work is getting done, because the processes are spending
all their time paging.
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 even 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.

Figure3: Thrashing

We can limit the effects of thrashing by using a local replacement algorithm (or priority
replacement algorithm). With local replacement, if one process starts thrashing, it cannot
steal frames from another process and cause the latter to thrash as well. However, the
problem is not entirely solved. If processes are thrashing, they will be in the queue for the
paging device most of the time. The average service time for a page fault will increase
because of the longer average queue for the paging device. Thus, the effective access time
will increase even for a process that is not thrashing. To prevent thrashing, we must provide a
process with as many frames as it needs.
we can know how many frames it “needs” by several techniques. The working-set strategy
starts by looking at how many frames a process is actually using. This approach defines the
locality model of process execution. The locality model states that, as a process executes, it
moves from locality to locality. A locality is a set of pages that are actively used together. A
program is generally composed of several different localities, which may overlap. For
example, when a function is called, it defines a new locality. In this locality, memory
references are made to the instructions of the function call, its local variables, and a subset of
the global variables. When we exit the function, the process leaves this locality, since the
local variables and instructions of the function are no longer in active use. We may return to
this locality later. we see that localities are defined by the program structure and its data
structures. The locality model states that all programs will exhibit this basic memory
reference structure.
We allocate enough frames to a process to accommodate its current locality. It will fault for
the pages in its locality until all these pages are in memory; then, it will not fault again until it
changes localities. If we do not allocate enough frames to accommodate the size of the
current locality, the process will thrash, since it cannot keep in memory all the pages that it is
actively using.

Page-Fault Frequency:
The working-set model is successful, and knowledge of the working set can be useful for pre-
paging , but it seems a clumsy way to control thrashing. A strategy that uses the page-fault
frequency (PFF) takes a more direct approach. The specific problem is how to prevent
thrashing. Thrashing has a high page-fault rate. Thus, we want to control the page-fault rate.
When it is too high, we know that the process needs more frames. Conversely, if the page-
fault rate is too low, then the process may have too many frames. We can establish upper and
lower bounds on the desired page-fault rate. If the actual page-fault rate exceeds the upper
limit, we allocate the process another frame. If the page-fault rate falls below the lower limit,
we remove a frame from the process. Thus, we can directly measure and control the page-
fault rate to prevent thrashing.

Overlays in Memory Management


In memory management, overlays refer to a technique used to manage memory efficiently
by overlaying a portion of memory with another program or data.
The idea behind overlays is to only load the necessary parts of a program into memory at a
given time, freeing up memory for other tasks. The unused portions of the program are kept
on disk or other storage, and are loaded into memory as needed. This allows programs to be
larger than the available memory, but still run smoothly.
The main problem in Fixed partitioning is the size of a process has to be limited by the
maximum size of the partition, which means a process can never be span over another. In
order to solve this problem, earlier people have used some solution which is called as
Overlays.
The concept of overlays is that whenever a process is running it will not use the complete
program at the same time, it will use only some part of it. Then overlays concept says that
whatever part you required, you load it and once the part is done, then you just unload it,
means just pull it back and get the new part you required and run it.
Formally,
“The process of transferring a block of program code or other data into internal memory,
replacing what is already stored”.
Sometimes it happens that compare to the size of the biggest partition, the size of the
program will be even more, then, in that case, you should go with overlays.

So overlay is a technique to run a program that is bigger than the size of the physical
memory by keeping only those instructions and data that are needed at any given
time.Divide the program into modules in such a way that not all modules need to be in the
memory at the same time.

Advantages of using overlays include:

 Increased memory utilization: Overlays allow multiple programs to share the


same physical memory space, increasing memory utilization and reducing the
need for additional memory.
 Reduced load time: Only the necessary parts of a program are loaded into
memory, reducing load time and increasing performance.
 Improved reliability: Overlays reduce the risk of memory overflow, which can
cause crashes or data loss.
 Reduce memory requirement
 Reduce time requirement

Disadvantages of using overlays include:

 Complexity: Overlays can be complex to implement and manage, especially for


large programs.
 Performance overhead: The process of loading and unloading overlays can result
in increased CPU and disk usage, which can slow down performance.
 Compatibility issues: Overlays may not work on all hardware and software
configurations, making it difficult to ensure compatibility across different
systems.
 Overlap map must be specified by programmer
 Programmer must know memory requirement
 Overlapped module must be completely disjoint
 Programming design of overlays structure is complex and not possible in all
cases
Example –
The best example of overlays is assembler. Consider the assembler has 2 passes, 2 pass
means at any time it will be doing only one thing, either the 1st pass or the 2nd pass. This
means it will finish 1st pass first and then 2nd pass.Let assume that available main memory
size is 150KB and total code size is 200KB
Pass 1.......................70KB
Pass 2.......................80KB
Symbol table.................30KB
Common routine...............20KB
As the total code size is 200KB and main memory size is 150KB, it is not possible to use 2
passes together. So, in this case, we should go with the overlays technique. According to
the overlays concept at any time only one pass will be used and both the passes always need
symbol table and common routine. Now the question is if overlays-driver* is 10KB, then
what is the minimum partition size required? For pass 1 total memory needed is = (70KB +
30KB + 20KB + 10KB) = 130KB and for pass 2 total memory needed is = (80KB + 30KB
+ 20KB + 10KB) = 140KB.So if we have minimum 140KB size partition then we can run
this code very easily.
*Overlays driver:-It is the user responsibility to take care of overlaying, the operating
system will not provide anything. Which means the user should write even what part is
required in the 1st pass and once the 1st pass is over, the user should write the code to pull
out the pass 1 and load the pass 2.That is what is the responsibility of the user, that is
known as the Overlays driver. Overlays driver will just help us to move out and move in the
various part of the code.
PRACTICE PROBLEMS BASED ON PAGE REPLACEMENT ALGORITHMS-

Problem-01:

A system uses 3 page frames for storing process pages in main memory. It uses the
First in First out (FIFO) page replacement policy. Assume that all the page frames are
initially empty. What is the total number of page faults that will occur while processing
the page reference string given below-
4 , 7, 6, 1, 7, 6, 1, 2, 7, 2
Also calculate the hit ratio and miss ratio.

Solution-

Total number of references = 10

From here,
Total number of page faults occurred = 6

Calculating Hit ratio-

Total number of page hits


= Total number of references – Total number of page misses or page faults
= 10 – 6
=4
Thus, Hit ratio
= Total number of page hits / Total number of references
= 4 / 10
= 0.4 or 40%

Calculating Miss ratio-

Total number of page misses or page faults = 6

Thus, Miss ratio


= Total number of page misses / Total number of references
= 6 / 10
= 0.6 or 60%

Alternatively,
Miss ratio
= 1 – Hit ratio
= 1 – 0.4
= 0.6 or 60%

Problem-02:

A system uses 3 page frames for storing process pages in main memory. It uses the
Least Recently Used (LRU) page replacement policy. Assume that all the page frames
are initially empty. What is the total number of page faults that will occur while
processing the page reference string given below-
4 , 7, 6, 1, 7, 6, 1, 2, 7, 2
Also calculate the hit ratio and miss ratio.

Solution-

Total number of references = 10

From here,
Total number of page faults occurred = 6

In the similar manner as above-


 Hit ratio = 0.4 or 40%
 Miss ratio = 0.6 or 60%

Problem-03:

A system uses 3 page frames for storing process pages in main memory. It uses the
Optimal page replacement policy. Assume that all the page frames are initially empty.
What is the total number of page faults that will occur while processing the page
reference string given below-
4 , 7, 6, 1, 7, 6, 1, 2, 7, 2

You might also like