Header Ads Widget

Virtual Memory in Operating System

Virtual Memory

It is most common used now days. Virtual Memory is a technique which provides an illusion to user that a very large size of main memory. It is done by using some part of secondary memory as the main memory.

In this technique, those processes which having the larger size than RAM can also be loaded. For this purpose, OS does not load the entire process in main memory. But it loads some parts of each process in main memory.

In this way more and more process are accommodated in main memory and the main target of degree of multi-programming is achieved.

Working of Virtual Memory 

Suppose, if some pages needs to be loaded in RAM for the execution and RAM is already full. Then the question is how these pages will enter into RAM? The answer is, OS use some Page Replacement algorithms (i.e. LRU) to load these pages in RAM. According to these algorithms some of the RAM pages are Swap OUT as the entering of new pages. The SWAP OUT pages are shifted to secondary memory.

All this, provides an illusion of unlimited RAM to user because all processes (even bigger than RAM) are executing successfully.

As we see in virtual memory, instead of loading the entire process in main memory, OS loads the some pages of a process in main memory.

The question is that which part of process is loaded in RAM? Because we cannot decide in advance that which part should be kept in main memory and which should be in secondary memory?

For this purpose OS use Demand Paging Concept. Demand paging uses the Page replacement algorithms. All of this is explained in the later lectures in detail.

Explain Through Example

Suppose 2 processes, P1 and P2, each process contains 8 pages. The size of each page is 1 KB. The main memory contains 16 frames of 1 KB each. The OS resides in the first partitions. First page of P1 is stored in the fifth partition and the other partitions are also filled with the different pages of processes in RAM.

The page tables of both the processes is also shown in the diagram

CPU registers contains the base address of each frame. So in the diagram base address of Page table 1 in the main memory is 5 and base address of Page table 2 is 6. The base address of any page table is added to the page number of the Logical address. In this way OS finds actual corresponding entry.

Virtual memory in OS

Advantages of Virtual Memory

  1. Degree of Multi-programming is achieved.
  2. Processes having even bigger the size of RAM are executed successfully.
  3. Cost effective because System can work through with smaller RAM size.

Disadvantages of Virtual Memory

  1. System becomes slower because of swapping, switching, and compaction etc.
  2. As virtual memory consumes some space of hard disk.

Page Fault Service Time :
The time taken to service the page fault is called as page fault service time. The page fault service time includes the time taken to perform all the above six steps.

Let Main memory access time is: m
Page fault service time is: s
Page fault rate is : p
Then, Effective memory access time = (p*s) + (1-p)*m

Swapping:

Swapping a process out means removing all of its pages from memory, or marking them so that they will be removed by the normal page replacement process. Suspending a process ensures that it is not runnable while it is swapped out. At some later time, the system swaps back the process from the secondary storage to main memory. When a process is busy swapping pages in and out then this situation is called thrashing.

swaping

Thrashing :
virtual_mem_2

At any given time, only few pages of any process are in main memory and therefore more processes can be maintained in memory. Furthermore time is saved because unused pages are not swapped in and out of memory. However, the OS must be clever about how it manages this scheme. In the steady state practically, all of main memory will be occupied with process’s pages, so that the processor and OS has direct access to as many processes as possible. Thus when the OS brings one page in, it must throw another out. If it throws out a page just before it is used, then it will just have to get that page again almost immediately. Too much of this leads to a condition called Thrashing. The system spends most of its time swapping pages rather than executing instructions. So a good page replacement algorithm is required.

In the given diagram, initial degree of multi programming upto some extent of point(lamda), the CPU utilization is very high and the system resources are utilized 100%. But if we further increase the degree of multi programming the CPU utilization will drastically fall down and the system will spent more time only in the page replacement and the time taken to complete the execution of the process will increase. This situation in the system is called as thrashing.

Causes of Thrashing :

  1. High degree of multiprogramming : If the number of processes keeps on increasing in the memory than number of frames allocated to each process will be decreased. So, less number of frames will be available to each process. Due to this, page fault will occur more frequently and more CPU time will be wasted in just swapping in and out of pages and the utilization will keep on decreasing.

    For example:
    Let free frames = 400
    Case 1: Number of process = 100
    Then, each process will get 4 frames.

    Case 2: Number of process = 400
    Each process will get 1 frame.
    Case 2 is a condition of thrashing, as the number of processes are increased,frames per process are decreased. Hence CPU time will be consumed in just swapping pages.

  2. Lacks of Frames: If a process has less number of frames then less pages of that process will be able to reside in memory and hence more frequent swapping in and out will be required. This may lead to thrashing. Hence sufficient amount of frames must be allocated to each process in order to prevent thrashing.

Recovery of Thrashing :

  • Do not allow the system to go into thrashing by instructing the long term scheduler not to bring the processes into memory after the threshold.
  • If the system is already in thrashing then instruct the mid term schedular to suspend some of the processes so that we can recover the system from thrashing.

Post a Comment

0 Comments