Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Contiguous Memory Allocation
2.1.
Fixed Sized Partitioning
2.1.1.
Problems With fixed Sized Partitioning 
2.2.
Dynamic Partitioning
2.2.1.
Problems With Dynamic Partitioning 
2.3.
Compaction
3.
Need For Paging
4.
Frequently Asked Questions
4.1.
What is the basic function of Paging?
4.2.
What is Fragmentation in OS?
4.3.
Differentiate between Internal and External Fragmentation.
5.
Conclusion
Last Updated: Mar 27, 2024

Need for Paging

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM
Operating Systems

Introduction

As you know paging is a very important concept of an Operating system, but have you ever wondered about the need for paging? Why is it being employed? 

Well, in this article, we will discuss the need for paging with an example: 

Before we directly jump onto the need let’s take a look at the different types of memory management techniques: 

There are two types of memory management techniques: 

  • Contiguous memory allocation, which is further divided into two types:
  • Non-contiguous memory allocation, which is further divided into two types: 

As you can see above, paging is under the category of non-contiguous memory allocation. By this, we can easily get a rough idea of the need for paging. It also indicates that there must be some issues or dilemmas that the Contiguous memory allocation is unable to handle. But in order to make this rough idea clearer, we’ll dive deeper into the concept of Contiguous memory allocation and will understand the core need for paging. 

Let’s get started now!

Must Recommended Topic, Internal and External Fragmentation

Contiguous Memory Allocation

Contiguous memory allocation is a way of allocating a contiguous section/part of memory to a process or file that requires it. The allotment will be contiguous, meaning scattering is prohibited. 

Fixed Sized Partitioning

In fixed-sized partitioning, the main memory is partitioned into divisions of equal or different sizes. The RAM is allocated to the processes in a logical order.

In fixed-sized partitioning,

  • The partitions cannot overlap.
  • A process must be continuously present in a partition for execution.

Problems With fixed Sized Partitioning 

The process should either be the same or small size as the partition but not more than that. Processes with lesser size than the partition cause memory wastage which is often known as internal fragmentation

So to solve this problem we have dynamic partitioning.

Dynamic Partitioning

In dynamic partitioning, the partition size is not mentioned at the start of this approach. It is stated when the process is loaded. The operating system takes up the first partition. The remaining area has been divided into sections. Each partition will be the same size as the entire process. The partition size varies according to the process's requirements in order to minimize internal fragmentation.

Problems With Dynamic Partitioning 

The absence of Internal fragmentation does not mean that external fragmentation will not occur.

External fragmentation occurs when we are unable to allocate space to a process despite having total memory space available, but that space is not contiguous.

The holes or vacant spaces left by leaving a process mark empty spaces in the memory, and the non-contiguous spaces are not a good fit for a new upcoming process.

You can refer to this article to understand the difference between Internal and External Fragmentation. 

However, the problem of External fragmentation can be solved by the Compaction method.

Compaction

Compaction is a method of gathering free space in a huge memory chunk in order to free up space for processes. Due to the processes going in and out of memory, swapping creates many fragments in the memory. Compaction is the process of integrating all of the vacant areas and processes.

When the system is compacted, the efficiency of the system is reduced since all of the available spaces are relocated from multiple locations to a single one.

This method will take a long time to complete, and the CPU will be idle the entire time. Despite the fact that the compaction avoids external fragmentation, it makes the system inefficient.

As you can see, the compaction makes the system inefficient. So to solve the problem of inefficiency we are shifting towards non-contiguous memory allocation.

Here, the paging comes into play: 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Need For Paging

Time for an example to understand the Need for Paging.

Let us consider a process P1 of size 4 MB and the main memory which is divided into three partitions. Out of the three partitions, two partitions are holes of size 2 MB each.

P1 needs 4 MB of space in the main memory to be loaded. We have two holes of 2 MB each but they are not contiguous.

However, there is 4 MB of space available in the main memory in the form of those holes that remain useless until it becomes contiguous. This is a serious problem to address.

We need to have some kind of mechanism which can store one process at different locations in the memory.

Illustration Image

The idea behind paging is to divide the process into pages so that we can store them in the memory at different holes. The concept of paging is to divide a process into pages so that we can store them in any available space. In the above example, the process P1 of size 4MB will be divided into two pages each of 2MB. 

Let’s briefly look at what exactly Paging entails: 

Paging

  • Paging is a storage mechanism that allows the operating system to fetch processes from secondary storage and store them as pages in the main memory in a non-contiguous manner. 
  • The primary memory is partitioned into fixed-size pieces of physical memory called frames in the Paging method. 
  • Paging is used for faster access to data, and it is a logical concept. 
  • Paging is used to address the issue of external fragmentation. This allows a process's logical address space to be noncontiguous, allowing the process to be allocated physical memory.

Let us now discuss some of the frequently asked questions related to the topic.

You can also read about the interleave memory.

Must Read Evolution of Operating System

Frequently Asked Questions

What is the basic function of Paging?

Paging is a memory management technique that allows a process's logical address space to be non-contiguous.

What is Fragmentation in OS?

Fragmentation is an unwanted problem in the operating system that occurs when processes are loaded and unloaded from memory, causing free memory space to be fragmented.

Differentiate between Internal and External Fragmentation.

Internal fragmentation occurs when a process is assigned to a memory block that is larger than what is required. Other processes will not be able to use the extra memory that has been allocated. External fragmentation occurs when a process has enough total memory to run but the memory is not contiguous, preventing it from using it.

Conclusion

This article discusses the need for paging in the operating system and how it is used to solve the problem faced in contiguous memory allocation. 

Recommended Reading:

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Live masterclass