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.
Advantages of Virtual Memory
- Degree of Multi-programming is achieved.
- Processes having even bigger the size of RAM are executed successfully.
- Cost effective because System can work through with smaller RAM size.
Disadvantages of Virtual Memory
- System becomes slower because of swapping, switching, and compaction etc.
- As virtual memory consumes some space of hard disk.
Let Main memory access time is: mPage fault service time is: sPage fault rate is : pThen, 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.
- 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 = 400Case 1: Number of process = 100Then, each process will get 4 frames.Case 2: Number of process = 400Each 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.
- 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.
0 Comments