The concept of virtual memory comes into rescue when the size of the process is very much bigger than the size of the main memory and we are unable to load the entire process into the Main Memory. Therefore, we load a part of the process and run to completion; once that part is over, pull it back and allocate the rest part.
In earlier days, the concept of virtual memory was used only when the size of the process>>the size of the main memory. But later, it is used to take the advantage of the degree of multiprogramming.
- The Degree of multiprogramming - many processes or a part of many processes possible present in the main memory.
Virtual Memory has introduced many advantages in Operating Systems but not that easy to implement! In simpler words, main memory is divided into several frames and there are many processes to be allocated inside main memory; the main concept behind virtual memory is too see many processes or a part of as many processes possible present in the main memory.
Without the concept of Virtual Memory, several processes cannot share one resource at the same time. For example, several friends sharing food from the same lunchbox. Think “friends” as PROCESSES and “lunchbox” as MAIN MEMORY. Without the advantage of virtual memory, it’s like one person eating from the lunch box alone i.e., the entire process will be present in the main memory which can be run till completion, then move it out and allocate another process and the story goes on...
Main Memory is divided into frames; each processes (P1, P2, P3, P4,P5) will be allocated to each frames.
Several processes share same main memory, therefore, it’s important to manage the processes so that each of them gets chance to share. Hence, two algorithms are used to allocate pages into frames accordingly.
Both the algorithms have significant impact on the system performance. In order to increase the performance of the system, these algorithms have to be designed and implemented well. Or else it might to lead into problems such as, thrashing.
- If we allocate no. of frames to a process comparatively less than the no of frames required; this will lead to thrashing which means for every instructions there will be a page fault (desired page not present in main memory). The overall execution time will increase exponentially. Trashing is highly undesirable and avoided as it slows the system performance.
1. FRAME ALLOCATION
This algorithm helps us to decide no. of frames to be allocated to any process. There’s always some requirement defined by the process/system architecture.
- The maximum number of frames required by any process is the size of the process itself.
- The minimum number of frames required depends on architecture for execution of the instruction
Strategies used from Frame Allocation
1.1. EQUAL ALLOCATION: each number of frames will be equally distributed among processes. Not very much useful as not every process will require equal number of frames; some process may require extra frames whereas some process may require less number of frames.
For example, given no. of frames: 6
No. of processes available: 3
Therefore, each process will get 2 frames
Main Memory divided into frames in such a way that every processes gets equal no. of frames.
1.2 WEIGHTED ALLOCATION: Depending on the size of the processes, number of frames will be allocated accordingly. More number of frames given the process of larger size.
- It is commonly used as compared to other algorithms.
For example: available processes of size P1: 20 Pages, P2: 30 Pages, P3: 50 Pages
Available frames: 10
Requirement: P1= 20/100*10=2 (P1+P2+P3:20+30+50=100)
Therefore,P1,P2,P3 will get 2,3,5 no of frames respectively.
1.3. PRIORITY ALLOCATION: The processes with higher priority no. gets more frame. If the process with higher priorities wants more frames; then it can forcefully replace processes with lower priorities. Suppose, Process P1 has higher priority than process P2 and requires more frame then P1 pulls out P2 and uses up the frame.
2. PAGE-REPLACEMENT ALGORITHM
Once we allocate the frame to the process, pages are using the main memory. Then when we need a new page which is not available in the main memory, we need to replace any frame already present in the main memory. The page replacement algorithm helps us to decide which page to replace to get lowest page fault rate and this decision has a significant impact on the system performance.
In the main memory ,already pages P1,P2,P3,P4,P5 are allocated. Now a new page P6 has arrived, therefore, we will use page replacement algorithm to decide which page to replace with P6.
2.1. LOCAL PAGE-REPLACEMENT: Local page replacement strategy works as static allocation. Whenever we need to replace a page from the main memory then we will replace the page only from the frames which are allocated to that particular process without disturbing any other pages of other processes.
2.2. GLOBAL PAGE-REPLACEMENT: This strategy works differently than local page replacement strategy while replacing any page we have to consider everything for replacement, we can consider all the available frames to replace.
Local page-replacement are preferred more than global page-replacement and practically used in most of the operating system.
There are several algorithms used for page-replacement but the most popular algorithms are:
- OPTIMAL: It replaces the pages which will not be referred for a very long period of time in future and gives least page faults. It works according to future requirement.
- LRU (LEAST-RECENTLY USED):- It replaces the page which has been least recently used or has not been referred for a long period of time. It works according to the past.
- FIFO (FIRST-IN-FIRST-OUT): The name itself says, the first one to be added in the memory will be the first to go.