• Articles
  • Tutorials
  • Interview Questions
  • Webinars

Paging in OS

Paging in OS

The concept of paging in operating systems is important in modern computer memory management because it determines how efficient and effective our computer systems are. In this blog, we will go into the details of paging, from its basics to its advanced forms, while explaining how it can be used to optimize memory usage, improve multitasking capabilities, and guarantee the smooth operation of various digital devices.

Statista states that Windows, a Microsoft product, is an operating system (OS) utilized in computers today by the highest number of people across the globe. It has a market share of 68.15% for desktops, tablets, and game consoles as of February 2024. The next most popular operating system is Apple’s MacOS, with its iOS mobile operating system ranking fourth as a standard installation across all iPad devices on the market. Linux OS versions are mainly meant for consumers who prefer open-source software and want to avoid being tied to big OS developers.

Watch this YouTube video on paging in OS by Intellipaat:

What Is Paging in the OS?

The necessity for a memory management technique that does not require physical memory allocations to be contiguous leads to paging. The activity of bringing back processes onto the main memory that were initially retained on secondary storage in terms of pages is known as paging. 

Paging acts as a mode of separating each process into pages. It will also include dividing the main memory into frames. This method allows the process’s physical address space to be non-continuous. In this scheme, physical memory is partitioned into fixed-size blocks called page frames, which have the same dimensions as the process pages. Likewise, the logical address space of a process is partitioned into fixed-sized blocks known as pages, which are equal in size to page frames. When requesting a new portion of memory during execution, an operating system assigns one or more page frames to a particular program and connects all its logical pages with those physical frames.

The mapping between logical pages and physical page frames is maintained utilizing a Page Table used by the Memory Management Unit (MMU) for translating logical addresses into their corresponding physical addresses. For every logical page number, the page table maps it to a certain physical frame number.

Paging in OS

Why Do We Need Paging?

To improve efficiency by giving out memory in blocks of fixed size or pages, paging is a technique of management that reduces internal fragmentation and makes possible the allocation of memory on demand. In addition, a simple mechanism for handling such issues as access rights and sharing can be put into action because each process has its page table. It is, however, worth noting that paging results in external fragmentation due to the maintenance of page tables and address translations, leading to reduced performance. However, address translation becomes simplified while memory usage is optimized, making it useful for contemporary operating systems, which should be efficient enough.

What Is Paging Protection?

The objective of paging protection in modern operating systems is to ensure that every process or program has exclusive control over its specific memory. This measure stops unauthorized modifications to and/or access to memories of other processes. The Memory Management Unit (MMU) validates access requests by examining the protection bits in page table entries; an interrupt alerts the operating system if such an effort infringes on already set permissions, like writing into read-only pages. Besides preventing illegal activities, this mechanism also fosters effective virtual memory management, which permits the simultaneous execution of several safe processes.

Get 100% Hike!

Master Most in Demand Skills Now !

How Does Paging in OS Work?

The paging concept in operating systems can be well understood through a simple example:

How Does Paging in OS Work

Main Memory and Frame Size

Let us assume we have a main memory of 16 KB. Assuming that the frame size is equal to 1 KB simplifies the problem. Consequently, our main memory will have 16 frames, each able to hold 1 KB of data.

Processes and Page Size

In this system, there are four processes, P1, P2, P3, and P4, each 4 KB long. Each process is divided into pages of size equal to the frame size. So, one page from the process fits in one frame available in the main memory.

Initial State

At first, all frames contain nothing. The pages of these processes get placed in these frames as they request more memory space. This is done to make sure that pages lie next to each other, filling up the frames as efficiently as possible.

Process Allocation

Process P1: Its pages are allocated to frames starting from frame 0.
Process P2: Its pages are allocated to frames starting from frame 4.
Process P3: Its pages are allocated to frames starting from frame 8.
Process P4: Its pages are allocated to frames starting from frame 12.
At this point, all frames are filled, and the processes are running smoothly.

