DEV Community

Ankit Thakur
Ankit Thakur

Posted on

Paging In OS

Paging in Operating System

Paging is a memory management technique used by an Operating System (OS). It is used to manage the main memory efficiently and allows a process to use memory even when its data is not stored continuously in physical memory.

In paging, the logical memory of a process is divided into fixed-size blocks called pages, while the physical memory (RAM) is divided into fixed-size blocks called frames. The size of a page and a frame is always the same.

How Paging Works

When a program is executed:

The program is divided into fixed-size pages.
The main memory is divided into fixed-size frames.
The OS loads the required pages into available frames.
A page table is maintained by the OS to keep track of which page is stored in which frame.
When the CPU generates a logical address, the OS uses the page table to convert it into a physical address.
Example

Suppose a process has 4 pages:

Page 0
Page 1
Page 2
Page 3

And RAM has available frames:

Frame 0
Frame 1
Frame 2
Frame 3

The pages do not have to be stored continuously. For example:

Page Frame
Page 0 Frame 2
Page 1 Frame 0
Page 2 Frame 3
Page 3 Frame 1

The page table stores this mapping.

So, if the CPU wants to access Page 2, the OS checks the page table and finds that Page 2 is present in Frame 3.

Logical and Physical Address

A logical address in paging is divided into two parts:

Logical Address = Page Number + Offset

Page Number: Identifies the page.
Offset: Identifies the exact location inside that page.

The page number is used to find the corresponding frame from the page table. Then the frame number and offset are combined to create the physical address.
Advantages of Paging
It reduces external fragmentation.
Memory can be used more efficiently.
A process does not need to be stored in continuous memory.
It makes virtual memory possible.
Memory allocation becomes easier for the OS.
Disadvantages of Paging
It requires extra memory for maintaining page tables.
Address translation can increase memory access time.
It can cause internal fragmentation, especially when a page is not completely filled.
Large processes may require large page tables.

Top comments (0)