Open In App

Algorithms For Demand Paging in OS

Last Updated : 11 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The operating system is necessary for managing computer resources and using hardware efficiently. Memory management involves allocating and releasing memory for various programs and files and is one of the most important aspects of modern working. Operating systems use a variety of methods to provide efficient memory management; one of them is request pagination. In this article, we will learn what paging is in the business context, why it is important, and how it improves computer performance.

Demand paging is a memory management technique used in operating systems to manage the allocation of physical memory (RAM) to ongoing programs in an effective manner. It is a fundamental component of current operating systems that helps them to make the greatest use of available memory resources while decreasing the time it takes to access data from secondary storage, such as hard disks or SSDs.

Terminologies Used in Denamd Paging

Before we get into demand paging, let’s define some key terms:

  • Paging: Paging is a memory management system that divides physical memory into fixed-size blocks known as “frames” and splits logical memory into fixed-size pieces known as “pages.”
  • Page Table: An operating system-maintained data structure that matches logical pages to physical frames. It aids the CPU in converting virtual addresses to physical addresses.
  • Virtual Memory: An abstraction that enables processes to use more memory than is physically available by combining RAM and secondary storage (often disk space).
  • Page fault: When a process attempts to access a page that is not already in physical memory, a page fault occurs. This initiates a request to load the required page from the server.

The Demand Paging Method

The following steps are involved in demand paging:

  • Initialization of the page: Only a part of a program’s code and data are loaded into physical memory as it begins to execute. The remainder of the program is still on secondary storage.
  • Fault Handling on the Page: A page fault is caused when the CPU attempts to access a page that is not in physical memory. The operating system then intervenes to handle the error.
  • Page Substitution: To create room for the requested page, the OS chooses a page from physical memory for replacement. For this reason, various page replacement algorithms such as Least Recently Used (LRU) or FIFO are used.
  • I/O on disk: The operating system fetches the necessary page from secondary storage (for example, a hard disk) and loads it into a free frame in physical memory. This necessitates a lengthy disk I/O transaction.
  • Page Table Refresh: The page table is changed to reflect the page’s new physical memory location.
  • Resumption of Execution: Once the page is in physical memory, the CPU can resume program execution as if nothing had happened. Because the requested page is now in RAM, subsequent memory requests are faster.

Example

Assume you have a text editor open on your PC and a large document. Only a few pages of the document are initially put into RAM. New pages are requested from secondary storage as you navigate through the document, resulting in page faults. The operating system manages page faults by loading the necessary pages into RAM and updating the page table.

Algorithms for Demand Paging

Below mentioned are the algorithms for the Demand Paging used in Operating Systems.

1. FIFO (First in First out)

The FIFO algorithm replaces the oldest page in memory with the new page when a page fault occurs.

Initial state: Empty memory frames.
Page references: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
Page faults: 1, 2, 3, 4 (filling up memory)
5 (replaces 1, as it's the oldest)
6 (replaces 2)
7 (replaces 3)
8 (replaces 4)
9 (replaces 5)
10 (replaces 6)
Total page faults: 7

2. LRU (Least Recently Used)

LRU replaces pages that have not been used for a long time. It works well in many situations but can be very cumbersome as it needs to keep track of the order in which pages are used.

Initial state: Empty memory frames.
Page references: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
Page faults: 1, 2, 3, 4 (filling up memory)
5 (replaces 1, as it's the least recently used)
6 (replaces 2)
7 (replaces 3)
8 (replaces 4)
9 (replaces 5)
10 (replaces 6)
Total page faults: 7

3. LFU (Least Frequently Used)

LFU replaces the least number of pages. It focuses on removing pages that are not frequently visited.

Initial state: Empty memory frames.
Page references: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
Page faults: 1, 2, 3, 4 (filling up memory)
5 (replaces 1, as it's the least frequently used)
6 (replaces 2)
7 (replaces 3)
8 (replaces 4)
9 (replaces 5)
10 (replaces 6)
Total page faults: 7

4. Optimal Page Replacement

The algorithm is theoretical and requires knowledge of future page access; It is impossible to achieve this. It is impossible to follow the application.

Advantages of Demand Paging

  • Optimal Memory Utilization: Demand paging allows the operating system to load only the elements of a program or process that need to be loaded into physical memory when they are required. This reduces memory waste and guarantees that actual RAM is used properly, allowing many processes to execute at the same time.
  • Fast Process: Processes can start rapidly with demand paging because they do not have to load their whole code and data into memory before execution. Only the pages needed to begin a process are loaded, minimizing starting time.
  • Virtual Memory Support: Demand paging creates the illusion of a much bigger virtual memory space than is available. This is advantageous for applications that require a large amount of memory because it allows them to run in a virtual address space that exceeds the restrictions of real RAM.
  • Improved System Responsiveness: Demand paging improves system responsiveness by loading only the pages that are actively in use. It guarantees that processes have timely access to the data and instructions they require, decreasing program execution latency and delays.
  • Efficient Multi programming: Demand paging allows several processes to share physical memory effectively in a multi-user or multitasking environment. Processes can be shifted in and out of memory as needed, allowing for efficient multi-programming without overcrowding.

Disadvantages of Demand Paging

  • Demand paging incurs overhead in terms of time and system resources. When a page fault occurs (that is, a necessary page is not in physical memory), the operating system must recover the page from secondary storage (for example, a hard drive or SSD). This method involves disk I/O, which is slower than reading data from RAM. This might cause latency and lower overall system performance.
  • Handling page faults can be difficult and resource-intensive for the operating system. The system must locate the appropriate page on secondary storage, load it into RAM, and update the page tables. Frequent page errors can result in unnecessary overhead, especially in settings with limited physical memory.
  • Garbage occurs when the system spends more time going in and out of physical memory rather than completing important tasks. It may be caused by insufficient physical memory to meet the demands of the operating system. Turbulence can slow down the body’s performance and make it unresponsive.
  • Disk fragmentation can occur when pages are frequently moved in and out of secondary storage. This fragmentation can cause additional delays when accessing data because required pages may be scattered across the disk rather than contiguous.

Conclusion

Demand paging is an important memory management method that allows operating systems to make better use of physical memory resources while avoiding delays in accessing data from secondary storage. Anyone interested in operating systems and computer architecture must understand its ideas and methods.

FAQs on Demand Paging in OS

Q.1: What happens if there is insufficient physical memory space for a requested page?

Answer:

If physical memory does not have enough free space, the operating system must select a page to evict (replace). This is when page replacement algorithms come into play, and a page is picked for replacement based on the algorithm used.

Q.2: What is the difference between demand paging and swapping?

Answer:

Demand paging only loads the pages of a process into memory as they are required. Swapping, on the other hand, entails moving a full process in and out of memory.

Q.3: Can demand paging cause performance problems?

Answer:

Although demand paging is necessary for efficient memory management, excessive page faults and frequent disk I/O operations might degrade performance. As a result, optimizing the page replacement process is critical for reducing performance difficulties.

Q.4: What are the benefits of demand paging?

Answer:

Demand paging makes efficient use of physical memory by loading just the necessary portions of processes. It also enables processes to outgrow physical memory, giving applications a larger address space.

Q.5: Is it possible to disable demand paging on my operating system?

Answer:

Disabling demand paging is not usually suggested because it can result in wasteful memory utilization and system instability. The majority of modern operating systems rely on demand paging for optimal memory management.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads