0% found this document useful (0 votes)
45 views8 pages

What Is Virtual Memory

Virtual memory notes

Uploaded by

Anagha G rao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views8 pages

What Is Virtual Memory

Virtual memory notes

Uploaded by

Anagha G rao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

VIRTUAL MEMORY MSC-IT

WHAT IS 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 large virtual memory is provided for user programs when a very small physical memory is there.
In real scenarios, most processes never need all their pages at once, for 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.

Virtual Memory:
1. In the most of the computer system, the physical main memory is not as large as address space of the
processor.
2. Suppose user tries to run a program.
3. If the program run by the user does not completely fit into the main memory then the parts of its
currently being executed are stored in main memory and remaining portion is stored in secondary
storage device such as HDD.
4. When a new part of program is to be brought into main memory for execution and if the memory is
full, it must replace another part which is already is in main memory.
5. As this secondary memory is not actually part of system memory, so for CPU, secondary memory is
considered as Virtual Memory.
6. Virtual memory is a memory management technique that is implemented using both hardware and
software.
7. It maps memory addresses used by a program, called virtual addresses, into physical addresses in
computer memory.

Department of MCA & MSC-IT, Dr. Shyama Prasad Mukherjee University, Ranchi
VIRTUAL MEMORY MSC-IT

Benefits of having Virtual Memory:


1. Large programs can be written, as 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.

Disadvantages of Virtual Memory:


1. Applications run slower if the system is using virtual memory.
2. It takes more time to switch between applications.
3. Less hard drive space for your use.
4. It reduces system stability.

Department of MCA & MSC-IT, Dr. Shyama Prasad Mukherjee University, Ranchi
VIRTUAL MEMORY MSC-IT

WHAT IS DEMAND PAGING?


The basic idea behind demand paging is that when a process is swapped in, its pages are not swapped in all
at once. Rather they are swapped in only when the process needs them(On demand). This is termed as lazy
swapper, although a pager is a more accurate term.

Initially only those pages are loaded which will be required the process immediately.
The pages that are not moved into the memory are marked as invalid in the page table. For an invalid entry
the rest of the table is empty. In case of pages that are loaded in the memory, they are marked as valid along
with the information about where to find the swapped out page.
When the process requires any of the pages that is not loaded into the memory, a page fault trap is triggered
and following steps are followed,

1. The memory address which is requested by the process is first checked, to verify the request made by
the process.
2. If it found to be invalid, the process is terminated.
3. In case the request by the process is valid, a free frame is located, possibly from a free-frame list,
where the required page will be moved.
4. A new operation is scheduled to move the necessary page from disk to the specified memory location.
(This will usually block the process on an I/O wait, allowing some other process to use the CPU in
the meantime.)
5. When the I/O operation is complete, the process's page table is updated with the new frame number,
and the invalid bit is changed to valid.
6. The instruction that caused the page fault must now be restarted from the beginning.

There are cases when no pages are loaded into the memory initially; pages are only loaded when demanded
by the process by generating page faults. This is called Pure Demand Paging.

Department of MCA & MSC-IT, Dr. Shyama Prasad Mukherjee University, Ranchi
VIRTUAL MEMORY MSC-IT

The only major issue with Demand Paging is, after a new page is loaded, the process starts execution from
the beginning. It is not a big issue for small programs, but for larger programs it affects performance
drastically.

Advantages

Following are the advantages of Demand Paging −

 Large virtual memory large virtual memory.


 More if More efficient use of memory efficient use of memory.
 There is no limit on degree of multiprogramming. There is no limit on degree of multiprogramming.

Disadvantages

 Number of tables and the amount of processor overhead for handling page interrupts are
greater than in the case of the simple paged management techniques.
 It suffers from internal fragmentation.
 There is an overhead of maintaining a page table for each process.
 The time taken to fetch the instruction increases since now two memory accesses is required.

PAGE REPLACEMENT
As studied in Demand Paging, only certain pages of a process are loaded initially into the memory. This
allows us to get more number of processes into the memory at the same time. But what happens when a
process requests for more pages and no free memory is available to bring them in. Following steps can be
taken to deal with this problem:

1. Put the process in the wait queue, until any other process finishes its execution thereby freeing
frames.
2. Or, remove some other process completely from the memory to free frames.
3. Or, find some pages that are not being used right now, move them to the disk to get free frames. This
technique is called Page replacement and is most commonly used. We have some great algorithms
to carry on page replacement efficiently.
4. Find the location of the page requested by ongoing process on the disk.
5. 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 any existing frame to be replaced, such frame is known as victim frame.
6. Write the victim frame to disk. Change all related page tables to indicate that this page is no longer in
memory.
7. Move the required page and store it in the frame. Adjust all related page and frame tables to indicate
the change.
8. Restart the process that was waiting for this page.

In an operating system that uses paging for memory management, a page replacement algorithm is
needed to decide which page needs to be replaced when new page comes in.

Department of MCA & MSC-IT, Dr. Shyama Prasad Mukherjee University, Ranchi
VIRTUAL MEMORY MSC-IT

Page Fault – A page fault happens when a running program accesses a memory page that is mapped
into the virtual address space, but not loaded in physical memory. Since actual physical memory is
much smaller than virtual memory, page faults happen. In case of page fault, Operating System might
have to replace one of the existing pages with the newly needed page. Different page replacement
algorithms suggest different ways to decide which page to replace. The target for all algorithms is to
reduce the number of page faults.

Page Replacement Algorithms:

 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-1Consider page reference string 1, 3, 0, 3, 5, 6 with 3 page frames. Find 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 come, 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 avilable so it replaces 0 1 page faults

Belady’s anomaly – 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 string 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.

 A very simple way of Page replacement is FIFO (First in First Out)


 As new pages are requested and are swapped in, they are added to tail of a queue and the page which
is at the head becomes the victim.
 Its not an effective way of page replacement but can be used for small systems.

Department of MCA & MSC-IT, Dr. Shyama Prasad Mukherjee University, Ranchi
VIRTUAL MEMORY MSC-IT

 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, 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.

 Least Recently Used –


In this algorithm page will be replaced which is least recently used.

Department of MCA & MSC-IT, Dr. Shyama Prasad Mukherjee University, Ranchi
VIRTUAL MEMORY MSC-IT

Example-3Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 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.

VIRTUAL MEMORY USING SEGMENTATION

Definition: Memory segmentation is the process in which we divide the primary memory of
the computer into segments. It is a memory management technique. Each segment can be allocated to a
process.
The segment table is the table which stores all the details about the segments which are further stored in one
of the segments. Segment table contains two information about the segment: one is the base address of the
segment and other is the length of the segment.

Need of Segmentation
Before segmentation, we use paging as our memory management technique as paging is more close to
the operating system instead of the user. Paging decreases the efficiency of the system as it can divide the
same function into different pages which may or may not be loaded into the memory at the same time. On the
other hand, segmentation divides the process into segments, and each segment can contain the same type of
functions.

Department of MCA & MSC-IT, Dr. Shyama Prasad Mukherjee University, Ranchi
VIRTUAL MEMORY MSC-IT

Difference between Paging and Segmentation


Paging Segmentation
Segmentation is closer to the user rather than the
Paging is closer to our operating system.
operating system.
There is no need of doing any external fragmentation. There is external fragmentation in segmentation.
In this, the page information is stored in the page In this, the information about segments is stored in
table. the segment table.
In this, our operating system is responsible. In this, the compiler is responsible for the working.
Paging suffers from internal fragmentation instead of Segmentation suffers from external fragmentation
external fragmentation. rather than internal fragmentation.
Paging divides our program into various fixed size Segmentation divides our program into various
pages. variable size segments.
In this, the logical address is divided into two things; In this, the logical address is divided into two things:
page number and page offset. segment number and segment offset.
Paging is faster than segmentation. Segmentation is slower than paging.
It is a non-contiguous memory allocation. It is a non-contiguous memory allocation.
The entry of the page table contains flag bits and On the other hand, the entry of segment table
frame number so that it can represent the details about contains base address and some protection bits of the
the pages. segments.

Translation of logical address into physical address by the segmentation


A logical address is generated by the CPU which consists of two things:
1. Segment number
2. Offset
The segment number which logical address contains is mapped into the segment table. Now we compare the
limit of the segment with the offset. If the respective limit is more than the offset, then there will be valid
address otherwise the address will be invalid. When the address in invalid, then it will show an error.
Through this, we get the physical address of the real word which is stored in the main memory.

Advantages of Memory Segmentation


• There is less overhead in segmentation.
• There is no internal fragmentation as external fragmentation is done in this.
• The size of the segment table is less whereas the size of the page table is big in paging.
• The average size of the segment is more as compared to the actual size of the page.
• We can easily relocate the segments as compared to the address space.

Disadvantages of Memory Segmentation


• It is a costly technique as compared to the other one.
• External fragmentation is there in it.
• Since there is a variably sized partition. So, it is difficult to allocate memory to them.

Department of MCA & MSC-IT, Dr. Shyama Prasad Mukherjee University, Ranchi

You might also like