Handling Wait Processes

It has been a while since P2 and P4 went into a waiting state. This has left eight frames lying empty. Meanwhile, another process known as P5 enters the ready queue, requiring 8 KB or 8 pages.

Loading New Process

Since paging accommodates non-contiguous allocation, we can load the pages of process P5 into the now-empty frames. For instance, by loading its pages using frames formerly occupied by processes P2 and P4, we have effectively replaced them until it is fully loaded.

Memory Management Unit (MMU)

In translating logical addresses (generated by the CPU) to actual locations in the main memory, MMU plays an important part. It makes use of page tables that map logical page numbers to physical frame numbers for this purpose.

An Example Scenario

If the CPU requests the 10th word on the 4th page of process P3, MMU refers to a particular entry in its page table from where it learns that frame 9 holds the 4th page of process P3. The answer is given by the tenth word in frame 9.

This illustration demonstrates the most important ideas about paging:

  • Division of both main memory and processes into fixed-size units (frames and pages). 
  • Allocation of pages to frames as needed, allowing for efficient use of memory. Issues on how to assign pages to frames are resolved by operating systems.
  • Use of the page table and MMU for address translation. The terminology used here is a bit complex. When we say ‘use’, it means that page tables must be effective in offering solutions to the problem of limited physical memory capacity through virtualizing the same.
  • Flexibility in loading processes into non-contiguous frames, enhancing memory utilization and system performance. Processes can thus be pre-loaded into several non-consecutive frames, thereby leading to improved system performance through better utilization of available memories.

Paging simplifies memory management by abstracting away from the complexities of physical memory layout and allowing for the implementation of virtual memory, which enables processes to use more memory than is physically available on the system. It is worth noting that implementation does not always mean adding up all these resources because only those processes that are active at any particular time need them.

Advantages and Disadvantages of Paging

Advantages of Paging

  1. Efficient Use of Memory: It splits the memory into small, manageable units called pages. This aids in the proper allocation and utilization of memory resources among different processes and applications. The result is reduced wastage and improved overall system performance.
  1. Protection and Security: The protection feature ensures that unauthorized access to the operating system and application data is blocked. Sensitive information is kept safe by assigning protection levels to each page.
  1. Reduced Fragmentation: In contrast to earlier approaches to memory allocation, paging structures memory management in a more organized fashion, thereby minimizing fragmentation problems. Consequently, this stops the scattering of data all over the hard disk, resulting in faster access and retrieval.
  1. Flexibility and Scalability: The dynamic nature of paging allows for the reallocation of memory resources when required because of changes in demand on an operating system. As a result, it works best under varying conditions.
  1. Virtual Memory Support: Paging facilitates virtual memory, which makes it possible for systems to use more RAM than they have by exchanging information between main memory (RAM) and secondary storage (disk). The capability greatly expands what a system can do, i.e., run applications or handle data.

Disadvantages of Paging

  1. Overhead and Latency: Introducing a paging system entails having overheads and latencies. Thereby, a flow is slowed down due to the necessity of continuous monitoring and maintenance, even when it has not been optimized or is resource-intensive.
  1. Fragmentation and Disk Space Issues: On the other hand, as far as memory disintegration is concerned, this could result in disk space fragmentation. Pages on the disc scatter data, thus wasting disc space as well as affecting systems’ operations.
  1. Increased Complexity: Indeed, including paging in an operating system contributes to its intricacies through design changes. Besides, grasping how it works properly can be tough, especially if one is new to this specific area.
  1. Memory Thrashing: Also, if excessive memory requests are made, resulting in the overloading of the page system, such conditions may be referred to as memory thrashing. In some instances, system performance can be significantly degraded, or the machine itself can be destabilized.
  1. Security Vulnerabilities: Unauthorized access or attacks may interfere with data saved on disks used for paging purposes. Furthermore, means of ensuring that data remains secure must be sought, for example, by encrypting such files.

Memory Management Unit

