CodeMaster
Article ID: 2511
operating-system
4 min read
Contiguous Allocation, Paging, and Segmentation

CONTIGUOUS MEMORY ALLOCATION Contiguous Memory Allocation is a memory management technique where each process is allocated a single continuous block of memory. This approach is simple and efficient...

CS Core
October 12, 2024
Tutorial Article

Contiguous Memory Allocation

Contiguous Memory Allocation is a memory management technique where each process is allocated a single continuous block of memory. This approach is simple and efficient in terms of access speed because the CPU can easily calculate the address of any location within the block.

Contiguous Memory Allocation

However, contiguous memory allocation suffers from fragmentation. External fragmentation occurs when there are sufficient total free memory spaces, but these are not contiguous, making it difficult to allocate large blocks of memory. Internal fragmentation happens when allocated memory blocks are slightly larger than the requested memory, leading to wasted space.

Additionally, the process of fitting processes into memory can be complex, requiring strategies like first-fit, best-fit, or worst-fit to manage available memory effectively.

Paging

Paging is a memory management scheme that eliminates the need for contiguous allocation of physical memory. In paging, the physical memory is divided into fixed-size blocks called frames, and the logical memory (used by processes) is divided into blocks of the same size called pages. When a process is loaded into memory, its pages can be loaded into any available memory frames, and a page table keeps track of the mapping between the process’s pages and the physical frames.

Paging

This allows the physical address space to be non-contiguous, which solves the problem of external fragmentation and makes it easier to allocate memory efficiently. Paging also simplifies memory management by eliminating the need for complex allocation algorithms.

Page Table Structure

The page table is a data structure used in paging to store the mapping between virtual addresses and physical addresses. Each entry in a page table corresponds to a page of the process's logical address space and contains the address of the frame in physical memory where the page is stored.

Page Table Structure

Page tables can be implemented in several ways, including single-level, multi-level, and inverted page tables. A single-level page table is a simple array, but it can become very large if the address space is large.

Paging: Advantages and Disadvantages

  • Advantages:
    • Avoids gaps between allocated memory blocks, ensuring efficient use of available memory.
    • Pages can be placed anywhere in physical memory, simplifying memory allocation.
    • Fixed-size pages mean that memory allocation does not require complex algorithms.
    • Allows larger programs to run even with limited physical memory.
    • Virtual memory allows parts of a program to be stored on disk and brought into memory as needed.
  • Disadvantages:
    • Each process requires a page table, which can consume a lot of memory for large address spaces.
    • Each memory access requires a lookup in the page table, which can add delay.
    • Unused space in the last page of a process can lead to internal fragmentation.

Segmentation: Concept and Organisation

Segmentation is a memory management technique that divides the process's memory into variable-sized segments, each of which can be a logical unit such as a function, array, or data structure. Unlike paging, segments vary in length, reflecting the logical structure of the process.

Segmentation

Each segment has a segment number and a length, and memory addresses within a segment are specified by an offset from the segment's base address.

A segment table keeps track of each segment's base address and length. Segmentation facilitates sharing and protection, as segments can be independently protected and shared between processes, enhancing modularity and security.

Segmentation: Advantages and Disadvantages

  • Advantages:
    • Improves program readability and manageability.
    • Each segment can have its own access rights, enhancing security.
    • Reduces wasted space within segments.
  • Disadvantages:
    • Can suffer from external fragmentation.
    • Requires more complex algorithms for allocation and deallocation.

Paging vs Segmentation

Feature Paging Segmentation
Memory Division Fixed-sized blocks called pages Variable-size blocks called segments
Physical Memory Divided into frames of the same size as pages Divided into segments of varying sizes
Address Structure Single-level address with page number and offset Two-level address with segment number and offset
Fragmentation Eliminates external fragmentation, may cause internal fragmentation within pages Can lead to external fragmentation, but no internal fragmentation
Logical Division Divides memory without considering logical structure Divides memory according to logical structure (e.g., functions, arrays)
Ease of Management Simpler due to fixed-size pages More complex due to variable-size segments
Protection and Sharing Easier to manage protection and sharing at page level More intuitive for logical groupings but complex
Memory Access Uniform size makes access time consistent Variable sizes can lead to varying access times
Use Case Commonly used in modern operating systems Less common, used for specific applications requiring logical division

Special thanks to Gauri Tomar for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article.