WikiGalaxy

Personalize

Paging and Segmentation in Operating Systems

Paging

Introduction to Paging:

Paging is a memory management scheme that eliminates the need for contiguous allocation of physical memory. It divides the process's memory into fixed-size units called pages, which correspond to frames in physical memory.

  • Pages are of fixed size.
  • Eliminates external fragmentation.
  • Logical address is divided into page number and offset.
  • Physical memory is divided into frames.

Page Table

Role of Page Table:

Page tables are used to keep track of the mapping between logical and physical addresses. Each process has its own page table.

  • Maps logical page numbers to physical frame numbers.
  • Stored in main memory.
  • Contains base address of each page in physical memory.
  • Essential for address translation.

int logicalAddress = 2049; // Example logical address
int pageSize = 1024; // Size of each page
int pageNumber = logicalAddress / pageSize;
int offset = logicalAddress % pageSize;
System.out.println("Page Number: " + pageNumber + ", Offset: " + offset);
        

Address Translation:

In paging, the logical address is divided into a page number and an offset. The page number is used to index into the page table to find the frame number, and the offset is added to get the physical address.

Console Output:

Page Number: 2, Offset: 1

Segmentation

Introduction to Segmentation:

Segmentation is a memory management technique that divides the memory into variable-sized segments where each segment represents a logical unit such as a function, array, or object.

  • Segments are of variable size.
  • Eliminates internal fragmentation.
  • Logical address is a pair of segment number and offset.
  • Each segment has a base and a limit.

Segment Table

Role of Segment Table:

Segment tables are used to store the base address and the limit of each segment, allowing for the translation from logical to physical address.

  • Maps segment numbers to base and limit.
  • Used for address translation and protection.
  • Ensures access within segment limits.
  • Stored in main memory.

int segmentNumber = 1; // Example segment number
int offset = 150; // Offset within the segment
int[] base = {0, 1000, 2000}; // Base addresses of segments
int[] limit = {999, 999, 999}; // Limits of segments
if (offset < limit[segmentNumber]) {
    int physicalAddress = base[segmentNumber] + offset;
    System.out.println("Physical Address: " + physicalAddress);
} else {
    System.out.println("Error: Offset exceeds segment limit.");
}
        

Address Translation:

In segmentation, the logical address consists of a segment number and an offset. The segment number is used to index into the segment table to find the base address, and the offset is added to get the physical address.

Console Output:

Physical Address: 1150

Advantages of Paging and Segmentation

Paging Advantages:

  • Eliminates external fragmentation.
  • Efficient memory utilization.
  • Simple implementation of memory allocation.
  • Supports virtual memory.

Segmentation Advantages:

  • Reflects logical program structure.
  • Allows for dynamic growing of segments.
  • Facilitates sharing and protection.
  • Improves modularity of programs.

Disadvantages of Paging and Segmentation

Paging Disadvantages:

  • Internal fragmentation due to fixed page size.
  • Overhead of maintaining page tables.
  • Page table size increases with process size.
  • Complexity in handling page faults.

Segmentation Disadvantages:

  • External fragmentation due to variable segment size.
  • Overhead of maintaining segment tables.
  • Complexity in memory allocation.
  • Difficulty in segment sharing.

Combining Paging and Segmentation

Hybrid Approach:

Some systems use a combination of paging and segmentation to leverage the benefits of both techniques while minimizing their disadvantages.

  • Segments are divided into pages.
  • Reduces external fragmentation.
  • Improves memory protection and sharing.
  • Supports large address spaces.

int segmentNumber = 2; // Example segment number
int pageNumber = 1; // Page number within segment
int offset = 128; // Offset within page
int[][] base = {{0, 1024}, {2048, 3072}, {4096, 5120}}; // Base addresses
int[][] limit = {{1023, 1023}, {1023, 1023}, {1023, 1023}}; // Limits
if (offset < limit[segmentNumber][pageNumber]) {
    int physicalAddress = base[segmentNumber][pageNumber] + offset;
    System.out.println("Physical Address: " + physicalAddress);
} else {
    System.out.println("Error: Offset exceeds page limit.");
}
        

Hybrid Benefits:

The hybrid approach of combining paging and segmentation allows systems to efficiently manage memory by taking advantage of the structured program design of segmentation along with the elimination of fragmentation through paging.

Console Output:

Physical Address: 4352

Case Study: Linux Memory Management

Linux Memory Management:

Linux uses a combination of paging and segmentation for memory management. It primarily uses paging for memory allocation and protection while using segmentation for logical separation of user and kernel spaces.

  • Uses a flat memory model with paging.
  • Segmentation is minimal and used for separation.
  • Efficient handling of large address spaces.
  • Supports virtual memory with demand paging.

// Linux page table example
int logicalAddress = 4096; // Example logical address
int pageSize = 4096; // Page size in Linux
int pageNumber = logicalAddress / pageSize;
int offset = logicalAddress % pageSize;
System.out.println("Page Number: " + pageNumber + ", Offset: " + offset);
        

Efficiency in Linux:

Linux's approach to memory management allows it to efficiently handle processes and memory allocation, providing robust support for multitasking and process isolation.

Console Output:

Page Number: 1, Offset: 0

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025