It would be impossible to imagine a computer system without the Memory Management Unit (MMU). It is a very important part of computers because it connects the central processing unit with physical memory. Efficiently, safely, and transparently managing CPU memory resources, and as such, it serves a crucial role.

main memory and secondary memory

Functions of the Memory Management Unit (MMU)

  • Address Translation: The MMU’s main job is to translate the CPU’s virtual addresses into real addresses near your computer’s common memory. It has to do this if the processor is to go to the right place in RAM and get the information it needs.
  • Memory Protection: MMU provides memory protection mechanisms that prevent unauthorized access to certain locations in memory. It ensures access-controlled policies are in place to safeguard the safety of an operating system and the integrity of data.
  • Virtual Memory Management: The MMU allows virtual memory implementation, which enables programs to run beyond the capacity of actual RAM. Thus, it supports multitasking by facilitating on-demand swapping of data between RAM and disk storage, enhancing system performance.
  • Memory Segmentation: Some MMUs offer memory segmentation, dividing the computer’s memory into sections with different permissions and attributes. This feature enhances memory access control and optimizes memory utilization.

Importance of the Memory Management Unit (MMU):

  • Efficient Memory Utilization: The MMU enables efficient use of memory resources by dynamically controlling the mapping between virtual and physical addresses. This prevents conflicts when running multiple programs simultaneously. 
  • Enhanced Security: Through its memory protection capabilities, the MMU contributes significantly to system security by preventing unauthorized access to critical memory regions and detecting software bugs.
  • Support for Virtual Memory: The MMU’s support for virtual memory management allows for the execution of larger programs than what the physical RAM can accommodate, thereby facilitating multitasking and boosting overall system performance. 
  • Facilitation of Operating System Functions: The MMU plays a vital role in supporting modern operating system functionalities, including process isolation, multitasking, and dynamic memory allocation. 

Keywords in MMU

  • Address Translation: This is a process of converting virtual addresses to physical addresses by the MMU to make the CPU access the right places in computer memory.
  • Cache Memory: It is a high-speed, quick data storage layer that stores some commonly used data or instructions to speed up their processing. The MMU can interact with this cache for better memory access.

In summary, the MMU is an important part of modern computer systems that ensures efficient and secure memory management, which is also transparent. Its roles and abilities are crucial to the operation of programs, system security, and overall computer productivity.

Important Points About Paging in Operating Systems

Memory management in operating systems uses a technique known as paging, which allows processes to use more memory than is available physically on the system. Following are some points about paging, summarized from the given sources:

Definition and Purpose

  • Paging is a memory management method that divides both the main memory and processes into fixed-size blocks referred to as pages. This division is great for the efficient utilization of memory and makes it possible for the non-contiguous allocation of memory with respect to processes.

Advantages of Paging

  • Efficiency and Performance Improvement: By allowing the Operating System to have more pages in use at any given time than could fit into physical memory, paging minimizes external fragmentation. In addition, it enhances memory utilization by ensuring that those pages often in demand are always resident while swapping out those not-so-frequently used thereby reducing the CPU’s idle time waiting for pages to be loaded from disk.
  • Simplified Memory Management: The process of tracking currently loaded pages simplifies overall system memory management. For instance, when swapping out a page there will be no need to worry about fragmentation because an operating system would pick a page with the least likely usage
  • Support for Virtual Memory: The use of paging makes virtual memory possible which gives each process its own address space even when there is inadequate physical memory to hold all processes; this greatly enhances multitasking and system performance

