What are Demand Paging and Pre-paging?
According to the concept of virtual memory, the entire process doesn’t need to be loaded into the main memory to execute any process at the given time. The process can be executed efficiently if only some of the pages are present in the main memory at a particular time. But, the problem here is how we decide the basis of the selection of pages to be loaded into the main memory for execution of a process beforehand. It means which page should be present in the main memory at a particular time and which one should not be there.
To resolve this problem, here is a concept of demand paging in an operating system. This concept says we should not load any page into the main memory until required or keep all the pages in secondary memory until demanded. In contrast, in Pre-Paging, the OS guess in advance which page the process will require and pre-loads them into the memory.
The concept of Pre-paging is used to reduce the large number of page faults that occur at the start of a process where the basic strategy is to bring all the pages into the memory that will be needed at the same time before the process references them.
What is Demand Paging in OS?
Demand paging is a technique used in virtual memory systems where the pages are brought in the main memory only when required or demanded by the CPU. Hence, it is also called lazy swapper because the swapping of pages is done only when the CPU requires it. Virtual memory is commonly implemented in demand paging.
In demand paging, the pager brings only those necessary pages into the memory instead of swapping in a whole process. Thus, demand paging avoids reading into memory pages that will not be used anyways, decreasing the swap time and the amount of physical memory needed.
How does Demand Paging Work?
The demand paging system depends on the page table implementation because the page table helps map logical memory to physical memory. Bitwise operators are implemented in the page table to indicate that pages are ok or not (valid or invalid). All valid pages exist in primary memory, and invalid pages exist in secondary memory. Now all processes go-ahead to access all pages, and then the following things will be happened, such as:
- Attempt to currently access page.
- If the page is ok (Valid), then all processing instructions work as normal.
- If anyone’s page is found invalid, then a page fault issue arises.
- Now memory reference is determined whether a valid reference exists on the auxiliary memory or not. If not exist, then the process is terminated, otherwise needed pages are paged in.
- Now disk operations are implemented to fetch the required page into primary memory.
Examples of Demand Paging
Suppose we have to execute a process P having four pages P0, P1, P2, and P3. Currently, in the page table, we have pages P1 and P3.
- If the CPU wants to access page P2 of a process P, it will first search the page in the page table.
- As the page table does not contain this page so it will be a trapor page fault. The control goes to the operating system as soon as the trap is generated and context switching happens.
- The OS system will put the process in a waiting/ blocked state. The OS system will now search that page in the backing store or secondary memory.
- The OS will then read the page from the backing store and load it to the main memory.
- Next, the OS system will update the page table entry accordingly.
- Finally, the control is taken back from the OS, and the execution of the process is resumed.
Hence, the operating system follows these steps whenever a page fault occurs and the required page is brought into memory.
So whenever a page fault occurs, as shown in all the above steps 2 to 6. This time taken to service the page fault is called the Page fault service time.
Effective Memory Access Time: When the page fault rate is ‘p’ while executing any process, then the effective memory access time is calculated as follows:
Effective Memory Access time = (p)*(s) + (1-p)*(m)
Where
- p is the page fault rate.
- s is the page fault service time.
- m is the main memory access time.
NOTE: EAT direct proportional to the page fault rate.
Advantages of Demand Paging
Here are the following advantages of demand paging in the operating system, such as:
- It increases the degree of multiprogramming as many processes can be present in the main memory simultaneously.
- There is a more efficient use of memory as processes having a size more than the size of the main memory can also be executed using this mechanism because we are not loading the whole page at a time.
- We have to the right for scaling of virtual memory.
- If any program is larger than physical memory, it helps run this program without compaction.
- Partition management is simpler.
- It is more useful in a time-sharing system.
- It has no limitations on the level of multi-programming.
- Discards external fragmentation.
- Easy to swap all pages.
Disadvantages of Demand Paging
Below are some disadvantages of demand paging in an operating system, such as:
- The amount of processor overhead and the number of tables used for handling the page faults is greater than in simple page management techniques.
- It has more probability of internal fragmentation.
- Its memory access time is longer.
- Page Table Length Register (PTLR) has a limit for virtual memory.
- Page map table is needed additional memory and registers.
What is Pre-paging in OS?
Pre-paging is used to overcome a major drawback of demand paging. A major drawback of demand paging is many page faults, which may occur as soon as a process starts to execute. The situation results from an effort to load the initial locality into memory, and the same situation may arise repeatedly.
For example, when a process is restarted after being swapped out, all its pages are present on the disk, and hence each of the pages must be brought back to the main memory for execution of the process by its own page fault the worst case.
If a system uses a Working Set Model, a list of pages is maintained with each process in its working set. If a process is suspended due to a lack of free frames or an I/O wait, the working set of the process is not lost. When a process is resumed, the entire working set is brought back into the memory before the process begins to execute again.
The above diagram shows that only one page was referenced or demanded by the CPU, but three more pages were pre-paged by the OS. The OS tries to predict which page would be next required by the processor and brings that page proactively into the main memory.
The major advantage of Pre-paging is that it might save time when a process references consecutive addresses. In this case, it is easy for the operating system to guess and load the appropriate pages, and, as there is a high probability of the guess being right for many pages, fewer page faults will occur.
Pre-paging may not always be beneficial. The advantage of Pre-paging is based on the answer to a simple question: whether the cost of implementing Pre-paging is less than the cost of servicing the corresponding page faults. It may be the case that a considerably large number of pages brought back into the memory by Pre-paging are not used. The disadvantage of the concept is that there is wastage of resources like time and memory if the pre-loaded pages are unused.
Advantages of Pre-paging
In an operating system, pre-paging has the following advantages, such as:
- It saves time when large contiguous structures are used. Consider anexample where the process requests consecutive addresses. So, in such cases, the operating system can guess the next pages. And, if the guesses are right, fewer page faults will occur, and the effective memory access time will increase.
Disadvantages of Pre-paging
Pre-paging also has the following disadvantages, such as:
- There is wastage of time and memory if those pre-paged pages are unused.
Difference between Demand Paging and Pre-paging
In demand paging, only those pages are brought into the main memory required to execute the program. When a program needs other pages, it will swap out the unused pages from the main memory and swap in the desired page. Thus, allowing it to execute the program through the space available is insufficient to bring the whole program into the main memory. One of the problems that arise due to demand paging is page fault which is caused due to the required page not being found in the main memory for which we require swapping.
In pre-paging, pages other than the one demanded by a page fault are brought in. The selection of such pages is done based on common access patterns, especially for secondary memory devices. Below are some more differences between the demand paging and pre-paging, such as:
Demand Paging | Pre-paging |
---|---|
Any page is not loaded into the main memory unless the process is referencing it at the present instant. | All the pages are loaded into the memory that will be needed simultaneously but before the process actually references them. |
The number of page faults is significantly high. | The number of page faults may be reduced in certain specific cases. |
The time taken to load the pages cannot decrease in any situation. | The time taken to load the pages decreases when a process references consecutive addresses. |
The pages loaded in the main memory are certainly used. | The pages loaded in the main memory might or might not be used. |
There is no wastage of resources as a page is loaded as and when needed. | There is wastage of resources as there is a high chance that the pages might be unused. |