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:
Examples of Demand PagingSuppose 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.
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
NOTE: EAT direct proportional to the page fault rate.Advantages of Demand PagingHere are the following advantages of demand paging in the operating system, such as:
Disadvantages of Demand PagingBelow are some disadvantages of demand paging in an operating system, such as:
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-pagingIn an operating system, pre-paging has the following advantages, such as:
Disadvantages of Pre-pagingPre-paging also has the following disadvantages, such as:
Difference between Demand Paging and Pre-pagingIn 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:
Next TopicWhat is Interrupt in OS |