Paging Techniques

  • Demand Paging: This is the most common technique, where only the currently used page is kept in memory, optimizing memory usage.
  • Anticipatory Paging: A more aggressive form that preloads pages near the requested page to reduce page faults and latency.
  • Prepaging: Similar to anticipatory paging but less aggressive, preloading pages are expected to be needed soon.
  • Free Page Queue, Stealing, and Reclamation: Techniques for keeping track of memory demands, repossessing the frames that are less used, and assigning their memory as needed. Free page queues guarantee more pages are available, whereas page stealing means the machine briefly monopolizes main memory by removing a few pages that haven’t been referenced recently.
  • Pre-cleaning: If a page looks like it is going to be modified again soon, then the contents of its modified pages are saved to disk and put back into memory. This improves startup times.
  • Copy-on-Write Paging: When a process attempts to alter or rewrite a write-protected page, the system creates a copy of that page and gives the original one to the process to edit without interference.
  • Combined Partitioning: The best of both worlds here: paging and segmentation. Here, memory is divided into chunks of various sizes and segments that contain page tables used for mapping virtual addresses to physical ones.
  • Inverted Paging: The page table is held in memory, but the main memory is divided into frames. Every access requires a lookup from the page table to identify the physical frame.

Implementation Details

  • Memory Management Unit (MMU): The MMU converts the logical addresses produced by the CPU into actual addresses on main memory so that data can be stored and retrieved correctly from RAM.

When to Use Paging

1. Efficient Memory Utilization: To maximize the usage of physical memory by allowing many processes to share it. This is very helpful in environments where there are fewer physical memory resources than what is required to run applications or processes.

2. Minimize External Fragmentation: To reduce external fragmentation that occurs when free memory consists of non-contiguous blocks scattered all over the system. Paging breaks down memory into fixed sizes, which makes allocating and deallocating memory without leaving gaps much easier.

3. Enhanced Performance and Responsiveness: Paging can drastically reduce page faults by keeping only the frequently used pages in memory and swapping out others. Particularly important for applications that need fast access to data.

4. Virtual Memory Support: If your system depends on virtual memory as a way of increasing the apparent amount of memory beyond what is physically there, then paging is a must. It supports the concept of virtual memory by enabling operating systems to map virtual addresses into physical ones, thereby making it possible for processes to operate as if they had direct access to more memory than exists.

Conclusion

Paging is an important aspect of modern computer operating systems, which offers great improvements both in efficiency and performance and also helps with memory management. Its different methods can meet various operational needs, thus making it a versatile tool for system resource management.

FAQs

What is the concept of paging?

Paging is a way for the operating system to provide memory management that brings programs in secondary storage into main memory or onto disk in terms of perspective pages.

Why is paging used?

Paging brings data to the data address quickly. A certain number of pages are copied from files on storage devices and put into unit memory because now processes can have space that is not in a continuous physical address space.

What are pages and frames?

Pages are fixed-size blocks of logical address space for a process, whereas frames are fixed-size blocks of physical memory where pages are stored.

How does paging reduce fragmentation?

Paging reduces internal fragmentation by dividing memory into fixed-size blocks (pages) that may be smaller than the process’s data segments, thus making memory usage more efficient.

What are the advantages of paging?

Paging offers several advantages, including:

  • Efficient Memory Allocation: Paging allows memory to be allocated on demand, enabling efficient use of memory by allocating only the necessary amount of memory to a process.
  • Protection: Paging protects by ensuring that a process cannot access memory allocated to another process, preventing unauthorized access and data corruption.
  • Sharing: Paging enables memory sharing between processes by allowing multiple processes to access the same page in memory, reducing memory usage and improving system performance.

Course Schedule

Name Date Details
Python Course 27 Jul 2024(Sat-Sun) Weekend Batch
View Details
Python Course 03 Aug 2024(Sat-Sun) Weekend Batch
View Details
Python Course 10 Aug 2024(Sat-Sun) Weekend Batch
View Details

About the Author

Senior Consultant Analytics & Data Science

Presenting Sahil Mattoo, a Senior Consultant Analytics & Data Science at Eli Lilly and Company is an accomplished professional with 14 years of experience across data science, analytics, and technical leadership domains, demonstrates a remarkable ability to drive business insights. Sahil holds a Post Graduate Program in Business Analytics and Business Intelligence from Great Lakes Institute of Management.