Table of contents
1.
Introduction
2.
What is demand paging in OS?
2.1.
How does demand paging work?
2.2.
Examples of demand paging
2.2.1.
Effective Memory Access Time: 
2.3.
Advantages of demand paging
2.4.
Disadvantages of demand paging
3.
What is Pre-paging in OS?
3.1.
Advantages of Pre-paging
3.2.
Disadvantages of Pre-paging
4.
Frequently Asked Questions
4.1.
What is the difference between demand paging and pre-paging?
4.2.
Are the two functions of paging a memory management function and multiprogramming a processor management function interdependent?
4.3.
What exactly is page cannibalism?
5.
Conclusion
Last Updated: Mar 27, 2024

What are Demand Paging and Pre-paging?

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Operating Systems

Introduction

According to what we had learned from Virtual Memory, the complete process does not need to be loaded into the main memory to execute any process. If some of the pages are present in the main memory at any given time, we can still carry on the process smoothly. However, the main issue is how we are going to decide which pages should be loaded into the main memory. 

The concept of demand paging in an Operating System can help us counter this problem. This notion states that we should not load any pages into the main memory until they are required and that we should maintain all pages in secondary memory until they are needed. In Pre-Paging, on the other hand, the OS predicts which pages the process will need ahead of time and pre-loads them into memory.

Pre-paging is a technique for reducing the high number of page faults at the start of a process. The primary method is to load all of the pages that will be needed simultaneously into memory before the process addresses them.

Also, see Need For Paging

What is demand paging in OS?

Demand paging is a virtual memory system approach that only brings pages into the main memory when the CPU requests them. As a result, it's often called as a lazy swapper because it only swaps pages when the CPU requests it. Virtual memory is widely used in demand paging. We had already known from the concept of cache, how virtual memory works. 

As we had done in traditional approaches we put the entire process, but here, demand paging allows the pager to bring only the required pages into memory. As a result, demand paging avoids pushing pages into memory pages that will not be used.

How does demand paging work?

Demand paging uses the concept of the page table which actually maps the logical memory to physical memory, so the demand paging system highly relies on its implementation. In the page table, we use bitwise operators to determine whether pages are acceptable or not (valid or invalid). In primary memory, all valid pages exist, and invalid pages reside in secondary memory. Now that all processes have been permitted to visit all pages, the following events will occur:


 

Illustration Image

  1. Currently attempting to access the page.
  2. All processing instructions will function normally if the page is valid.
  3. A page fault occurs when a user's page is deemed invalid.
  4. It is now determined whether or not a valid reference exists on the  Auxiliary memory. The procedure will be terminated if no pages are available; otherwise, needed pages will be paged in.
  5. The requested page is now fetched into primary memory via disc operations.

Examples of demand paging

Assume we need to execute a process P that has four pages: P0, P1, P2, and P3. We currently have pages P1 and P3 in the page table.

Illustration Image
  1. If the CPU needs to access page P2 of a process P, it must first search the page table for the page.
  2. It will be a page fault or trap because the page table does not contain this page. Context switching occurs when the trap is generated and control is passed to the operating system.
  3. The process will be placed in a blocked/waiting status by the operating system. The operating system will now search the backing store or secondary memory for that page.
  4. After the above step, the operating system will read the page from the backup, then store and load it into the main memory.
  5. Finally, control is regained from the OS, and the process execution is resumed.

As a result, whenever a page fault occurs, the operating system follows these steps and loads the required page into memory.

As a result, anytime a page fault occurs, all of the steps 2–6 are executed. The Page fault service time is the time it takes to fix a page fault.

Effective Memory Access Time: 

The effective memory access time is computed as follows when the page failure rate is 'p' while running any process:

The Effective Memory Access time = (p)*(s) + (1-p)*(m)

Here 

  • p here is the page fault rate.
  • s is the page fault service time.
  • And m is the main memory access time.

Advantages of demand paging

The following are some of the benefits of demand paging in the operating system:

  • It improves multiprogramming by allowing multiple processes to run in the main memory simultaneously.
  • For virtual memory scaling, we must go to the right.
  • It helps run any application that is greater than physical memory without compaction.
  • It's a lot easier to manage partitions now.
  • In a time-sharing system, it is more beneficial.

Disadvantages of demand paging

The following are some of the drawbacks of demand paging in an operating system:

  • It has a higher chance of internal fragmentation.
  • It takes longer to access memory.
  • Virtual memory is limited in the Page Table Length Register (PTLR).
  • Additional memory and registers are required for the page map table.


You can also read about the interleave memory.

What is Pre-paging in OS?

Pre-paging is used to solve one of the most significant drawbacks of demand paging. Many page faults, which can occur when a process starts to execute, are a vital disadvantage of demand paging. The condition arises due to an attempt to memorize the first location, and it may happen again.

Pre paging

When a process is resumed after being swapped out, all of its pages are present on the disc; therefore, in the worst case, each of the pages must be brought back to the main memory for execution by its page fault.

When a procedure refers to successive addresses, the main benefit of pre-paging is that it may save time. The operating system may easily guess and load the relevant pages in this situation. Because the assumption is likely to be correct for many pages, fewer page faults will occur.

Pre-paging isn't usually a good idea. The benefit of Pre-paging is based on whether the cost of adopting Pre-paging is less than the cost of servicing the related page faults. A significant proportion of pages brought back into memory by Pre-paging maybe never have been used. The drawback of the approach is that resources like time and memory are wasted if the pre-loaded pages are not used. However, in order to counter the drawback, we use two types of mapping, direct and associative mapping.

Advantages of Pre-paging

Pre-paging has the following advantages in an operating system:

  • When large continuous structures are used, it saves time. Let's take an example of a process that requires successive addresses. The operating system can make a guess about the upcoming pages in such cases. If the assumptions are correct, there will be fewer page faults, and the effective memory access time will increase.

Disadvantages of Pre-paging

Pre-paging also has some drawbacks, including:

  • If those pre-paged pages aren't used, there will be a waste of time and memory.

Frequently Asked Questions

What is the difference between demand paging and pre-paging?

A page is only brought into memory using demand paging when a position on that page is actually addressed during execution. Pre-paging involves bringing in pages other than the one needed by a page fault. The selection of such pages, especially for secondary memory devices, is based on frequent access patterns.

Are the two functions of paging a memory management function and multiprogramming a processor management function interdependent?

Yes.

What exactly is page cannibalism?

Page cannibalization refers to the act of switching or replacing pages.

Conclusion

In this article, we have extensively discussed the concept of demand paging and pre-paging. We hope that this blog has helped you enhance your knowledge and aspects that you should keep in mind while dealing with the demand paging and pre-paging techniques; here is an outline of operating systems and the topics which you must cover while learning OS. You can also check the introduction to operating systems here and the difference between 32-bit OS and 64-bit OS  here.

Recommended Reading:

We have the best track to learn and practice operating systems here; make sure to check it out and practice the questions to enhance your preparation. If you want to explore and learn operating systems from scratch for placement, make sure to check out this. If you would like to learn more, check out our articles here. Do upvote our blog to help other ninjas grow. 

Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practice top problems, you can check SQL problems here,  attempt mock tests, read interview experiences, and you can also check our guided path for the coding interview and much more!

Happy Learning!

Live masterclass