Working of Demand Paging in Operating System
This article explain a very important topic of operating system-Demand paging with real-life analogy
Have you ever wondered how your computer can run many programs simultaneously without crashing? It is because of paging and demand paging. Paging is a technique that splits program code and data into small, manageable pieces that the processor can quickly access. And demand paging is a technique in which only the required page is taken into main memory. This post will look closely at demand paging and how it works in modern operating systems. We’ll also discuss some of the benefits and drawbacks of this demand paging technique in this article and will also learn what demand paging is in os.
Must Check: History of Operating Systems
Table of Contents
Best-suited IT & Software courses for you
Learn IT & Software with these high-rated online courses
Real-life Analogy
Let’s look into real-life analogy first.
Suppose a customer goes to a shop to buy clothes. Then shopkeeper will first check if the demanded item is in the shop. For that, he will consult the records maintained for the items at the shop. If that required item is not there at the shop, he will send the helper to bring it from Warehouse, where they have all clothes in all sizes. So basically, the item is brought to the shop on the customer’s demand. All the things will not be there at the shop already because the shop has a limited area. And the second thing to note here is that customers will go and check the shop in case anything is required, not the Warehouse.
What Is Demand Paging?
Demand paging is a memory management scheme employed by modern operating systems to manage physical memory resources.
Demand paging is a process in which data is moved from secondary memory to RAM on a demand basis, which means all data is not stored in the main memory because the space is limited in RAM.So if the CPU demands the process, if that page is not in RAM, then swapping is needed. This means shifting the existing page from RAM and putting back in secondary memory and putting the new page in RAM.
You May Like – Difference Between Paging And Segmentation
Why Demand Paging?
Every process contains a lot of pages. But it is not efficient to put all the pages of that process in the main memory. As RAM size is limited too. So it’s better when the process is in execution then loading the pages according to the need. As it might be possible that any application doesn’t need all its pages for running the application. For example, e.g. If there is a program calculating the overall cost of painting a room. We have different pages having different codes. Let’s say this process has three pages P1, P2, and P3 containing different codes.
- P1-code for calculating cost of labor.
- P2- code for calculating the cost of the paint.
- P3- code for calculating Calculate surface area
Suppose the user wants to calculate the cost of labor only. Then CPU will demand only page 1, and there is no point in loading pages 2 and 3. If there was no concept of demand paging, then the whole program must have loaded.
Also read: What is Operating Systems (OS) – Types, Functions, and Examples
Advantages of Demand Paging
- It can improve performance by allowing the operating system to keep more programs and files in memory, thereby reducing the number of times that they need to be loaded from the disk.
- It can allow the operating system to use more memory than is physically installed by using virtual memory.
- Fast response times to requests,
- Greater throughput (the ability to service more requests in the same amount of time)
Disadvantages of Demand Paging
- Can slow down the system because it requires the kernel to keep track of which pages are in use by the applications and which are free.
- Lead to memory address space fragmentation, making it harder for the system to process applications.
- Causes the system to page in applications from disk too frequently, leading to data corruption.
- Memory access time is longer.
- Overhead increases due to page table access and interrupt handling.
How does Demand Paging Work?
When the request comes from the CPU for accessing any page, the page table is referred to find where that page is in the main memory. Suppose that page is found in the main memory, then well and good. If not found, then page fault occurs. So, page fault is a situation in which the page CPU wants to access is not in the main memory.
What to do in that case?
In that case, that particular page is brought(swapped) in. But from where is it swapped in? The answer is secondary memory. Swapped-in means removing a program from a hard disk and returning it to the main memory or RAM. But if the page has main memory, it is fetched from it.
Else, pages are loaded from secondary memory. Swapping is also done in that case. We also have valid and invalid bits. A valid-invalid bit is associated with each page table entry. So when the CPU tries to find the page in the main memory, it first checks the valid-invalid bit corresponding to that page.
- Valid bits-It is sometimes denoted by v.If the bit is valid, then the page is legal and in memory.
- Invalid bits-‘i’ denotes an invalid bit. If the bit is invalid, then that means the page is either not valid, or the page is valid but is currently not on the disk.
Let’s understand with an example.
Steps of Demand Paging
Whenever a page request comes, the table is consulted first. Some frames have invalid bit means either they are not in the main memory then-
- In case of a page fault, an interrupt is generated.
- After interrupting the current process, the operating system blocks the process and moves to the blocked state.
- The requested process is searched in the secondary memory.
- Check for the free frame, but page replacement algorithms are followed if you do not find any free frame.
- The page is replaced(from secondary memory to main memory) with the help of the page replacement algorithms.
- The CPU is informed about that update. And asked to proceed with the execution, and the process returns to its ready state. The page table is updated accordingly.
Conclusion
Demand paging is a memory management scheme used by modern operating systems to manage the limited memory available. When a process needs memory, the operating system will load the required pages from the hard drive. When a process no longer uses a page, the operating system will page it to free up space for other processes. Demand paging is an essential feature of modern operating systems, and it helps ensure that all processes have access to the memory they need. If you liked this article then help your friends in understanding this concept by sharing this article.
Happy learning!!!
More blogs people are reading in Operating System:
Functions of Operating System | Types of Operating System | Memory Management in Operating System | Process Management in Operating System | Demand Paging in Operating System | Operating System Interview Questions | CPU Scheduling algorithm in Operating System | Process Scheduling Algorithm in Operating System | Kernel and its types | Memory Management Techniques in Operating System | Interprocess Communication in Operating System | Swapping in Operating System | Segmentation Technique in Operating System | Paging in Operating System | Memory Hierarchy in Operating System | Interrupts in Operating System | Deadlock in Operating System
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio